Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal |
99504bb8 | 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 | |
99504bb8 | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Copied and adapted from NewFolderDialog | |
beb19106 | 11 | * Geneviève Bastien - Moved the actual copy code to model element's class |
12c155f5 FC |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.tmf.ui.project.wizards; | |
15 | ||
16 | import java.lang.reflect.InvocationTargetException; | |
17 | ||
12c155f5 FC |
18 | import org.eclipse.core.resources.IFolder; |
19 | import org.eclipse.core.resources.IResource; | |
20 | import org.eclipse.core.resources.IWorkspace; | |
21 | import org.eclipse.core.runtime.CoreException; | |
22 | import org.eclipse.core.runtime.IPath; | |
23 | import org.eclipse.core.runtime.IProgressMonitor; | |
24 | import org.eclipse.core.runtime.IStatus; | |
25 | import org.eclipse.core.runtime.OperationCanceledException; | |
26 | import org.eclipse.core.runtime.Path; | |
27 | import org.eclipse.core.runtime.Status; | |
28 | import org.eclipse.jface.dialogs.IDialogConstants; | |
29 | import org.eclipse.jface.dialogs.MessageDialog; | |
8fd82db5 | 30 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
12c155f5 FC |
31 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentFolder; | |
12c155f5 FC |
33 | import org.eclipse.osgi.util.NLS; |
34 | import org.eclipse.swt.SWT; | |
35 | import org.eclipse.swt.graphics.Font; | |
36 | import org.eclipse.swt.layout.GridData; | |
37 | import org.eclipse.swt.layout.GridLayout; | |
38 | import org.eclipse.swt.widgets.Composite; | |
39 | import org.eclipse.swt.widgets.Control; | |
40 | import org.eclipse.swt.widgets.Event; | |
41 | import org.eclipse.swt.widgets.Label; | |
42 | import org.eclipse.swt.widgets.Listener; | |
43 | import org.eclipse.swt.widgets.Shell; | |
44 | import org.eclipse.swt.widgets.Text; | |
45 | import org.eclipse.ui.PlatformUI; | |
46 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
47 | import org.eclipse.ui.dialogs.SelectionStatusDialog; | |
48 | ||
49 | /** | |
b544077e | 50 | * Implementation of the copy experiement dialog box. |
12c155f5 | 51 | * <p> |
11252342 | 52 | * |
b544077e BH |
53 | * @version 1.0 |
54 | * @author Francois Chouinard | |
12c155f5 FC |
55 | */ |
56 | public class CopyExperimentDialog extends SelectionStatusDialog { | |
57 | ||
58 | // ------------------------------------------------------------------------ | |
59 | // Members | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
11252342 AM |
62 | private final TmfExperimentElement fExperiment; |
63 | private Text fNewExperimentName; | |
3453cab5 | 64 | private IFolder fExperimentFolder; |
12c155f5 FC |
65 | |
66 | // ------------------------------------------------------------------------ | |
67 | // Constructor | |
68 | // ------------------------------------------------------------------------ | |
69 | ||
b544077e BH |
70 | /** |
71 | * Constructor | |
11252342 AM |
72 | * |
73 | * @param shell | |
74 | * The parent shell | |
75 | * @param experiment | |
76 | * The TMF experiment model element | |
b544077e | 77 | */ |
12c155f5 FC |
78 | public CopyExperimentDialog(Shell shell, TmfExperimentElement experiment) { |
79 | super(shell); | |
80 | fExperiment = experiment; | |
81 | TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent(); | |
82 | fExperimentFolder = folder.getResource(); | |
12c155f5 FC |
83 | setTitle(Messages.CopyExperimentDialog_DialogTitle); |
84 | setStatusLineAboveButtons(true); | |
85 | } | |
86 | ||
87 | // ------------------------------------------------------------------------ | |
88 | // Dialog | |
89 | // ------------------------------------------------------------------------ | |
99504bb8 | 90 | |
12c155f5 FC |
91 | @Override |
92 | protected Control createDialogArea(Composite parent) { | |
93 | Composite composite = (Composite) super.createDialogArea(parent); | |
94 | composite.setLayout(new GridLayout()); | |
95 | composite.setLayoutData(new GridData(GridData.FILL_BOTH)); | |
96 | ||
97 | createNewExperimentNameGroup(composite); | |
98 | return composite; | |
99 | } | |
100 | ||
101 | private void createNewExperimentNameGroup(Composite parent) { | |
102 | Font font = parent.getFont(); | |
103 | Composite folderGroup = new Composite(parent, SWT.NONE); | |
104 | GridLayout layout = new GridLayout(); | |
105 | layout.numColumns = 2; | |
106 | folderGroup.setLayout(layout); | |
107 | folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
108 | ||
109 | // Old experiment name label | |
110 | Label oldExperimentLabel = new Label(folderGroup, SWT.NONE); | |
111 | oldExperimentLabel.setFont(font); | |
112 | oldExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentName); | |
113 | ||
114 | // Old experiment name field | |
115 | Text oldExperimentName = new Text(folderGroup, SWT.BORDER); | |
116 | GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
117 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
118 | oldExperimentName.setLayoutData(data); | |
119 | oldExperimentName.setFont(font); | |
120 | oldExperimentName.setText(fExperiment.getName()); | |
121 | oldExperimentName.setEnabled(false); | |
122 | ||
123 | // New experiment name label | |
124 | Label newExperimentLabel = new Label(folderGroup, SWT.NONE); | |
125 | newExperimentLabel.setFont(font); | |
126 | newExperimentLabel.setText(Messages.CopyExperimentDialog_ExperimentNewName); | |
127 | ||
128 | // New experiment name entry field | |
129 | fNewExperimentName = new Text(folderGroup, SWT.BORDER); | |
130 | data = new GridData(GridData.FILL_HORIZONTAL); | |
131 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
132 | fNewExperimentName.setLayoutData(data); | |
133 | fNewExperimentName.setFont(font); | |
134 | fNewExperimentName.addListener(SWT.Modify, new Listener() { | |
135 | @Override | |
136 | public void handleEvent(Event event) { | |
11252342 | 137 | validateNewExperimentName(); |
12c155f5 FC |
138 | } |
139 | }); | |
140 | } | |
141 | ||
142 | private void validateNewExperimentName() { | |
99504bb8 | 143 | |
11252342 | 144 | String name = fNewExperimentName.getText(); |
12c155f5 FC |
145 | IWorkspace workspace = fExperimentFolder.getWorkspace(); |
146 | IStatus nameStatus = workspace.validateName(name, IResource.FOLDER); | |
147 | ||
148 | if ("".equals(name)) { //$NON-NLS-1$ | |
11252342 AM |
149 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null)); |
150 | return; | |
12c155f5 | 151 | } |
99504bb8 | 152 | |
9fa32496 | 153 | if (!nameStatus.isOK()) { |
11252342 AM |
154 | updateStatus(nameStatus); |
155 | return; | |
12c155f5 | 156 | } |
99504bb8 | 157 | |
12c155f5 FC |
158 | IPath path = new Path(name); |
159 | if (fExperimentFolder.getFolder(path).exists() || fExperimentFolder.getFile(path).exists()) { | |
8fd82db5 | 160 | updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null)); |
12c155f5 FC |
161 | return; |
162 | } | |
163 | ||
8fd82db5 | 164 | updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$ |
12c155f5 FC |
165 | } |
166 | ||
167 | // ------------------------------------------------------------------------ | |
168 | // SelectionStatusDialog | |
169 | // ------------------------------------------------------------------------ | |
11252342 | 170 | |
12c155f5 FC |
171 | @Override |
172 | protected void computeResult() { | |
173 | } | |
174 | ||
175 | @Override | |
176 | public void create() { | |
177 | super.create(); | |
178 | getButton(IDialogConstants.OK_ID).setEnabled(false); | |
179 | } | |
180 | ||
181 | @Override | |
182 | protected void okPressed() { | |
183 | IFolder folder = copyExperiment(fNewExperimentName.getText()); | |
184 | if (folder == null) { | |
185 | return; | |
186 | } | |
187 | setSelectionResult(new IFolder[] { folder }); | |
188 | super.okPressed(); | |
12c155f5 FC |
189 | } |
190 | ||
3453cab5 | 191 | private IFolder copyExperiment(final String newName) { |
12c155f5 | 192 | |
11252342 | 193 | WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { |
12c155f5 FC |
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 | } | |
beb19106 GB |
201 | |
202 | fExperiment.copy(newName, true); | |
203 | ||
12c155f5 FC |
204 | if (monitor.isCanceled()) { |
205 | throw new OperationCanceledException(); | |
206 | } | |
207 | } finally { | |
208 | monitor.done(); | |
209 | } | |
210 | } | |
211 | }; | |
212 | try { | |
213 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
214 | } catch (InterruptedException exception) { | |
215 | return null; | |
216 | } catch (InvocationTargetException exception) { | |
217 | MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$ | |
218 | return null; | |
219 | } catch (RuntimeException exception) { | |
220 | return null; | |
221 | } | |
222 | ||
223 | return fExperiment.getResource(); | |
224 | } | |
225 | ||
226 | } |