(no commit message)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / project / dialogs / NewExperimentDialog.java
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.lttng.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.lttng.ui.views.project.model.LTTngExperimentFolderNode;
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, LTTngExperimentFolderNode experimentFolder) {
73 super(parentShell);
74 this.container = experimentFolder.getFolder();
75 setTitle("LTTng 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() {
104 public void handleEvent(Event e) {
105 validateLinkedResource();
106 firstLinkCheck = false;
107 }
108 }, new CreateLinkedResourceGroup.IStringValue() {
109 public void setValue(String string) {
110 folderNameField.setText(string);
111 }
112
113 public String getValue() {
114 return folderNameField.getText();
115 }
116
117 public IResource getResource() {
118 // TODO Auto-generated method stub
119 return null;
120 }
121 });
122 }
123
124 /* (non-Javadoc)
125 * Method declared on Dialog.
126 */
127 @Override
128 protected Control createDialogArea(Composite parent) {
129 Composite composite = (Composite) super.createDialogArea(parent);
130 composite.setLayout(new GridLayout());
131 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
132
133 createFolderNameGroup(composite);
134 createLinkResourceGroup(composite);
135 return composite;
136 }
137
138 /**
139 * Creates the folder name specification controls.
140 *
141 * @param parent the parent composite
142 */
143 private void createFolderNameGroup(Composite parent) {
144 Font font = parent.getFont();
145 Composite folderGroup = new Composite(parent, SWT.NONE);
146 GridLayout layout = new GridLayout();
147 layout.numColumns = 2;
148 folderGroup.setLayout(layout);
149 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
150
151 // new folder label
152 Label folderLabel = new Label(folderGroup, SWT.NONE);
153 folderLabel.setFont(font);
154 folderLabel.setText("Experiment name: ");
155
156 // new folder name entry field
157 folderNameField = new Text(folderGroup, SWT.BORDER);
158 GridData data = new GridData(GridData.FILL_HORIZONTAL);
159 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
160 folderNameField.setLayoutData(data);
161 folderNameField.setFont(font);
162 folderNameField.addListener(SWT.Modify, new Listener() {
163 public void handleEvent(Event event) {
164 validateLinkedResource();
165 }
166 });
167 }
168
169 /**
170 * Creates a folder resource handle for the folder with the given name.
171 * The folder handle is created relative to the container specified during
172 * object creation.
173 *
174 * @param folderName the name of the folder resource to create a handle for
175 * @return the new folder resource handle
176 */
177 private IFolder createFolderHandle(String folderName) {
178 IWorkspaceRoot workspaceRoot = container.getWorkspace().getRoot();
179 IPath folderPath = container.getFullPath().append(folderName);
180 IFolder folderHandle = workspaceRoot.getFolder(folderPath);
181
182 return folderHandle;
183 }
184
185 /**
186 * Creates a new folder with the given name and optionally linking to
187 * the specified link target.
188 *
189 * @param folderName name of the new folder
190 * @param linkTarget name of the link target folder. may be null.
191 * @return IFolder the new folder
192 */
193 private IFolder createNewFolder(String folderName, final URI linkTarget) {
194 final IFolder folderHandle = createFolderHandle(folderName);
195
196 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
197 @Override
198 public void execute(IProgressMonitor monitor) throws CoreException {
199 try {
200 monitor.beginTask(IDEWorkbenchMessages.NewFolderDialog_progress, 2000);
201 if (monitor.isCanceled()) {
202 throw new OperationCanceledException();
203 }
204 if (linkTarget == null) {
205 folderHandle.create(false, true, monitor);
206 } else {
207 folderHandle.createLink(linkTarget, IResource.ALLOW_MISSING_LOCAL, monitor);
208 }
209 if (monitor.isCanceled()) {
210 throw new OperationCanceledException();
211 }
212 } finally {
213 monitor.done();
214 }
215 }
216 };
217 try {
218 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
219 } catch (InterruptedException exception) {
220 return null;
221 } catch (InvocationTargetException exception) {
222 if (exception.getTargetException() instanceof CoreException) {
223 ErrorDialog.openError(getShell(),
224 IDEWorkbenchMessages.NewFolderDialog_errorTitle, null, // no special message
225 ((CoreException) exception.getTargetException()).getStatus());
226 } else {
227 // CoreExceptions are handled above, but unexpected runtime exceptions and errors may still occur.
228 IDEWorkbenchPlugin.log(getClass(),
229 "createNewExperiment", exception.getTargetException());
230 MessageDialog.openError(getShell(),
231 IDEWorkbenchMessages.NewFolderDialog_errorTitle,
232 NLS.bind(IDEWorkbenchMessages.NewFolderDialog_internalError,
233 exception.getTargetException().getMessage()));
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
302 if ("".equals(name)) {
303 updateStatus(IStatus.ERROR, "Experiment name is empty");
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()) {
313 updateStatus(IStatus.ERROR, NLS.bind("Experiment already exists", name));
314 return false;
315 }
316 updateStatus(IStatus.OK, "");
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 }
This page took 0.037428 seconds and 5 git commands to generate.