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
.ITmfProjectModelElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
38 import org
.eclipse
.swt
.SWT
;
39 import org
.eclipse
.swt
.graphics
.Font
;
40 import org
.eclipse
.swt
.layout
.GridData
;
41 import org
.eclipse
.swt
.layout
.GridLayout
;
42 import org
.eclipse
.swt
.widgets
.Composite
;
43 import org
.eclipse
.swt
.widgets
.Control
;
44 import org
.eclipse
.swt
.widgets
.Event
;
45 import org
.eclipse
.swt
.widgets
.Label
;
46 import org
.eclipse
.swt
.widgets
.Listener
;
47 import org
.eclipse
.swt
.widgets
.Shell
;
48 import org
.eclipse
.swt
.widgets
.Text
;
49 import org
.eclipse
.ui
.IEditorReference
;
50 import org
.eclipse
.ui
.IWorkbench
;
51 import org
.eclipse
.ui
.IWorkbenchPage
;
52 import org
.eclipse
.ui
.IWorkbenchWindow
;
53 import org
.eclipse
.ui
.PlatformUI
;
54 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
55 import org
.eclipse
.ui
.dialogs
.SelectionStatusDialog
;
56 import org
.eclipse
.ui
.part
.FileEditorInput
;
59 * Implementation of a dialog box to rename a trace.
62 * @author Francois Chouinard
64 public class RenameTraceDialog
extends SelectionStatusDialog
{
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
70 private final TmfTraceElement fTrace
;
71 private Text fNewTraceNameText
;
72 private String fNewTraceName
;
73 private final IContainer fTraceFolder
;
74 private final TmfProjectElement fProject
;
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
81 * @param shell The parent shell
82 * @param trace The trace element to rename
84 public RenameTraceDialog(Shell shell
, TmfTraceElement trace
) {
87 TmfTraceFolder folder
= (TmfTraceFolder
) trace
.getParent();
88 fTraceFolder
= folder
.getResource();
89 fProject
= trace
.getProject();
90 setTitle(Messages
.RenameTraceDialog_DialogTitle
);
91 setStatusLineAboveButtons(true);
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
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 createNewTraceNameGroup(composite
);
108 private void createNewTraceNameGroup(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 trace name label
117 Label oldTraceLabel
= new Label(folderGroup
, SWT
.NONE
);
118 oldTraceLabel
.setFont(font
);
119 oldTraceLabel
.setText(Messages
.RenameTraceDialog_TraceName
);
121 // Old trace name field
122 Text oldTraceName
= new Text(folderGroup
, SWT
.BORDER
);
123 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
124 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
125 oldTraceName
.setLayoutData(data
);
126 oldTraceName
.setFont(font
);
127 oldTraceName
.setText(fTrace
.getName());
128 oldTraceName
.setEnabled(false);
130 // New trace name label
131 Label newTaceLabel
= new Label(folderGroup
, SWT
.NONE
);
132 newTaceLabel
.setFont(font
);
133 newTaceLabel
.setText(Messages
.RenameTraceDialog_TraceNewName
);
135 // New trace name entry field
136 fNewTraceNameText
= new Text(folderGroup
, SWT
.BORDER
);
137 data
= new GridData(GridData
.FILL_HORIZONTAL
);
138 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
139 fNewTraceNameText
.setLayoutData(data
);
140 fNewTraceNameText
.setFont(font
);
141 fNewTraceNameText
.addListener(SWT
.Modify
, new Listener() {
143 public void handleEvent(Event event
) {
144 validateNewTraceName();
150 * Returns the new trace name
151 * @return the new trace name
153 public String
getNewTraceName() {
154 return fNewTraceName
;
157 private void validateNewTraceName() {
159 fNewTraceName
= fNewTraceNameText
.getText();
160 IWorkspace workspace
= fTraceFolder
.getWorkspace();
161 IStatus nameStatus
= workspace
.validateName(fNewTraceName
, IResource
.FOLDER
);
163 if ("".equals(fNewTraceName
)) { //$NON-NLS-1$
164 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
,
165 Messages
.Dialog_EmptyNameError
, null));
169 if (!nameStatus
.isOK()) {
170 updateStatus(nameStatus
);
174 IPath path
= new Path(fNewTraceName
);
175 if (fTraceFolder
.getFolder(path
).exists() || fTraceFolder
.getFile(path
).exists()) {
176 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
,
177 Messages
.Dialog_ExistingNameError
, null));
181 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
184 // ------------------------------------------------------------------------
185 // SelectionStatusDialog
186 // ------------------------------------------------------------------------
189 protected void computeResult() {
193 public void create() {
195 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
199 protected void okPressed() {
200 IResource trace
= renameTrace(fNewTraceNameText
.getText());
204 setSelectionResult(new IResource
[] { trace
});
207 if (fProject
!= null) {
212 private IResource
renameTrace(final String newName
) {
214 final IPath oldPath
= fTrace
.getResource().getFullPath();
215 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
217 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
219 public void execute(IProgressMonitor monitor
) throws CoreException
{
221 monitor
.beginTask("", 1000); //$NON-NLS-1$
222 if (monitor
.isCanceled()) {
223 throw new OperationCanceledException();
225 // Close the trace if open
226 IFile file
= fTrace
.getBookmarksFile();
227 FileEditorInput input
= new FileEditorInput(file
);
228 IWorkbench wb
= PlatformUI
.getWorkbench();
229 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
230 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
231 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
232 if (editorReference
.getEditorInput().equals(input
)) {
233 wbPage
.closeEditor(editorReference
.getEditor(false), false);
238 TmfExperimentFolder experimentFolder
= fTrace
.getProject().getExperimentsFolder();
239 for (final ITmfProjectModelElement experiment
: experimentFolder
.getChildren()) {
240 for (final ITmfProjectModelElement trace
: experiment
.getChildren()) {
241 if (trace
.equals(fTrace
)) {
242 // Close the experiment if open
243 file
= ((TmfExperimentElement
) experiment
).getBookmarksFile();
244 input
= new FileEditorInput(file
);
245 for (IWorkbenchWindow wbWindow
: wb
.getWorkbenchWindows()) {
246 for (IWorkbenchPage wbPage
: wbWindow
.getPages()) {
247 for (IEditorReference editorReference
: wbPage
.getEditorReferences()) {
248 if (editorReference
.getEditorInput().equals(input
)) {
249 wbPage
.closeEditor(editorReference
.getEditor(false), false);
258 if (fTrace
.getResource() instanceof IFolder
) {
259 IFolder folder
= (IFolder
) fTrace
.getResource();
260 IFile bookmarksFile
= fTrace
.getBookmarksFile();
261 IFile newBookmarksFile
= folder
.getFile(bookmarksFile
.getName().replace(fTrace
.getName(), newName
));
262 if (bookmarksFile
.exists()) {
263 if (!newBookmarksFile
.exists()) {
264 IPath newBookmarksPath
= newBookmarksFile
.getFullPath();
265 bookmarksFile
.move(newBookmarksPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
270 fTrace
.renameSupplementaryFolder(newName
);
271 fTrace
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
272 if (monitor
.isCanceled()) {
273 throw new OperationCanceledException();
282 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
283 } catch (InterruptedException exception
) {
285 } catch (InvocationTargetException exception
) {
286 MessageDialog
.openError(getShell(), "", exception
.getTargetException().getMessage()); //$NON-NLS-1$
288 } catch (RuntimeException exception
) {
292 return fTrace
.getResource();