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