1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 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 - Copied and adapted from NewFolderDialog
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
;
15 import java
.lang
.reflect
.InvocationTargetException
;
17 import org
.eclipse
.core
.resources
.IContainer
;
18 import org
.eclipse
.core
.resources
.IFile
;
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
.runtime
.CoreException
;
23 import org
.eclipse
.core
.runtime
.IPath
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.IStatus
;
26 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
27 import org
.eclipse
.core
.runtime
.Path
;
28 import org
.eclipse
.core
.runtime
.Status
;
29 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
30 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
31 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
35 import org
.eclipse
.osgi
.util
.NLS
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swt
.graphics
.Font
;
38 import org
.eclipse
.swt
.layout
.GridData
;
39 import org
.eclipse
.swt
.layout
.GridLayout
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Control
;
42 import org
.eclipse
.swt
.widgets
.Event
;
43 import org
.eclipse
.swt
.widgets
.Label
;
44 import org
.eclipse
.swt
.widgets
.Listener
;
45 import org
.eclipse
.swt
.widgets
.Shell
;
46 import org
.eclipse
.swt
.widgets
.Text
;
47 import org
.eclipse
.ui
.PlatformUI
;
48 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
49 import org
.eclipse
.ui
.dialogs
.SelectionStatusDialog
;
52 * Implementation of a dialog box to rename an experiment.
55 * @author Francois Chouinard
57 public class RenameExperimentDialog
extends SelectionStatusDialog
{
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
63 private final TmfExperimentElement fExperiment
;
64 private Text fNewExperimentName
;
65 private final IContainer fExperimentFolder
;
66 private final TmfProjectElement fProject
;
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
74 * @param shell The parent shell
75 * @param experiment The experiment element rename
77 public RenameExperimentDialog(Shell shell
, TmfExperimentElement experiment
) {
79 fExperiment
= experiment
;
80 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
81 fExperimentFolder
= folder
.getResource();
82 fProject
= experiment
.getProject();
83 setTitle(Messages
.RenameExperimentDialog_DialogTitle
);
84 setStatusLineAboveButtons(true);
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
92 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
95 protected Control
createDialogArea(Composite parent
) {
96 Composite composite
= (Composite
) super.createDialogArea(parent
);
97 composite
.setLayout(new GridLayout());
98 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
100 createNewExperimentNameGroup(composite
);
104 private void createNewExperimentNameGroup(Composite parent
) {
105 Font font
= parent
.getFont();
106 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
107 GridLayout layout
= new GridLayout();
108 layout
.numColumns
= 2;
109 folderGroup
.setLayout(layout
);
110 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
112 // Old experiment name label
113 Label oldExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
114 oldExperimentLabel
.setFont(font
);
115 oldExperimentLabel
.setText(Messages
.RenameExperimentDialog_ExperimentName
);
117 // Old experiment name field
118 Text oldExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
119 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
120 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
121 oldExperimentName
.setLayoutData(data
);
122 oldExperimentName
.setFont(font
);
123 oldExperimentName
.setText(fExperiment
.getName());
124 oldExperimentName
.setEnabled(false);
126 // New experiment name label
127 Label newExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
128 newExperimentLabel
.setFont(font
);
129 newExperimentLabel
.setText(Messages
.RenameExperimentDialog_ExperimentNewName
);
131 // New experiment name entry field
132 fNewExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
133 data
= new GridData(GridData
.FILL_HORIZONTAL
);
134 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
135 fNewExperimentName
.setLayoutData(data
);
136 fNewExperimentName
.setFont(font
);
137 fNewExperimentName
.addListener(SWT
.Modify
, new Listener() {
139 public void handleEvent(Event event
) {
140 validateNewExperimentName();
145 private void validateNewExperimentName() {
147 String name
= fNewExperimentName
.getText();
148 IWorkspace workspace
= fExperimentFolder
.getWorkspace();
149 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
151 if ("".equals(name
)) { //$NON-NLS-1$
152 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_EmptyNameError
, null));
156 if (!nameStatus
.isOK()) {
157 updateStatus(nameStatus
);
161 IPath path
= new Path(name
);
162 if (fExperimentFolder
.getFolder(path
).exists() || fExperimentFolder
.getFile(path
).exists()) {
163 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_ExistingNameError
, null));
167 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
170 // ------------------------------------------------------------------------
171 // SelectionStatusDialog
172 // ------------------------------------------------------------------------
175 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
178 protected void computeResult() {
183 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#create()
186 public void create() {
188 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
193 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
196 protected void okPressed() {
197 IFolder folder
= renameExperiment(fNewExperimentName
.getText());
198 if (folder
== null) {
201 setSelectionResult(new IFolder
[] { folder
});
204 if (fProject
!= null) {
209 private IFolder
renameExperiment(final String newName
) {
211 IPath oldPath
= fExperiment
.getResource().getFullPath();
212 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
214 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
216 public void execute(IProgressMonitor monitor
) throws CoreException
{
218 monitor
.beginTask("", 1000); //$NON-NLS-1$
219 if (monitor
.isCanceled()) {
220 throw new OperationCanceledException();
222 IFolder folder
= fExperiment
.getResource();
223 IFile bookmarksFile
= folder
.getFile(fExperiment
.getName() + '_');
224 IFile newBookmarksFile
= folder
.getFile(newName
+ '_');
225 if (bookmarksFile
.exists()) {
226 if (!newBookmarksFile
.exists()) {
227 IPath newBookmarksPath
= newBookmarksFile
.getFullPath();
228 bookmarksFile
.move(newBookmarksPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
231 fExperiment
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
232 if (monitor
.isCanceled()) {
233 throw new OperationCanceledException();
241 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
242 } catch (InterruptedException exception
) {
244 } catch (InvocationTargetException exception
) {
245 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
247 } catch (RuntimeException exception
) {
251 return fExperiment
.getResource();