1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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 * Patrick Tasse - Close editors to release resources
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
;
16 import java
.lang
.reflect
.InvocationTargetException
;
18 import org
.eclipse
.core
.resources
.IContainer
;
19 import org
.eclipse
.core
.resources
.IFile
;
20 import org
.eclipse
.core
.resources
.IFolder
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.resources
.IWorkspace
;
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
.IDialogConstants
;
31 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
32 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
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
;
51 * 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
;
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
77 * The experiment element rename
79 public RenameExperimentDialog(Shell shell
, TmfExperimentElement experiment
) {
81 fExperiment
= experiment
;
82 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
83 fExperimentFolder
= folder
.getResource();
84 setTitle(Messages
.RenameExperimentDialog_DialogTitle
);
85 setStatusLineAboveButtons(true);
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
93 protected Control
createDialogArea(Composite parent
) {
94 Composite composite
= (Composite
) super.createDialogArea(parent
);
95 composite
.setLayout(new GridLayout());
96 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
98 createNewExperimentNameGroup(composite
);
102 private void createNewExperimentNameGroup(Composite parent
) {
103 Font font
= parent
.getFont();
104 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
105 GridLayout layout
= new GridLayout();
106 layout
.numColumns
= 2;
107 folderGroup
.setLayout(layout
);
108 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
110 // Old experiment name label
111 Label oldExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
112 oldExperimentLabel
.setFont(font
);
113 oldExperimentLabel
.setText(Messages
.RenameExperimentDialog_ExperimentName
);
115 // Old experiment name field
116 Text oldExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
117 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
118 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
119 oldExperimentName
.setLayoutData(data
);
120 oldExperimentName
.setFont(font
);
121 oldExperimentName
.setText(fExperiment
.getName());
122 oldExperimentName
.setEnabled(false);
124 // New experiment name label
125 Label newExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
126 newExperimentLabel
.setFont(font
);
127 newExperimentLabel
.setText(Messages
.RenameExperimentDialog_ExperimentNewName
);
129 // New experiment name entry field
130 fNewExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
131 data
= new GridData(GridData
.FILL_HORIZONTAL
);
132 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
133 fNewExperimentName
.setLayoutData(data
);
134 fNewExperimentName
.setFont(font
);
135 fNewExperimentName
.addListener(SWT
.Modify
, new Listener() {
137 public void handleEvent(Event event
) {
138 validateNewExperimentName();
143 private void validateNewExperimentName() {
145 String name
= fNewExperimentName
.getText();
146 IWorkspace workspace
= fExperimentFolder
.getWorkspace();
147 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
149 if ("".equals(name
)) { //$NON-NLS-1$
150 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_EmptyNameError
, null));
154 if (!nameStatus
.isOK()) {
155 updateStatus(nameStatus
);
159 IPath path
= new Path(name
);
160 if (fExperimentFolder
.getFolder(path
).exists() || fExperimentFolder
.getFile(path
).exists()) {
161 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_ExistingNameError
, null));
165 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
168 // ------------------------------------------------------------------------
169 // SelectionStatusDialog
170 // ------------------------------------------------------------------------
173 protected void computeResult() {
177 public void create() {
179 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
183 protected void okPressed() {
184 IFolder folder
= renameExperiment(fNewExperimentName
.getText());
185 if (folder
== null) {
188 setSelectionResult(new IFolder
[] { folder
});
192 private IFolder
renameExperiment(final String newName
) {
194 IPath oldPath
= fExperiment
.getResource().getFullPath();
195 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
197 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
199 public void execute(IProgressMonitor monitor
) throws CoreException
{
201 monitor
.beginTask("", 1000); //$NON-NLS-1$
202 if (monitor
.isCanceled()) {
203 throw new OperationCanceledException();
205 // Close the experiment if open
206 fExperiment
.closeEditors();
208 IFolder folder
= fExperiment
.getResource();
209 IFile bookmarksFile
= fExperiment
.getBookmarksFile();
210 IFile newBookmarksFile
= folder
.getFile(bookmarksFile
.getName().replace(fExperiment
.getName(), newName
));
211 if (bookmarksFile
.exists()) {
212 if (!newBookmarksFile
.exists()) {
213 IPath newBookmarksPath
= newBookmarksFile
.getFullPath();
214 bookmarksFile
.move(newBookmarksPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
218 fExperiment
.renameSupplementaryFolder(newName
);
219 fExperiment
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
220 if (monitor
.isCanceled()) {
221 throw new OperationCanceledException();
229 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
230 } catch (InterruptedException exception
) {
232 } catch (InvocationTargetException exception
) {
233 MessageDialog
.openError(getShell(), "", exception
.getTargetException().getMessage()); //$NON-NLS-1$
235 } catch (RuntimeException exception
) {
239 return fExperiment
.getResource();