Commit | Line | Data |
---|---|---|
828e5592 | 1 | /******************************************************************************* |
ab37ff41 | 2 | * Copyright (c) 2012, 2013 Ericsson |
abbdd66a | 3 | * |
828e5592 PT |
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 | * |
828e5592 PT |
9 | * Contributors: |
10 | * Patrick Tasse - Initial API and implementation | |
ab37ff41 | 11 | * Patrick Tasse - Add support for DROP_LINK and rename prompt on name clash |
828e5592 PT |
12 | *******************************************************************************/ |
13 | ||
d34665f9 | 14 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
828e5592 PT |
15 | |
16 | import java.io.File; | |
ab37ff41 PT |
17 | import java.io.FileInputStream; |
18 | import java.io.FileNotFoundException; | |
19 | import java.io.InputStream; | |
828e5592 | 20 | import java.lang.reflect.InvocationTargetException; |
ab37ff41 | 21 | import java.util.Arrays; |
828e5592 PT |
22 | import java.util.List; |
23 | import java.util.Map; | |
24 | ||
25 | import org.eclipse.core.resources.IFile; | |
26 | import org.eclipse.core.resources.IFolder; | |
27 | import org.eclipse.core.resources.IProject; | |
28 | import org.eclipse.core.resources.IResource; | |
29 | import org.eclipse.core.resources.IWorkspace; | |
30 | import org.eclipse.core.resources.ResourcesPlugin; | |
31 | import org.eclipse.core.runtime.CoreException; | |
32 | import org.eclipse.core.runtime.IPath; | |
ab37ff41 | 33 | import org.eclipse.core.runtime.IProgressMonitor; |
828e5592 PT |
34 | import org.eclipse.core.runtime.IStatus; |
35 | import org.eclipse.core.runtime.NullProgressMonitor; | |
36 | import org.eclipse.core.runtime.Path; | |
37 | import org.eclipse.core.runtime.QualifiedName; | |
38 | import org.eclipse.core.runtime.Status; | |
ab37ff41 | 39 | import org.eclipse.jface.operation.IRunnableWithProgress; |
828e5592 | 40 | import org.eclipse.jface.viewers.IStructuredSelection; |
8fd82db5 | 41 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
e12ecd30 | 42 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; |
fedfc72a | 43 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; |
828e5592 PT |
44 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; |
45 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; | |
46 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement; | |
47 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry; | |
48 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
49 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder; | |
ab37ff41 PT |
50 | import org.eclipse.osgi.util.NLS; |
51 | import org.eclipse.swt.SWT; | |
828e5592 PT |
52 | import org.eclipse.swt.dnd.DND; |
53 | import org.eclipse.swt.dnd.DropTargetEvent; | |
54 | import org.eclipse.swt.dnd.FileTransfer; | |
55 | import org.eclipse.swt.dnd.TransferData; | |
56 | import org.eclipse.swt.widgets.MessageBox; | |
57 | import org.eclipse.ui.PlatformUI; | |
ab37ff41 | 58 | import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation; |
828e5592 PT |
59 | import org.eclipse.ui.dialogs.IOverwriteQuery; |
60 | import org.eclipse.ui.navigator.CommonDropAdapter; | |
61 | import org.eclipse.ui.navigator.CommonDropAdapterAssistant; | |
62 | import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider; | |
63 | import org.eclipse.ui.wizards.datatransfer.ImportOperation; | |
64 | ||
65 | /** | |
66 | * Drop adapter assistant for project explorer | |
67 | */ | |
68 | public class DropAdapterAssistant extends CommonDropAdapterAssistant { | |
69 | ||
70 | /** | |
71 | * Default constructor | |
72 | */ | |
73 | public DropAdapterAssistant() { | |
74 | } | |
75 | ||
828e5592 PT |
76 | @Override |
77 | public boolean isSupportedType(TransferData aTransferType) { | |
78 | return super.isSupportedType(aTransferType) || FileTransfer.getInstance().isSupportedType(aTransferType); | |
79 | } | |
80 | ||
828e5592 PT |
81 | @Override |
82 | public IStatus validateDrop(Object target, int operation, TransferData transferType) { | |
83 | if (target instanceof TmfTraceFolder) { | |
828e5592 PT |
84 | return Status.OK_STATUS; |
85 | } | |
86 | if (target instanceof TmfExperimentElement) { | |
828e5592 PT |
87 | return Status.OK_STATUS; |
88 | } | |
89 | if (target instanceof TmfTraceElement) { | |
90 | ITmfProjectModelElement parent = ((TmfTraceElement) target).getParent(); | |
91 | if (parent instanceof TmfTraceFolder) { | |
828e5592 PT |
92 | return Status.OK_STATUS; |
93 | } | |
94 | if (parent instanceof TmfExperimentElement) { | |
828e5592 PT |
95 | return Status.OK_STATUS; |
96 | } | |
97 | } | |
98 | if (target instanceof IProject) { | |
828e5592 PT |
99 | return Status.OK_STATUS; |
100 | } | |
101 | return Status.CANCEL_STATUS; | |
102 | } | |
103 | ||
828e5592 PT |
104 | @Override |
105 | public IStatus handleDrop(CommonDropAdapter aDropAdapter, DropTargetEvent aDropTargetEvent, Object aTarget) { | |
106 | boolean ok = false; | |
107 | ||
9fa32496 BH |
108 | // Use local variable to avoid parameter assignment |
109 | Object targetToUse = aTarget; | |
110 | ||
ab37ff41 PT |
111 | int operation = aDropTargetEvent.detail; |
112 | if (operation != DND.DROP_LINK) { | |
113 | operation = DND.DROP_COPY; | |
114 | } | |
115 | ||
828e5592 | 116 | // If target is a trace, use its parent (either trace folder or experiment) |
9fa32496 BH |
117 | if (targetToUse instanceof TmfTraceElement) { |
118 | targetToUse = ((TmfTraceElement) targetToUse).getParent(); | |
828e5592 PT |
119 | } |
120 | ||
121 | // If target is a project, use its trace folder | |
9fa32496 BH |
122 | if (targetToUse instanceof IProject) { |
123 | TmfProjectElement projectElement = TmfProjectRegistry.getProject((IProject) targetToUse); | |
828e5592 | 124 | if (projectElement != null) { |
9fa32496 | 125 | targetToUse = projectElement.getTracesFolder(); |
828e5592 PT |
126 | } |
127 | } | |
128 | ||
129 | if (aDropTargetEvent.data instanceof IStructuredSelection) { | |
130 | IStructuredSelection selection = (IStructuredSelection) aDropTargetEvent.data; | |
131 | for (Object source : selection.toArray()) { | |
132 | if (source instanceof IResource) { | |
133 | // If source resource is a trace, use the trace element | |
134 | IResource sourceResource = (IResource) source; | |
135 | TmfProjectElement projectElement = TmfProjectRegistry.getProject(sourceResource.getProject()); | |
136 | if (projectElement != null && projectElement.getTracesFolder() != null) { | |
137 | for (TmfTraceElement trace : projectElement.getTracesFolder().getTraces()) { | |
138 | if (trace.getResource().equals(sourceResource)) { | |
139 | source = trace; | |
140 | break; | |
141 | } | |
142 | } | |
143 | } | |
144 | } | |
145 | if (source instanceof TmfTraceElement) { | |
146 | TmfTraceElement sourceTrace = (TmfTraceElement) source; | |
147 | // If source trace is under an experiment, use the original trace from the traces folder | |
ab37ff41 | 148 | sourceTrace = sourceTrace.getElementUnderTraceFolder(); |
9fa32496 BH |
149 | if (targetToUse instanceof TmfExperimentElement) { |
150 | TmfExperimentElement targetExperiment = (TmfExperimentElement) targetToUse; | |
ab37ff41 | 151 | ok |= drop(sourceTrace, targetExperiment, operation); |
9fa32496 BH |
152 | } else if (targetToUse instanceof TmfTraceFolder) { |
153 | TmfTraceFolder traceFolder = (TmfTraceFolder) targetToUse; | |
ab37ff41 | 154 | ok |= drop(sourceTrace, traceFolder, operation); |
828e5592 PT |
155 | } |
156 | } else if (source instanceof IResource) { | |
157 | IResource sourceResource = (IResource) source; | |
158 | if (sourceResource.getType() != IResource.FILE && sourceResource.getType() != IResource.FOLDER) { | |
159 | continue; | |
160 | } | |
9fa32496 BH |
161 | if (targetToUse instanceof TmfExperimentElement) { |
162 | TmfExperimentElement targetExperiment = (TmfExperimentElement) targetToUse; | |
ab37ff41 | 163 | ok |= (drop(sourceResource, targetExperiment, operation) != null); |
9fa32496 BH |
164 | } else if (targetToUse instanceof TmfTraceFolder) { |
165 | TmfTraceFolder traceFolder = (TmfTraceFolder) targetToUse; | |
ab37ff41 | 166 | ok |= (drop(sourceResource, traceFolder, operation) != null); |
828e5592 PT |
167 | } |
168 | } | |
169 | } | |
170 | } else if (aDropTargetEvent.data instanceof String[]) { | |
171 | String[] sources = (String[]) aDropTargetEvent.data; | |
172 | for (String source : sources) { | |
173 | Path path = new Path(source); | |
9fa32496 BH |
174 | if (targetToUse instanceof TmfExperimentElement) { |
175 | TmfExperimentElement targetExperiment = (TmfExperimentElement) targetToUse; | |
ab37ff41 | 176 | ok |= drop(path, targetExperiment, operation); |
9fa32496 BH |
177 | } else if (targetToUse instanceof TmfTraceFolder) { |
178 | TmfTraceFolder traceFolder = (TmfTraceFolder) targetToUse; | |
ab37ff41 | 179 | ok |= drop(path, traceFolder, operation); |
828e5592 PT |
180 | } |
181 | } | |
182 | } | |
183 | return (ok ? Status.OK_STATUS : Status.CANCEL_STATUS); | |
184 | } | |
185 | ||
abbdd66a | 186 | |
e12ecd30 | 187 | /** |
ab37ff41 | 188 | * Drop a trace by copying/linking a trace element in a target experiment |
abbdd66a | 189 | * |
e12ecd30 BH |
190 | * @param sourceTrace the source trace element to copy |
191 | * @param targetExperiment the target experiment | |
ab37ff41 | 192 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
e12ecd30 BH |
193 | * @return true if successful |
194 | */ | |
abbdd66a | 195 | private static boolean drop(TmfTraceElement sourceTrace, |
ab37ff41 PT |
196 | TmfExperimentElement targetExperiment, |
197 | int operation) { | |
abbdd66a | 198 | |
e12ecd30 | 199 | IResource sourceResource = sourceTrace.getResource(); |
ab37ff41 | 200 | IResource targetResource = drop(sourceResource, targetExperiment, operation); |
abbdd66a | 201 | |
ab37ff41 | 202 | if (targetResource != null) { |
c9378fe3 PT |
203 | if (! sourceTrace.getProject().equals(targetExperiment.getProject())) { |
204 | IFolder destinationSupplementaryFolder = targetExperiment.getTraceSupplementaryFolder(targetResource.getName()); | |
205 | sourceTrace.copySupplementaryFolder(destinationSupplementaryFolder); | |
206 | } | |
e12ecd30 BH |
207 | return true; |
208 | } | |
209 | return false; | |
210 | } | |
abbdd66a | 211 | |
828e5592 | 212 | /** |
ab37ff41 | 213 | * Drop a trace by copying/linking a resource in a target experiment |
abbdd66a | 214 | * |
828e5592 PT |
215 | * @param sourceResource the source resource |
216 | * @param targetExperiment the target experiment | |
ab37ff41 PT |
217 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
218 | * @return the target resource or null if unsuccessful | |
828e5592 | 219 | */ |
ab37ff41 PT |
220 | private static IResource drop(IResource sourceResource, |
221 | TmfExperimentElement targetExperiment, | |
222 | int operation) { | |
9fa32496 | 223 | |
ab37ff41 | 224 | IResource traceResource = sourceResource; |
9fa32496 | 225 | |
ab37ff41 | 226 | TmfProjectElement projectElement = TmfProjectRegistry.getProject(sourceResource.getProject()); |
828e5592 | 227 | for (TmfTraceElement trace : targetExperiment.getTraces()) { |
ab37ff41 PT |
228 | if (trace.getName().equals(sourceResource.getName()) && targetExperiment.getProject().equals(projectElement)) { |
229 | return null; | |
828e5592 PT |
230 | } |
231 | } | |
ab37ff41 PT |
232 | if (!targetExperiment.getProject().equals(projectElement)) { |
233 | String targetName = sourceResource.getName(); | |
828e5592 | 234 | for (TmfTraceElement trace : targetExperiment.getProject().getTracesFolder().getTraces()) { |
ab37ff41 PT |
235 | if (trace.getName().equals(targetName)) { |
236 | targetName = promptRename(trace); | |
237 | if (targetName == null) { | |
238 | return null; | |
239 | } | |
828e5592 PT |
240 | break; |
241 | } | |
242 | } | |
ab37ff41 PT |
243 | try { |
244 | if (operation == DND.DROP_COPY) { | |
245 | IPath destination = targetExperiment.getProject().getTracesFolder().getResource().getFullPath().addTrailingSeparator().append(targetName); | |
246 | sourceResource.copy(destination, false, null); | |
fedfc72a | 247 | cleanupBookmarks(destination); |
ab37ff41 PT |
248 | } else { |
249 | createLink(targetExperiment.getProject().getTracesFolder().getResource(), sourceResource, targetName); | |
250 | } | |
251 | // use the copied resource for the experiment | |
252 | if (sourceResource.getType() == IResource.FILE) { | |
253 | traceResource = targetExperiment.getProject().getTracesFolder().getResource().getFile(targetName); | |
254 | } else if (sourceResource.getType() == IResource.FOLDER) { | |
255 | traceResource = targetExperiment.getProject().getTracesFolder().getResource().getFolder(targetName); | |
828e5592 | 256 | } |
ab37ff41 PT |
257 | } catch (CoreException e) { |
258 | displayException(e); | |
259 | return null; | |
828e5592 PT |
260 | } |
261 | } | |
ab37ff41 PT |
262 | if (traceResource != null && traceResource.exists()) { |
263 | createLink(targetExperiment.getResource(), traceResource, traceResource.getName()); | |
264 | return traceResource; | |
828e5592 | 265 | } |
ab37ff41 | 266 | return null; |
828e5592 PT |
267 | } |
268 | ||
e12ecd30 | 269 | /** |
ab37ff41 | 270 | * Drop a trace by copying/linking a trace element in a trace folder |
abbdd66a | 271 | * |
e12ecd30 BH |
272 | * @param sourceTrace the source trace |
273 | * @param traceFolder the target trace folder | |
ab37ff41 | 274 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
e12ecd30 BH |
275 | * @return true if successful |
276 | */ | |
ab37ff41 PT |
277 | private static boolean drop(TmfTraceElement sourceTrace, |
278 | TmfTraceFolder traceFolder, | |
279 | int operation) { | |
280 | ||
e12ecd30 | 281 | IResource sourceResource = sourceTrace.getResource(); |
ab37ff41 PT |
282 | IResource targetResource = drop(sourceResource, traceFolder, operation); |
283 | ||
284 | if (targetResource != null) { | |
285 | IFolder destinationSupplementaryFolder = traceFolder.getTraceSupplementaryFolder(targetResource.getName()); | |
e12ecd30 BH |
286 | sourceTrace.copySupplementaryFolder(destinationSupplementaryFolder); |
287 | return true; | |
288 | } | |
289 | return false; | |
290 | } | |
291 | ||
828e5592 | 292 | /** |
ab37ff41 | 293 | * Drop a trace by copying/linking a resource in a trace folder |
abbdd66a | 294 | * |
828e5592 PT |
295 | * @param sourceResource the source resource |
296 | * @param traceFolder the target trace folder | |
ab37ff41 PT |
297 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
298 | * @return the target resource or null if unsuccessful | |
828e5592 | 299 | */ |
ab37ff41 PT |
300 | private static IResource drop(IResource sourceResource, |
301 | TmfTraceFolder traceFolder, | |
302 | int operation) { | |
e12ecd30 | 303 | |
ab37ff41 PT |
304 | if (sourceResource.getProject().equals(traceFolder.getResource().getProject())) { |
305 | return null; | |
306 | } | |
307 | String targetName = sourceResource.getName(); | |
828e5592 | 308 | for (TmfTraceElement trace : traceFolder.getTraces()) { |
ab37ff41 PT |
309 | if (trace.getName().equals(targetName)) { |
310 | targetName = promptRename(trace); | |
311 | if (targetName == null) { | |
312 | return null; | |
313 | } | |
828e5592 PT |
314 | break; |
315 | } | |
316 | } | |
ab37ff41 PT |
317 | try { |
318 | if (operation == DND.DROP_COPY) { | |
319 | IPath destination = traceFolder.getResource().getFullPath().addTrailingSeparator().append(targetName); | |
828e5592 | 320 | sourceResource.copy(destination, false, null); |
fedfc72a | 321 | cleanupBookmarks(destination); |
ab37ff41 PT |
322 | } else { |
323 | createLink(traceFolder.getResource(), sourceResource, targetName); | |
828e5592 | 324 | } |
ab37ff41 PT |
325 | return traceFolder.getResource().findMember(targetName); |
326 | } catch (CoreException e) { | |
327 | displayException(e); | |
828e5592 | 328 | } |
ab37ff41 | 329 | return null; |
828e5592 | 330 | } |
abbdd66a | 331 | |
828e5592 | 332 | /** |
ab37ff41 | 333 | * Drop a trace by importing/linking a path in a target experiment |
abbdd66a | 334 | * |
828e5592 PT |
335 | * @param path the source path |
336 | * @param targetExperiment the target experiment | |
ab37ff41 | 337 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
828e5592 PT |
338 | * @return true if successful |
339 | */ | |
ab37ff41 PT |
340 | private static boolean drop(Path path, |
341 | TmfExperimentElement targetExperiment, | |
342 | int operation) { | |
abbdd66a | 343 | |
9fa32496 BH |
344 | // Use local variable to avoid parameter assignment |
345 | Path pathToUse = path; | |
346 | ||
828e5592 | 347 | for (TmfTraceElement trace : targetExperiment.getTraces()) { |
ab37ff41 PT |
348 | if (trace.getName().equals(pathToUse.lastSegment()) && pathToUse.toString().startsWith(targetExperiment.getProject().getResource().getLocation().toString())) { |
349 | return false; | |
828e5592 PT |
350 | } |
351 | } | |
ab37ff41 PT |
352 | if (!pathToUse.toString().startsWith(targetExperiment.getProject().getResource().getLocation().toString())) { |
353 | String targetName = pathToUse.lastSegment(); | |
828e5592 | 354 | for (TmfTraceElement trace : targetExperiment.getProject().getTracesFolder().getTraces()) { |
ab37ff41 PT |
355 | if (trace.getName().equals(targetName)) { |
356 | targetName = promptRename(trace); | |
357 | if (targetName == null) { | |
358 | return false; | |
359 | } | |
828e5592 PT |
360 | break; |
361 | } | |
362 | } | |
ab37ff41 PT |
363 | if (operation == DND.DROP_COPY) { |
364 | importTrace(targetExperiment.getProject().getTracesFolder().getResource(), pathToUse, targetName); | |
365 | } else { | |
366 | createLink(targetExperiment.getProject().getTracesFolder().getResource(), pathToUse, targetName); | |
828e5592 | 367 | } |
ab37ff41 | 368 | // use the copied resource for the experiment |
828e5592 | 369 | IResource resource = null; |
9fa32496 | 370 | File file = new File(pathToUse.toString()); |
828e5592 | 371 | if (file.exists() && file.isFile()) { |
ab37ff41 | 372 | resource = targetExperiment.getProject().getTracesFolder().getResource().getFile(targetName); |
828e5592 | 373 | } else if (file.exists() && file.isDirectory()) { |
ab37ff41 | 374 | resource = targetExperiment.getProject().getTracesFolder().getResource().getFolder(targetName); |
828e5592 PT |
375 | } |
376 | if (resource != null && resource.exists()) { | |
ab37ff41 | 377 | createLink(targetExperiment.getResource(), resource, resource.getName()); |
828e5592 PT |
378 | return true; |
379 | } | |
380 | } | |
381 | return false; | |
382 | } | |
383 | ||
384 | /** | |
ab37ff41 | 385 | * Drop a trace by importing/linking a path in a trace folder |
abbdd66a | 386 | * |
828e5592 PT |
387 | * @param path the source path |
388 | * @param traceFolder the target trace folder | |
ab37ff41 | 389 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
828e5592 PT |
390 | * @return true if successful |
391 | */ | |
ab37ff41 PT |
392 | private static boolean drop(Path path, |
393 | TmfTraceFolder traceFolder, | |
394 | int operation) { | |
395 | ||
396 | String targetName = path.lastSegment(); | |
828e5592 | 397 | for (TmfTraceElement trace : traceFolder.getTraces()) { |
ab37ff41 PT |
398 | if (trace.getName().equals(targetName)) { |
399 | targetName = promptRename(trace); | |
400 | if (targetName == null) { | |
401 | return false; | |
402 | } | |
828e5592 PT |
403 | break; |
404 | } | |
405 | } | |
ab37ff41 PT |
406 | if (operation == DND.DROP_COPY) { |
407 | importTrace(traceFolder.getResource(), path, targetName); | |
408 | } else { | |
409 | createLink(traceFolder.getResource(), path, targetName); | |
828e5592 | 410 | } |
ab37ff41 | 411 | return true; |
828e5592 PT |
412 | } |
413 | ||
414 | /** | |
415 | * Import a trace to the trace folder | |
abbdd66a | 416 | * |
ab37ff41 | 417 | * @param folder the trace folder resource |
828e5592 | 418 | * @param path the path to the trace to import |
ab37ff41 | 419 | * @param targetName the target name |
828e5592 | 420 | */ |
ab37ff41 PT |
421 | private static void importTrace(final IFolder folder, final Path path, final String targetName) { |
422 | final File source = new File(path.toString()); | |
423 | if (source.isDirectory()) { | |
424 | IPath containerPath = folder.getFullPath().addTrailingSeparator().append(targetName); | |
425 | IOverwriteQuery overwriteImplementor = new IOverwriteQuery() { | |
426 | @Override | |
427 | public String queryOverwrite(String pathString) { | |
428 | return IOverwriteQuery.NO_ALL; | |
429 | } | |
430 | }; | |
431 | List<File> filesToImport = Arrays.asList(source.listFiles()); | |
432 | ImportOperation operation = new ImportOperation( | |
433 | containerPath, | |
434 | source, | |
435 | FileSystemStructureProvider.INSTANCE, | |
436 | overwriteImplementor, | |
437 | filesToImport); | |
438 | operation.setCreateContainerStructure(false); | |
439 | try { | |
440 | operation.run(new NullProgressMonitor()); | |
441 | } catch (InvocationTargetException e) { | |
442 | displayException(e); | |
443 | } catch (InterruptedException e) { | |
444 | displayException(e); | |
445 | } | |
446 | } else { | |
447 | IRunnableWithProgress runnable = new IRunnableWithProgress() { | |
448 | @Override | |
449 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
450 | try { | |
451 | InputStream inputStream = new FileInputStream(source); | |
452 | IFile targetFile = folder.getFile(targetName); | |
453 | targetFile.create(inputStream, IResource.NONE, monitor); | |
454 | } catch (CoreException e) { | |
455 | displayException(e); | |
456 | } catch (FileNotFoundException e) { | |
457 | displayException(e); | |
458 | } | |
459 | } | |
460 | }; | |
461 | WorkspaceModifyDelegatingOperation operation = new WorkspaceModifyDelegatingOperation(runnable); | |
462 | try { | |
463 | operation.run(new NullProgressMonitor()); | |
464 | } catch (InvocationTargetException e) { | |
465 | displayException(e); | |
466 | } catch (InterruptedException e) { | |
467 | displayException(e); | |
828e5592 | 468 | } |
828e5592 | 469 | } |
abbdd66a | 470 | } |
fedfc72a | 471 | |
828e5592 PT |
472 | /** |
473 | * Create a link to the actual trace and set the trace type | |
abbdd66a | 474 | * |
828e5592 PT |
475 | * @param parentFolder the parent folder |
476 | * @param resource the resource | |
ab37ff41 | 477 | * @param targetName the target name |
828e5592 | 478 | */ |
ab37ff41 | 479 | private static void createLink(IFolder parentFolder, IResource resource, String targetName) { |
828e5592 PT |
480 | IPath location = resource.getLocation(); |
481 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); | |
482 | try { | |
483 | Map<QualifiedName, String> properties = resource.getPersistentProperties(); | |
e12ecd30 BH |
484 | String bundleName = properties.get(TmfCommonConstants.TRACEBUNDLE); |
485 | String traceType = properties.get(TmfCommonConstants.TRACETYPE); | |
486 | String iconUrl = properties.get(TmfCommonConstants.TRACEICON); | |
487 | String supplFolder = properties.get(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER); | |
488 | ||
828e5592 | 489 | if (resource instanceof IFolder) { |
ab37ff41 | 490 | IFolder folder = parentFolder.getFolder(targetName); |
828e5592 PT |
491 | if (workspace.validateLinkLocation(folder, location).isOK()) { |
492 | folder.createLink(location, IResource.REPLACE, null); | |
e12ecd30 | 493 | setProperties(folder, bundleName, traceType, iconUrl, supplFolder); |
828e5592 PT |
494 | |
495 | } else { | |
8fd82db5 | 496 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ |
828e5592 PT |
497 | } |
498 | } else { | |
ab37ff41 | 499 | IFile file = parentFolder.getFile(targetName); |
828e5592 PT |
500 | if (workspace.validateLinkLocation(file, location).isOK()) { |
501 | file.createLink(location, IResource.REPLACE, null); | |
e12ecd30 | 502 | setProperties(file, bundleName, traceType, iconUrl, supplFolder); |
828e5592 | 503 | } else { |
8fd82db5 | 504 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ |
828e5592 PT |
505 | } |
506 | } | |
507 | } catch (CoreException e) { | |
508 | displayException(e); | |
509 | } | |
510 | } | |
511 | ||
ab37ff41 PT |
512 | /** |
513 | * Create a link to a file or folder | |
514 | * | |
515 | * @param parentFolder the parent folder | |
516 | * @param source the file or folder | |
517 | * @param targetName the target name | |
518 | */ | |
519 | private static void createLink(IFolder parentFolder, IPath location, String targetName) { | |
520 | File source = new File(location.toString()); | |
521 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); | |
522 | try { | |
523 | ||
524 | if (source.isDirectory()) { | |
525 | IFolder folder = parentFolder.getFolder(targetName); | |
526 | if (workspace.validateLinkLocation(folder, location).isOK()) { | |
527 | folder.createLink(location, IResource.REPLACE, null); | |
528 | } else { | |
529 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ | |
530 | } | |
531 | } else { | |
532 | IFile file = parentFolder.getFile(targetName); | |
533 | if (workspace.validateLinkLocation(file, location).isOK()) { | |
534 | file.createLink(location, IResource.REPLACE, null); | |
535 | } else { | |
536 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ | |
537 | } | |
538 | } | |
539 | } catch (CoreException e) { | |
540 | displayException(e); | |
541 | } | |
542 | } | |
543 | ||
544 | /** | |
545 | * Prompts the user to rename a trace | |
546 | * | |
547 | * @param trace the existing trace | |
548 | * @return the new name to use or null if rename is canceled | |
549 | */ | |
550 | private static String promptRename(TmfTraceElement trace) { | |
551 | MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK); | |
552 | mb.setText(Messages.DropAdapterAssistant_RenameTraceTitle); | |
553 | mb.setMessage(NLS.bind(Messages.DropAdapterAssistant_RenameTraceMessage, trace.getName())); | |
554 | if (mb.open() != SWT.OK) { | |
555 | return null; | |
556 | } | |
557 | IFolder folder = trace.getProject().getTracesFolder().getResource(); | |
558 | int i = 2; | |
559 | while (true) { | |
560 | String name = trace.getName() + '-' + Integer.toString(i++); | |
561 | IResource resource = folder.findMember(name); | |
562 | if (resource == null) { | |
563 | return name; | |
564 | } | |
565 | } | |
566 | } | |
567 | ||
fedfc72a PT |
568 | /** |
569 | * Cleanup bookmarks file in copied trace | |
570 | */ | |
abbdd66a | 571 | private static void cleanupBookmarks(IPath path) { |
fedfc72a PT |
572 | IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(path); |
573 | if (folder.exists()) { | |
574 | try { | |
575 | for (IResource member : folder.members()) { | |
e12ecd30 | 576 | if (TmfTrace.class.getCanonicalName().equals(member.getPersistentProperty(TmfCommonConstants.TRACETYPE))) { |
fedfc72a PT |
577 | member.delete(true, null); |
578 | } | |
579 | } | |
580 | } catch (CoreException e) { | |
581 | displayException(e); | |
582 | } | |
583 | } | |
584 | } | |
585 | ||
828e5592 PT |
586 | /** |
587 | * Set the trace persistent properties | |
abbdd66a | 588 | * |
828e5592 PT |
589 | * @param resource the trace resource |
590 | * @param bundleName the bundle name | |
591 | * @param traceType the trace type | |
592 | * @param iconUrl the icon URL | |
abbdd66a | 593 | * @param supplFolder the directory of the directory for supplementary information or null to ignore the property |
828e5592 PT |
594 | * @throws CoreException |
595 | */ | |
abbdd66a AM |
596 | private static void setProperties(IResource resource, String bundleName, |
597 | String traceType, String iconUrl, String supplFolder) | |
598 | throws CoreException { | |
e12ecd30 BH |
599 | resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, bundleName); |
600 | resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType); | |
601 | resource.setPersistentProperty(TmfCommonConstants.TRACEICON, iconUrl); | |
602 | resource.setPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, supplFolder); | |
828e5592 PT |
603 | } |
604 | ||
605 | /** | |
606 | * Display an exception in a message box | |
abbdd66a | 607 | * |
828e5592 PT |
608 | * @param e the exception |
609 | */ | |
abbdd66a | 610 | private static void displayException(Exception e) { |
828e5592 PT |
611 | MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
612 | mb.setText(e.getClass().getName()); | |
613 | mb.setMessage(e.getMessage()); | |
614 | mb.open(); | |
615 | } | |
616 | ||
617 | } |