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