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 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#isSupportedType(org.eclipse.swt.dnd.TransferData)
80 public boolean isSupportedType(TransferData aTransferType
) {
81 return super.isSupportedType(aTransferType
) || FileTransfer
.getInstance().isSupportedType(aTransferType
);
85 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
88 public IStatus
validateDrop(Object target
, int operation
, TransferData transferType
) {
89 if (target
instanceof TmfTraceFolder
) {
90 return Status
.OK_STATUS
;
92 if (target
instanceof TmfExperimentElement
) {
93 return Status
.OK_STATUS
;
95 if (target
instanceof TmfTraceElement
) {
96 ITmfProjectModelElement parent
= ((TmfTraceElement
) target
).getParent();
97 if (parent
instanceof TmfTraceFolder
) {
98 return Status
.OK_STATUS
;
100 if (parent
instanceof TmfExperimentElement
) {
101 return Status
.OK_STATUS
;
104 if (target
instanceof IProject
) {
105 return Status
.OK_STATUS
;
107 return Status
.CANCEL_STATUS
;
111 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
114 public IStatus
handleDrop(CommonDropAdapter aDropAdapter
, DropTargetEvent aDropTargetEvent
, Object aTarget
) {
117 // Use local variable to avoid parameter assignment
118 Object targetToUse
= aTarget
;
120 int operation
= aDropTargetEvent
.detail
;
121 if (operation
!= DND
.DROP_LINK
) {
122 operation
= DND
.DROP_COPY
;
125 // If target is a trace, use its parent (either trace folder or experiment)
126 if (targetToUse
instanceof TmfTraceElement
) {
127 targetToUse
= ((TmfTraceElement
) targetToUse
).getParent();
130 // If target is a project, use its trace folder
131 if (targetToUse
instanceof IProject
) {
132 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject((IProject
) targetToUse
);
133 if (projectElement
!= null) {
134 targetToUse
= projectElement
.getTracesFolder();
138 if (aDropTargetEvent
.data
instanceof IStructuredSelection
) {
139 IStructuredSelection selection
= (IStructuredSelection
) aDropTargetEvent
.data
;
140 for (Object source
: selection
.toArray()) {
141 if (source
instanceof IResource
) {
142 // If source resource is a trace, use the trace element
143 IResource sourceResource
= (IResource
) source
;
144 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(sourceResource
.getProject());
145 if (projectElement
!= null && projectElement
.getTracesFolder() != null) {
146 for (TmfTraceElement trace
: projectElement
.getTracesFolder().getTraces()) {
147 if (trace
.getResource().equals(sourceResource
)) {
154 if (source
instanceof TmfTraceElement
) {
155 TmfTraceElement sourceTrace
= (TmfTraceElement
) source
;
156 // If source trace is under an experiment, use the original trace from the traces folder
157 sourceTrace
= sourceTrace
.getElementUnderTraceFolder();
158 if (targetToUse
instanceof TmfExperimentElement
) {
159 TmfExperimentElement targetExperiment
= (TmfExperimentElement
) targetToUse
;
160 ok
|= drop(sourceTrace
, targetExperiment
, operation
);
161 } else if (targetToUse
instanceof TmfTraceFolder
) {
162 TmfTraceFolder traceFolder
= (TmfTraceFolder
) targetToUse
;
163 ok
|= drop(sourceTrace
, traceFolder
, operation
);
165 } else if (source
instanceof IResource
) {
166 IResource sourceResource
= (IResource
) source
;
167 if (sourceResource
.getType() != IResource
.FILE
&& sourceResource
.getType() != IResource
.FOLDER
) {
170 if (targetToUse
instanceof TmfExperimentElement
) {
171 TmfExperimentElement targetExperiment
= (TmfExperimentElement
) targetToUse
;
172 ok
|= (drop(sourceResource
, targetExperiment
, operation
) != null);
173 } else if (targetToUse
instanceof TmfTraceFolder
) {
174 TmfTraceFolder traceFolder
= (TmfTraceFolder
) targetToUse
;
175 ok
|= (drop(sourceResource
, traceFolder
, operation
) != null);
179 } else if (aDropTargetEvent
.data
instanceof String
[]) {
180 String
[] sources
= (String
[]) aDropTargetEvent
.data
;
181 for (String source
: sources
) {
182 Path path
= new Path(source
);
183 if (targetToUse
instanceof TmfExperimentElement
) {
184 TmfExperimentElement targetExperiment
= (TmfExperimentElement
) targetToUse
;
185 ok
|= drop(path
, targetExperiment
, operation
);
186 } else if (targetToUse
instanceof TmfTraceFolder
) {
187 TmfTraceFolder traceFolder
= (TmfTraceFolder
) targetToUse
;
188 ok
|= drop(path
, traceFolder
, operation
);
192 return (ok ? Status
.OK_STATUS
: Status
.CANCEL_STATUS
);
197 * Drop a trace by copying/linking a trace element in a target experiment
199 * @param sourceTrace the source trace element to copy
200 * @param targetExperiment the target experiment
201 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
202 * @return true if successful
204 private static boolean drop(TmfTraceElement sourceTrace
,
205 TmfExperimentElement targetExperiment
,
208 IResource sourceResource
= sourceTrace
.getResource();
209 IResource targetResource
= drop(sourceResource
, targetExperiment
, operation
);
211 if (targetResource
!= null) {
212 IFolder destinationSupplementaryFolder
= targetExperiment
.getTraceSupplementaryFolder(targetResource
.getName());
213 sourceTrace
.copySupplementaryFolder(destinationSupplementaryFolder
);
220 * Drop a trace by copying/linking a resource in a target experiment
222 * @param sourceResource the source resource
223 * @param targetExperiment the target experiment
224 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
225 * @return the target resource or null if unsuccessful
227 private static IResource
drop(IResource sourceResource
,
228 TmfExperimentElement targetExperiment
,
231 IResource traceResource
= sourceResource
;
233 TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(sourceResource
.getProject());
234 for (TmfTraceElement trace
: targetExperiment
.getTraces()) {
235 if (trace
.getName().equals(sourceResource
.getName()) && targetExperiment
.getProject().equals(projectElement
)) {
239 if (!targetExperiment
.getProject().equals(projectElement
)) {
240 String targetName
= sourceResource
.getName();
241 for (TmfTraceElement trace
: targetExperiment
.getProject().getTracesFolder().getTraces()) {
242 if (trace
.getName().equals(targetName
)) {
243 targetName
= promptRename(trace
);
244 if (targetName
== null) {
251 if (operation
== DND
.DROP_COPY
) {
252 IPath destination
= targetExperiment
.getProject().getTracesFolder().getResource().getFullPath().addTrailingSeparator().append(targetName
);
253 sourceResource
.copy(destination
, false, null);
254 cleanupBookmarks(destination
);
256 createLink(targetExperiment
.getProject().getTracesFolder().getResource(), sourceResource
, targetName
);
258 // use the copied resource for the experiment
259 if (sourceResource
.getType() == IResource
.FILE
) {
260 traceResource
= targetExperiment
.getProject().getTracesFolder().getResource().getFile(targetName
);
261 } else if (sourceResource
.getType() == IResource
.FOLDER
) {
262 traceResource
= targetExperiment
.getProject().getTracesFolder().getResource().getFolder(targetName
);
264 } catch (CoreException e
) {
269 if (traceResource
!= null && traceResource
.exists()) {
270 createLink(targetExperiment
.getResource(), traceResource
, traceResource
.getName());
271 return traceResource
;
277 * Drop a trace by copying/linking a trace element in a trace folder
279 * @param sourceTrace the source trace
280 * @param traceFolder the target trace folder
281 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
282 * @return true if successful
284 private static boolean drop(TmfTraceElement sourceTrace
,
285 TmfTraceFolder traceFolder
,
288 IResource sourceResource
= sourceTrace
.getResource();
289 IResource targetResource
= drop(sourceResource
, traceFolder
, operation
);
291 if (targetResource
!= null) {
292 IFolder destinationSupplementaryFolder
= traceFolder
.getTraceSupplementaryFolder(targetResource
.getName());
293 sourceTrace
.copySupplementaryFolder(destinationSupplementaryFolder
);
300 * Drop a trace by copying/linking a resource in a trace folder
302 * @param sourceResource the source resource
303 * @param traceFolder the target trace folder
304 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
305 * @return the target resource or null if unsuccessful
307 private static IResource
drop(IResource sourceResource
,
308 TmfTraceFolder traceFolder
,
311 if (sourceResource
.getProject().equals(traceFolder
.getResource().getProject())) {
314 String targetName
= sourceResource
.getName();
315 for (TmfTraceElement trace
: traceFolder
.getTraces()) {
316 if (trace
.getName().equals(targetName
)) {
317 targetName
= promptRename(trace
);
318 if (targetName
== null) {
325 if (operation
== DND
.DROP_COPY
) {
326 IPath destination
= traceFolder
.getResource().getFullPath().addTrailingSeparator().append(targetName
);
327 sourceResource
.copy(destination
, false, null);
328 cleanupBookmarks(destination
);
330 createLink(traceFolder
.getResource(), sourceResource
, targetName
);
332 return traceFolder
.getResource().findMember(targetName
);
333 } catch (CoreException e
) {
340 * Drop a trace by importing/linking a path in a target experiment
342 * @param path the source path
343 * @param targetExperiment the target experiment
344 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
345 * @return true if successful
347 private static boolean drop(Path path
,
348 TmfExperimentElement targetExperiment
,
351 // Use local variable to avoid parameter assignment
352 Path pathToUse
= path
;
354 for (TmfTraceElement trace
: targetExperiment
.getTraces()) {
355 if (trace
.getName().equals(pathToUse
.lastSegment()) && pathToUse
.toString().startsWith(targetExperiment
.getProject().getResource().getLocation().toString())) {
359 if (!pathToUse
.toString().startsWith(targetExperiment
.getProject().getResource().getLocation().toString())) {
360 String targetName
= pathToUse
.lastSegment();
361 for (TmfTraceElement trace
: targetExperiment
.getProject().getTracesFolder().getTraces()) {
362 if (trace
.getName().equals(targetName
)) {
363 targetName
= promptRename(trace
);
364 if (targetName
== null) {
370 if (operation
== DND
.DROP_COPY
) {
371 importTrace(targetExperiment
.getProject().getTracesFolder().getResource(), pathToUse
, targetName
);
373 createLink(targetExperiment
.getProject().getTracesFolder().getResource(), pathToUse
, targetName
);
375 // use the copied resource for the experiment
376 IResource resource
= null;
377 File file
= new File(pathToUse
.toString());
378 if (file
.exists() && file
.isFile()) {
379 resource
= targetExperiment
.getProject().getTracesFolder().getResource().getFile(targetName
);
380 } else if (file
.exists() && file
.isDirectory()) {
381 resource
= targetExperiment
.getProject().getTracesFolder().getResource().getFolder(targetName
);
383 if (resource
!= null && resource
.exists()) {
384 createLink(targetExperiment
.getResource(), resource
, resource
.getName());
392 * Drop a trace by importing/linking a path in a trace folder
394 * @param path the source path
395 * @param traceFolder the target trace folder
396 * @param operation the drop operation (DND.DROP_COPY | DND.DROP_LINK)
397 * @return true if successful
399 private static boolean drop(Path path
,
400 TmfTraceFolder traceFolder
,
403 String targetName
= path
.lastSegment();
404 for (TmfTraceElement trace
: traceFolder
.getTraces()) {
405 if (trace
.getName().equals(targetName
)) {
406 targetName
= promptRename(trace
);
407 if (targetName
== null) {
413 if (operation
== DND
.DROP_COPY
) {
414 importTrace(traceFolder
.getResource(), path
, targetName
);
416 createLink(traceFolder
.getResource(), path
, targetName
);
422 * Import a trace to the trace folder
424 * @param folder the trace folder resource
425 * @param path the path to the trace to import
426 * @param targetName the target name
428 private static void importTrace(final IFolder folder
, final Path path
, final String targetName
) {
429 final File source
= new File(path
.toString());
430 if (source
.isDirectory()) {
431 IPath containerPath
= folder
.getFullPath().addTrailingSeparator().append(targetName
);
432 IOverwriteQuery overwriteImplementor
= new IOverwriteQuery() {
434 public String
queryOverwrite(String pathString
) {
435 return IOverwriteQuery
.NO_ALL
;
438 List
<File
> filesToImport
= Arrays
.asList(source
.listFiles());
439 ImportOperation operation
= new ImportOperation(
442 FileSystemStructureProvider
.INSTANCE
,
443 overwriteImplementor
,
445 operation
.setCreateContainerStructure(false);
447 operation
.run(new NullProgressMonitor());
448 } catch (InvocationTargetException e
) {
450 } catch (InterruptedException e
) {
454 IRunnableWithProgress runnable
= new IRunnableWithProgress() {
456 public void run(IProgressMonitor monitor
) throws InvocationTargetException
, InterruptedException
{
458 InputStream inputStream
= new FileInputStream(source
);
459 IFile targetFile
= folder
.getFile(targetName
);
460 targetFile
.create(inputStream
, IResource
.NONE
, monitor
);
461 } catch (CoreException e
) {
463 } catch (FileNotFoundException e
) {
468 WorkspaceModifyDelegatingOperation operation
= new WorkspaceModifyDelegatingOperation(runnable
);
470 operation
.run(new NullProgressMonitor());
471 } catch (InvocationTargetException e
) {
473 } catch (InterruptedException e
) {
480 * Create a link to the actual trace and set the trace type
482 * @param parentFolder the parent folder
483 * @param resource the resource
484 * @param targetName the target name
486 private static void createLink(IFolder parentFolder
, IResource resource
, String targetName
) {
487 IPath location
= resource
.getLocation();
488 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
490 Map
<QualifiedName
, String
> properties
= resource
.getPersistentProperties();
491 String bundleName
= properties
.get(TmfCommonConstants
.TRACEBUNDLE
);
492 String traceType
= properties
.get(TmfCommonConstants
.TRACETYPE
);
493 String iconUrl
= properties
.get(TmfCommonConstants
.TRACEICON
);
494 String supplFolder
= properties
.get(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
496 if (resource
instanceof IFolder
) {
497 IFolder folder
= parentFolder
.getFolder(targetName
);
498 if (workspace
.validateLinkLocation(folder
, location
).isOK()) {
499 folder
.createLink(location
, IResource
.REPLACE
, null);
500 setProperties(folder
, bundleName
, traceType
, iconUrl
, supplFolder
);
503 Activator
.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$
506 IFile file
= parentFolder
.getFile(targetName
);
507 if (workspace
.validateLinkLocation(file
, location
).isOK()) {
508 file
.createLink(location
, IResource
.REPLACE
, null);
509 setProperties(file
, bundleName
, traceType
, iconUrl
, supplFolder
);
511 Activator
.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$
514 } catch (CoreException e
) {
520 * Create a link to a file or folder
522 * @param parentFolder the parent folder
523 * @param source the file or folder
524 * @param targetName the target name
526 private static void createLink(IFolder parentFolder
, IPath location
, String targetName
) {
527 File source
= new File(location
.toString());
528 IWorkspace workspace
= ResourcesPlugin
.getWorkspace();
531 if (source
.isDirectory()) {
532 IFolder folder
= parentFolder
.getFolder(targetName
);
533 if (workspace
.validateLinkLocation(folder
, location
).isOK()) {
534 folder
.createLink(location
, IResource
.REPLACE
, null);
536 Activator
.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$
539 IFile file
= parentFolder
.getFile(targetName
);
540 if (workspace
.validateLinkLocation(file
, location
).isOK()) {
541 file
.createLink(location
, IResource
.REPLACE
, null);
543 Activator
.getDefault().logError("Invalid Trace Location"); //$NON-NLS-1$
546 } catch (CoreException e
) {
552 * Prompts the user to rename a trace
554 * @param trace the existing trace
555 * @return the new name to use or null if rename is canceled
557 private static String
promptRename(TmfTraceElement trace
) {
558 MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT
.ICON_QUESTION
| SWT
.CANCEL
| SWT
.OK
);
559 mb
.setText(Messages
.DropAdapterAssistant_RenameTraceTitle
);
560 mb
.setMessage(NLS
.bind(Messages
.DropAdapterAssistant_RenameTraceMessage
, trace
.getName()));
561 if (mb
.open() != SWT
.OK
) {
564 IFolder folder
= trace
.getProject().getTracesFolder().getResource();
567 String name
= trace
.getName() + '-' + Integer
.toString(i
++);
568 IResource resource
= folder
.findMember(name
);
569 if (resource
== null) {
576 * Cleanup bookmarks file in copied trace
578 private static void cleanupBookmarks(IPath path
) {
579 IFolder folder
= ResourcesPlugin
.getWorkspace().getRoot().getFolder(path
);
580 if (folder
.exists()) {
582 for (IResource member
: folder
.members()) {
583 if (TmfTrace
.class.getCanonicalName().equals(member
.getPersistentProperty(TmfCommonConstants
.TRACETYPE
))) {
584 member
.delete(true, null);
587 } catch (CoreException e
) {
594 * Set the trace persistent properties
596 * @param resource the trace resource
597 * @param bundleName the bundle name
598 * @param traceType the trace type
599 * @param iconUrl the icon URL
600 * @param supplFolder the directory of the directory for supplementary information or null to ignore the property
601 * @throws CoreException
603 private static void setProperties(IResource resource
, String bundleName
,
604 String traceType
, String iconUrl
, String supplFolder
)
605 throws CoreException
{
606 resource
.setPersistentProperty(TmfCommonConstants
.TRACEBUNDLE
, bundleName
);
607 resource
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, traceType
);
608 resource
.setPersistentProperty(TmfCommonConstants
.TRACEICON
, iconUrl
);
609 resource
.setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, supplFolder
);
613 * Display an exception in a message box
615 * @param e the exception
617 private static void displayException(Exception e
) {
618 MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
619 mb
.setText(e
.getClass().getName());
620 mb
.setMessage(e
.getMessage());