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
.EnumSet
;
14 import java
.util
.HashSet
;
15 import java
.util
.Iterator
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
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
.handlers
.HandlerUtil
;
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
.ITaxonTreeNode
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
35 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
36 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
37 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.MoveTaxonOperation
;
38 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
39 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
40 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
43 * <p>TaxonNodeDropAdapterAssistant class.</p>
49 public class TreeNodeDropAdapterAssistant
extends CommonDropAdapterAssistant
implements IPostOperationEnabled
{
51 private static final Logger logger
= Logger
.getLogger(TreeNodeDropAdapterAssistant
.class);
53 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
54 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
56 private static final EnumSet
<CRUD
> UPDATE
= EnumSet
.of(CRUD
.UPDATE
);
59 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
63 public IStatus
handleDrop(CommonDropAdapter dropAdapter
,
64 DropTargetEvent dropTargetEvent
, Object target
) {
66 if (target
instanceof ITaxonTreeNode
) {
67 Set
<TaxonNode
> taxonNodes
= getSelectedTaxa();
68 ITaxonTreeNode targetTreeNode
= (ITaxonTreeNode
) target
;
69 if(taxonNodes
!= null) {
70 if (taxonNodes
.size() == 1){
71 return moveTaxon(taxonNodes
.iterator().next(), targetTreeNode
);
73 if( MessageDialog
.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
80 return Status
.CANCEL_STATUS
;
83 private Set
<TaxonNode
> getSelectedTaxa(){
84 HashSet
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
86 ISelection selection
= LocalSelectionTransfer
.getTransfer().getSelection();
87 if (selection
instanceof TreeSelection
) {
89 Iterator selectionIterator
= ((TreeSelection
) selection
).iterator();
91 while (selectionIterator
.hasNext()){
92 Object object
= selectionIterator
.next();
93 if(object
instanceof TaxonNode
){
94 TaxonNode taxonNode
= (TaxonNode
) object
;
95 taxonNodes
.add(taxonNode
);
99 return taxonNodes
.size() > 0 ? taxonNodes
: null;
103 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
107 public IStatus
validateDrop(Object target
, int operation
,
108 TransferData transferType
) {
109 if (target
instanceof ITaxonTreeNode
) {
111 // check users permissions with target taxonnode and taxon
112 if (target
instanceof TaxonNode
) {
113 TaxonNode targetNode
= (TaxonNode
)target
;
114 Boolean hasTargetNodePermission
= CdmStore
.currentAuthentiationHasPermission(targetNode
, UPDATE
);
115 Boolean hasTargetTaxonPermission
= CdmStore
.currentAuthentiationHasPermission(targetNode
.getTaxon(), UPDATE
);
117 if(logger
.isDebugEnabled()){
118 logger
.debug("target: " + targetNode
.getTaxon().getTitleCache());
121 if(!hasTargetNodePermission
|| ! hasTargetNodePermission
){
122 if(logger
.isDebugEnabled()){
123 logger
.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission
.toString() + " " + hasTargetTaxonPermission
.toString() + " ");
125 return Status
.CANCEL_STATUS
;
129 // do not allow to drop onto itself and
130 // check users permissions with all selected taxon nodes and taxa
131 for(TaxonNode taxonNode
: getSelectedTaxa()){
132 logger
.debug("selectedTaxa: " + taxonNode
.getTaxon().getTitleCache());
133 Boolean isSameTaxonNode
= taxonNode
.equals(target
);
134 Boolean hasTaxonNodePermission
= CdmStore
.currentAuthentiationHasPermission(taxonNode
, UPDATE
);
135 Boolean hasTaxonPermission
= CdmStore
.currentAuthentiationHasPermission(taxonNode
.getTaxon(), UPDATE
);
138 || !hasTaxonNodePermission
139 || !hasTaxonPermission
141 if(logger
.isDebugEnabled()){
142 logger
.debug("CANCEL_STATUS for selected " + isSameTaxonNode
.toString() + " " + hasTaxonNodePermission
.toString() + " " + hasTaxonPermission
.toString() + " ");
144 return Status
.CANCEL_STATUS
;
147 logger
.debug("OK_STATUS");
148 return Status
.OK_STATUS
;
150 logger
.debug("CANCEL_STATUS");
151 return Status
.CANCEL_STATUS
;
156 * @param childTaxonNode
160 private IStatus
moveTaxon(TaxonNode taxonNode
, ITaxonTreeNode targetITaxonTreeNode
) {
162 TaxonNavigator taxonNavigator
;
163 taxonNavigator
= (TaxonNavigator
) NavigationUtil
.showView(TaxonNavigator
.ID
);
165 if(targetITaxonTreeNode
instanceof TaxonNode
){
167 TaxonNode targetTaxonNode
= (TaxonNode
) targetITaxonTreeNode
;
169 // for(TaxonNode taxonNode : taxonNodes){
170 // if (taxonNode.equals(targetTaxonNode)) {
171 // return Status.CANCEL_STATUS;
175 // Make sure parent taxon does not have unsaved changes
176 if (NavigationUtil
.isDirty(targetTaxonNode
)){
177 MessageDialog
.openWarning(NavigationUtil
.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
178 "changes in the parent taxon. Pleas save first.");
179 return Status
.CANCEL_STATUS
;
183 // Make sure parentTaxon is not the drop target
184 // if (!childTaxonNode.isTopmostNode() && childTaxonNode.getParent().equals(targetTaxonNode)){
185 // return Status.CANCEL_STATUS;
188 // Make sure taxon is not being dropped onto itself
189 // if (childTaxonNode.equals(targetTaxonNode)) {
190 // return Status.CANCEL_STATUS;
197 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
198 if (workspaceUndoContext
== null) {
199 logger
.error("Workspace undo context is null. DND operation cancelled");
200 return Status
.CANCEL_STATUS
;
203 AbstractPostOperation operation
= new MoveTaxonOperation
204 ("Move Taxon", workspaceUndoContext
, taxonNode
, targetITaxonTreeNode
, this, taxonNavigator
);
205 NavigationUtil
.executeOperation(operation
);
207 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
);
208 return Status
.OK_STATUS
;
212 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
216 public boolean postOperation(CdmBase objectAffectedByOperation
) {
226 public boolean onComplete() {
227 // TODO Auto-generated method stub