[Bug 303523] LTTng/TMF udpates:
[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.osgi.util.NLS;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.graphics.Font;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Event;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.swt.widgets.Listener;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.actions.WorkspaceModifyOperation;
47 import org.eclipse.ui.dialogs.SelectionStatusDialog;
48 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
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, IContainer container) {
72 super(parentShell);
73 this.container = container;
74 setTitle("LTTng Experiment");
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() {
103 public void handleEvent(Event e) {
104 validateLinkedResource();
105 firstLinkCheck = false;
106 }
107 }, new CreateLinkedResourceGroup.IStringValue() {
108 public void setValue(String string) {
109 folderNameField.setText(string);
110 }
111
112 public String getValue() {
113 return folderNameField.getText();
114 }
115
116 public IResource getResource() {
117 // TODO Auto-generated method stub
118 return null;
119 }
120 });
121 }
122
123 /* (non-Javadoc)
124 * Method declared on Dialog.
125 */
126 @Override
127 protected Control createDialogArea(Composite parent) {
128 Composite composite = (Composite) super.createDialogArea(parent);
129 composite.setLayout(new GridLayout());
130 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
131
132 createFolderNameGroup(composite);
133 createLinkResourceGroup(composite);
134 return composite;
135 }
136
137 /**
138 * Creates the folder name specification controls.
139 *
140 * @param parent the parent composite
141 */
142 private void createFolderNameGroup(Composite parent) {
143 Font font = parent.getFont();
144 Composite folderGroup = new Composite(parent, SWT.NONE);
145 GridLayout layout = new GridLayout();
146 layout.numColumns = 2;
147 folderGroup.setLayout(layout);
148 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
149
150 // new folder label
151 Label folderLabel = new Label(folderGroup, SWT.NONE);
152 folderLabel.setFont(font);
153 folderLabel.setText("Experiment name: ");
154
155 // new folder name entry field
156 folderNameField = new Text(folderGroup, SWT.BORDER);
157 GridData data = new GridData(GridData.FILL_HORIZONTAL);
158 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
159 folderNameField.setLayoutData(data);
160 folderNameField.setFont(font);
161 folderNameField.addListener(SWT.Modify, new Listener() {
162 public void handleEvent(Event event) {
163 validateLinkedResource();
164 }
165 });
166 }
167
168 /**
169 * Creates a folder resource handle for the folder with the given name.
170 * The folder handle is created relative to the container specified during
171 * object creation.
172 *
173 * @param folderName the name of the folder resource to create a handle for
174 * @return the new folder resource handle
175 */
176 private IFolder createFolderHandle(String folderName) {
177 IWorkspaceRoot workspaceRoot = container.getWorkspace().getRoot();
178 IPath folderPath = container.getFullPath().append(folderName);
179 IFolder folderHandle = workspaceRoot.getFolder(folderPath);
180
181 return folderHandle;
182 }
183
184 /**
185 * Creates a new folder with the given name and optionally linking to
186 * the specified link target.
187 *
188 * @param folderName name of the new folder
189 * @param linkTarget name of the link target folder. may be null.
190 * @return IFolder the new folder
191 */
192 private IFolder createNewFolder(String folderName, final URI linkTarget) {
193 final IFolder folderHandle = createFolderHandle(folderName);
194
195 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
196 @Override
197 public void execute(IProgressMonitor monitor) throws CoreException {
198 try {
199 monitor.beginTask(IDEWorkbenchMessages.NewFolderDialog_progress, 2000);
200 if (monitor.isCanceled()) {
201 throw new OperationCanceledException();
202 }
203 if (linkTarget == null) {
204 folderHandle.create(false, true, monitor);
205 } else {
206 folderHandle.createLink(linkTarget, IResource.ALLOW_MISSING_LOCAL, monitor);
207 }
208 if (monitor.isCanceled()) {
209 throw new OperationCanceledException();
210 }
211 } finally {
212 monitor.done();
213 }
214 }
215 };
216 try {
217 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
218 } catch (InterruptedException exception) {
219 return null;
220 } catch (InvocationTargetException exception) {
221 if (exception.getTargetException() instanceof CoreException) {
222 ErrorDialog.openError(getShell(),
223 IDEWorkbenchMessages.NewFolderDialog_errorTitle, null, // no special message
224 ((CoreException) exception.getTargetException()).getStatus());
225 } else {
226 // CoreExceptions are handled above, but unexpected runtime exceptions and errors may still occur.
227 IDEWorkbenchPlugin.log(getClass(),
228 "createNewExperiment", exception.getTargetException());
229 MessageDialog.openError(getShell(),
230 IDEWorkbenchMessages.NewFolderDialog_errorTitle,
231 NLS.bind(IDEWorkbenchMessages.NewFolderDialog_internalError,
232 exception.getTargetException().getMessage()));
233 }
234 return null;
235 }
236 return folderHandle;
237 }
238
239 /**
240 * Update the dialog's status line to reflect the given status. It is safe to call
241 * this method before the dialog has been opened.
242 */
243 @Override
244 protected void updateStatus(IStatus status) {
245 if (firstLinkCheck && status != null) {
246 Status newStatus = new Status(IStatus.OK, status.getPlugin(),
247 status.getCode(), status.getMessage(), status.getException());
248 super.updateStatus(newStatus);
249 } else {
250 super.updateStatus(status);
251 }
252 }
253
254 /**
255 * Update the dialog's status line to reflect the given status. It is safe to call
256 * this method before the dialog has been opened.
257 * @param severity
258 * @param message
259 */
260 private void updateStatus(int severity, String message) {
261 updateStatus(new Status(severity, IDEWorkbenchPlugin.IDE_WORKBENCH, severity, message, null));
262 }
263
264 /**
265 * Checks whether the folder name and link location are valid.
266 * Disable the OK button if the folder name and link location are valid.
267 * a message that indicates the problem otherwise.
268 */
269 private void validateLinkedResource() {
270 boolean valid = validateFolderName();
271
272 if (valid) {
273 IFolder linkHandle = createFolderHandle(folderNameField.getText());
274 IStatus status = linkedResourceGroup.validateLinkLocation(linkHandle);
275
276 if (status.getSeverity() != IStatus.ERROR) {
277 getOkButton().setEnabled(true);
278 } else {
279 getOkButton().setEnabled(false);
280 }
281
282 if (status.isOK() == false) {
283 updateStatus(status);
284 }
285 } else {
286 getOkButton().setEnabled(false);
287 }
288 }
289
290 /**
291 * Checks if the folder name is valid.
292 *
293 * @return null if the new folder name is valid.
294 * a message that indicates the problem otherwise.
295 */
296 private boolean validateFolderName() {
297 String name = folderNameField.getText();
298 IWorkspace workspace = container.getWorkspace();
299 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
300
301 if ("".equals(name)) {
302 updateStatus(IStatus.ERROR, "Experiment name is empty");
303 return false;
304 }
305 if (nameStatus.isOK() == false) {
306 updateStatus(nameStatus);
307 return false;
308 }
309 IPath path = new Path(name);
310 if (container.getFolder(path).exists()
311 || container.getFile(path).exists()) {
312 updateStatus(IStatus.ERROR, NLS.bind("Experiment already exists", name));
313 return false;
314 }
315 updateStatus(IStatus.OK, "");
316 return true;
317 }
318
319 /* (non-Javadoc)
320 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
321 */
322 @Override
323 protected void okPressed() {
324 URI linkTarget = linkedResourceGroup.getLinkTargetURI();
325 IFolder folder = createNewFolder(folderNameField.getText(), linkTarget);
326 if (folder == null) {
327 return;
328 }
329
330 setSelectionResult(new IFolder[] { folder });
331
332 super.okPressed();
333 }
334 }
This page took 0.037796 seconds and 5 git commands to generate.