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
.TmfTraceElement
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
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 a trace.
54 * @author Francois Chouinard
56 public class RenameTraceDialog
extends SelectionStatusDialog
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 private final TmfTraceElement fTrace
;
63 private Text fNewTraceNameText
;
64 private String fNewTraceName
;
65 private final IContainer fTraceFolder
;
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 * @param shell The parent shell
73 * @param trace The trace element to rename
75 public RenameTraceDialog(Shell shell
, TmfTraceElement trace
) {
78 TmfTraceFolder folder
= (TmfTraceFolder
) trace
.getParent();
79 fTraceFolder
= folder
.getResource();
80 setTitle(Messages
.RenameTraceDialog_DialogTitle
);
81 setStatusLineAboveButtons(true);
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
89 protected Control
createDialogArea(Composite parent
) {
90 Composite composite
= (Composite
) super.createDialogArea(parent
);
91 composite
.setLayout(new GridLayout());
92 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
94 createNewTraceNameGroup(composite
);
98 private void createNewTraceNameGroup(Composite parent
) {
99 Font font
= parent
.getFont();
100 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
101 GridLayout layout
= new GridLayout();
102 layout
.numColumns
= 2;
103 folderGroup
.setLayout(layout
);
104 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
106 // Old trace name label
107 Label oldTraceLabel
= new Label(folderGroup
, SWT
.NONE
);
108 oldTraceLabel
.setFont(font
);
109 oldTraceLabel
.setText(Messages
.RenameTraceDialog_TraceName
);
111 // Old trace name field
112 Text oldTraceName
= new Text(folderGroup
, SWT
.BORDER
);
113 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
114 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
115 oldTraceName
.setLayoutData(data
);
116 oldTraceName
.setFont(font
);
117 oldTraceName
.setText(fTrace
.getName());
118 oldTraceName
.setEnabled(false);
120 // New trace name label
121 Label newTaceLabel
= new Label(folderGroup
, SWT
.NONE
);
122 newTaceLabel
.setFont(font
);
123 newTaceLabel
.setText(Messages
.RenameTraceDialog_TraceNewName
);
125 // New trace name entry field
126 fNewTraceNameText
= new Text(folderGroup
, SWT
.BORDER
);
127 data
= new GridData(GridData
.FILL_HORIZONTAL
);
128 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
129 fNewTraceNameText
.setLayoutData(data
);
130 fNewTraceNameText
.setFont(font
);
131 fNewTraceNameText
.addListener(SWT
.Modify
, new Listener() {
133 public void handleEvent(Event event
) {
134 validateNewTraceName();
140 * Returns the new trace name
141 * @return the new trace name
143 public String
getNewTraceName() {
144 return fNewTraceName
;
147 private void validateNewTraceName() {
149 fNewTraceName
= fNewTraceNameText
.getText();
150 IWorkspace workspace
= fTraceFolder
.getWorkspace();
151 IStatus nameStatus
= workspace
.validateName(fNewTraceName
, IResource
.FOLDER
);
153 if ("".equals(fNewTraceName
)) { //$NON-NLS-1$
154 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
,
155 Messages
.Dialog_EmptyNameError
, null));
159 if (!nameStatus
.isOK()) {
160 updateStatus(nameStatus
);
164 IPath path
= new Path(fNewTraceName
);
165 if (fTraceFolder
.getFolder(path
).exists() || fTraceFolder
.getFile(path
).exists()) {
166 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
,
167 Messages
.Dialog_ExistingNameError
, null));
171 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
174 // ------------------------------------------------------------------------
175 // SelectionStatusDialog
176 // ------------------------------------------------------------------------
179 protected void computeResult() {
183 public void create() {
185 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
189 protected void okPressed() {
190 IResource trace
= renameTrace(fNewTraceNameText
.getText());
194 setSelectionResult(new IResource
[] { trace
});
198 private IResource
renameTrace(final String newName
) {
200 final IPath oldPath
= fTrace
.getResource().getFullPath();
201 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
203 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
205 public void execute(IProgressMonitor monitor
) throws CoreException
{
207 monitor
.beginTask("", 1000); //$NON-NLS-1$
208 if (monitor
.isCanceled()) {
209 throw new OperationCanceledException();
211 // Close the trace if open
212 fTrace
.closeEditors();
214 if (fTrace
.getResource() instanceof IFolder
) {
215 IFolder folder
= (IFolder
) fTrace
.getResource();
216 IFile bookmarksFile
= fTrace
.getBookmarksFile();
217 IFile newBookmarksFile
= folder
.getFile(bookmarksFile
.getName().replace(fTrace
.getName(), newName
));
218 if (bookmarksFile
.exists()) {
219 if (!newBookmarksFile
.exists()) {
220 IPath newBookmarksPath
= newBookmarksFile
.getFullPath();
221 bookmarksFile
.move(newBookmarksPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
226 fTrace
.renameSupplementaryFolder(newName
);
227 fTrace
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
228 if (monitor
.isCanceled()) {
229 throw new OperationCanceledException();
238 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
239 } catch (InterruptedException exception
) {
241 } catch (InvocationTargetException exception
) {
242 MessageDialog
.openError(getShell(), "", exception
.getTargetException().getMessage()); //$NON-NLS-1$
244 } catch (RuntimeException exception
) {
248 return fTrace
.getResource();