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
.runtime
.IStatus
;
20 import org
.eclipse
.core
.runtime
.Status
;
21 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
22 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
23 import org
.eclipse
.jface
.viewers
.ISelection
;
24 import org
.eclipse
.jface
.viewers
.TreeSelection
;
25 import org
.eclipse
.swt
.dnd
.DropTargetEvent
;
26 import org
.eclipse
.swt
.dnd
.TransferData
;
27 import org
.eclipse
.ui
.navigator
.CommonDropAdapter
;
28 import org
.eclipse
.ui
.navigator
.CommonDropAdapterAssistant
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.ITreeNode
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
33 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
34 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.MoveTaxonOperation
;
35 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
36 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
39 * <p>TaxonNodeDropAdapterAssistant class.</p>
45 public class TreeNodeDropAdapterAssistant
extends CommonDropAdapterAssistant
implements IPostOperationEnabled
{
46 private static final Logger logger
= Logger
47 .getLogger(TreeNodeDropAdapterAssistant
.class);
49 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
50 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
53 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
57 public IStatus
handleDrop(CommonDropAdapter dropAdapter
,
58 DropTargetEvent dropTargetEvent
, Object target
) {
60 if (target
instanceof ITreeNode
) {
61 Set
<TaxonNode
> taxonNodes
= getSelectedTaxa();
62 ITreeNode targetTreeNode
= (ITreeNode
) target
;
63 if(taxonNodes
!= null)
64 return moveTaxon(taxonNodes
, targetTreeNode
);
67 return Status
.CANCEL_STATUS
;
70 private Set
<TaxonNode
> getSelectedTaxa(){
71 HashSet
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
73 ISelection selection
= LocalSelectionTransfer
.getTransfer().getSelection();
74 if (selection
instanceof TreeSelection
) {
76 Iterator selectionIterator
= ((TreeSelection
) selection
).iterator();
78 while (selectionIterator
.hasNext()){
79 Object object
= selectionIterator
.next();
80 if(object
instanceof TaxonNode
){
81 TaxonNode taxonNode
= (TaxonNode
) object
;
82 taxonNodes
.add(taxonNode
);
86 return taxonNodes
.size() > 0 ? taxonNodes
: null;
90 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
94 public IStatus
validateDrop(Object target
, int operation
,
95 TransferData transferType
) {
96 if (target
instanceof ITreeNode
) {
97 // do not allow to drop onto itself
98 for(TaxonNode taxonNode
: getSelectedTaxa()){
99 if (taxonNode
.equals(target
)) {
100 return Status
.CANCEL_STATUS
;
103 return Status
.OK_STATUS
;
105 return Status
.CANCEL_STATUS
;
110 * @param childTaxonNode
114 private IStatus
moveTaxon(Set
<TaxonNode
> taxonNodes
, ITreeNode targetITreeNode
) {
116 TaxonNavigator taxonNavigator
;
117 taxonNavigator
= (TaxonNavigator
) NavigationUtil
.showView(TaxonNavigator
.ID
);
119 if(targetITreeNode
instanceof TaxonNode
){
121 TaxonNode targetTaxonNode
= (TaxonNode
) targetITreeNode
;
123 // for(TaxonNode taxonNode : taxonNodes){
124 // if (taxonNode.equals(targetTaxonNode)) {
125 // return Status.CANCEL_STATUS;
129 // Make sure parent taxon does not have unsaved changes
130 if (NavigationUtil
.isDirty(targetTaxonNode
)){
131 MessageDialog
.openWarning(NavigationUtil
.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
132 "changes in the parent taxon. Pleas save first.");
133 return Status
.CANCEL_STATUS
;
137 // Make sure parentTaxon is not the drop target
138 // if (!childTaxonNode.isTopmostNode() && childTaxonNode.getParent().equals(targetTaxonNode)){
139 // return Status.CANCEL_STATUS;
142 // Make sure taxon is not being dropped onto itself
143 // if (childTaxonNode.equals(targetTaxonNode)) {
144 // return Status.CANCEL_STATUS;
151 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
152 if (workspaceUndoContext
== null) {
153 logger
.error("Workspace undo context is null. DND operation cancelled");
154 return Status
.CANCEL_STATUS
;
157 AbstractPostOperation operation
= new MoveTaxonOperation
158 ("Move Taxon", workspaceUndoContext
, taxonNodes
, targetITreeNode
, this, taxonNavigator
);
159 NavigationUtil
.executeOperation(operation
);
161 logger
.info("Moved taxa to new parent " + targetITreeNode
);
162 return Status
.OK_STATUS
;
166 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
169 public boolean postOperation(CdmBase objectAffectedByOperation
) {
178 public boolean onComplete() {
179 // TODO Auto-generated method stub