lttng: Rename lttng2 feature/plugins to lttng2.control
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / RenameTraceDialog.java
CommitLineData
12c155f5 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2011, 2014 Ericsson
6256d8ad 3 *
12c155f5
FC
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
6256d8ad 8 *
12c155f5
FC
9 * Contributors:
10 * Francois Chouinard - Copied and adapted from NewFolderDialog
a72a6830 11 * Patrick Tasse - Close editors to release resources
12c155f5
FC
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.tmf.ui.project.wizards;
15
16import java.lang.reflect.InvocationTargetException;
17
18import org.eclipse.core.resources.IContainer;
828e5592
PT
19import org.eclipse.core.resources.IFile;
20import org.eclipse.core.resources.IFolder;
12c155f5
FC
21import org.eclipse.core.resources.IResource;
22import org.eclipse.core.resources.IWorkspace;
23import org.eclipse.core.runtime.CoreException;
24import org.eclipse.core.runtime.IPath;
25import org.eclipse.core.runtime.IProgressMonitor;
26import org.eclipse.core.runtime.IStatus;
27import org.eclipse.core.runtime.OperationCanceledException;
28import org.eclipse.core.runtime.Path;
29import org.eclipse.core.runtime.Status;
30import org.eclipse.jface.dialogs.IDialogConstants;
31import org.eclipse.jface.dialogs.MessageDialog;
8fd82db5 32import org.eclipse.linuxtools.internal.tmf.ui.Activator;
12c155f5
FC
33import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
34import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
12c155f5
FC
35import org.eclipse.swt.SWT;
36import org.eclipse.swt.graphics.Font;
37import org.eclipse.swt.layout.GridData;
38import org.eclipse.swt.layout.GridLayout;
39import org.eclipse.swt.widgets.Composite;
40import org.eclipse.swt.widgets.Control;
41import org.eclipse.swt.widgets.Event;
42import org.eclipse.swt.widgets.Label;
43import org.eclipse.swt.widgets.Listener;
44import org.eclipse.swt.widgets.Shell;
45import org.eclipse.swt.widgets.Text;
46import org.eclipse.ui.PlatformUI;
47import org.eclipse.ui.actions.WorkspaceModifyOperation;
48import org.eclipse.ui.dialogs.SelectionStatusDialog;
49
50/**
b544077e 51 * Implementation of a dialog box to rename a trace.
6256d8ad 52 * <p>
b544077e
BH
53 * @version 1.0
54 * @author Francois Chouinard
12c155f5
FC
55 */
56public class RenameTraceDialog extends SelectionStatusDialog {
57
58 // ------------------------------------------------------------------------
59 // Members
60 // ------------------------------------------------------------------------
61
62 private final TmfTraceElement fTrace;
63 private Text fNewTraceNameText;
64 private String fNewTraceName;
6256d8ad 65 private final IContainer fTraceFolder;
12c155f5
FC
66
67 // ------------------------------------------------------------------------
68 // Constructor
69 // ------------------------------------------------------------------------
b544077e
BH
70 /**
71 * Constructor
72 * @param shell The parent shell
73 * @param trace The trace element to rename
74 */
12c155f5
FC
75 public RenameTraceDialog(Shell shell, TmfTraceElement trace) {
76 super(shell);
77 fTrace = trace;
78 TmfTraceFolder folder = (TmfTraceFolder) trace.getParent();
79 fTraceFolder = folder.getResource();
12c155f5
FC
80 setTitle(Messages.RenameTraceDialog_DialogTitle);
81 setStatusLineAboveButtons(true);
82 }
83
84 // ------------------------------------------------------------------------
85 // Dialog
86 // ------------------------------------------------------------------------
11252342 87
12c155f5
FC
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
b544077e
BH
139 /**
140 * Returns the new trace name
141 * @return the new trace name
142 */
12c155f5
FC
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$
8fd82db5 154 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
12c155f5
FC
155 Messages.Dialog_EmptyNameError, null));
156 return;
157 }
158
9fa32496 159 if (!nameStatus.isOK()) {
12c155f5
FC
160 updateStatus(nameStatus);
161 return;
162 }
163
164 IPath path = new Path(fNewTraceName);
165 if (fTraceFolder.getFolder(path).exists() || fTraceFolder.getFile(path).exists()) {
8fd82db5 166 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
12c155f5
FC
167 Messages.Dialog_ExistingNameError, null));
168 return;
169 }
170
8fd82db5 171 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
12c155f5
FC
172 }
173
174 // ------------------------------------------------------------------------
175 // SelectionStatusDialog
176 // ------------------------------------------------------------------------
11252342 177
12c155f5
FC
178 @Override
179 protected void computeResult() {
180 }
11252342 181
12c155f5
FC
182 @Override
183 public void create() {
184 super.create();
185 getButton(IDialogConstants.OK_ID).setEnabled(false);
186 }
11252342 187
12c155f5
FC
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();
12c155f5
FC
196 }
197
828e5592 198 private IResource renameTrace(final String newName) {
12c155f5 199
828e5592 200 final IPath oldPath = fTrace.getResource().getFullPath();
12c155f5
FC
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 }
c4c81d91 211 // Close the trace if open
a72a6830 212 fTrace.closeEditors();
c4c81d91 213
828e5592
PT
214 if (fTrace.getResource() instanceof IFolder) {
215 IFolder folder = (IFolder) fTrace.getResource();
c4c81d91
PT
216 IFile bookmarksFile = fTrace.getBookmarksFile();
217 IFile newBookmarksFile = folder.getFile(bookmarksFile.getName().replace(fTrace.getName(), newName));
828e5592
PT
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 }
5e4bf87d 225
e12ecd30 226 fTrace.renameSupplementaryFolder(newName);
12c155f5
FC
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) {
c4c81d91 242 MessageDialog.openError(getShell(), "", exception.getTargetException().getMessage()); //$NON-NLS-1$
12c155f5
FC
243 return null;
244 } catch (RuntimeException exception) {
245 return null;
246 }
247
248 return fTrace.getResource();
249 }
250
251}
This page took 0.059104 seconds and 5 git commands to generate.