tmf: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / wizards / RenameExperimentDialog.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Copied and adapted from NewFolderDialog
11 * Patrick Tasse - Close editors to release resources
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ui.project.wizards;
15
16 import java.lang.reflect.InvocationTargetException;
17
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.swt.SWT;
33 import org.eclipse.swt.graphics.Font;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Event;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Listener;
41 import org.eclipse.swt.widgets.Shell;
42 import org.eclipse.swt.widgets.Text;
43 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
44 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
45 import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentFolder;
46 import org.eclipse.ui.PlatformUI;
47 import org.eclipse.ui.actions.WorkspaceModifyOperation;
48 import org.eclipse.ui.dialogs.SelectionStatusDialog;
49
50 /**
51 * Implementation of a dialog box to rename an experiment.
52 * <p>
53 *
54 * @version 1.0
55 * @author Francois Chouinard
56 */
57 public class RenameExperimentDialog extends SelectionStatusDialog {
58
59 // ------------------------------------------------------------------------
60 // Members
61 // ------------------------------------------------------------------------
62
63 private final TmfExperimentElement fExperiment;
64 private Text fNewExperimentName;
65 private final IContainer fExperimentFolder;
66
67 // ------------------------------------------------------------------------
68 // Constructor
69 // ------------------------------------------------------------------------
70
71 /**
72 * Constructor
73 *
74 * @param shell
75 * The parent shell
76 * @param experiment
77 * The experiment element rename
78 */
79 public RenameExperimentDialog(Shell shell, TmfExperimentElement experiment) {
80 super(shell);
81 fExperiment = experiment;
82 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
83 fExperimentFolder = folder.getResource();
84 setTitle(Messages.RenameExperimentDialog_DialogTitle);
85 setStatusLineAboveButtons(true);
86 }
87
88 // ------------------------------------------------------------------------
89 // Dialog
90 // ------------------------------------------------------------------------
91
92 @Override
93 protected Control createDialogArea(Composite parent) {
94 Composite composite = (Composite) super.createDialogArea(parent);
95 composite.setLayout(new GridLayout());
96 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
97
98 createNewExperimentNameGroup(composite);
99 return composite;
100 }
101
102 private void createNewExperimentNameGroup(Composite parent) {
103 Font font = parent.getFont();
104 Composite folderGroup = new Composite(parent, SWT.NONE);
105 GridLayout layout = new GridLayout();
106 layout.numColumns = 2;
107 folderGroup.setLayout(layout);
108 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
109
110 // Old experiment name label
111 Label oldExperimentLabel = new Label(folderGroup, SWT.NONE);
112 oldExperimentLabel.setFont(font);
113 oldExperimentLabel.setText(Messages.RenameExperimentDialog_ExperimentName);
114
115 // Old experiment name field
116 Text oldExperimentName = new Text(folderGroup, SWT.BORDER);
117 GridData data = new GridData(GridData.FILL_HORIZONTAL);
118 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
119 oldExperimentName.setLayoutData(data);
120 oldExperimentName.setFont(font);
121 oldExperimentName.setText(fExperiment.getName());
122 oldExperimentName.setEnabled(false);
123
124 // New experiment name label
125 Label newExperimentLabel = new Label(folderGroup, SWT.NONE);
126 newExperimentLabel.setFont(font);
127 newExperimentLabel.setText(Messages.RenameExperimentDialog_ExperimentNewName);
128
129 // New experiment name entry field
130 fNewExperimentName = new Text(folderGroup, SWT.BORDER);
131 data = new GridData(GridData.FILL_HORIZONTAL);
132 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
133 fNewExperimentName.setLayoutData(data);
134 fNewExperimentName.setFont(font);
135 fNewExperimentName.addListener(SWT.Modify, new Listener() {
136 @Override
137 public void handleEvent(Event event) {
138 validateNewExperimentName();
139 }
140 });
141 }
142
143 private void validateNewExperimentName() {
144
145 String name = fNewExperimentName.getText();
146 IWorkspace workspace = fExperimentFolder.getWorkspace();
147 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
148
149 if ("".equals(name)) { //$NON-NLS-1$
150 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null));
151 return;
152 }
153
154 if (!nameStatus.isOK()) {
155 updateStatus(nameStatus);
156 return;
157 }
158
159 IPath path = new Path(name);
160 if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) {
161 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null));
162 return;
163 }
164
165 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
166 }
167
168 // ------------------------------------------------------------------------
169 // SelectionStatusDialog
170 // ------------------------------------------------------------------------
171
172 @Override
173 protected void computeResult() {
174 }
175
176 @Override
177 public void create() {
178 super.create();
179 getButton(IDialogConstants.OK_ID).setEnabled(false);
180 }
181
182 @Override
183 protected void okPressed() {
184 IFolder folder = renameExperiment(fNewExperimentName.getText());
185 if (folder == null) {
186 return;
187 }
188 setSelectionResult(new IFolder[] { folder });
189 super.okPressed();
190 }
191
192 private IFolder renameExperiment(final String newName) {
193
194 IPath oldPath = fExperiment.getResource().getFullPath();
195 final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$
196
197 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
198 @Override
199 public void execute(IProgressMonitor monitor) throws CoreException {
200 try {
201 monitor.beginTask("", 1000); //$NON-NLS-1$
202 if (monitor.isCanceled()) {
203 throw new OperationCanceledException();
204 }
205 // Close the experiment if open
206 fExperiment.closeEditors();
207
208 IFolder folder = fExperiment.getResource();
209 IFile bookmarksFile = fExperiment.getBookmarksFile();
210 IFile newBookmarksFile = folder.getFile(bookmarksFile.getName().replace(fExperiment.getName(), newName));
211 if (bookmarksFile.exists()) {
212 if (!newBookmarksFile.exists()) {
213 IPath newBookmarksPath = newBookmarksFile.getFullPath();
214 bookmarksFile.move(newBookmarksPath, IResource.FORCE | IResource.SHALLOW, null);
215 }
216 }
217
218 fExperiment.renameSupplementaryFolder(newName);
219 fExperiment.getResource().move(newPath, IResource.FORCE | IResource.SHALLOW, null);
220 if (monitor.isCanceled()) {
221 throw new OperationCanceledException();
222 }
223 } finally {
224 monitor.done();
225 }
226 }
227 };
228 try {
229 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
230 } catch (InterruptedException exception) {
231 return null;
232 } catch (InvocationTargetException exception) {
233 MessageDialog.openError(getShell(), "", exception.getTargetException().getMessage()); //$NON-NLS-1$
234 return null;
235 } catch (RuntimeException exception) {
236 return null;
237 }
238
239 return fExperiment.getResource();
240 }
241
242 }
This page took 0.037792 seconds and 5 git commands to generate.