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
.IResource
;
19 import org
.eclipse
.core
.resources
.IWorkspace
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.core
.runtime
.IPath
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.IStatus
;
24 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
25 import org
.eclipse
.core
.runtime
.Path
;
26 import org
.eclipse
.core
.runtime
.Status
;
27 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
28 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.TmfUiPlugin
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
33 import org
.eclipse
.osgi
.util
.NLS
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.graphics
.Font
;
36 import org
.eclipse
.swt
.layout
.GridData
;
37 import org
.eclipse
.swt
.layout
.GridLayout
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Control
;
40 import org
.eclipse
.swt
.widgets
.Event
;
41 import org
.eclipse
.swt
.widgets
.Label
;
42 import org
.eclipse
.swt
.widgets
.Listener
;
43 import org
.eclipse
.swt
.widgets
.Shell
;
44 import org
.eclipse
.swt
.widgets
.Text
;
45 import org
.eclipse
.ui
.PlatformUI
;
46 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
47 import org
.eclipse
.ui
.dialogs
.SelectionStatusDialog
;
50 * <b><u>RenameTraceDialog</u></b>
53 public class RenameTraceDialog
extends SelectionStatusDialog
{
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 private final TmfTraceElement fTrace
;
60 private Text fNewTraceNameText
;
61 private String fNewTraceName
;
62 private IContainer fTraceFolder
;
63 private TmfProjectElement fProject
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
69 public RenameTraceDialog(Shell shell
, TmfTraceElement trace
) {
72 TmfTraceFolder folder
= (TmfTraceFolder
) trace
.getParent();
73 fTraceFolder
= folder
.getResource();
74 fProject
= trace
.getProject();
75 setTitle(Messages
.RenameTraceDialog_DialogTitle
);
76 setStatusLineAboveButtons(true);
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
84 protected Control
createDialogArea(Composite parent
) {
85 Composite composite
= (Composite
) super.createDialogArea(parent
);
86 composite
.setLayout(new GridLayout());
87 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
89 createNewTraceNameGroup(composite
);
93 private void createNewTraceNameGroup(Composite parent
) {
94 Font font
= parent
.getFont();
95 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
96 GridLayout layout
= new GridLayout();
97 layout
.numColumns
= 2;
98 folderGroup
.setLayout(layout
);
99 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
101 // Old trace name label
102 Label oldTraceLabel
= new Label(folderGroup
, SWT
.NONE
);
103 oldTraceLabel
.setFont(font
);
104 oldTraceLabel
.setText(Messages
.RenameTraceDialog_TraceName
);
106 // Old trace name field
107 Text oldTraceName
= new Text(folderGroup
, SWT
.BORDER
);
108 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
109 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
110 oldTraceName
.setLayoutData(data
);
111 oldTraceName
.setFont(font
);
112 oldTraceName
.setText(fTrace
.getName());
113 oldTraceName
.setEnabled(false);
115 // New trace name label
116 Label newTaceLabel
= new Label(folderGroup
, SWT
.NONE
);
117 newTaceLabel
.setFont(font
);
118 newTaceLabel
.setText(Messages
.RenameTraceDialog_TraceNewName
);
120 // New trace name entry field
121 fNewTraceNameText
= new Text(folderGroup
, SWT
.BORDER
);
122 data
= new GridData(GridData
.FILL_HORIZONTAL
);
123 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
124 fNewTraceNameText
.setLayoutData(data
);
125 fNewTraceNameText
.setFont(font
);
126 fNewTraceNameText
.addListener(SWT
.Modify
, new Listener() {
128 public void handleEvent(Event event
) {
129 validateNewTraceName();
134 public String
getNewTraceName() {
135 return fNewTraceName
;
138 private void validateNewTraceName() {
140 fNewTraceName
= fNewTraceNameText
.getText();
141 IWorkspace workspace
= fTraceFolder
.getWorkspace();
142 IStatus nameStatus
= workspace
.validateName(fNewTraceName
, IResource
.FOLDER
);
144 if ("".equals(fNewTraceName
)) { //$NON-NLS-1$
145 updateStatus(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, IStatus
.ERROR
,
146 Messages
.Dialog_EmptyNameError
, null));
150 if (nameStatus
.isOK() == false) {
151 updateStatus(nameStatus
);
155 IPath path
= new Path(fNewTraceName
);
156 if (fTraceFolder
.getFolder(path
).exists() || fTraceFolder
.getFile(path
).exists()) {
157 updateStatus(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, IStatus
.ERROR
,
158 Messages
.Dialog_ExistingNameError
, null));
162 updateStatus(new Status(IStatus
.OK
, TmfUiPlugin
.PLUGIN_ID
, "")); //$NON-NLS-1$
165 // ------------------------------------------------------------------------
166 // SelectionStatusDialog
167 // ------------------------------------------------------------------------
170 protected void computeResult() {
174 public void create() {
176 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
180 protected void okPressed() {
181 IResource trace
= renameTrace(fNewTraceNameText
.getText());
185 setSelectionResult(new IResource
[] { trace
});
188 if (fProject
!= null) {
193 private IResource
renameTrace(String newName
) {
195 IPath oldPath
= fTrace
.getResource().getFullPath();
196 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
198 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
200 public void execute(IProgressMonitor monitor
) throws CoreException
{
202 monitor
.beginTask("", 1000); //$NON-NLS-1$
203 if (monitor
.isCanceled()) {
204 throw new OperationCanceledException();
206 fTrace
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
207 if (monitor
.isCanceled()) {
208 throw new OperationCanceledException();
217 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
218 } catch (InterruptedException exception
) {
220 } catch (InvocationTargetException exception
) {
221 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
223 } catch (RuntimeException exception
) {
227 return fTrace
.getResource();