1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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
.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
.IEditorReference
;
47 import org
.eclipse
.ui
.IWorkbench
;
48 import org
.eclipse
.ui
.IWorkbenchPage
;
49 import org
.eclipse
.ui
.IWorkbenchWindow
;
50 import org
.eclipse
.ui
.PlatformUI
;
51 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
52 import org
.eclipse
.ui
.dialogs
.SelectionStatusDialog
;
53 import org
.eclipse
.ui
.part
.FileEditorInput
;
56 * Implementation of a dialog box to rename an experiment.
60 * @author Francois Chouinard
62 public class RenameExperimentDialog
extends SelectionStatusDialog
{
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
68 private final TmfExperimentElement fExperiment
;
69 private Text fNewExperimentName
;
70 private final IContainer fExperimentFolder
;
71 private final TmfProjectElement fProject
;
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
83 * The experiment element rename
85 public RenameExperimentDialog(Shell shell
, TmfExperimentElement experiment
) {
87 fExperiment
= experiment
;
88 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
89 fExperimentFolder
= folder
.getResource();
90 fProject
= experiment
.getProject();
91 setTitle(Messages
.RenameExperimentDialog_DialogTitle
);
92 setStatusLineAboveButtons(true);
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
100 protected Control
createDialogArea(Composite parent
) {
101 Composite composite
= (Composite
) super.createDialogArea(parent
);
102 composite
.setLayout(new GridLayout());
103 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
105 createNewExperimentNameGroup(composite
);
109 private void createNewExperimentNameGroup(Composite parent
) {
110 Font font
= parent
.getFont();
111 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
112 GridLayout layout
= new GridLayout();
113 layout
.numColumns
= 2;
114 folderGroup
.setLayout(layout
);
115 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
117 // Old experiment name label
118 Label oldExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
119 oldExperimentLabel
.setFont(font
);
120 oldExperimentLabel
.setText(Messages
.RenameExperimentDialog_ExperimentName
);
122 // Old experiment name field
123 Text oldExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
124 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
125 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
126 oldExperimentName
.setLayoutData(data
);
127 oldExperimentName
.setFont(font
);
128 oldExperimentName
.setText(fExperiment
.getName());
129 oldExperimentName
.setEnabled(false);
131 // New experiment name label
132 Label newExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
133 newExperimentLabel
.setFont(font
);
134 newExperimentLabel
.setText(Messages
.RenameExperimentDialog_ExperimentNewName
);
136 // New experiment name entry field
137 fNewExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
138 data
= new GridData(GridData
.FILL_HORIZONTAL
);
139 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
140 fNewExperimentName
.setLayoutData(data
);
141 fNewExperimentName
.setFont(font
);
142 fNewExperimentName
.addListener(SWT
.Modify
, new Listener() {
144 public void handleEvent(Event event
) {
145 validateNewExperimentName();
150 private void validateNewExperimentName() {
152 String name
= fNewExperimentName
.getText();
153 IWorkspace workspace
= fExperimentFolder
.getWorkspace();
154 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
156 if ("".equals(name
)) { //$NON-NLS-1$
157 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_EmptyNameError
, null));
161 if (!nameStatus
.isOK()) {
162 updateStatus(nameStatus
);
166 IPath path
= new Path(name
);
167 if (fExperimentFolder
.getFolder(path
).exists() || fExperimentFolder
.getFile(path
).exists()) {
168 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_ExistingNameError
, null));
172 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
175 // ------------------------------------------------------------------------
176 // SelectionStatusDialog
177 // ------------------------------------------------------------------------
180 protected void computeResult() {
184 public void create() {
186 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
190 protected void okPressed() {
191 IFolder folder
= renameExperiment(fNewExperimentName
.getText());
192 if (folder
== null) {
195 setSelectionResult(new IFolder
[] { folder
});
198 if (fProject
!= null) {
203 private IFolder
renameExperiment(final String newName
) {
205 IPath oldPath
= fExperiment
.getResource().getFullPath();
206 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
208 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
210 public void execute(IProgressMonitor monitor
) throws CoreException
{
212 monitor
.beginTask("", 1000); //$NON-NLS-1$
213 if (monitor
.isCanceled()) {
214 throw new OperationCanceledException();
216 // Close the experiment if open
217 IFile file
= fExperiment
.getBookmarksFile();
218 FileEditorInput input
= new FileEditorInput(file
);
219 IWorkbench wb
= PlatformUI
.getWorkbench();
220 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
221 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
222 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
223 if (editorReference
.getEditorInput().equals(input
)) {
224 wbPage
.closeEditor(editorReference
.getEditor(false), false);
230 IFolder folder
= fExperiment
.getResource();
231 IFile bookmarksFile
= fExperiment
.getBookmarksFile();
232 IFile newBookmarksFile
= folder
.getFile(bookmarksFile
.getName().replace(fExperiment
.getName(), newName
));
233 if (bookmarksFile
.exists()) {
234 if (!newBookmarksFile
.exists()) {
235 IPath newBookmarksPath
= newBookmarksFile
.getFullPath();
236 bookmarksFile
.move(newBookmarksPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
240 fExperiment
.renameSupplementaryFolder(newName
);
241 fExperiment
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
242 if (monitor
.isCanceled()) {
243 throw new OperationCanceledException();
251 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
252 } catch (InterruptedException exception
) {
254 } catch (InvocationTargetException exception
) {
255 MessageDialog
.openError(getShell(), "", exception
.getTargetException().getMessage()); //$NON-NLS-1$
257 } catch (RuntimeException exception
) {
261 return fExperiment
.getResource();