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
.tmf
.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
.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
;
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
, TmfExperimentFolderNode experimentFolder
) {
74 this.container
= experimentFolder
.getFolder();
75 setTitle("Tmf 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
,
105 public void handleEvent(Event e
) {
106 validateLinkedResource();
107 firstLinkCheck
= false;
109 }, new CreateLinkedResourceGroup
.IStringValue() {
111 public void setValue(String string
) {
112 folderNameField
.setText(string
);
116 public String
getValue() {
117 return folderNameField
.getText();
121 public IResource
getResource() {
122 // TODO Auto-generated method stub
129 * Method declared on Dialog.
132 protected Control
createDialogArea(Composite parent
) {
133 Composite composite
= (Composite
) super.createDialogArea(parent
);
134 composite
.setLayout(new GridLayout());
135 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
137 createFolderNameGroup(composite
);
138 createLinkResourceGroup(composite
);
143 * Creates the folder name specification controls.
145 * @param parent the parent composite
147 private void createFolderNameGroup(Composite parent
) {
148 Font font
= parent
.getFont();
149 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
150 GridLayout layout
= new GridLayout();
151 layout
.numColumns
= 2;
152 folderGroup
.setLayout(layout
);
153 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
156 Label folderLabel
= new Label(folderGroup
, SWT
.NONE
);
157 folderLabel
.setFont(font
);
158 folderLabel
.setText("Experiment name: ");
160 // new folder name entry field
161 folderNameField
= new Text(folderGroup
, SWT
.BORDER
);
162 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
163 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
164 folderNameField
.setLayoutData(data
);
165 folderNameField
.setFont(font
);
166 folderNameField
.addListener(SWT
.Modify
, new Listener() {
168 public void handleEvent(Event event
) {
169 validateLinkedResource();
175 * Creates a folder resource handle for the folder with the given name.
176 * The folder handle is created relative to the container specified during
179 * @param folderName the name of the folder resource to create a handle for
180 * @return the new folder resource handle
182 private IFolder
createFolderHandle(String folderName
) {
183 IWorkspaceRoot workspaceRoot
= container
.getWorkspace().getRoot();
184 IPath folderPath
= container
.getFullPath().append(folderName
);
185 IFolder folderHandle
= workspaceRoot
.getFolder(folderPath
);
191 * Creates a new folder with the given name and optionally linking to
192 * the specified link target.
194 * @param folderName name of the new folder
195 * @param linkTarget name of the link target folder. may be null.
196 * @return IFolder the new folder
198 private IFolder
createNewFolder(String folderName
, final URI linkTarget
) {
199 final IFolder folderHandle
= createFolderHandle(folderName
);
201 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
203 public void execute(IProgressMonitor monitor
) throws CoreException
{
205 monitor
.beginTask(IDEWorkbenchMessages
.NewFolderDialog_progress
, 2000);
206 if (monitor
.isCanceled()) {
207 throw new OperationCanceledException();
209 if (linkTarget
== null) {
210 folderHandle
.create(false, true, monitor
);
212 folderHandle
.createLink(linkTarget
, IResource
.ALLOW_MISSING_LOCAL
, monitor
);
214 if (monitor
.isCanceled()) {
215 throw new OperationCanceledException();
223 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
224 } catch (InterruptedException exception
) {
226 } catch (InvocationTargetException exception
) {
227 if (exception
.getTargetException() instanceof CoreException
) {
228 ErrorDialog
.openError(getShell(),
229 IDEWorkbenchMessages
.NewFolderDialog_errorTitle
, null, // no special message
230 ((CoreException
) exception
.getTargetException()).getStatus());
232 // CoreExceptions are handled above, but unexpected runtime exceptions and errors may still occur.
233 IDEWorkbenchPlugin
.log(getClass(),
234 "createNewExperiment", exception
.getTargetException());
235 MessageDialog
.openError(getShell(),
236 IDEWorkbenchMessages
.NewFolderDialog_errorTitle
,
237 NLS
.bind(IDEWorkbenchMessages
.NewFolderDialog_internalError
,
238 exception
.getTargetException().getMessage()));
246 * Update the dialog's status line to reflect the given status. It is safe to call
247 * this method before the dialog has been opened.
250 protected void updateStatus(IStatus status
) {
251 if (firstLinkCheck
&& status
!= null) {
252 Status newStatus
= new Status(IStatus
.OK
, status
.getPlugin(),
253 status
.getCode(), status
.getMessage(), status
.getException());
254 super.updateStatus(newStatus
);
256 super.updateStatus(status
);
261 * Update the dialog's status line to reflect the given status. It is safe to call
262 * this method before the dialog has been opened.
266 private void updateStatus(int severity
, String message
) {
267 updateStatus(new Status(severity
, IDEWorkbenchPlugin
.IDE_WORKBENCH
, severity
, message
, null));
271 * Checks whether the folder name and link location are valid.
272 * Disable the OK button if the folder name and link location are valid.
273 * a message that indicates the problem otherwise.
275 private void validateLinkedResource() {
276 boolean valid
= validateFolderName();
279 IFolder linkHandle
= createFolderHandle(folderNameField
.getText());
280 IStatus status
= linkedResourceGroup
.validateLinkLocation(linkHandle
);
282 if (status
.getSeverity() != IStatus
.ERROR
) {
283 getOkButton().setEnabled(true);
285 getOkButton().setEnabled(false);
288 if (status
.isOK() == false) {
289 updateStatus(status
);
292 getOkButton().setEnabled(false);
297 * Checks if the folder name is valid.
299 * @return null if the new folder name is valid.
300 * a message that indicates the problem otherwise.
302 private boolean validateFolderName() {
303 String name
= folderNameField
.getText();
304 IWorkspace workspace
= container
.getWorkspace();
305 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
307 if ("".equals(name
)) {
308 updateStatus(IStatus
.ERROR
, "Experiment name is empty");
311 if (nameStatus
.isOK() == false) {
312 updateStatus(nameStatus
);
315 IPath path
= new Path(name
);
316 if (container
.getFolder(path
).exists()
317 || container
.getFile(path
).exists()) {
318 updateStatus(IStatus
.ERROR
, NLS
.bind("Experiment already exists", name
));
321 updateStatus(IStatus
.OK
, "");
326 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
329 protected void okPressed() {
330 URI linkTarget
= linkedResourceGroup
.getLinkTargetURI();
331 IFolder folder
= createNewFolder(folderNameField
.getText(), linkTarget
);
332 if (folder
== null) {
336 setSelectionResult(new IFolder
[] { folder
});