Commit | Line | Data |
---|---|---|
12c155f5 FC |
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 | ||
12c155f5 FC |
17 | import org.eclipse.core.resources.IFolder; |
18 | import org.eclipse.core.resources.IResource; | |
19 | import org.eclipse.core.resources.IWorkspace; | |
20 | import org.eclipse.core.runtime.CoreException; | |
21 | import org.eclipse.core.runtime.IPath; | |
22 | import org.eclipse.core.runtime.IProgressMonitor; | |
23 | import org.eclipse.core.runtime.IStatus; | |
24 | import org.eclipse.core.runtime.OperationCanceledException; | |
25 | import org.eclipse.core.runtime.Path; | |
26 | import org.eclipse.core.runtime.Status; | |
27 | import org.eclipse.jface.dialogs.IDialogConstants; | |
28 | import org.eclipse.jface.dialogs.MessageDialog; | |
d34665f9 | 29 | import org.eclipse.linuxtools.internal.tmf.ui.TmfUiPlugin; |
e12ecd30 | 30 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
3453cab5 | 31 | import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment; |
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 | /** | |
52 | * <b><u>CopyExperimentDialog</u></b> | |
53 | * <p> | |
54 | */ | |
55 | public class CopyExperimentDialog extends SelectionStatusDialog { | |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Members | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
61 | private final TmfExperimentElement fExperiment; | |
62 | private Text fNewExperimentName; | |
3453cab5 | 63 | private IFolder fExperimentFolder; |
12c155f5 FC |
64 | private TmfProjectElement fProject; |
65 | ||
66 | // ------------------------------------------------------------------------ | |
67 | // Constructor | |
68 | // ------------------------------------------------------------------------ | |
69 | ||
70 | public CopyExperimentDialog(Shell shell, TmfExperimentElement experiment) { | |
71 | super(shell); | |
72 | fExperiment = experiment; | |
73 | TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent(); | |
74 | fExperimentFolder = folder.getResource(); | |
75 | fProject = experiment.getProject(); | |
76 | setTitle(Messages.CopyExperimentDialog_DialogTitle); | |
77 | setStatusLineAboveButtons(true); | |
78 | } | |
79 | ||
80 | // ------------------------------------------------------------------------ | |
81 | // Dialog | |
82 | // ------------------------------------------------------------------------ | |
83 | ||
84 | @Override | |
85 | protected Control createDialogArea(Composite parent) { | |
86 | Composite composite = (Composite) super.createDialogArea(parent); | |
87 | composite.setLayout(new GridLayout()); | |
88 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
89 | ||
90 | createNewExperimentNameGroup(composite); | |
91 | return composite; | |
92 | } | |
93 | ||
94 | private void createNewExperimentNameGroup(Composite parent) { | |
95 | Font font = parent.getFont(); | |
96 | Composite folderGroup = new Composite(parent, SWT.NONE); | |
97 | GridLayout layout = new GridLayout(); | |
98 | layout.numColumns = 2; | |
99 | folderGroup.setLayout(layout); | |
100 | folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
101 | ||
102 | // Old experiment name label | |
103 | Label oldExperimentLabel = new Label(folderGroup, SWT.NONE); | |
104 | oldExperimentLabel.setFont(font); | |
105 | oldExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentName); | |
106 | ||
107 | // Old experiment name field | |
108 | Text oldExperimentName = new Text(folderGroup, SWT.BORDER); | |
109 | GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
110 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
111 | oldExperimentName.setLayoutData(data); | |
112 | oldExperimentName.setFont(font); | |
113 | oldExperimentName.setText(fExperiment.getName()); | |
114 | oldExperimentName.setEnabled(false); | |
115 | ||
116 | // New experiment name label | |
117 | Label newExperimentLabel = new Label(folderGroup, SWT.NONE); | |
118 | newExperimentLabel.setFont(font); | |
119 | newExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentNewName); | |
120 | ||
121 | // New experiment name entry field | |
122 | fNewExperimentName = new Text(folderGroup, SWT.BORDER); | |
123 | data = new GridData(GridData.FILL_HORIZONTAL); | |
124 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
125 | fNewExperimentName.setLayoutData(data); | |
126 | fNewExperimentName.setFont(font); | |
127 | fNewExperimentName.addListener(SWT.Modify, new Listener() { | |
128 | @Override | |
129 | public void handleEvent(Event event) { | |
130 | validateNewExperimentName(); | |
131 | } | |
132 | }); | |
133 | } | |
134 | ||
135 | private void validateNewExperimentName() { | |
136 | ||
137 | String name = fNewExperimentName.getText(); | |
138 | IWorkspace workspace = fExperimentFolder.getWorkspace(); | |
139 | IStatus nameStatus = workspace.validateName(name, IResource.FOLDER); | |
140 | ||
141 | if ("".equals(name)) { //$NON-NLS-1$ | |
142 | updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null)); | |
143 | return; | |
144 | } | |
145 | ||
146 | if (nameStatus.isOK() == false) { | |
147 | updateStatus(nameStatus); | |
148 | return; | |
149 | } | |
150 | ||
151 | IPath path = new Path(name); | |
152 | if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) { | |
153 | updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null)); | |
154 | return; | |
155 | } | |
156 | ||
157 | updateStatus(new Status(IStatus.OK, TmfUiPlugin.PLUGIN_ID, "")); //$NON-NLS-1$ | |
158 | } | |
159 | ||
160 | // ------------------------------------------------------------------------ | |
161 | // SelectionStatusDialog | |
162 | // ------------------------------------------------------------------------ | |
163 | ||
164 | @Override | |
165 | protected void computeResult() { | |
166 | } | |
167 | ||
168 | @Override | |
169 | public void create() { | |
170 | super.create(); | |
171 | getButton(IDialogConstants.OK_ID).setEnabled(false); | |
172 | } | |
173 | ||
174 | @Override | |
175 | protected void okPressed() { | |
176 | IFolder folder = copyExperiment(fNewExperimentName.getText()); | |
177 | if (folder == null) { | |
178 | return; | |
179 | } | |
180 | setSelectionResult(new IFolder[] { folder }); | |
181 | super.okPressed(); | |
182 | ||
183 | if (fProject != null) { | |
184 | fProject.refresh(); | |
185 | } | |
186 | } | |
187 | ||
3453cab5 | 188 | private IFolder copyExperiment(final String newName) { |
12c155f5 FC |
189 | |
190 | IPath oldPath = fExperiment.getResource().getFullPath(); | |
191 | final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$ | |
192 | ||
193 | WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { | |
194 | @Override | |
195 | public void execute(IProgressMonitor monitor) throws CoreException { | |
196 | try { | |
197 | monitor.beginTask("", 1000); //$NON-NLS-1$ | |
198 | if (monitor.isCanceled()) { | |
199 | throw new OperationCanceledException(); | |
200 | } | |
3453cab5 PT |
201 | fExperiment.getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null); |
202 | // Delete any bookmarks file found in copied experiment folder | |
203 | IFolder folder = fExperimentFolder.getFolder(newName); | |
204 | if (folder.exists()) { | |
205 | for (IResource member : folder.members()) { | |
e12ecd30 | 206 | if (TmfExperiment.class.getCanonicalName().equals(member.getPersistentProperty(TmfCommonConstants.TRACETYPE))) { |
3453cab5 PT |
207 | member.delete(true, null); |
208 | } | |
209 | } | |
210 | } | |
12c155f5 FC |
211 | if (monitor.isCanceled()) { |
212 | throw new OperationCanceledException(); | |
213 | } | |
214 | } finally { | |
215 | monitor.done(); | |
216 | } | |
217 | } | |
218 | }; | |
219 | try { | |
220 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
221 | } catch (InterruptedException exception) { | |
222 | return null; | |
223 | } catch (InvocationTargetException exception) { | |
224 | MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$ | |
225 | return null; | |
226 | } catch (RuntimeException exception) { | |
227 | return null; | |
228 | } | |
229 | ||
230 | return fExperiment.getResource(); | |
231 | } | |
232 | ||
233 | } |