tmf/lttng: Update 2014 copyrights
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / RenameTraceDialog.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.linuxtools.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.linuxtools.internal.tmf.ui.Activator;
33 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
34 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
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;
49
50 /**
51 * Implementation of a dialog box to rename a trace.
52 * <p>
53 * @version 1.0
54 * @author Francois Chouinard
55 */
56 public class RenameTraceDialog extends SelectionStatusDialog {
57
58 // ------------------------------------------------------------------------
59 // Members
60 // ------------------------------------------------------------------------
61
62 private final TmfTraceElement fTrace;
63 private Text fNewTraceNameText;
64 private String fNewTraceName;
65 private final IContainer fTraceFolder;
66
67 // ------------------------------------------------------------------------
68 // Constructor
69 // ------------------------------------------------------------------------
70 /**
71 * Constructor
72 * @param shell The parent shell
73 * @param trace The trace element to rename
74 */
75 public RenameTraceDialog(Shell shell, TmfTraceElement trace) {
76 super(shell);
77 fTrace = trace;
78 TmfTraceFolder folder = (TmfTraceFolder) trace.getParent();
79 fTraceFolder = folder.getResource();
80 setTitle(Messages.RenameTraceDialog_DialogTitle);
81 setStatusLineAboveButtons(true);
82 }
83
84 // ------------------------------------------------------------------------
85 // Dialog
86 // ------------------------------------------------------------------------
87
88 @Override
89 protected Control createDialogArea(Composite parent) {
90 Composite composite = (Composite) super.createDialogArea(parent);
91 composite.setLayout(new GridLayout());
92 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
93
94 createNewTraceNameGroup(composite);
95 return composite;
96 }
97
98 private void createNewTraceNameGroup(Composite parent) {
99 Font font = parent.getFont();
100 Composite folderGroup = new Composite(parent, SWT.NONE);
101 GridLayout layout = new GridLayout();
102 layout.numColumns = 2;
103 folderGroup.setLayout(layout);
104 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
105
106 // Old trace name label
107 Label oldTraceLabel = new Label(folderGroup, SWT.NONE);
108 oldTraceLabel.setFont(font);
109 oldTraceLabel.setText(Messages.RenameTraceDialog_TraceName);
110
111 // Old trace name field
112 Text oldTraceName = new Text(folderGroup, SWT.BORDER);
113 GridData data = new GridData(GridData.FILL_HORIZONTAL);
114 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
115 oldTraceName.setLayoutData(data);
116 oldTraceName.setFont(font);
117 oldTraceName.setText(fTrace.getName());
118 oldTraceName.setEnabled(false);
119
120 // New trace name label
121 Label newTaceLabel = new Label(folderGroup, SWT.NONE);
122 newTaceLabel.setFont(font);
123 newTaceLabel.setText(Messages.RenameTraceDialog_TraceNewName);
124
125 // New trace name entry field
126 fNewTraceNameText = new Text(folderGroup, SWT.BORDER);
127 data = new GridData(GridData.FILL_HORIZONTAL);
128 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
129 fNewTraceNameText.setLayoutData(data);
130 fNewTraceNameText.setFont(font);
131 fNewTraceNameText.addListener(SWT.Modify, new Listener() {
132 @Override
133 public void handleEvent(Event event) {
134 validateNewTraceName();
135 }
136 });
137 }
138
139 /**
140 * Returns the new trace name
141 * @return the new trace name
142 */
143 public String getNewTraceName() {
144 return fNewTraceName;
145 }
146
147 private void validateNewTraceName() {
148
149 fNewTraceName = fNewTraceNameText.getText();
150 IWorkspace workspace = fTraceFolder.getWorkspace();
151 IStatus nameStatus = workspace.validateName(fNewTraceName, IResource.FOLDER);
152
153 if ("".equals(fNewTraceName)) { //$NON-NLS-1$
154 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
155 Messages.Dialog_EmptyNameError, null));
156 return;
157 }
158
159 if (!nameStatus.isOK()) {
160 updateStatus(nameStatus);
161 return;
162 }
163
164 IPath path = new Path(fNewTraceName);
165 if (fTraceFolder.getFolder(path).exists() || fTraceFolder.getFile(path).exists()) {
166 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
167 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 @Override
179 protected void computeResult() {
180 }
181
182 @Override
183 public void create() {
184 super.create();
185 getButton(IDialogConstants.OK_ID).setEnabled(false);
186 }
187
188 @Override
189 protected void okPressed() {
190 IResource trace = renameTrace(fNewTraceNameText.getText());
191 if (trace == null) {
192 return;
193 }
194 setSelectionResult(new IResource[] { trace });
195 super.okPressed();
196 }
197
198 private IResource renameTrace(final String newName) {
199
200 final IPath oldPath = fTrace.getResource().getFullPath();
201 final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$
202
203 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
204 @Override
205 public void execute(IProgressMonitor monitor) throws CoreException {
206 try {
207 monitor.beginTask("", 1000); //$NON-NLS-1$
208 if (monitor.isCanceled()) {
209 throw new OperationCanceledException();
210 }
211 // Close the trace if open
212 fTrace.closeEditors();
213
214 if (fTrace.getResource() instanceof IFolder) {
215 IFolder folder = (IFolder) fTrace.getResource();
216 IFile bookmarksFile = fTrace.getBookmarksFile();
217 IFile newBookmarksFile = folder.getFile(bookmarksFile.getName().replace(fTrace.getName(), newName));
218 if (bookmarksFile.exists()) {
219 if (!newBookmarksFile.exists()) {
220 IPath newBookmarksPath = newBookmarksFile.getFullPath();
221 bookmarksFile.move(newBookmarksPath, IResource.FORCE | IResource.SHALLOW, null);
222 }
223 }
224 }
225
226 fTrace.renameSupplementaryFolder(newName);
227 fTrace.getResource().move(newPath, IResource.FORCE | IResource.SHALLOW, null);
228 if (monitor.isCanceled()) {
229 throw new OperationCanceledException();
230 }
231 } finally {
232 monitor.done();
233 }
234 }
235 };
236
237 try {
238 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
239 } catch (InterruptedException exception) {
240 return null;
241 } catch (InvocationTargetException exception) {
242 MessageDialog.openError(getShell(), "", exception.getTargetException().getMessage()); //$NON-NLS-1$
243 return null;
244 } catch (RuntimeException exception) {
245 return null;
246 }
247
248 return fTrace.getResource();
249 }
250
251 }
This page took 0.04118 seconds and 6 git commands to generate.