Commit | Line | Data |
---|---|---|
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 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.wizards; | |
14 | ||
15 | import java.lang.reflect.InvocationTargetException; | |
16 | ||
17 | import org.eclipse.core.resources.IContainer; | |
965c9e29 | 18 | import org.eclipse.core.resources.IFile; |
12c155f5 FC |
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; | |
8fd82db5 | 31 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
12c155f5 FC |
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 | /** | |
b544077e BH |
52 | * Implementation of a dialog box to rename an experiment. |
53 | * <p> | |
54 | * @version 1.0 | |
55 | * @author Francois Chouinard | |
12c155f5 FC |
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 | ||
b544077e BH |
72 | /** |
73 | * Constructor | |
74 | * @param shell The parent shell | |
75 | * @param experiment The experiment element rename | |
76 | */ | |
12c155f5 FC |
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 | // ------------------------------------------------------------------------ | |
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 | 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$ | |
8fd82db5 | 152 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null)); |
12c155f5 FC |
153 | return; |
154 | } | |
155 | ||
9fa32496 | 156 | if (!nameStatus.isOK()) { |
12c155f5 FC |
157 | updateStatus(nameStatus); |
158 | return; | |
159 | } | |
160 | ||
161 | IPath path = new Path(name); | |
162 | if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) { | |
8fd82db5 | 163 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null)); |
12c155f5 FC |
164 | return; |
165 | } | |
166 | ||
8fd82db5 | 167 | updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$ |
12c155f5 FC |
168 | } |
169 | ||
170 | // ------------------------------------------------------------------------ | |
171 | // SelectionStatusDialog | |
172 | // ------------------------------------------------------------------------ | |
b544077e BH |
173 | /* |
174 | * (non-Javadoc) | |
175 | * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult() | |
176 | */ | |
12c155f5 FC |
177 | @Override |
178 | protected void computeResult() { | |
179 | } | |
180 | ||
b544077e BH |
181 | /* |
182 | * (non-Javadoc) | |
183 | * @see org.eclipse.ui.dialogs.SelectionStatusDialog#create() | |
184 | */ | |
12c155f5 FC |
185 | @Override |
186 | public void create() { | |
187 | super.create(); | |
188 | getButton(IDialogConstants.OK_ID).setEnabled(false); | |
189 | } | |
b544077e BH |
190 | |
191 | /* | |
192 | * (non-Javadoc) | |
193 | * @see org.eclipse.ui.dialogs.SelectionStatusDialog#okPressed() | |
194 | */ | |
12c155f5 FC |
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 | ||
965c9e29 | 209 | private IFolder renameExperiment(final String newName) { |
12c155f5 FC |
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(); | |
965c9e29 PT |
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 | } | |
12c155f5 FC |
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 | } |