1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Bernd Hufmann - Initial API and implementation
12 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.lttng
.ui
.tracecontrol
.dialogs
;
17 import org
.eclipse
.jface
.dialogs
.Dialog
;
18 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
19 import org
.eclipse
.linuxtools
.internal
.lttng
.ui
.tracecontrol
.Messages
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.layout
.GridData
;
22 import org
.eclipse
.swt
.layout
.GridLayout
;
23 import org
.eclipse
.swt
.widgets
.Button
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Control
;
26 import org
.eclipse
.swt
.widgets
.DirectoryDialog
;
27 import org
.eclipse
.swt
.widgets
.Event
;
28 import org
.eclipse
.swt
.widgets
.Group
;
29 import org
.eclipse
.swt
.widgets
.Label
;
30 import org
.eclipse
.swt
.widgets
.Listener
;
31 import org
.eclipse
.swt
.widgets
.Shell
;
32 import org
.eclipse
.swt
.widgets
.Text
;
33 import org
.eclipse
.ui
.ISharedImages
;
34 import org
.eclipse
.ui
.PlatformUI
;
37 * <b><u>SelectTracePathDialog</u></b>
39 * Dialog box to configure and select a trace path.
42 public class SelectTracePathDialog
extends Dialog
{
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
48 private Text fPath
= null;
49 private String fPathName
= null;
50 private String fTracePathError
= null;
51 private Label fErrorLabel
= null;
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
60 * @param parentShell The paren shell
62 public SelectTracePathDialog(Shell parentShell
) {
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
72 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
75 protected void configureShell(Shell newShell
) {
76 super.configureShell(newShell
);
77 newShell
.setText(Messages
.SeletctTracePathDialog_Title
);
78 newShell
.setImage(PlatformUI
.getWorkbench().getSharedImages().getImage(ISharedImages
.IMG_OBJ_FOLDER
));
83 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
86 protected Control
createDialogArea(Composite parent
) {
88 Composite composite
= new Composite(parent
, SWT
.RESIZE
);
89 GridLayout mainLayout
= new GridLayout(1, true);
90 composite
.setLayout(mainLayout
);
92 fErrorLabel
= new Label(composite
, SWT
.LEFT
);
93 fErrorLabel
.setLayoutData(new GridData(GridData
.FILL
,
94 GridData
.CENTER
, true, false, 6, 1));
95 fErrorLabel
.setForeground(getShell().getDisplay().getSystemColor(SWT
.COLOR_RED
));
97 Group group
= new Group(composite
, SWT
.SHADOW_OUT
);
98 group
.setText(Messages
.ConfigureTraceDialog_Trace_Location
);
99 group
.setLayoutData(new GridData(GridData
.FILL
, GridData
.CENTER
, true, false, 1, 1));
101 GridLayout groupLayout
= new GridLayout(6, true);
102 group
.setLayout(groupLayout
);
104 Label tracePathLabel
= new Label(group
, SWT
.LEFT
);
105 tracePathLabel
.setText(Messages
.ConfigureTraceDialog_Trace_Path
);
106 tracePathLabel
.setLayoutData(new GridData(GridData
.FILL
,
107 GridData
.CENTER
, true, false, 1, 1));
109 fPath
= new Text(group
, SWT
.LEFT
);
110 fPath
.setLayoutData(new GridData(GridData
.FILL
,
111 GridData
.CENTER
, true, false, 4, 1));
113 fPath
.addListener(SWT
.Modify
, new Listener() {
115 public void handleEvent(Event event
) {
116 Button ok
= getButton(IDialogConstants
.OK_ID
);
117 ok
.setEnabled(validatePathName(fPath
.getText()));
121 Button browseButton
= new Button(group
, SWT
.PUSH
);
122 browseButton
.setText(Messages
.ConfigureTraceDialog_Browse
+ "..."); //$NON-NLS-1$
123 browseButton
.setLayoutData(new GridData(GridData
.FILL
, GridData
.CENTER
, true, false, 1, 1));
125 browseButton
.setEnabled(true);
126 browseButton
.addListener(SWT
.Selection
, new Listener() {
128 public void handleEvent(Event event
) {
129 DirectoryDialog dialog
= new DirectoryDialog(getShell());
130 String newPath
= dialog
.open();
131 if (newPath
!= null) {
132 fPath
.setText(newPath
);
141 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
144 protected void createButtonsForButtonBar(Composite parent
) {
145 Button ok
= createButton(parent
, IDialogConstants
.OK_ID
, IDialogConstants
.OK_LABEL
, true);
146 createButton(parent
, IDialogConstants
.CANCEL_ID
, IDialogConstants
.CANCEL_LABEL
, false);
148 ok
.setEnabled(false);
153 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
156 protected void okPressed() {
157 fPathName
= fPath
.getText();
159 File newDir
= new File(fPathName
);
160 if (!newDir
.exists()) {
163 } catch (Exception e
) {
172 * Validates the trace path.
174 private boolean validatePathName(String path
) {
175 if (path
.length() > 0) {
176 final char c0
= path
.charAt(0);
178 fTracePathError
= Messages
.ConfigureTraceDialog_Error_Invalid_Path
;
179 fErrorLabel
.setText(fTracePathError
);
182 String
[] folders
= path
.split("/"); //$NON-NLS-1$
183 for (int i
= 0; i
< folders
.length
; i
++) {
184 final char[] chars
= folders
[i
].toCharArray();
185 for (int x
= 0; x
< chars
.length
; x
++) {
186 final char c
= chars
[x
];
187 if ((c
>= 'a') && (c
<= 'z')) {
188 continue; // lowercase
190 if ((c
>= 'A') && (c
<= 'Z')) {
191 continue; // uppercase
193 if ((c
>= '0') && (c
<= '9')) {
196 fTracePathError
= Messages
.ConfigureTraceDialog_Error_Invalid_Folder
;
197 fErrorLabel
.setText(fTracePathError
);
201 if (path
.length() > 1) {
202 for (int i
= 0; i
< path
.length() - 1; i
++) {
203 if ((path
.charAt(i
) == '/') && (path
.charAt(i
+ 1) == '/')) {
204 fTracePathError
= Messages
.ConfigureTraceDialog_Error_Multiple_Seps
;
205 fErrorLabel
.setText(fTracePathError
);
212 File file
= new File(path
);
214 fTracePathError
= Messages
.ConfigureTraceDialog_Error_File_Exists
;
215 fErrorLabel
.setText(fTracePathError
);
218 if (path
.length() > 1 && !file
.getParentFile().canWrite()) {
219 fTracePathError
= Messages
.ConfigureTraceDialog_Error_Can_Not_Write
;
220 fErrorLabel
.setText(fTracePathError
);
224 fErrorLabel
.setText(""); //$NON-NLS-1$
225 fTracePathError
= ""; //$NON-NLS-1$
230 * Returns the trace path.
234 public String
getTracePath() {