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
.IFolder
;
19 import org
.eclipse
.core
.resources
.IResource
;
20 import org
.eclipse
.core
.resources
.IWorkspace
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.core
.runtime
.IPath
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
26 import org
.eclipse
.core
.runtime
.Path
;
27 import org
.eclipse
.core
.runtime
.Status
;
28 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
29 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.TmfUiPlugin
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentFolder
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
34 import org
.eclipse
.osgi
.util
.NLS
;
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 * <b><u>RenameExperimentDialog</u></b>
54 public class RenameExperimentDialog
extends SelectionStatusDialog
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 private final TmfExperimentElement fExperiment
;
61 private Text fNewExperimentName
;
62 private IContainer fExperimentFolder
;
63 private TmfProjectElement fProject
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
69 public RenameExperimentDialog(Shell shell
, TmfExperimentElement experiment
) {
71 fExperiment
= experiment
;
72 TmfExperimentFolder folder
= (TmfExperimentFolder
) experiment
.getParent();
73 fExperimentFolder
= folder
.getResource();
74 fProject
= experiment
.getProject();
75 setTitle(Messages
.RenameExperimentDialog_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 createNewExperimentNameGroup(composite
);
93 private void createNewExperimentNameGroup(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 experiment name label
102 Label oldExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
103 oldExperimentLabel
.setFont(font
);
104 oldExperimentLabel
.setText(Messages
.RenameExperimentDialog_ExperimentName
);
106 // Old experiment name field
107 Text oldExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
108 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
109 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
110 oldExperimentName
.setLayoutData(data
);
111 oldExperimentName
.setFont(font
);
112 oldExperimentName
.setText(fExperiment
.getName());
113 oldExperimentName
.setEnabled(false);
115 // New experiment name label
116 Label newExperimentLabel
= new Label(folderGroup
, SWT
.NONE
);
117 newExperimentLabel
.setFont(font
);
118 newExperimentLabel
.setText(Messages
.RenameExperimentDialog_ExperimentNewName
);
120 // New experiment name entry field
121 fNewExperimentName
= new Text(folderGroup
, SWT
.BORDER
);
122 data
= new GridData(GridData
.FILL_HORIZONTAL
);
123 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
124 fNewExperimentName
.setLayoutData(data
);
125 fNewExperimentName
.setFont(font
);
126 fNewExperimentName
.addListener(SWT
.Modify
, new Listener() {
128 public void handleEvent(Event event
) {
129 validateNewExperimentName();
134 private void validateNewExperimentName() {
136 String name
= fNewExperimentName
.getText();
137 IWorkspace workspace
= fExperimentFolder
.getWorkspace();
138 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
140 if ("".equals(name
)) { //$NON-NLS-1$
141 updateStatus(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_EmptyNameError
, null));
145 if (nameStatus
.isOK() == false) {
146 updateStatus(nameStatus
);
150 IPath path
= new Path(name
);
151 if (fExperimentFolder
.getFolder(path
).exists() || fExperimentFolder
.getFile(path
).exists()) {
152 updateStatus(new Status(IStatus
.ERROR
, TmfUiPlugin
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_ExistingNameError
, null));
156 updateStatus(new Status(IStatus
.OK
, TmfUiPlugin
.PLUGIN_ID
, "")); //$NON-NLS-1$
159 // ------------------------------------------------------------------------
160 // SelectionStatusDialog
161 // ------------------------------------------------------------------------
164 protected void computeResult() {
168 public void create() {
170 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
174 protected void okPressed() {
175 IFolder folder
= renameExperiment(fNewExperimentName
.getText());
176 if (folder
== null) {
179 setSelectionResult(new IFolder
[] { folder
});
182 if (fProject
!= null) {
187 private IFolder
renameExperiment(String newName
) {
189 IPath oldPath
= fExperiment
.getResource().getFullPath();
190 final IPath newPath
= oldPath
.append("../" + newName
); //$NON-NLS-1$
192 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
194 public void execute(IProgressMonitor monitor
) throws CoreException
{
196 monitor
.beginTask("", 1000); //$NON-NLS-1$
197 if (monitor
.isCanceled()) {
198 throw new OperationCanceledException();
200 fExperiment
.getResource().move(newPath
, IResource
.FORCE
| IResource
.SHALLOW
, null);
201 if (monitor
.isCanceled()) {
202 throw new OperationCanceledException();
210 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
211 } catch (InterruptedException exception
) {
213 } catch (InvocationTargetException exception
) {
214 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
216 } catch (RuntimeException exception
) {
220 return fExperiment
.getResource();