tmf/lttng: Update 2014 copyrights
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / RenameExperimentDialog.java
CommitLineData
12c155f5 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2011, 2014 Ericsson
abbdd66a 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
abbdd66a 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;
965c9e29 19import org.eclipse.core.resources.IFile;
12c155f5
FC
20import org.eclipse.core.resources.IFolder;
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.TmfExperimentElement;
34import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder;
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 an experiment.
abbdd66a 52 * <p>
11252342 53 *
b544077e
BH
54 * @version 1.0
55 * @author Francois Chouinard
12c155f5
FC
56 */
57public class RenameExperimentDialog extends SelectionStatusDialog {
58
59 // ------------------------------------------------------------------------
60 // Members
61 // ------------------------------------------------------------------------
62
11252342
AM
63 private final TmfExperimentElement fExperiment;
64 private Text fNewExperimentName;
abbdd66a 65 private final IContainer fExperimentFolder;
12c155f5
FC
66
67 // ------------------------------------------------------------------------
68 // Constructor
69 // ------------------------------------------------------------------------
70
b544077e
BH
71 /**
72 * Constructor
11252342
AM
73 *
74 * @param shell
75 * The parent shell
76 * @param experiment
77 * The experiment element rename
b544077e 78 */
12c155f5
FC
79 public RenameExperimentDialog(Shell shell, TmfExperimentElement experiment) {
80 super(shell);
81 fExperiment = experiment;
82 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
83 fExperimentFolder = folder.getResource();
12c155f5
FC
84 setTitle(Messages.RenameExperimentDialog_DialogTitle);
85 setStatusLineAboveButtons(true);
86 }
87
88 // ------------------------------------------------------------------------
89 // Dialog
90 // ------------------------------------------------------------------------
11252342 91
12c155f5
FC
92 @Override
93 protected Control createDialogArea(Composite parent) {
94 Composite composite = (Composite) super.createDialogArea(parent);
95 composite.setLayout(new GridLayout());
96 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
97
98 createNewExperimentNameGroup(composite);
99 return composite;
100 }
101
102 private void createNewExperimentNameGroup(Composite parent) {
103 Font font = parent.getFont();
104 Composite folderGroup = new Composite(parent, SWT.NONE);
105 GridLayout layout = new GridLayout();
106 layout.numColumns = 2;
107 folderGroup.setLayout(layout);
108 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
109
110 // Old experiment name label
111 Label oldExperimentLabel = new Label(folderGroup, SWT.NONE);
112 oldExperimentLabel.setFont(font);
113 oldExperimentLabel.setText(Messages.RenameExperimentDialog_ExperimentName);
114
115 // Old experiment name field
116 Text oldExperimentName = new Text(folderGroup, SWT.BORDER);
117 GridData data = new GridData(GridData.FILL_HORIZONTAL);
118 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
119 oldExperimentName.setLayoutData(data);
120 oldExperimentName.setFont(font);
121 oldExperimentName.setText(fExperiment.getName());
122 oldExperimentName.setEnabled(false);
123
124 // New experiment name label
125 Label newExperimentLabel = new Label(folderGroup, SWT.NONE);
126 newExperimentLabel.setFont(font);
127 newExperimentLabel.setText(Messages.RenameExperimentDialog_ExperimentNewName);
128
129 // New experiment name entry field
130 fNewExperimentName = new Text(folderGroup, SWT.BORDER);
131 data = new GridData(GridData.FILL_HORIZONTAL);
132 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
133 fNewExperimentName.setLayoutData(data);
134 fNewExperimentName.setFont(font);
135 fNewExperimentName.addListener(SWT.Modify, new Listener() {
136 @Override
137 public void handleEvent(Event event) {
11252342 138 validateNewExperimentName();
12c155f5
FC
139 }
140 });
141 }
142
143 private void validateNewExperimentName() {
abbdd66a 144
11252342 145 String name = fNewExperimentName.getText();
12c155f5
FC
146 IWorkspace workspace = fExperimentFolder.getWorkspace();
147 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
148
149 if ("".equals(name)) { //$NON-NLS-1$
11252342
AM
150 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null));
151 return;
12c155f5 152 }
abbdd66a 153
9fa32496 154 if (!nameStatus.isOK()) {
11252342
AM
155 updateStatus(nameStatus);
156 return;
12c155f5 157 }
abbdd66a 158
12c155f5
FC
159 IPath path = new Path(name);
160 if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) {
8fd82db5 161 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null));
12c155f5
FC
162 return;
163 }
164
8fd82db5 165 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
12c155f5
FC
166 }
167
168 // ------------------------------------------------------------------------
169 // SelectionStatusDialog
170 // ------------------------------------------------------------------------
11252342 171
12c155f5
FC
172 @Override
173 protected void computeResult() {
174 }
175
176 @Override
177 public void create() {
178 super.create();
179 getButton(IDialogConstants.OK_ID).setEnabled(false);
180 }
abbdd66a 181
12c155f5
FC
182 @Override
183 protected void okPressed() {
184 IFolder folder = renameExperiment(fNewExperimentName.getText());
185 if (folder == null) {
186 return;
187 }
188 setSelectionResult(new IFolder[] { folder });
189 super.okPressed();
12c155f5
FC
190 }
191
965c9e29 192 private IFolder renameExperiment(final String newName) {
12c155f5 193
11252342
AM
194 IPath oldPath = fExperiment.getResource().getFullPath();
195 final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$
12c155f5 196
11252342 197 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
12c155f5
FC
198 @Override
199 public void execute(IProgressMonitor monitor) throws CoreException {
200 try {
201 monitor.beginTask("", 1000); //$NON-NLS-1$
202 if (monitor.isCanceled()) {
203 throw new OperationCanceledException();
965c9e29 204 }
c4c81d91 205 // Close the experiment if open
a72a6830 206 fExperiment.closeEditors();
c4c81d91 207
abbdd66a 208 IFolder folder = fExperiment.getResource();
c4c81d91
PT
209 IFile bookmarksFile = fExperiment.getBookmarksFile();
210 IFile newBookmarksFile = folder.getFile(bookmarksFile.getName().replace(fExperiment.getName(), newName));
965c9e29
PT
211 if (bookmarksFile.exists()) {
212 if (!newBookmarksFile.exists()) {
213 IPath newBookmarksPath = newBookmarksFile.getFullPath();
214 bookmarksFile.move(newBookmarksPath, IResource.FORCE | IResource.SHALLOW, null);
215 }
12c155f5 216 }
99504bb8
GB
217
218 fExperiment.renameSupplementaryFolder(newName);
11252342 219 fExperiment.getResource().move(newPath, IResource.FORCE | IResource.SHALLOW, null);
12c155f5
FC
220 if (monitor.isCanceled()) {
221 throw new OperationCanceledException();
222 }
223 } finally {
224 monitor.done();
225 }
226 }
227 };
228 try {
229 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
230 } catch (InterruptedException exception) {
231 return null;
232 } catch (InvocationTargetException exception) {
c4c81d91 233 MessageDialog.openError(getShell(), "", exception.getTargetException().getMessage()); //$NON-NLS-1$
12c155f5
FC
234 return null;
235 } catch (RuntimeException exception) {
236 return null;
237 }
238
239 return fExperiment.getResource();
240 }
241
242}
This page took 0.055797 seconds and 5 git commands to generate.