tmf: Bump plugins version to 2.0 for Kepler branch
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / RenameTraceDialog.java
CommitLineData
12c155f5 1/*******************************************************************************
b544077e 2 * Copyright (c) 2011, 2012 Ericsson
12c155f5
FC
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
13package org.eclipse.linuxtools.tmf.ui.project.wizards;
14
15import java.lang.reflect.InvocationTargetException;
16
17import org.eclipse.core.resources.IContainer;
828e5592
PT
18import org.eclipse.core.resources.IFile;
19import org.eclipse.core.resources.IFolder;
12c155f5
FC
20import org.eclipse.core.resources.IResource;
21import org.eclipse.core.resources.IWorkspace;
22import org.eclipse.core.runtime.CoreException;
23import org.eclipse.core.runtime.IPath;
24import org.eclipse.core.runtime.IProgressMonitor;
25import org.eclipse.core.runtime.IStatus;
26import org.eclipse.core.runtime.OperationCanceledException;
27import org.eclipse.core.runtime.Path;
28import org.eclipse.core.runtime.Status;
29import org.eclipse.jface.dialogs.IDialogConstants;
30import org.eclipse.jface.dialogs.MessageDialog;
8fd82db5 31import org.eclipse.linuxtools.internal.tmf.ui.Activator;
12c155f5
FC
32import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
33import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
34import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
35import org.eclipse.osgi.util.NLS;
36import org.eclipse.swt.SWT;
37import org.eclipse.swt.graphics.Font;
38import org.eclipse.swt.layout.GridData;
39import org.eclipse.swt.layout.GridLayout;
40import org.eclipse.swt.widgets.Composite;
41import org.eclipse.swt.widgets.Control;
42import org.eclipse.swt.widgets.Event;
43import org.eclipse.swt.widgets.Label;
44import org.eclipse.swt.widgets.Listener;
45import org.eclipse.swt.widgets.Shell;
46import org.eclipse.swt.widgets.Text;
47import org.eclipse.ui.PlatformUI;
48import org.eclipse.ui.actions.WorkspaceModifyOperation;
49import org.eclipse.ui.dialogs.SelectionStatusDialog;
50
51/**
b544077e
BH
52 * Implementation of a dialog box to rename a trace.
53 * <p>
54 * @version 1.0
55 * @author Francois Chouinard
12c155f5
FC
56 */
57public class RenameTraceDialog extends SelectionStatusDialog {
58
59 // ------------------------------------------------------------------------
60 // Members
61 // ------------------------------------------------------------------------
62
63 private final TmfTraceElement fTrace;
64 private Text fNewTraceNameText;
65 private String fNewTraceName;
66 private IContainer fTraceFolder;
67 private TmfProjectElement fProject;
68
69 // ------------------------------------------------------------------------
70 // Constructor
71 // ------------------------------------------------------------------------
b544077e
BH
72 /**
73 * Constructor
74 * @param shell The parent shell
75 * @param trace The trace element to rename
76 */
12c155f5
FC
77 public RenameTraceDialog(Shell shell, TmfTraceElement trace) {
78 super(shell);
79 fTrace = trace;
80 TmfTraceFolder folder = (TmfTraceFolder) trace.getParent();
81 fTraceFolder = folder.getResource();
82 fProject = trace.getProject();
83 setTitle(Messages.RenameTraceDialog_DialogTitle);
84 setStatusLineAboveButtons(true);
85 }
86
87 // ------------------------------------------------------------------------
88 // Dialog
89 // ------------------------------------------------------------------------
b544077e
BH
90 /*
91 * (non-Javadoc)
92 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
93 */
12c155f5
FC
94 @Override
95 protected Control createDialogArea(Composite parent) {
96 Composite composite = (Composite) super.createDialogArea(parent);
97 composite.setLayout(new GridLayout());
98 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
99
100 createNewTraceNameGroup(composite);
101 return composite;
102 }
103
104 private void createNewTraceNameGroup(Composite parent) {
105 Font font = parent.getFont();
106 Composite folderGroup = new Composite(parent, SWT.NONE);
107 GridLayout layout = new GridLayout();
108 layout.numColumns = 2;
109 folderGroup.setLayout(layout);
110 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
111
112 // Old trace name label
113 Label oldTraceLabel = new Label(folderGroup, SWT.NONE);
114 oldTraceLabel.setFont(font);
115 oldTraceLabel.setText(Messages.RenameTraceDialog_TraceName);
116
117 // Old trace name field
118 Text oldTraceName = new Text(folderGroup, SWT.BORDER);
119 GridData data = new GridData(GridData.FILL_HORIZONTAL);
120 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
121 oldTraceName.setLayoutData(data);
122 oldTraceName.setFont(font);
123 oldTraceName.setText(fTrace.getName());
124 oldTraceName.setEnabled(false);
125
126 // New trace name label
127 Label newTaceLabel = new Label(folderGroup, SWT.NONE);
128 newTaceLabel.setFont(font);
129 newTaceLabel.setText(Messages.RenameTraceDialog_TraceNewName);
130
131 // New trace name entry field
132 fNewTraceNameText = new Text(folderGroup, SWT.BORDER);
133 data = new GridData(GridData.FILL_HORIZONTAL);
134 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
135 fNewTraceNameText.setLayoutData(data);
136 fNewTraceNameText.setFont(font);
137 fNewTraceNameText.addListener(SWT.Modify, new Listener() {
138 @Override
139 public void handleEvent(Event event) {
140 validateNewTraceName();
141 }
142 });
143 }
144
b544077e
BH
145 /**
146 * Returns the new trace name
147 * @return the new trace name
148 */
12c155f5
FC
149 public String getNewTraceName() {
150 return fNewTraceName;
151 }
152
153 private void validateNewTraceName() {
154
155 fNewTraceName = fNewTraceNameText.getText();
156 IWorkspace workspace = fTraceFolder.getWorkspace();
157 IStatus nameStatus = workspace.validateName(fNewTraceName, IResource.FOLDER);
158
159 if ("".equals(fNewTraceName)) { //$NON-NLS-1$
8fd82db5 160 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
12c155f5
FC
161 Messages.Dialog_EmptyNameError, null));
162 return;
163 }
164
9fa32496 165 if (!nameStatus.isOK()) {
12c155f5
FC
166 updateStatus(nameStatus);
167 return;
168 }
169
170 IPath path = new Path(fNewTraceName);
171 if (fTraceFolder.getFolder(path).exists() || fTraceFolder.getFile(path).exists()) {
8fd82db5 172 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
12c155f5
FC
173 Messages.Dialog_ExistingNameError, null));
174 return;
175 }
176
8fd82db5 177 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
12c155f5
FC
178 }
179
180 // ------------------------------------------------------------------------
181 // SelectionStatusDialog
182 // ------------------------------------------------------------------------
b544077e
BH
183 /*
184 * (non-Javadoc)
185 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
186 */
12c155f5
FC
187 @Override
188 protected void computeResult() {
189 }
b544077e
BH
190 /*
191 * (non-Javadoc)
192 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#create()
193 */
12c155f5
FC
194 @Override
195 public void create() {
196 super.create();
197 getButton(IDialogConstants.OK_ID).setEnabled(false);
198 }
b544077e
BH
199 /*
200 * (non-Javadoc)
201 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
202 */
12c155f5
FC
203 @Override
204 protected void okPressed() {
205 IResource trace = renameTrace(fNewTraceNameText.getText());
206 if (trace == null) {
207 return;
208 }
209 setSelectionResult(new IResource[] { trace });
210 super.okPressed();
211
212 if (fProject != null) {
213 fProject.refresh();
214 }
215 }
216
828e5592 217 private IResource renameTrace(final String newName) {
12c155f5 218
828e5592 219 final IPath oldPath = fTrace.getResource().getFullPath();
12c155f5
FC
220 final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$
221
222 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
223 @Override
224 public void execute(IProgressMonitor monitor) throws CoreException {
225 try {
226 monitor.beginTask("", 1000); //$NON-NLS-1$
227 if (monitor.isCanceled()) {
228 throw new OperationCanceledException();
229 }
828e5592
PT
230 if (fTrace.getResource() instanceof IFolder) {
231 IFolder folder = (IFolder) fTrace.getResource();
32defecf
PT
232 IFile bookmarksFile = folder.getFile(fTrace.getName() + '_');
233 IFile newBookmarksFile = folder.getFile(newName + '_');
828e5592
PT
234 if (bookmarksFile.exists()) {
235 if (!newBookmarksFile.exists()) {
236 IPath newBookmarksPath = newBookmarksFile.getFullPath();
237 bookmarksFile.move(newBookmarksPath, IResource.FORCE | IResource.SHALLOW, null);
238 }
239 }
240 }
5e4bf87d 241
e12ecd30 242 fTrace.renameSupplementaryFolder(newName);
12c155f5
FC
243 fTrace.getResource().move(newPath, IResource.FORCE | IResource.SHALLOW, null);
244 if (monitor.isCanceled()) {
245 throw new OperationCanceledException();
246 }
247 } finally {
248 monitor.done();
249 }
250 }
251 };
252
253 try {
254 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
255 } catch (InterruptedException exception) {
256 return null;
257 } catch (InvocationTargetException exception) {
258 MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
259 return null;
260 } catch (RuntimeException exception) {
261 return null;
262 }
263
264 return fTrace.getResource();
265 }
266
267}
This page took 0.052439 seconds and 5 git commands to generate.