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()); | |
d9030b38 | 264 | targetExperiment.deleteSupplementaryResources(); |
639181ba | 265 | targetExperiment.closeEditors(); |
ab37ff41 | 266 | return traceResource; |
828e5592 | 267 | } |
ab37ff41 | 268 | return null; |
828e5592 PT |
269 | } |
270 | ||
e12ecd30 | 271 | /** |
ab37ff41 | 272 | * Drop a trace by copying/linking a trace element in a trace folder |
abbdd66a | 273 | * |
e12ecd30 BH |
274 | * @param sourceTrace the source trace |
275 | * @param traceFolder the target trace folder | |
ab37ff41 | 276 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
e12ecd30 BH |
277 | * @return true if successful |
278 | */ | |
ab37ff41 PT |
279 | private static boolean drop(TmfTraceElement sourceTrace, |
280 | TmfTraceFolder traceFolder, | |
281 | int operation) { | |
282 | ||
e12ecd30 | 283 | IResource sourceResource = sourceTrace.getResource(); |
ab37ff41 PT |
284 | IResource targetResource = drop(sourceResource, traceFolder, operation); |
285 | ||
286 | if (targetResource != null) { | |
287 | IFolder destinationSupplementaryFolder = traceFolder.getTraceSupplementaryFolder(targetResource.getName()); | |
e12ecd30 BH |
288 | sourceTrace.copySupplementaryFolder(destinationSupplementaryFolder); |
289 | return true; | |
290 | } | |
291 | return false; | |
292 | } | |
293 | ||
828e5592 | 294 | /** |
ab37ff41 | 295 | * Drop a trace by copying/linking a resource in a trace folder |
abbdd66a | 296 | * |
828e5592 PT |
297 | * @param sourceResource the source resource |
298 | * @param traceFolder the target trace folder | |
ab37ff41 PT |
299 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
300 | * @return the target resource or null if unsuccessful | |
828e5592 | 301 | */ |
ab37ff41 PT |
302 | private static IResource drop(IResource sourceResource, |
303 | TmfTraceFolder traceFolder, | |
304 | int operation) { | |
e12ecd30 | 305 | |
ab37ff41 PT |
306 | if (sourceResource.getProject().equals(traceFolder.getResource().getProject())) { |
307 | return null; | |
308 | } | |
309 | String targetName = sourceResource.getName(); | |
828e5592 | 310 | for (TmfTraceElement trace : traceFolder.getTraces()) { |
ab37ff41 PT |
311 | if (trace.getName().equals(targetName)) { |
312 | targetName = promptRename(trace); | |
313 | if (targetName == null) { | |
314 | return null; | |
315 | } | |
828e5592 PT |
316 | break; |
317 | } | |
318 | } | |
ab37ff41 PT |
319 | try { |
320 | if (operation == DND.DROP_COPY) { | |
321 | IPath destination = traceFolder.getResource().getFullPath().addTrailingSeparator().append(targetName); | |
828e5592 | 322 | sourceResource.copy(destination, false, null); |
fedfc72a | 323 | cleanupBookmarks(destination); |
ab37ff41 PT |
324 | } else { |
325 | createLink(traceFolder.getResource(), sourceResource, targetName); | |
828e5592 | 326 | } |
ab37ff41 PT |
327 | return traceFolder.getResource().findMember(targetName); |
328 | } catch (CoreException e) { | |
329 | displayException(e); | |
828e5592 | 330 | } |
ab37ff41 | 331 | return null; |
828e5592 | 332 | } |
abbdd66a | 333 | |
828e5592 | 334 | /** |
ab37ff41 | 335 | * Drop a trace by importing/linking a path in a target experiment |
abbdd66a | 336 | * |
828e5592 PT |
337 | * @param path the source path |
338 | * @param targetExperiment the target experiment | |
ab37ff41 | 339 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
828e5592 PT |
340 | * @return true if successful |
341 | */ | |
ab37ff41 PT |
342 | private static boolean drop(Path path, |
343 | TmfExperimentElement targetExperiment, | |
344 | int operation) { | |
abbdd66a | 345 | |
9fa32496 BH |
346 | // Use local variable to avoid parameter assignment |
347 | Path pathToUse = path; | |
348 | ||
828e5592 | 349 | for (TmfTraceElement trace : targetExperiment.getTraces()) { |
ab37ff41 PT |
350 | if (trace.getName().equals(pathToUse.lastSegment()) && pathToUse.toString().startsWith(targetExperiment.getProject().getResource().getLocation().toString())) { |
351 | return false; | |
828e5592 PT |
352 | } |
353 | } | |
ab37ff41 PT |
354 | if (!pathToUse.toString().startsWith(targetExperiment.getProject().getResource().getLocation().toString())) { |
355 | String targetName = pathToUse.lastSegment(); | |
828e5592 | 356 | for (TmfTraceElement trace : targetExperiment.getProject().getTracesFolder().getTraces()) { |
ab37ff41 PT |
357 | if (trace.getName().equals(targetName)) { |
358 | targetName = promptRename(trace); | |
359 | if (targetName == null) { | |
360 | return false; | |
361 | } | |
828e5592 PT |
362 | break; |
363 | } | |
364 | } | |
ab37ff41 PT |
365 | if (operation == DND.DROP_COPY) { |
366 | importTrace(targetExperiment.getProject().getTracesFolder().getResource(), pathToUse, targetName); | |
367 | } else { | |
368 | createLink(targetExperiment.getProject().getTracesFolder().getResource(), pathToUse, targetName); | |
828e5592 | 369 | } |
ab37ff41 | 370 | // use the copied resource for the experiment |
828e5592 | 371 | IResource resource = null; |
9fa32496 | 372 | File file = new File(pathToUse.toString()); |
828e5592 | 373 | if (file.exists() && file.isFile()) { |
ab37ff41 | 374 | resource = targetExperiment.getProject().getTracesFolder().getResource().getFile(targetName); |
828e5592 | 375 | } else if (file.exists() && file.isDirectory()) { |
ab37ff41 | 376 | resource = targetExperiment.getProject().getTracesFolder().getResource().getFolder(targetName); |
828e5592 PT |
377 | } |
378 | if (resource != null && resource.exists()) { | |
ab37ff41 | 379 | createLink(targetExperiment.getResource(), resource, resource.getName()); |
d9030b38 | 380 | targetExperiment.deleteSupplementaryResources(); |
639181ba | 381 | targetExperiment.closeEditors(); |
828e5592 PT |
382 | return true; |
383 | } | |
384 | } | |
385 | return false; | |
386 | } | |
387 | ||
388 | /** | |
ab37ff41 | 389 | * Drop a trace by importing/linking a path in a trace folder |
abbdd66a | 390 | * |
828e5592 PT |
391 | * @param path the source path |
392 | * @param traceFolder the target trace folder | |
ab37ff41 | 393 | * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK) |
828e5592 PT |
394 | * @return true if successful |
395 | */ | |
ab37ff41 PT |
396 | private static boolean drop(Path path, |
397 | TmfTraceFolder traceFolder, | |
398 | int operation) { | |
399 | ||
400 | String targetName = path.lastSegment(); | |
828e5592 | 401 | for (TmfTraceElement trace : traceFolder.getTraces()) { |
ab37ff41 PT |
402 | if (trace.getName().equals(targetName)) { |
403 | targetName = promptRename(trace); | |
404 | if (targetName == null) { | |
405 | return false; | |
406 | } | |
828e5592 PT |
407 | break; |
408 | } | |
409 | } | |
ab37ff41 PT |
410 | if (operation == DND.DROP_COPY) { |
411 | importTrace(traceFolder.getResource(), path, targetName); | |
412 | } else { | |
413 | createLink(traceFolder.getResource(), path, targetName); | |
828e5592 | 414 | } |
ab37ff41 | 415 | return true; |
828e5592 PT |
416 | } |
417 | ||
418 | /** | |
419 | * Import a trace to the trace folder | |
abbdd66a | 420 | * |
ab37ff41 | 421 | * @param folder the trace folder resource |
828e5592 | 422 | * @param path the path to the trace to import |
ab37ff41 | 423 | * @param targetName the target name |
828e5592 | 424 | */ |
ab37ff41 PT |
425 | private static void importTrace(final IFolder folder, final Path path, final String targetName) { |
426 | final File source = new File(path.toString()); | |
427 | if (source.isDirectory()) { | |
428 | IPath containerPath = folder.getFullPath().addTrailingSeparator().append(targetName); | |
429 | IOverwriteQuery overwriteImplementor = new IOverwriteQuery() { | |
430 | @Override | |
431 | public String queryOverwrite(String pathString) { | |
432 | return IOverwriteQuery.NO_ALL; | |
433 | } | |
434 | }; | |
435 | List<File> filesToImport = Arrays.asList(source.listFiles()); | |
436 | ImportOperation operation = new ImportOperation( | |
437 | containerPath, | |
438 | source, | |
439 | FileSystemStructureProvider.INSTANCE, | |
440 | overwriteImplementor, | |
441 | filesToImport); | |
442 | operation.setCreateContainerStructure(false); | |
443 | try { | |
444 | operation.run(new NullProgressMonitor()); | |
445 | } catch (InvocationTargetException e) { | |
446 | displayException(e); | |
447 | } catch (InterruptedException e) { | |
448 | displayException(e); | |
449 | } | |
450 | } else { | |
451 | IRunnableWithProgress runnable = new IRunnableWithProgress() { | |
452 | @Override | |
453 | public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | |
454 | try { | |
455 | InputStream inputStream = new FileInputStream(source); | |
456 | IFile targetFile = folder.getFile(targetName); | |
457 | targetFile.create(inputStream, IResource.NONE, monitor); | |
458 | } catch (CoreException e) { | |
459 | displayException(e); | |
460 | } catch (FileNotFoundException e) { | |
461 | displayException(e); | |
462 | } | |
463 | } | |
464 | }; | |
465 | WorkspaceModifyDelegatingOperation operation = new WorkspaceModifyDelegatingOperation(runnable); | |
466 | try { | |
467 | operation.run(new NullProgressMonitor()); | |
468 | } catch (InvocationTargetException e) { | |
469 | displayException(e); | |
470 | } catch (InterruptedException e) { | |
471 | displayException(e); | |
828e5592 | 472 | } |
828e5592 | 473 | } |
abbdd66a | 474 | } |
fedfc72a | 475 | |
828e5592 PT |
476 | /** |
477 | * Create a link to the actual trace and set the trace type | |
abbdd66a | 478 | * |
828e5592 PT |
479 | * @param parentFolder the parent folder |
480 | * @param resource the resource | |
ab37ff41 | 481 | * @param targetName the target name |
828e5592 | 482 | */ |
ab37ff41 | 483 | private static void createLink(IFolder parentFolder, IResource resource, String targetName) { |
828e5592 PT |
484 | IPath location = resource.getLocation(); |
485 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); | |
486 | try { | |
487 | Map<QualifiedName, String> properties = resource.getPersistentProperties(); | |
e12ecd30 BH |
488 | String bundleName = properties.get(TmfCommonConstants.TRACEBUNDLE); |
489 | String traceType = properties.get(TmfCommonConstants.TRACETYPE); | |
490 | String iconUrl = properties.get(TmfCommonConstants.TRACEICON); | |
491 | String supplFolder = properties.get(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER); | |
492 | ||
828e5592 | 493 | if (resource instanceof IFolder) { |
ab37ff41 | 494 | IFolder folder = parentFolder.getFolder(targetName); |
828e5592 PT |
495 | if (workspace.validateLinkLocation(folder, location).isOK()) { |
496 | folder.createLink(location, IResource.REPLACE, null); | |
e12ecd30 | 497 | setProperties(folder, bundleName, traceType, iconUrl, supplFolder); |
828e5592 PT |
498 | |
499 | } else { | |
8fd82db5 | 500 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ |
828e5592 PT |
501 | } |
502 | } else { | |
ab37ff41 | 503 | IFile file = parentFolder.getFile(targetName); |
828e5592 PT |
504 | if (workspace.validateLinkLocation(file, location).isOK()) { |
505 | file.createLink(location, IResource.REPLACE, null); | |
e12ecd30 | 506 | setProperties(file, bundleName, traceType, iconUrl, supplFolder); |
828e5592 | 507 | } else { |
8fd82db5 | 508 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ |
828e5592 PT |
509 | } |
510 | } | |
511 | } catch (CoreException e) { | |
512 | displayException(e); | |
513 | } | |
514 | } | |
515 | ||
ab37ff41 PT |
516 | /** |
517 | * Create a link to a file or folder | |
518 | * | |
519 | * @param parentFolder the parent folder | |
520 | * @param source the file or folder | |
521 | * @param targetName the target name | |
522 | */ | |
523 | private static void createLink(IFolder parentFolder, IPath location, String targetName) { | |
524 | File source = new File(location.toString()); | |
525 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); | |
526 | try { | |
527 | ||
528 | if (source.isDirectory()) { | |
529 | IFolder folder = parentFolder.getFolder(targetName); | |
530 | if (workspace.validateLinkLocation(folder, location).isOK()) { | |
531 | folder.createLink(location, IResource.REPLACE, null); | |
532 | } else { | |
533 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ | |
534 | } | |
535 | } else { | |
536 | IFile file = parentFolder.getFile(targetName); | |
537 | if (workspace.validateLinkLocation(file, location).isOK()) { | |
538 | file.createLink(location, IResource.REPLACE, null); | |
539 | } else { | |
540 | Activator.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$ | |
541 | } | |
542 | } | |
543 | } catch (CoreException e) { | |
544 | displayException(e); | |
545 | } | |
546 | } | |
547 | ||
548 | /** | |
549 | * Prompts the user to rename a trace | |
550 | * | |
551 | * @param trace the existing trace | |
552 | * @return the new name to use or null if rename is canceled | |
553 | */ | |
554 | private static String promptRename(TmfTraceElement trace) { | |
555 | MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.ICON_QUESTION | SWT.CANCEL | SWT.OK); | |
556 | mb.setText(Messages.DropAdapterAssistant_RenameTraceTitle); | |
557 | mb.setMessage(NLS.bind(Messages.DropAdapterAssistant_RenameTraceMessage, trace.getName())); | |
558 | if (mb.open() != SWT.OK) { | |
559 | return null; | |
560 | } | |
561 | IFolder folder = trace.getProject().getTracesFolder().getResource(); | |
562 | int i = 2; | |
563 | while (true) { | |
564 | String name = trace.getName() + '-' + Integer.toString(i++); | |
565 | IResource resource = folder.findMember(name); | |
566 | if (resource == null) { | |
567 | return name; | |
568 | } | |
569 | } | |
570 | } | |
571 | ||
fedfc72a PT |
572 | /** |
573 | * Cleanup bookmarks file in copied trace | |
574 | */ | |
abbdd66a | 575 | private static void cleanupBookmarks(IPath path) { |
fedfc72a PT |
576 | IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(path); |
577 | if (folder.exists()) { | |
578 | try { | |
579 | for (IResource member : folder.members()) { | |
e12ecd30 | 580 | if (TmfTrace.class.getCanonicalName().equals(member.getPersistentProperty(TmfCommonConstants.TRACETYPE))) { |
fedfc72a PT |
581 | member.delete(true, null); |
582 | } | |
583 | } | |
584 | } catch (CoreException e) { | |
585 | displayException(e); | |
586 | } | |
587 | } | |
588 | } | |
589 | ||
828e5592 PT |
590 | /** |
591 | * Set the trace persistent properties | |
abbdd66a | 592 | * |
828e5592 PT |
593 | * @param resource the trace resource |
594 | * @param bundleName the bundle name | |
595 | * @param traceType the trace type | |
596 | * @param iconUrl the icon URL | |
abbdd66a | 597 | * @param supplFolder the directory of the directory for supplementary information or null to ignore the property |
828e5592 PT |
598 | * @throws CoreException |
599 | */ | |
abbdd66a AM |
600 | private static void setProperties(IResource resource, String bundleName, |
601 | String traceType, String iconUrl, String supplFolder) | |
602 | throws CoreException { | |
e12ecd30 BH |
603 | resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, bundleName); |
604 | resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceType); | |
605 | resource.setPersistentProperty(TmfCommonConstants.TRACEICON, iconUrl); | |
606 | resource.setPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, supplFolder); | |
828e5592 PT |
607 | } |
608 | ||
609 | /** | |
610 | * Display an exception in a message box | |
abbdd66a | 611 | * |
828e5592 PT |
612 | * @param e the exception |
613 | */ | |
abbdd66a | 614 | private static void displayException(Exception e) { |
828e5592 PT |
615 | MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
616 | mb.setText(e.getClass().getName()); | |
617 | mb.setMessage(e.getMessage()); | |
618 | mb.open(); | |
619 | } | |
620 | ||
621 | } |