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