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.
59 * @author Francois Chouinard
61 public class RenameExperimentDialog
extends SelectionStatusDialog
{
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
67 private final TmfExperimentElement fExperiment
;
68 private Text fNewExperimentName
;
69 private final IContainer fExperimentFolder
;
70 private final TmfProjectElement fProject
;
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
78 * @param shell The parent shell
79 * @param experiment The experiment element rename
81 public RenameExperimentDialog(Shell shell
, TmfExperimentElement experiment
) {
83 fExperiment
= experiment
;
84 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
85 fExperimentFolder
= folder
.getResource();
86 fProject
= experiment
.getProject();
87 setTitle(Messages
.RenameExperimentDialog_DialogTitle
);
88 setStatusLineAboveButtons(true);
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
96 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
99 protected Control
createDialogArea(Composite parent
) {
100 Composite composite
= (Composite
) super.createDialogArea(parent
);
101 composite
.setLayout(new GridLayout());
102 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
104 createNewExperimentNameGroup(composite
);
108 private void createNewExperimentNameGroup(Composite parent
) {
109 Font font
= parent
.getFont();
110 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
111 GridLayout layout
= new GridLayout();
112 layout
.numColumns
= 2;
113 folderGroup
.setLayout(layout
);
114 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
116 // Old experiment name label
117 Label oldExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
118 oldExperimentLabel
.setFont(font
);
119 oldExperimentLabel
.setText(Messages
.RenameExperimentDialog_ExperimentName
);
121 // Old experiment name field
122 Text oldExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
123 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
124 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
125 oldExperimentName
.setLayoutData(data
);
126 oldExperimentName
.setFont(font
);
127 oldExperimentName
.setText(fExperiment
.getName());
128 oldExperimentName
.setEnabled(false);
130 // New experiment name label
131 Label newExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
132 newExperimentLabel
.setFont(font
);
133 newExperimentLabel
.setText(Messages
.RenameExperimentDialog_ExperimentNewName
);
135 // New experiment name entry field
136 fNewExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
137 data
= new GridData(GridData
.FILL_HORIZONTAL
);
138 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
139 fNewExperimentName
.setLayoutData(data
);
140 fNewExperimentName
.setFont(font
);
141 fNewExperimentName
.addListener(SWT
.Modify
, new Listener() {
143 public void handleEvent(Event event
) {
144 validateNewExperimentName();
149 private void validateNewExperimentName() {
151 String name
= fNewExperimentName
.getText();
152 IWorkspace workspace
= fExperimentFolder
.getWorkspace();
153 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
155 if ("".equals(name
)) { //$NON-NLS-1$
156 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_EmptyNameError
, null));
160 if (!nameStatus
.isOK()) {
161 updateStatus(nameStatus
);
165 IPath path
= new Path(name
);
166 if (fExperimentFolder
.getFolder(path
).exists() || fExperimentFolder
.getFile(path
).exists()) {
167 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_ExistingNameError
, null));
171 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
174 // ------------------------------------------------------------------------
175 // SelectionStatusDialog
176 // ------------------------------------------------------------------------
179 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
182 protected void computeResult() {
187 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#create()
190 public void create() {
192 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
197 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
200 protected void okPressed() {
201 IFolder folder
= renameExperiment(fNewExperimentName
.getText());
202 if (folder
== null) {
205 setSelectionResult(new IFolder
[] { folder
});
208 if (fProject
!= null) {
213 private IFolder
renameExperiment(final String newName
) {
215 IPath oldPath
= fExperiment
.getResource().getFullPath();
216 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
218 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
220 public void execute(IProgressMonitor monitor
) throws CoreException
{
222 monitor
.beginTask("", 1000); //$NON-NLS-1$
223 if (monitor
.isCanceled()) {
224 throw new OperationCanceledException();
226 // Close the experiment if open
227 IFile file
= fExperiment
.getBookmarksFile();
228 FileEditorInput input
= new FileEditorInput(file
);
229 IWorkbench wb
= PlatformUI
.getWorkbench();
230 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
231 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
232 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
233 if (editorReference
.getEditorInput().equals(input
)) {
234 wbPage
.closeEditor(editorReference
.getEditor(false), false);
240 IFolder folder
= fExperiment
.getResource();
241 IFile bookmarksFile
= fExperiment
.getBookmarksFile();
242 IFile newBookmarksFile
= folder
.getFile(bookmarksFile
.getName().replace(fExperiment
.getName(), newName
));
243 if (bookmarksFile
.exists()) {
244 if (!newBookmarksFile
.exists()) {
245 IPath newBookmarksPath
= newBookmarksFile
.getFullPath();
246 bookmarksFile
.move(newBookmarksPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
250 fExperiment
.renameSupplementaryFolder(newName
);
251 fExperiment
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
252 if (monitor
.isCanceled()) {
253 throw new OperationCanceledException();
261 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
262 } catch (InterruptedException exception
) {
264 } catch (InvocationTargetException exception
) {
265 MessageDialog
.openError(getShell(), "", exception
.getTargetException().getMessage()); //$NON-NLS-1$
267 } catch (RuntimeException exception
) {
271 return fExperiment
.getResource();