tmf: Simple warning fixes in tmf.core and tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / RenameExperimentDialog.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2012 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.TmfExperimentElement;
33 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder;
34 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
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 * Implementation of a dialog box to rename an experiment.
53 * <p>
54 * @version 1.0
55 * @author Francois Chouinard
56 */
57 public class RenameExperimentDialog extends SelectionStatusDialog {
58
59 // ------------------------------------------------------------------------
60 // Members
61 // ------------------------------------------------------------------------
62
63 private final TmfExperimentElement fExperiment;
64 private Text fNewExperimentName;
65 private IContainer fExperimentFolder;
66 private TmfProjectElement fProject;
67
68 // ------------------------------------------------------------------------
69 // Constructor
70 // ------------------------------------------------------------------------
71
72 /**
73 * Constructor
74 * @param shell The parent shell
75 * @param experiment The experiment element rename
76 */
77 public RenameExperimentDialog(Shell shell, TmfExperimentElement experiment) {
78 super(shell);
79 fExperiment = experiment;
80 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
81 fExperimentFolder = folder.getResource();
82 fProject = experiment.getProject();
83 setTitle(Messages.RenameExperimentDialog_DialogTitle);
84 setStatusLineAboveButtons(true);
85 }
86
87 // ------------------------------------------------------------------------
88 // Dialog
89 // ------------------------------------------------------------------------
90 /*
91 * (non-Javadoc)
92 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
93 */
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 createNewExperimentNameGroup(composite);
101 return composite;
102 }
103
104 private void createNewExperimentNameGroup(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 experiment name label
113 Label oldExperimentLabel = new Label(folderGroup, SWT.NONE);
114 oldExperimentLabel.setFont(font);
115 oldExperimentLabel.setText(Messages.RenameExperimentDialog_ExperimentName);
116
117 // Old experiment name field
118 Text oldExperimentName = new Text(folderGroup, SWT.BORDER);
119 GridData data = new GridData(GridData.FILL_HORIZONTAL);
120 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
121 oldExperimentName.setLayoutData(data);
122 oldExperimentName.setFont(font);
123 oldExperimentName.setText(fExperiment.getName());
124 oldExperimentName.setEnabled(false);
125
126 // New experiment name label
127 Label newExperimentLabel = new Label(folderGroup, SWT.NONE);
128 newExperimentLabel.setFont(font);
129 newExperimentLabel.setText(Messages.RenameExperimentDialog_ExperimentNewName);
130
131 // New experiment name entry field
132 fNewExperimentName = new Text(folderGroup, SWT.BORDER);
133 data = new GridData(GridData.FILL_HORIZONTAL);
134 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
135 fNewExperimentName.setLayoutData(data);
136 fNewExperimentName.setFont(font);
137 fNewExperimentName.addListener(SWT.Modify, new Listener() {
138 @Override
139 public void handleEvent(Event event) {
140 validateNewExperimentName();
141 }
142 });
143 }
144
145 private void validateNewExperimentName() {
146
147 String name = fNewExperimentName.getText();
148 IWorkspace workspace = fExperimentFolder.getWorkspace();
149 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
150
151 if ("".equals(name)) { //$NON-NLS-1$
152 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null));
153 return;
154 }
155
156 if (!nameStatus.isOK()) {
157 updateStatus(nameStatus);
158 return;
159 }
160
161 IPath path = new Path(name);
162 if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) {
163 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null));
164 return;
165 }
166
167 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
168 }
169
170 // ------------------------------------------------------------------------
171 // SelectionStatusDialog
172 // ------------------------------------------------------------------------
173 /*
174 * (non-Javadoc)
175 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
176 */
177 @Override
178 protected void computeResult() {
179 }
180
181 /*
182 * (non-Javadoc)
183 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#create()
184 */
185 @Override
186 public void create() {
187 super.create();
188 getButton(IDialogConstants.OK_ID).setEnabled(false);
189 }
190
191 /*
192 * (non-Javadoc)
193 * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed()
194 */
195 @Override
196 protected void okPressed() {
197 IFolder folder = renameExperiment(fNewExperimentName.getText());
198 if (folder == null) {
199 return;
200 }
201 setSelectionResult(new IFolder[] { folder });
202 super.okPressed();
203
204 if (fProject != null) {
205 fProject.refresh();
206 }
207 }
208
209 private IFolder renameExperiment(final String newName) {
210
211 IPath oldPath = fExperiment.getResource().getFullPath();
212 final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$
213
214 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
215 @Override
216 public void execute(IProgressMonitor monitor) throws CoreException {
217 try {
218 monitor.beginTask("", 1000); //$NON-NLS-1$
219 if (monitor.isCanceled()) {
220 throw new OperationCanceledException();
221 }
222 IFolder folder = (IFolder) fExperiment.getResource();
223 IFile bookmarksFile = folder.getFile(fExperiment.getName() + '_');
224 IFile newBookmarksFile = folder.getFile(newName + '_');
225 if (bookmarksFile.exists()) {
226 if (!newBookmarksFile.exists()) {
227 IPath newBookmarksPath = newBookmarksFile.getFullPath();
228 bookmarksFile.move(newBookmarksPath, IResource.FORCE | IResource.SHALLOW, null);
229 }
230 }
231 fExperiment.getResource().move(newPath, IResource.FORCE | IResource.SHALLOW, null);
232 if (monitor.isCanceled()) {
233 throw new OperationCanceledException();
234 }
235 } finally {
236 monitor.done();
237 }
238 }
239 };
240 try {
241 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
242 } catch (InterruptedException exception) {
243 return null;
244 } catch (InvocationTargetException exception) {
245 MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
246 return null;
247 } catch (RuntimeException exception) {
248 return null;
249 }
250
251 return fExperiment.getResource();
252 }
253
254 }
This page took 0.036463 seconds and 5 git commands to generate.