3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.navigation
.navigator
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
19 import org
.eclipse
.core
.commands
.operations
.IUndoableOperation
;
20 import org
.eclipse
.core
.runtime
.IStatus
;
21 import org
.eclipse
.core
.runtime
.Status
;
22 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
23 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
24 import org
.eclipse
.jface
.viewers
.ISelection
;
25 import org
.eclipse
.jface
.viewers
.TreeSelection
;
26 import org
.eclipse
.swt
.dnd
.DropTargetEvent
;
27 import org
.eclipse
.swt
.dnd
.TransferData
;
28 import org
.eclipse
.ui
.PartInitException
;
29 import org
.eclipse
.ui
.navigator
.CommonDropAdapter
;
30 import org
.eclipse
.ui
.navigator
.CommonDropAdapterAssistant
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.ITreeNode
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
35 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
36 import eu
.etaxonomy
.taxeditor
.operations
.IPostOperationEnabled
;
37 import eu
.etaxonomy
.taxeditor
.operations
.MoveTaxonOperation
;
44 public class TaxonDropAdapterAssistant
extends CommonDropAdapterAssistant
implements IPostOperationEnabled
{
45 private static final Logger logger
= Logger
46 .getLogger(TaxonDropAdapterAssistant
.class);
48 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
51 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
54 public IStatus
handleDrop(CommonDropAdapter dropAdapter
,
55 DropTargetEvent dropTargetEvent
, Object target
) {
57 if (target
instanceof ITreeNode
) {
58 Set
<TaxonNode
> taxonNodes
= getSelectedTaxa();
59 ITreeNode targetTreeNode
= (ITreeNode
) target
;
60 if(taxonNodes
!= null)
61 return moveTaxon(taxonNodes
, targetTreeNode
);
64 return Status
.CANCEL_STATUS
;
67 private Set
<TaxonNode
> getSelectedTaxa(){
68 HashSet
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
70 ISelection selection
= LocalSelectionTransfer
.getTransfer().getSelection();
71 if (selection
instanceof TreeSelection
) {
73 Iterator selectionIterator
= ((TreeSelection
) selection
).iterator();
75 while (selectionIterator
.hasNext()){
76 Object object
= selectionIterator
.next();
77 if(object
instanceof TaxonNode
){
78 TaxonNode taxonNode
= (TaxonNode
) object
;
79 taxonNodes
.add(taxonNode
);
83 return taxonNodes
.size() > 0 ? taxonNodes
: null;
87 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
90 public IStatus
validateDrop(Object target
, int operation
,
91 TransferData transferType
) {
92 if (target
instanceof ITreeNode
) {
93 // do not allow to drop onto itself
94 for(TaxonNode taxonNode
: getSelectedTaxa()){
95 if (taxonNode
.equals(target
)) {
96 return Status
.CANCEL_STATUS
;
99 return Status
.OK_STATUS
;
101 return Status
.CANCEL_STATUS
;
106 * @param childTaxonNode
110 private IStatus
moveTaxon(Set
<TaxonNode
> taxonNodes
, ITreeNode targetITreeNode
) {
112 TaxonNavigator taxonNavigator
;
113 taxonNavigator
= (TaxonNavigator
) NavigationUtil
.showView(TaxonNavigator
.ID
);
115 if(targetITreeNode
instanceof TaxonNode
){
117 TaxonNode targetTaxonNode
= (TaxonNode
) targetITreeNode
;
119 // for(TaxonNode taxonNode : taxonNodes){
120 // if (taxonNode.equals(targetTaxonNode)) {
121 // return Status.CANCEL_STATUS;
125 // Make sure parent taxon does not have unsaved changes
126 if (NavigationUtil
.isDirty(targetTaxonNode
)){
127 MessageDialog
.openWarning(NavigationUtil
.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
128 "changes in the parent taxon. Pleas save first.");
129 return Status
.CANCEL_STATUS
;
133 // Make sure parentTaxon is not the drop target
134 // if (!childTaxonNode.isTopmostNode() && childTaxonNode.getParent().equals(targetTaxonNode)){
135 // return Status.CANCEL_STATUS;
138 // Make sure taxon is not being dropped onto itself
139 // if (childTaxonNode.equals(targetTaxonNode)) {
140 // return Status.CANCEL_STATUS;
147 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
148 if (workspaceUndoContext
== null) {
149 logger
.error("Workspace undo context is null. DND operation cancelled");
150 return Status
.CANCEL_STATUS
;
153 IUndoableOperation operation
= new MoveTaxonOperation
154 ("Move Taxon", workspaceUndoContext
, taxonNodes
, targetITreeNode
, this, taxonNavigator
);
155 NavigationUtil
.executeOperation(operation
);
157 logger
.info("Moved taxa to new parent " + targetITreeNode
);
158 return Status
.OK_STATUS
;
162 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
164 public boolean postOperation(CdmBase objectAffectedByOperation
) {