Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
c8422608 | 2 | * Copyright (c) 2011, 2013 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 | |
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; | |
12c155f5 FC |
35 | import org.eclipse.swt.SWT; |
36 | import org.eclipse.swt.graphics.Font; | |
37 | import org.eclipse.swt.layout.GridData; | |
38 | import org.eclipse.swt.layout.GridLayout; | |
39 | import org.eclipse.swt.widgets.Composite; | |
40 | import org.eclipse.swt.widgets.Control; | |
41 | import org.eclipse.swt.widgets.Event; | |
42 | import org.eclipse.swt.widgets.Label; | |
43 | import org.eclipse.swt.widgets.Listener; | |
44 | import org.eclipse.swt.widgets.Shell; | |
45 | import org.eclipse.swt.widgets.Text; | |
c4c81d91 PT |
46 | import org.eclipse.ui.IEditorReference; |
47 | import org.eclipse.ui.IWorkbench; | |
48 | import org.eclipse.ui.IWorkbenchPage; | |
49 | import org.eclipse.ui.IWorkbenchWindow; | |
12c155f5 FC |
50 | import org.eclipse.ui.PlatformUI; |
51 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
52 | import org.eclipse.ui.dialogs.SelectionStatusDialog; | |
c4c81d91 | 53 | import org.eclipse.ui.part.FileEditorInput; |
12c155f5 FC |
54 | |
55 | /** | |
b544077e | 56 | * Implementation of a dialog box to rename an experiment. |
abbdd66a | 57 | * <p> |
11252342 | 58 | * |
b544077e BH |
59 | * @version 1.0 |
60 | * @author Francois Chouinard | |
12c155f5 FC |
61 | */ |
62 | public class RenameExperimentDialog extends SelectionStatusDialog { | |
63 | ||
64 | // ------------------------------------------------------------------------ | |
65 | // Members | |
66 | // ------------------------------------------------------------------------ | |
67 | ||
11252342 AM |
68 | private final TmfExperimentElement fExperiment; |
69 | private Text fNewExperimentName; | |
abbdd66a AM |
70 | private final IContainer fExperimentFolder; |
71 | private final TmfProjectElement fProject; | |
12c155f5 FC |
72 | |
73 | // ------------------------------------------------------------------------ | |
74 | // Constructor | |
75 | // ------------------------------------------------------------------------ | |
76 | ||
b544077e BH |
77 | /** |
78 | * Constructor | |
11252342 AM |
79 | * |
80 | * @param shell | |
81 | * The parent shell | |
82 | * @param experiment | |
83 | * The experiment element rename | |
b544077e | 84 | */ |
12c155f5 FC |
85 | public RenameExperimentDialog(Shell shell, TmfExperimentElement experiment) { |
86 | super(shell); | |
87 | fExperiment = experiment; | |
88 | TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent(); | |
89 | fExperimentFolder = folder.getResource(); | |
90 | fProject = experiment.getProject(); | |
91 | setTitle(Messages.RenameExperimentDialog_DialogTitle); | |
92 | setStatusLineAboveButtons(true); | |
93 | } | |
94 | ||
95 | // ------------------------------------------------------------------------ | |
96 | // Dialog | |
97 | // ------------------------------------------------------------------------ | |
11252342 | 98 | |
12c155f5 FC |
99 | @Override |
100 | protected Control createDialogArea(Composite parent) { | |
101 | Composite composite = (Composite) super.createDialogArea(parent); | |
102 | composite.setLayout(new GridLayout()); | |
103 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
104 | ||
105 | createNewExperimentNameGroup(composite); | |
106 | return composite; | |
107 | } | |
108 | ||
109 | private void createNewExperimentNameGroup(Composite parent) { | |
110 | Font font = parent.getFont(); | |
111 | Composite folderGroup = new Composite(parent, SWT.NONE); | |
112 | GridLayout layout = new GridLayout(); | |
113 | layout.numColumns = 2; | |
114 | folderGroup.setLayout(layout); | |
115 | folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
116 | ||
117 | // Old experiment name label | |
118 | Label oldExperimentLabel = new Label(folderGroup, SWT.NONE); | |
119 | oldExperimentLabel.setFont(font); | |
120 | oldExperimentLabel.setText(Messages.RenameExperimentDialog_ExperimentName); | |
121 | ||
122 | // Old experiment name field | |
123 | Text oldExperimentName = new Text(folderGroup, SWT.BORDER); | |
124 | GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
125 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
126 | oldExperimentName.setLayoutData(data); | |
127 | oldExperimentName.setFont(font); | |
128 | oldExperimentName.setText(fExperiment.getName()); | |
129 | oldExperimentName.setEnabled(false); | |
130 | ||
131 | // New experiment name label | |
132 | Label newExperimentLabel = new Label(folderGroup, SWT.NONE); | |
133 | newExperimentLabel.setFont(font); | |
134 | newExperimentLabel.setText(Messages.RenameExperimentDialog_ExperimentNewName); | |
135 | ||
136 | // New experiment name entry field | |
137 | fNewExperimentName = new Text(folderGroup, SWT.BORDER); | |
138 | data = new GridData(GridData.FILL_HORIZONTAL); | |
139 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
140 | fNewExperimentName.setLayoutData(data); | |
141 | fNewExperimentName.setFont(font); | |
142 | fNewExperimentName.addListener(SWT.Modify, new Listener() { | |
143 | @Override | |
144 | public void handleEvent(Event event) { | |
11252342 | 145 | validateNewExperimentName(); |
12c155f5 FC |
146 | } |
147 | }); | |
148 | } | |
149 | ||
150 | private void validateNewExperimentName() { | |
abbdd66a | 151 | |
11252342 | 152 | String name = fNewExperimentName.getText(); |
12c155f5 FC |
153 | IWorkspace workspace = fExperimentFolder.getWorkspace(); |
154 | IStatus nameStatus = workspace.validateName(name, IResource.FOLDER); | |
155 | ||
156 | if ("".equals(name)) { //$NON-NLS-1$ | |
11252342 AM |
157 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null)); |
158 | return; | |
12c155f5 | 159 | } |
abbdd66a | 160 | |
9fa32496 | 161 | if (!nameStatus.isOK()) { |
11252342 AM |
162 | updateStatus(nameStatus); |
163 | return; | |
12c155f5 | 164 | } |
abbdd66a | 165 | |
12c155f5 FC |
166 | IPath path = new Path(name); |
167 | if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) { | |
8fd82db5 | 168 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null)); |
12c155f5 FC |
169 | return; |
170 | } | |
171 | ||
8fd82db5 | 172 | updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$ |
12c155f5 FC |
173 | } |
174 | ||
175 | // ------------------------------------------------------------------------ | |
176 | // SelectionStatusDialog | |
177 | // ------------------------------------------------------------------------ | |
11252342 | 178 | |
12c155f5 FC |
179 | @Override |
180 | protected void computeResult() { | |
181 | } | |
182 | ||
183 | @Override | |
184 | public void create() { | |
185 | super.create(); | |
186 | getButton(IDialogConstants.OK_ID).setEnabled(false); | |
187 | } | |
abbdd66a | 188 | |
12c155f5 FC |
189 | @Override |
190 | protected void okPressed() { | |
191 | IFolder folder = renameExperiment(fNewExperimentName.getText()); | |
192 | if (folder == null) { | |
193 | return; | |
194 | } | |
195 | setSelectionResult(new IFolder[] { folder }); | |
196 | super.okPressed(); | |
197 | ||
198 | if (fProject != null) { | |
199 | fProject.refresh(); | |
200 | } | |
201 | } | |
202 | ||
965c9e29 | 203 | private IFolder renameExperiment(final String newName) { |
12c155f5 | 204 | |
11252342 AM |
205 | IPath oldPath = fExperiment.getResource().getFullPath(); |
206 | final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$ | |
12c155f5 | 207 | |
11252342 | 208 | WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { |
12c155f5 FC |
209 | @Override |
210 | public void execute(IProgressMonitor monitor) throws CoreException { | |
211 | try { | |
212 | monitor.beginTask("", 1000); //$NON-NLS-1$ | |
213 | if (monitor.isCanceled()) { | |
214 | throw new OperationCanceledException(); | |
965c9e29 | 215 | } |
c4c81d91 PT |
216 | // Close the experiment if open |
217 | IFile file = fExperiment.getBookmarksFile(); | |
218 | FileEditorInput input = new FileEditorInput(file); | |
219 | IWorkbench wb = PlatformUI.getWorkbench(); | |
220 | for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) { | |
221 | for (IWorkbenchPage wbPage : wbWindow.getPages()) { | |
222 | for (IEditorReference editorReference : wbPage.getEditorReferences()) { | |
223 | if (editorReference.getEditorInput().equals(input)) { | |
224 | wbPage.closeEditor(editorReference.getEditor(false), false); | |
225 | } | |
226 | } | |
227 | } | |
228 | } | |
229 | ||
abbdd66a | 230 | IFolder folder = fExperiment.getResource(); |
c4c81d91 PT |
231 | IFile bookmarksFile = fExperiment.getBookmarksFile(); |
232 | IFile newBookmarksFile = folder.getFile(bookmarksFile.getName().replace(fExperiment.getName(), newName)); | |
965c9e29 PT |
233 | if (bookmarksFile.exists()) { |
234 | if (!newBookmarksFile.exists()) { | |
235 | IPath newBookmarksPath = newBookmarksFile.getFullPath(); | |
236 | bookmarksFile.move(newBookmarksPath, IResource.FORCE | IResource.SHALLOW, null); | |
237 | } | |
12c155f5 | 238 | } |
99504bb8 GB |
239 | |
240 | fExperiment.renameSupplementaryFolder(newName); | |
11252342 | 241 | fExperiment.getResource().move(newPath, IResource.FORCE | IResource.SHALLOW, null); |
12c155f5 FC |
242 | if (monitor.isCanceled()) { |
243 | throw new OperationCanceledException(); | |
244 | } | |
245 | } finally { | |
246 | monitor.done(); | |
247 | } | |
248 | } | |
249 | }; | |
250 | try { | |
251 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
252 | } catch (InterruptedException exception) { | |
253 | return null; | |
254 | } catch (InvocationTargetException exception) { | |
c4c81d91 | 255 | MessageDialog.openError(getShell(), "", exception.getTargetException().getMessage()); //$NON-NLS-1$ |
12c155f5 FC |
256 | return null; |
257 | } catch (RuntimeException exception) { | |
258 | return null; | |
259 | } | |
260 | ||
261 | return fExperiment.getResource(); | |
262 | } | |
263 | ||
264 | } |