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 | ||
3453cab5 | 17 | import org.eclipse.core.resources.IFolder; |
12c155f5 FC |
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.trace.TmfTrace; |
12c155f5 FC |
32 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; |
33 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
3453cab5 | 34 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; |
12c155f5 FC |
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 CopyTraceDialog extends SelectionStatusDialog { | |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Members | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
61 | private final TmfTraceElement fTrace; | |
62 | private Text fNewTraceName; | |
3453cab5 | 63 | private IFolder fTraceFolder; |
12c155f5 FC |
64 | private TmfProjectElement fProject; |
65 | ||
66 | // ------------------------------------------------------------------------ | |
67 | // Constructor | |
68 | // ------------------------------------------------------------------------ | |
69 | ||
70 | public CopyTraceDialog(Shell shell, TmfTraceElement trace) { | |
71 | super(shell); | |
72 | fTrace = trace; | |
3453cab5 PT |
73 | TmfTraceFolder folder = (TmfTraceFolder) trace.getParent(); |
74 | fTraceFolder = folder.getResource(); | |
12c155f5 FC |
75 | fProject = trace.getProject(); |
76 | setTitle(Messages.CopyTraceDialog_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 | createNewTraceNameGroup(composite); | |
91 | return composite; | |
92 | } | |
93 | ||
94 | private void createNewTraceNameGroup(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 trace name label | |
103 | Label oldTraceLabel = new Label(folderGroup, SWT.NONE); | |
104 | oldTraceLabel.setFont(font); | |
105 | oldTraceLabel.setText(Messages.CopyTraceDialog_TraceName); | |
106 | ||
107 | // Old trace name field | |
108 | Text oldTraceName = new Text(folderGroup, SWT.BORDER); | |
109 | GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
110 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
111 | oldTraceName.setLayoutData(data); | |
112 | oldTraceName.setFont(font); | |
113 | oldTraceName.setText(fTrace.getName()); | |
114 | oldTraceName.setEnabled(false); | |
115 | ||
116 | // New trace name label | |
117 | Label newTraceLabel = new Label(folderGroup, SWT.NONE); | |
118 | newTraceLabel.setFont(font); | |
119 | newTraceLabel.setText(Messages.CopyTraceDialog_TraceNewName); | |
120 | ||
121 | // New trace name entry field | |
122 | fNewTraceName = new Text(folderGroup, SWT.BORDER); | |
123 | data = new GridData(GridData.FILL_HORIZONTAL); | |
124 | data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; | |
125 | fNewTraceName.setLayoutData(data); | |
126 | fNewTraceName.setFont(font); | |
127 | fNewTraceName.addListener(SWT.Modify, new Listener() { | |
128 | @Override | |
129 | public void handleEvent(Event event) { | |
130 | validateNewTraceName(); | |
131 | } | |
132 | }); | |
133 | } | |
134 | ||
135 | private void validateNewTraceName() { | |
136 | ||
137 | String name = fNewTraceName.getText(); | |
138 | IWorkspace workspace = fTraceFolder.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, | |
143 | Messages.Dialog_EmptyNameError, null)); | |
144 | return; | |
145 | } | |
146 | ||
147 | if (nameStatus.isOK() == false) { | |
148 | updateStatus(nameStatus); | |
149 | return; | |
150 | } | |
151 | ||
152 | IPath path = new Path(name); | |
153 | if (fTraceFolder.getFolder(path).exists() || fTraceFolder.getFile(path).exists()) { | |
154 | updateStatus(new Status(IStatus.ERROR, TmfUiPlugin.PLUGIN_ID, IStatus.ERROR, | |
155 | Messages.Dialog_ExistingNameError, null)); | |
156 | return; | |
157 | } | |
158 | ||
159 | updateStatus(new Status(IStatus.OK, TmfUiPlugin.PLUGIN_ID, "")); //$NON-NLS-1$ | |
160 | } | |
161 | ||
162 | // ------------------------------------------------------------------------ | |
163 | // SelectionStatusDialog | |
164 | // ------------------------------------------------------------------------ | |
165 | ||
166 | @Override | |
167 | protected void computeResult() { | |
168 | } | |
169 | ||
170 | @Override | |
171 | public void create() { | |
172 | super.create(); | |
173 | getButton(IDialogConstants.OK_ID).setEnabled(false); | |
174 | } | |
175 | ||
176 | @Override | |
177 | protected void okPressed() { | |
178 | IResource trace = copyTrace(fNewTraceName.getText()); | |
179 | if (trace == null) { | |
180 | return; | |
181 | } | |
182 | setSelectionResult(new IResource[] { trace }); | |
183 | super.okPressed(); | |
184 | ||
185 | if (fProject != null) { | |
186 | fProject.refresh(); | |
187 | } | |
188 | } | |
189 | ||
3453cab5 | 190 | private IResource copyTrace(final String newName) { |
12c155f5 FC |
191 | |
192 | IPath oldPath = fTrace.getResource().getFullPath(); | |
193 | final IPath newPath = oldPath.append("../" + newName); //$NON-NLS-1$ | |
194 | ||
195 | WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { | |
196 | @Override | |
197 | public void execute(IProgressMonitor monitor) throws CoreException { | |
198 | try { | |
199 | monitor.beginTask("", 1000); //$NON-NLS-1$ | |
200 | if (monitor.isCanceled()) { | |
201 | throw new OperationCanceledException(); | |
202 | } | |
5e4bf87d | 203 | // Copy supplementary files first |
e12ecd30 | 204 | fTrace.copySupplementaryFolder(newName); |
5e4bf87d | 205 | // Copy the trace |
12c155f5 | 206 | fTrace.getResource().copy(newPath, IResource.FORCE | IResource.SHALLOW, null); |
3453cab5 PT |
207 | // Delete any bookmarks file found in copied trace folder |
208 | IFolder folder = fTraceFolder.getFolder(newName); | |
209 | if (folder.exists()) { | |
210 | for (IResource member : folder.members()) { | |
e12ecd30 | 211 | if (TmfTrace.class.getCanonicalName().equals(member.getPersistentProperty(TmfCommonConstants.TRACETYPE))) { |
3453cab5 PT |
212 | member.delete(true, null); |
213 | } | |
214 | } | |
215 | } | |
12c155f5 FC |
216 | if (monitor.isCanceled()) { |
217 | throw new OperationCanceledException(); | |
218 | } | |
219 | } finally { | |
220 | monitor.done(); | |
221 | } | |
222 | } | |
223 | }; | |
224 | ||
225 | try { | |
226 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
227 | } catch (InterruptedException exception) { | |
228 | return null; | |
229 | } catch (InvocationTargetException exception) { | |
230 | MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$ | |
231 | return null; | |
232 | } catch (RuntimeException exception) { | |
233 | return null; | |
234 | } | |
235 | ||
236 | return fTrace.getResource(); | |
237 | } | |
238 | ||
239 | } |