1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 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
.TmfProjectElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
35 import org
.eclipse
.osgi
.util
.NLS
;
36 import org
.eclipse
.swt
.SWT
;
37 import org
.eclipse
.swt
.graphics
.Font
;
38 import org
.eclipse
.swt
.layout
.GridData
;
39 import org
.eclipse
.swt
.layout
.GridLayout
;
40 import org
.eclipse
.swt
.widgets
.Composite
;
41 import org
.eclipse
.swt
.widgets
.Control
;
42 import org
.eclipse
.swt
.widgets
.Event
;
43 import org
.eclipse
.swt
.widgets
.Label
;
44 import org
.eclipse
.swt
.widgets
.Listener
;
45 import org
.eclipse
.swt
.widgets
.Shell
;
46 import org
.eclipse
.swt
.widgets
.Text
;
47 import org
.eclipse
.ui
.PlatformUI
;
48 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
49 import org
.eclipse
.ui
.dialogs
.SelectionStatusDialog
;
52 * Implementation of a dialog box to rename a trace.
55 * @author Francois Chouinard
57 public class RenameTraceDialog
extends SelectionStatusDialog
{
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
63 private final TmfTraceElement fTrace
;
64 private Text fNewTraceNameText
;
65 private String fNewTraceName
;
66 private final IContainer fTraceFolder
;
67 private final TmfProjectElement fProject
;
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
74 * @param shell The parent shell
75 * @param trace The trace element to rename
77 public RenameTraceDialog(Shell shell
, TmfTraceElement trace
) {
80 TmfTraceFolder folder
= (TmfTraceFolder
) trace
.getParent();
81 fTraceFolder
= folder
.getResource();
82 fProject
= trace
.getProject();
83 setTitle(Messages
.RenameTraceDialog_DialogTitle
);
84 setStatusLineAboveButtons(true);
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
92 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
95 protected Control
createDialogArea(Composite parent
) {
96 Composite composite
= (Composite
) super.createDialogArea(parent
);
97 composite
.setLayout(new GridLayout());
98 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
100 createNewTraceNameGroup(composite
);
104 private void createNewTraceNameGroup(Composite parent
) {
105 Font font
= parent
.getFont();
106 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
107 GridLayout layout
= new GridLayout();
108 layout
.numColumns
= 2;
109 folderGroup
.setLayout(layout
);
110 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
112 // Old trace name label
113 Label oldTraceLabel
= new Label(folderGroup
, SWT
.NONE
);
114 oldTraceLabel
.setFont(font
);
115 oldTraceLabel
.setText(Messages
.RenameTraceDialog_TraceName
);
117 // Old trace name field
118 Text oldTraceName
= new Text(folderGroup
, SWT
.BORDER
);
119 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
120 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
121 oldTraceName
.setLayoutData(data
);
122 oldTraceName
.setFont(font
);
123 oldTraceName
.setText(fTrace
.getName());
124 oldTraceName
.setEnabled(false);
126 // New trace name label
127 Label newTaceLabel
= new Label(folderGroup
, SWT
.NONE
);
128 newTaceLabel
.setFont(font
);
129 newTaceLabel
.setText(Messages
.RenameTraceDialog_TraceNewName
);
131 // New trace name entry field
132 fNewTraceNameText
= new Text(folderGroup
, SWT
.BORDER
);
133 data
= new GridData(GridData
.FILL_HORIZONTAL
);
134 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
135 fNewTraceNameText
.setLayoutData(data
);
136 fNewTraceNameText
.setFont(font
);
137 fNewTraceNameText
.addListener(SWT
.Modify
, new Listener() {
139 public void handleEvent(Event event
) {
140 validateNewTraceName();
146 * Returns the new trace name
147 * @return the new trace name
149 public String
getNewTraceName() {
150 return fNewTraceName
;
153 private void validateNewTraceName() {
155 fNewTraceName
= fNewTraceNameText
.getText();
156 IWorkspace workspace
= fTraceFolder
.getWorkspace();
157 IStatus nameStatus
= workspace
.validateName(fNewTraceName
, IResource
.FOLDER
);
159 if ("".equals(fNewTraceName
)) { //$NON-NLS-1$
160 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
,
161 Messages
.Dialog_EmptyNameError
, null));
165 if (!nameStatus
.isOK()) {
166 updateStatus(nameStatus
);
170 IPath path
= new Path(fNewTraceName
);
171 if (fTraceFolder
.getFolder(path
).exists() || fTraceFolder
.getFile(path
).exists()) {
172 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
,
173 Messages
.Dialog_ExistingNameError
, null));
177 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
180 // ------------------------------------------------------------------------
181 // SelectionStatusDialog
182 // ------------------------------------------------------------------------
185 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
188 protected void computeResult() {
192 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#create()
195 public void create() {
197 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
201 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
204 protected void okPressed() {
205 IResource trace
= renameTrace(fNewTraceNameText
.getText());
209 setSelectionResult(new IResource
[] { trace
});
212 if (fProject
!= null) {
217 private IResource
renameTrace(final String newName
) {
219 final IPath oldPath
= fTrace
.getResource().getFullPath();
220 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
222 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
224 public void execute(IProgressMonitor monitor
) throws CoreException
{
226 monitor
.beginTask("", 1000); //$NON-NLS-1$
227 if (monitor
.isCanceled()) {
228 throw new OperationCanceledException();
230 if (fTrace
.getResource() instanceof IFolder
) {
231 IFolder folder
= (IFolder
) fTrace
.getResource();
232 IFile bookmarksFile
= folder
.getFile(fTrace
.getName() + '_');
233 IFile newBookmarksFile
= folder
.getFile(newName
+ '_');
234 if (bookmarksFile
.exists()) {
235 if (!newBookmarksFile
.exists()) {
236 IPath newBookmarksPath
= newBookmarksFile
.getFullPath();
237 bookmarksFile
.move(newBookmarksPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
242 fTrace
.renameSupplementaryFolder(newName
);
243 fTrace
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
244 if (monitor
.isCanceled()) {
245 throw new OperationCanceledException();
254 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
255 } catch (InterruptedException exception
) {
257 } catch (InvocationTargetException exception
) {
258 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
260 } catch (RuntimeException exception
) {
264 return fTrace
.getResource();