1 /*******************************************************************************
2 * Copyright (c) 2011 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
.tmf
.ui
.TmfUiPlugin
;
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 * <b><u>RenameTraceDialog</u></b>
55 public class RenameTraceDialog
extends SelectionStatusDialog
{
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 private final TmfTraceElement fTrace
;
62 private Text fNewTraceNameText
;
63 private String fNewTraceName
;
64 private IContainer fTraceFolder
;
65 private TmfProjectElement fProject
;
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
71 public RenameTraceDialog(Shell shell
, TmfTraceElement trace
) {
74 TmfTraceFolder folder
= (TmfTraceFolder
) trace
.getParent();
75 fTraceFolder
= folder
.getResource();
76 fProject
= trace
.getProject();
77 setTitle(Messages
.RenameTraceDialog_DialogTitle
);
78 setStatusLineAboveButtons(true);
81 // ------------------------------------------------------------------------
83 // ------------------------------------------------------------------------
86 protected Control
createDialogArea(Composite parent
) {
87 Composite composite
= (Composite
) super.createDialogArea(parent
);
88 composite
.setLayout(new GridLayout());
89 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
91 createNewTraceNameGroup(composite
);
95 private void createNewTraceNameGroup(Composite parent
) {
96 Font font
= parent
.getFont();
97 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
98 GridLayout layout
= new GridLayout();
99 layout
.numColumns
= 2;
100 folderGroup
.setLayout(layout
);
101 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
103 // Old trace name label
104 Label oldTraceLabel
= new Label(folderGroup
, SWT
.NONE
);
105 oldTraceLabel
.setFont(font
);
106 oldTraceLabel
.setText(Messages
.RenameTraceDialog_TraceName
);
108 // Old trace name field
109 Text oldTraceName
= new Text(folderGroup
, SWT
.BORDER
);
110 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
111 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
112 oldTraceName
.setLayoutData(data
);
113 oldTraceName
.setFont(font
);
114 oldTraceName
.setText(fTrace
.getName());
115 oldTraceName
.setEnabled(false);
117 // New trace name label
118 Label newTaceLabel
= new Label(folderGroup
, SWT
.NONE
);
119 newTaceLabel
.setFont(font
);
120 newTaceLabel
.setText(Messages
.RenameTraceDialog_TraceNewName
);
122 // New trace name entry field
123 fNewTraceNameText
= new Text(folderGroup
, SWT
.BORDER
);
124 data
= new GridData(GridData
.FILL_HORIZONTAL
);
125 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
126 fNewTraceNameText
.setLayoutData(data
);
127 fNewTraceNameText
.setFont(font
);
128 fNewTraceNameText
.addListener(SWT
.Modify
, new Listener() {
130 public void handleEvent(Event event
) {
131 validateNewTraceName();
136 public String
getNewTraceName() {
137 return fNewTraceName
;
140 private void validateNewTraceName() {
142 fNewTraceName
= fNewTraceNameText
.getText();
143 IWorkspace workspace
= fTraceFolder
.getWorkspace();
144 IStatus nameStatus
= workspace
.validateName(fNewTraceName
, IResource
.FOLDER
);
146 if ("".equals(fNewTraceName
)) { //$NON-NLS-1$
147 updateStatus(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, IStatus
.ERROR
,
148 Messages
.Dialog_EmptyNameError
, null));
152 if (nameStatus
.isOK() == false) {
153 updateStatus(nameStatus
);
157 IPath path
= new Path(fNewTraceName
);
158 if (fTraceFolder
.getFolder(path
).exists() || fTraceFolder
.getFile(path
).exists()) {
159 updateStatus(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, IStatus
.ERROR
,
160 Messages
.Dialog_ExistingNameError
, null));
164 updateStatus(new Status(IStatus
.OK
, TmfUiPlugin
.PLUGIN_ID
, "")); //$NON-NLS-1$
167 // ------------------------------------------------------------------------
168 // SelectionStatusDialog
169 // ------------------------------------------------------------------------
172 protected void computeResult() {
176 public void create() {
178 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
182 protected void okPressed() {
183 IResource trace
= renameTrace(fNewTraceNameText
.getText());
187 setSelectionResult(new IResource
[] { trace
});
190 if (fProject
!= null) {
195 private IResource
renameTrace(final String newName
) {
197 final IPath oldPath
= fTrace
.getResource().getFullPath();
198 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
200 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
202 public void execute(IProgressMonitor monitor
) throws CoreException
{
204 monitor
.beginTask("", 1000); //$NON-NLS-1$
205 if (monitor
.isCanceled()) {
206 throw new OperationCanceledException();
208 if (fTrace
.getResource() instanceof IFolder
) {
209 IFolder folder
= (IFolder
) fTrace
.getResource();
210 IFile bookmarksFile
= folder
.getFile(fTrace
.getName() + '_');
211 IFile newBookmarksFile
= folder
.getFile(newName
+ '_');
212 if (bookmarksFile
.exists()) {
213 if (!newBookmarksFile
.exists()) {
214 IPath newBookmarksPath
= newBookmarksFile
.getFullPath();
215 bookmarksFile
.move(newBookmarksPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
219 fTrace
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
220 if (monitor
.isCanceled()) {
221 throw new OperationCanceledException();
230 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
231 } catch (InterruptedException exception
) {
233 } catch (InvocationTargetException exception
) {
234 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
236 } catch (RuntimeException exception
) {
240 return fTrace
.getResource();