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