Commit | Line | Data |
---|---|---|
abfad0aa FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 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 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.views.project.dialogs; | |
14 | ||
15 | import java.lang.reflect.InvocationTargetException; | |
16 | import java.net.URI; | |
17 | ||
18 | import org.eclipse.core.resources.IContainer; | |
19 | import org.eclipse.core.resources.IFolder; | |
20 | import org.eclipse.core.resources.IResource; | |
21 | import org.eclipse.core.resources.IWorkspace; | |
22 | import org.eclipse.core.resources.IWorkspaceRoot; | |
23 | import org.eclipse.core.runtime.CoreException; | |
24 | import org.eclipse.core.runtime.IPath; | |
25 | import org.eclipse.core.runtime.IProgressMonitor; | |
26 | import org.eclipse.core.runtime.IStatus; | |
27 | import org.eclipse.core.runtime.OperationCanceledException; | |
28 | import org.eclipse.core.runtime.Path; | |
29 | import org.eclipse.core.runtime.Status; | |
30 | import org.eclipse.jface.dialogs.ErrorDialog; | |
31 | import org.eclipse.jface.dialogs.IDialogConstants; | |
32 | import org.eclipse.jface.dialogs.MessageDialog; | |
33 | import org.eclipse.linuxtools.tmf.ui.views.project.model.TmfExperimentFolderNode; | |
34 | import org.eclipse.osgi.util.NLS; | |
35 | import org.eclipse.swt.SWT; | |
36 | import org.eclipse.swt.graphics.Font; | |
37 | import org.eclipse.swt.layout.GridData; | |
38 | import org.eclipse.swt.layout.GridLayout; | |
39 | import org.eclipse.swt.widgets.Composite; | |
40 | import org.eclipse.swt.widgets.Control; | |
41 | import org.eclipse.swt.widgets.Event; | |
42 | import org.eclipse.swt.widgets.Label; | |
43 | import org.eclipse.swt.widgets.Listener; | |
44 | import org.eclipse.swt.widgets.Shell; | |
45 | import org.eclipse.swt.widgets.Text; | |
46 | import org.eclipse.ui.PlatformUI; | |
47 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
48 | import org.eclipse.ui.dialogs.SelectionStatusDialog; | |
49 | import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; | |
50 | import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; | |
51 | import org.eclipse.ui.internal.ide.dialogs.CreateLinkedResourceGroup; | |
52 | ||
53 | /** | |
54 | * NewExperimentDialog | |
55 | * | |
56 | * This is stripped down version of NewFolderDialog. | |
57 | */ | |
58 | @SuppressWarnings("restriction") | |
59 | public class NewExperimentDialog extends SelectionStatusDialog { | |
60 | ||
61 | private Text folderNameField; | |
62 | private IContainer container; | |
63 | private boolean firstLinkCheck = true; | |
64 | private CreateLinkedResourceGroup linkedResourceGroup; | |
65 | ||
66 | /** | |
67 | * Creates a NewFolderDialog | |
68 | * | |
69 | * @param parentShell parent of the new dialog | |
70 | * @param container parent of the new folder | |
71 | */ | |
72 | public NewExperimentDialog(Shell parentShell, TmfExperimentFolderNode experimentFolder) { | |
73 | super(parentShell); | |
74 | this.container = experimentFolder.getFolder(); | |
75 | setTitle("Tmf Experiment"); | |
76 | setStatusLineAboveButtons(true); | |
77 | } | |
78 | ||
79 | /** | |
80 | * Creates the folder using the name and link target entered by the user. | |
81 | * Sets the dialog result to the created folder. | |
82 | */ | |
83 | @Override | |
84 | protected void computeResult() { | |
85 | } | |
86 | ||
87 | /** | |
88 | * @see org.eclipse.jface.window.Window#create() | |
89 | */ | |
90 | @Override | |
91 | public void create() { | |
92 | super.create(); | |
93 | getButton(IDialogConstants.OK_ID).setEnabled(false); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Creates the widget for advanced options. | |
98 | * | |
99 | * @param parent the parent composite | |
100 | */ | |
101 | protected void createLinkResourceGroup(Composite parent) { | |
102 | linkedResourceGroup = new CreateLinkedResourceGroup(IResource.FOLDER, | |
103 | new Listener() { | |
d4011df2 | 104 | @Override |
abfad0aa FC |
105 | public void handleEvent(Event e) { |
106 | validateLinkedResource(); | |
107 | firstLinkCheck = false; | |
108 | } | |
109 | }, new CreateLinkedResourceGroup.IStringValue() { | |
d4011df2 | 110 | @Override |
abfad0aa FC |
111 | public void setValue(String string) { |
112 | folderNameField.setText(string); | |
113 | } | |
114 | ||
d4011df2 | 115 | @Override |
abfad0aa FC |
116 | public String getValue() { |
117 | return folderNameField.getText(); | |
118 | } | |
119 | ||
d4011df2 | 120 | @Override |
abfad0aa FC |
121 | public IResource getResource() { |
122 | // TODO Auto-generated method stub | |
123 | return null; | |
124 | } | |
125 | }); | |
126 | } | |
127 | ||
128 | /* (non-Javadoc) | |
129 | * Method declared on Dialog. | |
130 | */ | |
131 | @Override | |
132 | protected Control createDialogArea(Composite parent) { | |
133 | Composite composite = (Composite) super.createDialogArea(parent); | |
134 | composite.setLayout(new GridLayout()); | |
135 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
136 | ||
137 | createFolderNameGroup(composite); | |
138 | createLinkResourceGroup(composite); | |
139 | return composite; | |
140 | } | |
141 | ||
142 | /** | |
143 | * Creates the folder name specification controls. | |
144 | * | |
145 | * @param parent the parent composite | |
146 | */ | |
147 | private void createFolderNameGroup(Composite parent) { | |
148 | Font font = parent.getFont(); | |
149 | Composite folderGroup = new Composite(parent, SWT.NONE); | |
150 | GridLayout layout = new GridLayout(); | |
151 | layout.numColumns = 2; | |
152 | folderGroup.setLayout(layout); | |
153 | folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
154 | ||
155 | // new folder label | |
156 | Label folderLabel = new Label(folderGroup, SWT.NONE); | |
157 | folderLabel.setFont(font); | |
158 | folderLabel.setText("Experiment name: "); | |
159 | ||
160 | // new folder name entry field | |
161 | folderNameField = new Text(folderGroup, SWT.BORDER); | |
162 | GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
163 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
164 | folderNameField.setLayoutData(data); | |
165 | folderNameField.setFont(font); | |
166 | folderNameField.addListener(SWT.Modify, new Listener() { | |
d4011df2 | 167 | @Override |
abfad0aa FC |
168 | public void handleEvent(Event event) { |
169 | validateLinkedResource(); | |
170 | } | |
171 | }); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Creates a folder resource handle for the folder with the given name. | |
176 | * The folder handle is created relative to the container specified during | |
177 | * object creation. | |
178 | * | |
179 | * @param folderName the name of the folder resource to create a handle for | |
180 | * @return the new folder resource handle | |
181 | */ | |
182 | private IFolder createFolderHandle(String folderName) { | |
183 | IWorkspaceRoot workspaceRoot = container.getWorkspace().getRoot(); | |
184 | IPath folderPath = container.getFullPath().append(folderName); | |
185 | IFolder folderHandle = workspaceRoot.getFolder(folderPath); | |
186 | ||
187 | return folderHandle; | |
188 | } | |
189 | ||
190 | /** | |
191 | * Creates a new folder with the given name and optionally linking to | |
192 | * the specified link target. | |
193 | * | |
194 | * @param folderName name of the new folder | |
195 | * @param linkTarget name of the link target folder. may be null. | |
196 | * @return IFolder the new folder | |
197 | */ | |
198 | private IFolder createNewFolder(String folderName, final URI linkTarget) { | |
199 | final IFolder folderHandle = createFolderHandle(folderName); | |
200 | ||
201 | WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { | |
202 | @Override | |
203 | public void execute(IProgressMonitor monitor) throws CoreException { | |
204 | try { | |
205 | monitor.beginTask(IDEWorkbenchMessages.NewFolderDialog_progress, 2000); | |
206 | if (monitor.isCanceled()) { | |
207 | throw new OperationCanceledException(); | |
208 | } | |
209 | if (linkTarget == null) { | |
210 | folderHandle.create(false, true, monitor); | |
211 | } else { | |
212 | folderHandle.createLink(linkTarget, IResource.ALLOW_MISSING_LOCAL, monitor); | |
213 | } | |
214 | if (monitor.isCanceled()) { | |
215 | throw new OperationCanceledException(); | |
216 | } | |
217 | } finally { | |
218 | monitor.done(); | |
219 | } | |
220 | } | |
221 | }; | |
222 | try { | |
223 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
224 | } catch (InterruptedException exception) { | |
225 | return null; | |
226 | } catch (InvocationTargetException exception) { | |
227 | if (exception.getTargetException() instanceof CoreException) { | |
228 | ErrorDialog.openError(getShell(), | |
229 | IDEWorkbenchMessages.NewFolderDialog_errorTitle, null, // no special message | |
230 | ((CoreException) exception.getTargetException()).getStatus()); | |
231 | } else { | |
232 | // CoreExceptions are handled above, but unexpected runtime exceptions and errors may still occur. | |
233 | IDEWorkbenchPlugin.log(getClass(), | |
234 | "createNewExperiment", exception.getTargetException()); | |
235 | MessageDialog.openError(getShell(), | |
236 | IDEWorkbenchMessages.NewFolderDialog_errorTitle, | |
237 | NLS.bind(IDEWorkbenchMessages.NewFolderDialog_internalError, | |
238 | exception.getTargetException().getMessage())); | |
239 | } | |
240 | return null; | |
241 | } | |
242 | return folderHandle; | |
243 | } | |
244 | ||
245 | /** | |
246 | * Update the dialog's status line to reflect the given status. It is safe to call | |
247 | * this method before the dialog has been opened. | |
248 | */ | |
249 | @Override | |
250 | protected void updateStatus(IStatus status) { | |
251 | if (firstLinkCheck && status != null) { | |
252 | Status newStatus = new Status(IStatus.OK, status.getPlugin(), | |
253 | status.getCode(), status.getMessage(), status.getException()); | |
254 | super.updateStatus(newStatus); | |
255 | } else { | |
256 | super.updateStatus(status); | |
257 | } | |
258 | } | |
259 | ||
260 | /** | |
261 | * Update the dialog's status line to reflect the given status. It is safe to call | |
262 | * this method before the dialog has been opened. | |
263 | * @param severity | |
264 | * @param message | |
265 | */ | |
266 | private void updateStatus(int severity, String message) { | |
267 | updateStatus(new Status(severity, IDEWorkbenchPlugin.IDE_WORKBENCH, severity, message, null)); | |
268 | } | |
269 | ||
270 | /** | |
271 | * Checks whether the folder name and link location are valid. | |
272 | * Disable the OK button if the folder name and link location are valid. | |
273 | * a message that indicates the problem otherwise. | |
274 | */ | |
275 | private void validateLinkedResource() { | |
276 | boolean valid = validateFolderName(); | |
277 | ||
278 | if (valid) { | |
279 | IFolder linkHandle = createFolderHandle(folderNameField.getText()); | |
280 | IStatus status = linkedResourceGroup.validateLinkLocation(linkHandle); | |
281 | ||
282 | if (status.getSeverity() != IStatus.ERROR) { | |
283 | getOkButton().setEnabled(true); | |
284 | } else { | |
285 | getOkButton().setEnabled(false); | |
286 | } | |
287 | ||
288 | if (status.isOK() == false) { | |
289 | updateStatus(status); | |
290 | } | |
291 | } else { | |
292 | getOkButton().setEnabled(false); | |
293 | } | |
294 | } | |
295 | ||
296 | /** | |
297 | * Checks if the folder name is valid. | |
298 | * | |
299 | * @return null if the new folder name is valid. | |
300 | * a message that indicates the problem otherwise. | |
301 | */ | |
302 | private boolean validateFolderName() { | |
303 | String name = folderNameField.getText(); | |
304 | IWorkspace workspace = container.getWorkspace(); | |
305 | IStatus nameStatus = workspace.validateName(name, IResource.FOLDER); | |
306 | ||
9c4eb5f7 FC |
307 | if ("".equals(name)) { //$NON-NLS-1$ |
308 | updateStatus(IStatus.ERROR, "Experiment name is empty"); //$NON-NLS-1$ | |
abfad0aa FC |
309 | return false; |
310 | } | |
311 | if (nameStatus.isOK() == false) { | |
312 | updateStatus(nameStatus); | |
313 | return false; | |
314 | } | |
315 | IPath path = new Path(name); | |
316 | if (container.getFolder(path).exists() | |
317 | || container.getFile(path).exists()) { | |
9c4eb5f7 | 318 | updateStatus(IStatus.ERROR, NLS.bind("Experiment already exists", name)); //$NON-NLS-1$ |
abfad0aa FC |
319 | return false; |
320 | } | |
3b38ea61 | 321 | updateStatus(IStatus.OK, ""); //$NON-NLS-1$ |
abfad0aa FC |
322 | return true; |
323 | } | |
324 | ||
325 | /* (non-Javadoc) | |
326 | * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed() | |
327 | */ | |
328 | @Override | |
329 | protected void okPressed() { | |
330 | URI linkTarget = linkedResourceGroup.getLinkTargetURI(); | |
331 | IFolder folder = createNewFolder(folderNameField.getText(), linkTarget); | |
332 | if (folder == null) { | |
333 | return; | |
334 | } | |
335 | ||
336 | setSelectionResult(new IFolder[] { folder }); | |
337 | ||
338 | super.okPressed(); | |
339 | } | |
340 | } |