1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.dialogs
;
15 import java
.lang
.reflect
.InvocationTargetException
;
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
;
56 * This is stripped down version of NewFolderDialog.
58 @SuppressWarnings("restriction")
59 public class NewExperimentDialog
extends SelectionStatusDialog
{
61 private Text folderNameField
;
62 private IContainer container
;
63 private boolean firstLinkCheck
= true;
64 private CreateLinkedResourceGroup linkedResourceGroup
;
67 * Creates a NewFolderDialog
69 * @param parentShell parent of the new dialog
70 * @param container parent of the new folder
72 public NewExperimentDialog(Shell parentShell
, LTTngExperimentFolderNode experimentFolder
) {
74 this.container
= experimentFolder
.getFolder();
75 setTitle("LTTng Experiment");
76 setStatusLineAboveButtons(true);
80 * Creates the folder using the name and link target entered by the user.
81 * Sets the dialog result to the created folder.
84 protected void computeResult() {
88 * @see org.eclipse.jface.window.Window#create()
91 public void create() {
93 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
97 * Creates the widget for advanced options.
99 * @param parent the parent composite
101 protected void createLinkResourceGroup(Composite parent
) {
102 linkedResourceGroup
= new CreateLinkedResourceGroup(IResource
.FOLDER
,
104 public void handleEvent(Event e
) {
105 validateLinkedResource();
106 firstLinkCheck
= false;
108 }, new CreateLinkedResourceGroup
.IStringValue() {
109 public void setValue(String string
) {
110 folderNameField
.setText(string
);
113 public String
getValue() {
114 return folderNameField
.getText();
117 public IResource
getResource() {
118 // TODO Auto-generated method stub
125 * Method declared on Dialog.
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
));
133 createFolderNameGroup(composite
);
134 createLinkResourceGroup(composite
);
139 * Creates the folder name specification controls.
141 * @param parent the parent composite
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
));
152 Label folderLabel
= new Label(folderGroup
, SWT
.NONE
);
153 folderLabel
.setFont(font
);
154 folderLabel
.setText("Experiment name: ");
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();
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
174 * @param folderName the name of the folder resource to create a handle for
175 * @return the new folder resource handle
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
);
186 * Creates a new folder with the given name and optionally linking to
187 * the specified link target.
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
193 private IFolder
createNewFolder(String folderName
, final URI linkTarget
) {
194 final IFolder folderHandle
= createFolderHandle(folderName
);
196 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
198 public void execute(IProgressMonitor monitor
) throws CoreException
{
200 monitor
.beginTask(IDEWorkbenchMessages
.NewFolderDialog_progress
, 2000);
201 if (monitor
.isCanceled()) {
202 throw new OperationCanceledException();
204 if (linkTarget
== null) {
205 folderHandle
.create(false, true, monitor
);
207 folderHandle
.createLink(linkTarget
, IResource
.ALLOW_MISSING_LOCAL
, monitor
);
209 if (monitor
.isCanceled()) {
210 throw new OperationCanceledException();
218 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
219 } catch (InterruptedException exception
) {
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());
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()));
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.
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
);
251 super.updateStatus(status
);
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.
261 private void updateStatus(int severity
, String message
) {
262 updateStatus(new Status(severity
, IDEWorkbenchPlugin
.IDE_WORKBENCH
, severity
, message
, null));
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.
270 private void validateLinkedResource() {
271 boolean valid
= validateFolderName();
274 IFolder linkHandle
= createFolderHandle(folderNameField
.getText());
275 IStatus status
= linkedResourceGroup
.validateLinkLocation(linkHandle
);
277 if (status
.getSeverity() != IStatus
.ERROR
) {
278 getOkButton().setEnabled(true);
280 getOkButton().setEnabled(false);
283 if (status
.isOK() == false) {
284 updateStatus(status
);
287 getOkButton().setEnabled(false);
292 * Checks if the folder name is valid.
294 * @return null if the new folder name is valid.
295 * a message that indicates the problem otherwise.
297 private boolean validateFolderName() {
298 String name
= folderNameField
.getText();
299 IWorkspace workspace
= container
.getWorkspace();
300 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
302 if ("".equals(name
)) {
303 updateStatus(IStatus
.ERROR
, "Experiment name is empty");
306 if (nameStatus
.isOK() == false) {
307 updateStatus(nameStatus
);
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
));
316 updateStatus(IStatus
.OK
, "");
321 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
324 protected void okPressed() {
325 URI linkTarget
= linkedResourceGroup
.getLinkTargetURI();
326 IFolder folder
= createNewFolder(folderNameField
.getText(), linkTarget
);
327 if (folder
== null) {
331 setSelectionResult(new IFolder
[] { folder
});