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
.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
;
55 * This is stripped down version of NewFolderDialog.
57 @SuppressWarnings("restriction")
58 public class NewExperimentDialog
extends SelectionStatusDialog
{
60 private Text folderNameField
;
61 private IContainer container
;
62 private boolean firstLinkCheck
= true;
63 private CreateLinkedResourceGroup linkedResourceGroup
;
66 * Creates a NewFolderDialog
68 * @param parentShell parent of the new dialog
69 * @param container parent of the new folder
71 public NewExperimentDialog(Shell parentShell
, IContainer container
) {
73 this.container
= container
;
74 setTitle("LTTng Experiment");
75 setStatusLineAboveButtons(true);
79 * Creates the folder using the name and link target entered by the user.
80 * Sets the dialog result to the created folder.
83 protected void computeResult() {
87 * @see org.eclipse.jface.window.Window#create()
90 public void create() {
92 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
96 * Creates the widget for advanced options.
98 * @param parent the parent composite
100 protected void createLinkResourceGroup(Composite parent
) {
101 linkedResourceGroup
= new CreateLinkedResourceGroup(IResource
.FOLDER
,
103 public void handleEvent(Event e
) {
104 validateLinkedResource();
105 firstLinkCheck
= false;
107 }, new CreateLinkedResourceGroup
.IStringValue() {
108 public void setValue(String string
) {
109 folderNameField
.setText(string
);
112 public String
getValue() {
113 return folderNameField
.getText();
116 public IResource
getResource() {
117 // TODO Auto-generated method stub
124 * Method declared on Dialog.
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
));
132 createFolderNameGroup(composite
);
133 createLinkResourceGroup(composite
);
138 * Creates the folder name specification controls.
140 * @param parent the parent composite
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
));
151 Label folderLabel
= new Label(folderGroup
, SWT
.NONE
);
152 folderLabel
.setFont(font
);
153 folderLabel
.setText("Experiment name: ");
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();
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
173 * @param folderName the name of the folder resource to create a handle for
174 * @return the new folder resource handle
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
);
185 * Creates a new folder with the given name and optionally linking to
186 * the specified link target.
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
192 private IFolder
createNewFolder(String folderName
, final URI linkTarget
) {
193 final IFolder folderHandle
= createFolderHandle(folderName
);
195 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
197 public void execute(IProgressMonitor monitor
) throws CoreException
{
199 monitor
.beginTask(IDEWorkbenchMessages
.NewFolderDialog_progress
, 2000);
200 if (monitor
.isCanceled()) {
201 throw new OperationCanceledException();
203 if (linkTarget
== null) {
204 folderHandle
.create(false, true, monitor
);
206 folderHandle
.createLink(linkTarget
, IResource
.ALLOW_MISSING_LOCAL
, monitor
);
208 if (monitor
.isCanceled()) {
209 throw new OperationCanceledException();
217 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
218 } catch (InterruptedException exception
) {
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());
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()));
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.
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
);
250 super.updateStatus(status
);
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.
260 private void updateStatus(int severity
, String message
) {
261 updateStatus(new Status(severity
, IDEWorkbenchPlugin
.IDE_WORKBENCH
, severity
, message
, null));
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.
269 private void validateLinkedResource() {
270 boolean valid
= validateFolderName();
273 IFolder linkHandle
= createFolderHandle(folderNameField
.getText());
274 IStatus status
= linkedResourceGroup
.validateLinkLocation(linkHandle
);
276 if (status
.getSeverity() != IStatus
.ERROR
) {
277 getOkButton().setEnabled(true);
279 getOkButton().setEnabled(false);
282 if (status
.isOK() == false) {
283 updateStatus(status
);
286 getOkButton().setEnabled(false);
291 * Checks if the folder name is valid.
293 * @return null if the new folder name is valid.
294 * a message that indicates the problem otherwise.
296 private boolean validateFolderName() {
297 String name
= folderNameField
.getText();
298 IWorkspace workspace
= container
.getWorkspace();
299 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
301 if ("".equals(name
)) {
302 updateStatus(IStatus
.ERROR
, "Experiment name is empty");
305 if (nameStatus
.isOK() == false) {
306 updateStatus(nameStatus
);
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
));
315 updateStatus(IStatus
.OK
, "");
320 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
323 protected void okPressed() {
324 URI linkTarget
= linkedResourceGroup
.getLinkTargetURI();
325 IFolder folder
= createNewFolder(folderNameField
.getText(), linkTarget
);
326 if (folder
== null) {
330 setSelectionResult(new IFolder
[] { folder
});