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