Commit | Line | Data |
---|---|---|
e8d771d5 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Bernd Hufmann - Initial API and implementation | |
11 | * | |
12 | *******************************************************************************/ | |
638eac44 | 13 | package org.eclipse.linuxtools.internal.lttng.ui.tracecontrol.dialogs; |
e8d771d5 BH |
14 | |
15 | import java.io.File; | |
16 | ||
17 | import org.eclipse.jface.dialogs.Dialog; | |
18 | import org.eclipse.jface.dialogs.IDialogConstants; | |
638eac44 | 19 | import org.eclipse.linuxtools.internal.lttng.ui.tracecontrol.Messages; |
e8d771d5 BH |
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; | |
35 | ||
36 | /** | |
37 | * <b><u>SelectTracePathDialog</u></b> | |
38 | * <p> | |
39 | * Dialog box to configure and select a trace path. | |
40 | * </p> | |
41 | */ | |
42 | public class SelectTracePathDialog extends Dialog { | |
43 | ||
44 | // ------------------------------------------------------------------------ | |
45 | // Attributes | |
46 | // ------------------------------------------------------------------------ | |
47 | ||
48 | private Text fPath = null; | |
49 | private String fPathName = null; | |
50 | private String fTracePathError = null; | |
51 | private Label fErrorLabel = null; | |
52 | ||
53 | // ------------------------------------------------------------------------ | |
54 | // Constructors | |
55 | // ------------------------------------------------------------------------ | |
56 | ||
57 | /** | |
58 | * Constructor | |
59 | * | |
60 | * @param parentShell The paren shell | |
61 | */ | |
62 | public SelectTracePathDialog(Shell parentShell) { | |
63 | super(parentShell); | |
64 | } | |
65 | ||
66 | // ------------------------------------------------------------------------ | |
67 | // Operations | |
68 | // ------------------------------------------------------------------------ | |
69 | ||
70 | /* | |
71 | * (non-Javadoc) | |
72 | * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) | |
73 | */ | |
74 | @Override | |
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)); | |
79 | } | |
80 | ||
81 | /* | |
82 | * (non-Javadoc) | |
83 | * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) | |
84 | */ | |
85 | @Override | |
86 | protected Control createDialogArea(Composite parent) { | |
87 | //Main dialog panel | |
88 | Composite composite = new Composite(parent, SWT.RESIZE); | |
89 | GridLayout mainLayout = new GridLayout(1, true); | |
90 | composite.setLayout(mainLayout); | |
91 | ||
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)); | |
96 | ||
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)); | |
100 | ||
101 | GridLayout groupLayout = new GridLayout(6, true); | |
102 | group.setLayout(groupLayout); | |
103 | ||
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)); | |
108 | ||
109 | fPath = new Text(group, SWT.LEFT); | |
110 | fPath.setLayoutData(new GridData(GridData.FILL, | |
111 | GridData.CENTER, true, false, 4, 1)); | |
112 | ||
113 | fPath.addListener(SWT.Modify, new Listener() { | |
114 | @Override | |
115 | public void handleEvent(Event event) { | |
116 | Button ok = getButton(IDialogConstants.OK_ID); | |
117 | ok.setEnabled(validatePathName(fPath.getText())); | |
118 | } | |
119 | }); | |
120 | ||
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)); | |
124 | ||
125 | browseButton.setEnabled(true); | |
126 | browseButton.addListener(SWT.Selection, new Listener() { | |
127 | @Override | |
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); | |
133 | } | |
134 | } | |
135 | }); | |
136 | return composite; | |
137 | } | |
138 | ||
139 | /* | |
140 | * (non-Javadoc) | |
141 | * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) | |
142 | */ | |
143 | @Override | |
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); | |
147 | ||
148 | ok.setEnabled(false); | |
149 | } | |
150 | ||
151 | /* | |
152 | * (non-Javadoc) | |
153 | * @see org.eclipse.jface.dialogs.Dialog#okPressed() | |
154 | */ | |
155 | @Override | |
156 | protected void okPressed() { | |
157 | fPathName = fPath.getText(); | |
158 | ||
159 | File newDir = new File(fPathName); | |
160 | if (!newDir.exists()) { | |
161 | try { | |
162 | newDir.mkdirs(); | |
163 | } catch (Exception e) { | |
164 | // Should not happen | |
165 | } | |
166 | } | |
167 | ||
168 | super.okPressed(); | |
169 | } | |
170 | ||
171 | /* | |
172 | * Validates the trace path. | |
173 | */ | |
174 | private boolean validatePathName(String path) { | |
175 | if (path.length() > 0) { | |
176 | final char c0 = path.charAt(0); | |
177 | if (c0 != '/') { | |
178 | fTracePathError = Messages.ConfigureTraceDialog_Error_Invalid_Path; | |
179 | fErrorLabel.setText(fTracePathError); | |
180 | return false; | |
181 | } else { | |
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 | |
189 | } | |
190 | if ((c >= 'A') && (c <= 'Z')) { | |
191 | continue; // uppercase | |
192 | } | |
193 | if ((c >= '0') && (c <= '9')) { | |
194 | continue; // numeric | |
195 | } | |
196 | fTracePathError = Messages.ConfigureTraceDialog_Error_Invalid_Folder; | |
197 | fErrorLabel.setText(fTracePathError); | |
198 | return false; | |
199 | } | |
200 | } | |
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); | |
206 | return false; | |
207 | } | |
208 | } | |
209 | } | |
210 | } | |
211 | ||
212 | File file = new File(path); | |
213 | if (file.isFile()) { | |
214 | fTracePathError = Messages.ConfigureTraceDialog_Error_File_Exists; | |
215 | fErrorLabel.setText(fTracePathError); | |
216 | return false; | |
217 | } | |
218 | if (path.length() > 1 && !file.getParentFile().canWrite()) { | |
219 | fTracePathError = Messages.ConfigureTraceDialog_Error_Can_Not_Write; | |
220 | fErrorLabel.setText(fTracePathError); | |
221 | return false; | |
222 | } | |
223 | } | |
224 | fErrorLabel.setText(""); //$NON-NLS-1$ | |
225 | fTracePathError = ""; //$NON-NLS-1$ | |
226 | return true; | |
227 | } | |
228 | ||
229 | /** | |
230 | * Returns the trace path. | |
231 | * | |
232 | * @return trace path | |
233 | */ | |
234 | public String getTracePath() { | |
235 | return fPathName; | |
236 | } | |
237 | } |