1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 * Patrick Tasse - Add support for DROP_LINK and rename prompt on name clash
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.handlers
;
17 import java
.io
.FileInputStream
;
18 import java
.io
.FileNotFoundException
;
19 import java
.io
.InputStream
;
20 import java
.lang
.reflect
.InvocationTargetException
;
21 import java
.util
.Arrays
;
22 import java
.util
.List
;
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
;
33 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
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
;
39 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
40 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
41 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
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
;
50 import org
.eclipse
.osgi
.util
.NLS
;
51 import org
.eclipse
.swt
.SWT
;
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
;
58 import org
.eclipse
.ui
.actions
.WorkspaceModifyDelegatingOperation
;
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
;
66 * Drop adapter assistant for project explorer
68 public class DropAdapterAssistant
extends CommonDropAdapterAssistant
{
73 public DropAdapterAssistant() {
77 public boolean isSupportedType(TransferData aTransferType
) {
78 return super.isSupportedType(aTransferType
) || FileTransfer
.getInstance().isSupportedType(aTransferType
);
82 public IStatus
validateDrop(Object target
, int operation
, TransferData transferType
) {
83 if (target
instanceof TmfTraceFolder
) {
84 return Status
.OK_STATUS
;
86 if (target
instanceof TmfExperimentElement
) {
87 return Status
.OK_STATUS
;
89 if (target
instanceof TmfTraceElement
) {
90 ITmfProjectModelElement parent
= ((TmfTraceElement
) target
).getParent();
91 if (parent
instanceof TmfTraceFolder
) {
92 return Status
.OK_STATUS
;
94 if (parent
instanceof TmfExperimentElement
) {
95 return Status
.OK_STATUS
;
98 if (target
instanceof IProject
) {
99 return Status
.OK_STATUS
;
101 return Status
.CANCEL_STATUS
;
105 public IStatus
handleDrop(CommonDropAdapter aDropAdapter
, DropTargetEvent aDropTargetEvent
, Object aTarget
) {
108 // Use local variable to avoid parameter assignment
109 Object targetToUse
= aTarget
;
111 int operation
= aDropTargetEvent
.detail
;
112 if (operation
!= DND
.DROP_LINK
) {
113 operation
= DND
.DROP_COPY
;
116 // If target is a trace, use its parent (either trace folder or experiment)
117 if (targetToUse
instanceof TmfTraceElement
) {
118 targetToUse
= ((TmfTraceElement
) targetToUse
).getParent();
121 // If target is a project, use its trace folder
122 if (targetToUse
instanceof IProject
) {
123 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject((IProject
) targetToUse
);
124 if (projectElement
!= null) {
125 targetToUse
= projectElement
.getTracesFolder();
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
)) {
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
148 sourceTrace
= sourceTrace
.getElementUnderTraceFolder();
149 if (targetToUse
instanceof TmfExperimentElement
) {
150 TmfExperimentElement targetExperiment
= (TmfExperimentElement
) targetToUse
;
151 ok
|= drop(sourceTrace
, targetExperiment
, operation
);
152 } else if (targetToUse
instanceof TmfTraceFolder
) {
153 TmfTraceFolder traceFolder
= (TmfTraceFolder
) targetToUse
;
154 ok
|= drop(sourceTrace
, traceFolder
, operation
);
156 } else if (source
instanceof IResource
) {
157 IResource sourceResource
= (IResource
) source
;
158 if (sourceResource
.getType() != IResource
.FILE
&& sourceResource
.getType() != IResource
.FOLDER
) {
161 if (targetToUse
instanceof TmfExperimentElement
) {
162 TmfExperimentElement targetExperiment
= (TmfExperimentElement
) targetToUse
;
163 ok
|= (drop(sourceResource
, targetExperiment
, operation
) != null);
164 } else if (targetToUse
instanceof TmfTraceFolder
) {
165 TmfTraceFolder traceFolder
= (TmfTraceFolder
) targetToUse
;
166 ok
|= (drop(sourceResource
, traceFolder
, operation
) != null);
170 } else if (aDropTargetEvent
.data
instanceof String
[]) {
171 String
[] sources
= (String
[]) aDropTargetEvent
.data
;
172 for (String source
: sources
) {
173 Path path
= new Path(source
);
174 if (targetToUse
instanceof TmfExperimentElement
) {
175 TmfExperimentElement targetExperiment
= (TmfExperimentElement
) targetToUse
;
176 ok
|= drop(path
, targetExperiment
, operation
);
177 } else if (targetToUse
instanceof TmfTraceFolder
) {
178 TmfTraceFolder traceFolder
= (TmfTraceFolder
) targetToUse
;
179 ok
|= drop(path
, traceFolder
, operation
);
183 return (ok ? Status
.OK_STATUS
: Status
.CANCEL_STATUS
);
188 * Drop a trace by copying/linking a trace element in a target experiment
190 * @param sourceTrace the source trace element to copy
191 * @param targetExperiment the target experiment
192 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
193 * @return true if successful
195 private static boolean drop(TmfTraceElement sourceTrace
,
196 TmfExperimentElement targetExperiment
,
199 IResource sourceResource
= sourceTrace
.getResource();
200 IResource targetResource
= drop(sourceResource
, targetExperiment
, operation
);
202 if (targetResource
!= null) {
203 if (! sourceTrace
.getProject().equals(targetExperiment
.getProject())) {
204 IFolder destinationSupplementaryFolder
= targetExperiment
.getTraceSupplementaryFolder(targetResource
.getName());
205 sourceTrace
.copySupplementaryFolder(destinationSupplementaryFolder
);
213 * Drop a trace by copying/linking a resource in a target experiment
215 * @param sourceResource the source resource
216 * @param targetExperiment the target experiment
217 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
218 * @return the target resource or null if unsuccessful
220 private static IResource
drop(IResource sourceResource
,
221 TmfExperimentElement targetExperiment
,
224 IResource traceResource
= sourceResource
;
226 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(sourceResource
.getProject());
227 for (TmfTraceElement trace
: targetExperiment
.getTraces()) {
228 if (trace
.getName().equals(sourceResource
.getName()) && targetExperiment
.getProject().equals(projectElement
)) {
232 if (!targetExperiment
.getProject().equals(projectElement
)) {
233 String targetName
= sourceResource
.getName();
234 for (TmfTraceElement trace
: targetExperiment
.getProject().getTracesFolder().getTraces()) {
235 if (trace
.getName().equals(targetName
)) {
236 targetName
= promptRename(trace
);
237 if (targetName
== null) {
244 if (operation
== DND
.DROP_COPY
) {
245 IPath destination
= targetExperiment
.getProject().getTracesFolder().getResource().getFullPath().addTrailingSeparator().append(targetName
);
246 sourceResource
.copy(destination
, false, null);
247 cleanupBookmarks(destination
);
249 createLink(targetExperiment
.getProject().getTracesFolder().getResource(), sourceResource
, targetName
);
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
);
257 } catch (CoreException e
) {
262 if (traceResource
!= null && traceResource
.exists()) {
263 createLink(targetExperiment
.getResource(), traceResource
, traceResource
.getName());
264 return traceResource
;
270 * Drop a trace by copying/linking a trace element in a trace folder
272 * @param sourceTrace the source trace
273 * @param traceFolder the target trace folder
274 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
275 * @return true if successful
277 private static boolean drop(TmfTraceElement sourceTrace
,
278 TmfTraceFolder traceFolder
,
281 IResource sourceResource
= sourceTrace
.getResource();
282 IResource targetResource
= drop(sourceResource
, traceFolder
, operation
);
284 if (targetResource
!= null) {
285 IFolder destinationSupplementaryFolder
= traceFolder
.getTraceSupplementaryFolder(targetResource
.getName());
286 sourceTrace
.copySupplementaryFolder(destinationSupplementaryFolder
);
293 * Drop a trace by copying/linking a resource in a trace folder
295 * @param sourceResource the source resource
296 * @param traceFolder the target trace folder
297 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
298 * @return the target resource or null if unsuccessful
300 private static IResource
drop(IResource sourceResource
,
301 TmfTraceFolder traceFolder
,
304 if (sourceResource
.getProject().equals(traceFolder
.getResource().getProject())) {
307 String targetName
= sourceResource
.getName();
308 for (TmfTraceElement trace
: traceFolder
.getTraces()) {
309 if (trace
.getName().equals(targetName
)) {
310 targetName
= promptRename(trace
);
311 if (targetName
== null) {
318 if (operation
== DND
.DROP_COPY
) {
319 IPath destination
= traceFolder
.getResource().getFullPath().addTrailingSeparator().append(targetName
);
320 sourceResource
.copy(destination
, false, null);
321 cleanupBookmarks(destination
);
323 createLink(traceFolder
.getResource(), sourceResource
, targetName
);
325 return traceFolder
.getResource().findMember(targetName
);
326 } catch (CoreException e
) {
333 * Drop a trace by importing/linking a path in a target experiment
335 * @param path the source path
336 * @param targetExperiment the target experiment
337 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
338 * @return true if successful
340 private static boolean drop(Path path
,
341 TmfExperimentElement targetExperiment
,
344 // Use local variable to avoid parameter assignment
345 Path pathToUse
= path
;
347 for (TmfTraceElement trace
: targetExperiment
.getTraces()) {
348 if (trace
.getName().equals(pathToUse
.lastSegment()) && pathToUse
.toString().startsWith(targetExperiment
.getProject().getResource().getLocation().toString())) {
352 if (!pathToUse
.toString().startsWith(targetExperiment
.getProject().getResource().getLocation().toString())) {
353 String targetName
= pathToUse
.lastSegment();
354 for (TmfTraceElement trace
: targetExperiment
.getProject().getTracesFolder().getTraces()) {
355 if (trace
.getName().equals(targetName
)) {
356 targetName
= promptRename(trace
);
357 if (targetName
== null) {
363 if (operation
== DND
.DROP_COPY
) {
364 importTrace(targetExperiment
.getProject().getTracesFolder().getResource(), pathToUse
, targetName
);
366 createLink(targetExperiment
.getProject().getTracesFolder().getResource(), pathToUse
, targetName
);
368 // use the copied resource for the experiment
369 IResource resource
= null;
370 File file
= new File(pathToUse
.toString());
371 if (file
.exists() && file
.isFile()) {
372 resource
= targetExperiment
.getProject().getTracesFolder().getResource().getFile(targetName
);
373 } else if (file
.exists() && file
.isDirectory()) {
374 resource
= targetExperiment
.getProject().getTracesFolder().getResource().getFolder(targetName
);
376 if (resource
!= null && resource
.exists()) {
377 createLink(targetExperiment
.getResource(), resource
, resource
.getName());
385 * Drop a trace by importing/linking a path in a trace folder
387 * @param path the source path
388 * @param traceFolder the target trace folder
389 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
390 * @return true if successful
392 private static boolean drop(Path path
,
393 TmfTraceFolder traceFolder
,
396 String targetName
= path
.lastSegment();
397 for (TmfTraceElement trace
: traceFolder
.getTraces()) {
398 if (trace
.getName().equals(targetName
)) {
399 targetName
= promptRename(trace
);
400 if (targetName
== null) {
406 if (operation
== DND
.DROP_COPY
) {
407 importTrace(traceFolder
.getResource(), path
, targetName
);
409 createLink(traceFolder
.getResource(), path
, targetName
);
415 * Import a trace to the trace folder
417 * @param folder the trace folder resource
418 * @param path the path to the trace to import
419 * @param targetName the target name
421 private static void importTrace(final IFolder folder
, final Path path
, final String targetName
) {
422 final File source
= new File(path
.toString());
423 if (source
.isDirectory()) {
424 IPath containerPath
= folder
.getFullPath().addTrailingSeparator().append(targetName
);
425 IOverwriteQuery overwriteImplementor
= new IOverwriteQuery() {
427 public String
queryOverwrite(String pathString
) {
428 return IOverwriteQuery
.NO_ALL
;
431 List
<File
> filesToImport
= Arrays
.asList(source
.listFiles());
432 ImportOperation operation
= new ImportOperation(
435 FileSystemStructureProvider
.INSTANCE
,
436 overwriteImplementor
,
438 operation
.setCreateContainerStructure(false);
440 operation
.run(new NullProgressMonitor());
441 } catch (InvocationTargetException e
) {
443 } catch (InterruptedException e
) {
447 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
449 public void run(IProgressMonitor monitor
) throws InvocationTargetException
, InterruptedException
{
451 InputStream inputStream
= new FileInputStream(source
);
452 IFile targetFile
= folder
.getFile(targetName
);
453 targetFile
.create(inputStream
, IResource
.NONE
, monitor
);
454 } catch (CoreException e
) {
456 } catch (FileNotFoundException e
) {
461 WorkspaceModifyDelegatingOperation operation
= new WorkspaceModifyDelegatingOperation(runnable
);
463 operation
.run(new NullProgressMonitor());
464 } catch (InvocationTargetException e
) {
466 } catch (InterruptedException e
) {
473 * Create a link to the actual trace and set the trace type
475 * @param parentFolder the parent folder
476 * @param resource the resource
477 * @param targetName the target name
479 private static void createLink(IFolder parentFolder
, IResource resource
, String targetName
) {
480 IPath location
= resource
.getLocation();
481 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
483 Map
<QualifiedName
, String
> properties
= resource
.getPersistentProperties();
484 String bundleName
= properties
.get(TmfCommonConstants
.TRACEBUNDLE
);
485 String traceType
= properties
.get(TmfCommonConstants
.TRACETYPE
);
486 String iconUrl
= properties
.get(TmfCommonConstants
.TRACEICON
);
487 String supplFolder
= properties
.get(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
489 if (resource
instanceof IFolder
) {
490 IFolder folder
= parentFolder
.getFolder(targetName
);
491 if (workspace
.validateLinkLocation(folder
, location
).isOK()) {
492 folder
.createLink(location
, IResource
.REPLACE
, null);
493 setProperties(folder
, bundleName
, traceType
, iconUrl
, supplFolder
);
496 Activator
.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$
499 IFile file
= parentFolder
.getFile(targetName
);
500 if (workspace
.validateLinkLocation(file
, location
).isOK()) {
501 file
.createLink(location
, IResource
.REPLACE
, null);
502 setProperties(file
, bundleName
, traceType
, iconUrl
, supplFolder
);
504 Activator
.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$
507 } catch (CoreException e
) {
513 * Create a link to a file or folder
515 * @param parentFolder the parent folder
516 * @param source the file or folder
517 * @param targetName the target name
519 private static void createLink(IFolder parentFolder
, IPath location
, String targetName
) {
520 File source
= new File(location
.toString());
521 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
524 if (source
.isDirectory()) {
525 IFolder folder
= parentFolder
.getFolder(targetName
);
526 if (workspace
.validateLinkLocation(folder
, location
).isOK()) {
527 folder
.createLink(location
, IResource
.REPLACE
, null);
529 Activator
.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$
532 IFile file
= parentFolder
.getFile(targetName
);
533 if (workspace
.validateLinkLocation(file
, location
).isOK()) {
534 file
.createLink(location
, IResource
.REPLACE
, null);
536 Activator
.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$
539 } catch (CoreException e
) {
545 * Prompts the user to rename a trace
547 * @param trace the existing trace
548 * @return the new name to use or null if rename is canceled
550 private static String
promptRename(TmfTraceElement trace
) {
551 MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT
.ICON_QUESTION
| SWT
.CANCEL
| SWT
.OK
);
552 mb
.setText(Messages
.DropAdapterAssistant_RenameTraceTitle
);
553 mb
.setMessage(NLS
.bind(Messages
.DropAdapterAssistant_RenameTraceMessage
, trace
.getName()));
554 if (mb
.open() != SWT
.OK
) {
557 IFolder folder
= trace
.getProject().getTracesFolder().getResource();
560 String name
= trace
.getName() + '-' + Integer
.toString(i
++);
561 IResource resource
= folder
.findMember(name
);
562 if (resource
== null) {
569 * Cleanup bookmarks file in copied trace
571 private static void cleanupBookmarks(IPath path
) {
572 IFolder folder
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(path
);
573 if (folder
.exists()) {
575 for (IResource member
: folder
.members()) {
576 if (TmfTrace
.class.getCanonicalName().equals(member
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
))) {
577 member
.delete(true, null);
580 } catch (CoreException e
) {
587 * Set the trace persistent properties
589 * @param resource the trace resource
590 * @param bundleName the bundle name
591 * @param traceType the trace type
592 * @param iconUrl the icon URL
593 * @param supplFolder the directory of the directory for supplementary information or null to ignore the property
594 * @throws CoreException
596 private static void setProperties(IResource resource
, String bundleName
,
597 String traceType
, String iconUrl
, String supplFolder
)
598 throws CoreException
{
599 resource
.setPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
, bundleName
);
600 resource
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceType
);
601 resource
.setPersistentProperty(TmfCommonConstants
.TRACEICON
, iconUrl
);
602 resource
.setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, supplFolder
);
606 * Display an exception in a message box
608 * @param e the exception
610 private static void displayException(Exception e
) {
611 MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
612 mb
.setText(e
.getClass().getName());
613 mb
.setMessage(e
.getMessage());