tmf/lttng: Remove unneeded (non-Javadoc) comments
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / RenameTraceDialog.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.ITmfProjectModelElement;
33 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
34 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder;
35 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
36 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
37 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.graphics.Font;
40 import org.eclipse.swt.layout.GridData;
41 import org.eclipse.swt.layout.GridLayout;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Control;
44 import org.eclipse.swt.widgets.Event;
45 import org.eclipse.swt.widgets.Label;
46 import org.eclipse.swt.widgets.Listener;
47 import org.eclipse.swt.widgets.Shell;
48 import org.eclipse.swt.widgets.Text;
49 import org.eclipse.ui.IEditorReference;
50 import org.eclipse.ui.IWorkbench;
51 import org.eclipse.ui.IWorkbenchPage;
52 import org.eclipse.ui.IWorkbenchWindow;
53 import org.eclipse.ui.PlatformUI;
54 import org.eclipse.ui.actions.WorkspaceModifyOperation;
55 import org.eclipse.ui.dialogs.SelectionStatusDialog;
56 import org.eclipse.ui.part.FileEditorInput;
57
58 /**
59 * Implementation of a dialog box to rename a trace.
60 * <p>
61 * @version 1.0
62 * @author Francois Chouinard
63 */
64 public class RenameTraceDialog extends SelectionStatusDialog {
65
66 // ------------------------------------------------------------------------
67 // Members
68 // ------------------------------------------------------------------------
69
70 private final TmfTraceElement fTrace;
71 private Text fNewTraceNameText;
72 private String fNewTraceName;
73 private final IContainer fTraceFolder;
74 private final TmfProjectElement fProject;
75
76 // ------------------------------------------------------------------------
77 // Constructor
78 // ------------------------------------------------------------------------
79 /**
80 * Constructor
81 * @param shell The parent shell
82 * @param trace The trace element to rename
83 */
84 public RenameTraceDialog(Shell shell, TmfTraceElement trace) {
85 super(shell);
86 fTrace = trace;
87 TmfTraceFolder folder = (TmfTraceFolder) trace.getParent();
88 fTraceFolder = folder.getResource();
89 fProject = trace.getProject();
90 setTitle(Messages.RenameTraceDialog_DialogTitle);
91 setStatusLineAboveButtons(true);
92 }
93
94 // ------------------------------------------------------------------------
95 // Dialog
96 // ------------------------------------------------------------------------
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 createNewTraceNameGroup(composite);
105 return composite;
106 }
107
108 private void createNewTraceNameGroup(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 trace name label
117 Label oldTraceLabel = new Label(folderGroup, SWT.NONE);
118 oldTraceLabel.setFont(font);
119 oldTraceLabel.setText(Messages.RenameTraceDialog_TraceName);
120
121 // Old trace name field
122 Text oldTraceName = new Text(folderGroup, SWT.BORDER);
123 GridData data = new GridData(GridData.FILL_HORIZONTAL);
124 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
125 oldTraceName.setLayoutData(data);
126 oldTraceName.setFont(font);
127 oldTraceName.setText(fTrace.getName());
128 oldTraceName.setEnabled(false);
129
130 // New trace name label
131 Label newTaceLabel = new Label(folderGroup, SWT.NONE);
132 newTaceLabel.setFont(font);
133 newTaceLabel.setText(Messages.RenameTraceDialog_TraceNewName);
134
135 // New trace name entry field
136 fNewTraceNameText = new Text(folderGroup, SWT.BORDER);
137 data = new GridData(GridData.FILL_HORIZONTAL);
138 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
139 fNewTraceNameText.setLayoutData(data);
140 fNewTraceNameText.setFont(font);
141 fNewTraceNameText.addListener(SWT.Modify, new Listener() {
142 @Override
143 public void handleEvent(Event event) {
144 validateNewTraceName();
145 }
146 });
147 }
148
149 /**
150 * Returns the new trace name
151 * @return the new trace name
152 */
153 public String getNewTraceName() {
154 return fNewTraceName;
155 }
156
157 private void validateNewTraceName() {
158
159 fNewTraceName = fNewTraceNameText.getText();
160 IWorkspace workspace = fTraceFolder.getWorkspace();
161 IStatus nameStatus = workspace.validateName(fNewTraceName, IResource.FOLDER);
162
163 if ("".equals(fNewTraceName)) { //$NON-NLS-1$
164 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
165 Messages.Dialog_EmptyNameError, null));
166 return;
167 }
168
169 if (!nameStatus.isOK()) {
170 updateStatus(nameStatus);
171 return;
172 }
173
174 IPath path = new Path(fNewTraceName);
175 if (fTraceFolder.getFolder(path).exists() || fTraceFolder.getFile(path).exists()) {
176 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
177 Messages.Dialog_ExistingNameError, null));
178 return;
179 }
180
181 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
182 }
183
184 // ------------------------------------------------------------------------
185 // SelectionStatusDialog
186 // ------------------------------------------------------------------------
187
188 @Override
189 protected void computeResult() {
190 }
191
192 @Override
193 public void create() {
194 super.create();
195 getButton(IDialogConstants.OK_ID).setEnabled(false);
196 }
197
198 @Override
199 protected void okPressed() {
200 IResource trace = renameTrace(fNewTraceNameText.getText());
201 if (trace == null) {
202 return;
203 }
204 setSelectionResult(new IResource[] { trace });
205 super.okPressed();
206
207 if (fProject != null) {
208 fProject.refresh();
209 }
210 }
211
212 private IResource renameTrace(final String newName) {
213
214 final IPath oldPath = fTrace.getResource().getFullPath();
215 final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$
216
217 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
218 @Override
219 public void execute(IProgressMonitor monitor) throws CoreException {
220 try {
221 monitor.beginTask("", 1000); //$NON-NLS-1$
222 if (monitor.isCanceled()) {
223 throw new OperationCanceledException();
224 }
225 // Close the trace if open
226 IFile file = fTrace.getBookmarksFile();
227 FileEditorInput input = new FileEditorInput(file);
228 IWorkbench wb = PlatformUI.getWorkbench();
229 for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
230 for (IWorkbenchPage wbPage : wbWindow.getPages()) {
231 for (IEditorReference editorReference : wbPage.getEditorReferences()) {
232 if (editorReference.getEditorInput().equals(input)) {
233 wbPage.closeEditor(editorReference.getEditor(false), false);
234 }
235 }
236 }
237 }
238 TmfExperimentFolder experimentFolder = fTrace.getProject().getExperimentsFolder();
239 for (final ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
240 for (final ITmfProjectModelElement trace : experiment.getChildren()) {
241 if (trace.equals(fTrace)) {
242 // Close the experiment if open
243 file = ((TmfExperimentElement) experiment).getBookmarksFile();
244 input = new FileEditorInput(file);
245 for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
246 for (IWorkbenchPage wbPage : wbWindow.getPages()) {
247 for (IEditorReference editorReference : wbPage.getEditorReferences()) {
248 if (editorReference.getEditorInput().equals(input)) {
249 wbPage.closeEditor(editorReference.getEditor(false), false);
250 }
251 }
252 }
253 }
254 }
255 }
256 }
257
258 if (fTrace.getResource() instanceof IFolder) {
259 IFolder folder = (IFolder) fTrace.getResource();
260 IFile bookmarksFile = fTrace.getBookmarksFile();
261 IFile newBookmarksFile = folder.getFile(bookmarksFile.getName().replace(fTrace.getName(), newName));
262 if (bookmarksFile.exists()) {
263 if (!newBookmarksFile.exists()) {
264 IPath newBookmarksPath = newBookmarksFile.getFullPath();
265 bookmarksFile.move(newBookmarksPath, IResource.FORCE | IResource.SHALLOW, null);
266 }
267 }
268 }
269
270 fTrace.renameSupplementaryFolder(newName);
271 fTrace.getResource().move(newPath, IResource.FORCE | IResource.SHALLOW, null);
272 if (monitor.isCanceled()) {
273 throw new OperationCanceledException();
274 }
275 } finally {
276 monitor.done();
277 }
278 }
279 };
280
281 try {
282 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
283 } catch (InterruptedException exception) {
284 return null;
285 } catch (InvocationTargetException exception) {
286 MessageDialog.openError(getShell(), "", exception.getTargetException().getMessage()); //$NON-NLS-1$
287 return null;
288 } catch (RuntimeException exception) {
289 return null;
290 }
291
292 return fTrace.getResource();
293 }
294
295 }
This page took 0.038162 seconds and 6 git commands to generate.