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