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
.hibernate
.HibernateProxyHelper
;
33 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
37 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.MoveTaxonOperation
;
40 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
41 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
42 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
45 * <p>TaxonNodeDropAdapterAssistant class.</p>
51 public class TreeNodeDropAdapterAssistant
extends CommonDropAdapterAssistant
implements IPostOperationEnabled
{
53 private static final Logger logger
= Logger
.getLogger(TreeNodeDropAdapterAssistant
.class);
55 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
56 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
58 private static final EnumSet
<CRUD
> UPDATE
= EnumSet
.of(CRUD
.UPDATE
);
61 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
65 public IStatus
handleDrop(CommonDropAdapter dropAdapter
,
66 DropTargetEvent dropTargetEvent
, Object target
) {
68 if (target
instanceof ITaxonTreeNode
) {
69 Set
<TaxonNode
> taxonNodes
= getSelectedTaxa();
70 ITaxonTreeNode targetTreeNode
= (ITaxonTreeNode
) target
;
71 if (targetTreeNode
instanceof Classification
){
72 targetTreeNode
= ((Classification
)targetTreeNode
).getRootNode();
73 targetTreeNode
= HibernateProxyHelper
.deproxy(targetTreeNode
, TaxonNode
.class);
75 if(taxonNodes
!= null) {
76 if (taxonNodes
.size() == 1){
77 return moveTaxon(taxonNodes
.iterator().next(), targetTreeNode
);
79 if( MessageDialog
.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
86 return Status
.CANCEL_STATUS
;
89 private Set
<TaxonNode
> getSelectedTaxa(){
90 HashSet
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
92 ISelection selection
= LocalSelectionTransfer
.getTransfer().getSelection();
93 if (selection
instanceof TreeSelection
) {
95 Iterator selectionIterator
= ((TreeSelection
) selection
).iterator();
97 while (selectionIterator
.hasNext()){
98 Object object
= selectionIterator
.next();
99 if(object
instanceof TaxonNode
){
100 TaxonNode taxonNode
= (TaxonNode
) object
;
101 taxonNodes
.add(taxonNode
);
105 return taxonNodes
.size() > 0 ? taxonNodes
: null;
109 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
113 public IStatus
validateDrop(Object target
, int operation
,
114 TransferData transferType
) {
115 if (target
instanceof ITaxonTreeNode
) {
117 // check users permissions with target taxonnode and taxon
118 if (target
instanceof TaxonNode
) {
119 TaxonNode targetNode
= (TaxonNode
)target
;
120 Boolean hasTargetNodePermission
= CdmStore
.currentAuthentiationHasPermission(targetNode
, UPDATE
);
121 Boolean hasTargetTaxonPermission
= CdmStore
.currentAuthentiationHasPermission(targetNode
.getTaxon(), UPDATE
);
123 if(logger
.isDebugEnabled()){
124 logger
.debug("target: " + targetNode
.getTaxon().getTitleCache());
127 if(!hasTargetNodePermission
|| ! hasTargetNodePermission
){
128 if(logger
.isDebugEnabled()){
129 logger
.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission
.toString() + " " + hasTargetTaxonPermission
.toString() + " ");
131 return Status
.CANCEL_STATUS
;
135 // do not allow to drop onto itself and
136 // check users permissions with all selected taxon nodes and taxa
137 for(TaxonNode taxonNode
: getSelectedTaxa()){
138 logger
.debug("selectedTaxa: " + taxonNode
.getTaxon().getTitleCache());
139 Boolean isSameTaxonNode
= taxonNode
.equals(target
);
140 Boolean hasTaxonNodePermission
= CdmStore
.currentAuthentiationHasPermission(taxonNode
, UPDATE
);
141 Boolean hasTaxonPermission
= CdmStore
.currentAuthentiationHasPermission(taxonNode
.getTaxon(), UPDATE
);
144 || !hasTaxonNodePermission
145 || !hasTaxonPermission
147 if(logger
.isDebugEnabled()){
148 logger
.debug("CANCEL_STATUS for selected " + isSameTaxonNode
.toString() + " " + hasTaxonNodePermission
.toString() + " " + hasTaxonPermission
.toString() + " ");
150 return Status
.CANCEL_STATUS
;
153 logger
.debug("OK_STATUS");
154 return Status
.OK_STATUS
;
156 logger
.debug("CANCEL_STATUS");
157 return Status
.CANCEL_STATUS
;
162 * @param childTaxonNode
166 private IStatus
moveTaxon(TaxonNode taxonNode
, ITaxonTreeNode targetITaxonTreeNode
) {
168 TaxonNavigator taxonNavigator
;
169 taxonNavigator
= (TaxonNavigator
) NavigationUtil
.showView(TaxonNavigator
.ID
);
171 if(targetITaxonTreeNode
instanceof TaxonNode
){
173 TaxonNode targetTaxonNode
= (TaxonNode
) targetITaxonTreeNode
;
175 // for(TaxonNode taxonNode : taxonNodes){
176 // if (taxonNode.equals(targetTaxonNode)) {
177 // return Status.CANCEL_STATUS;
181 // Make sure parent taxon does not have unsaved changes
182 if (NavigationUtil
.isDirty(targetTaxonNode
)){
183 MessageDialog
.openWarning(NavigationUtil
.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
184 "changes in the parent taxon. Pleas save first.");
185 return Status
.CANCEL_STATUS
;
189 /*if (((TaxonNode) targetITaxonTreeNode).isTopmostNode()) {
190 MessageDialog.openConfirm(null, "Taxonnode can not be moved", "A taxonnode can not be moved to the classification");
193 // Make sure parentTaxon is not the drop target
194 // if (!childTaxonNode.isTopmostNode() && childTaxonNode.getParent().equals(targetTaxonNode)){
195 // return Status.CANCEL_STATUS;
198 // Make sure taxon is not being dropped onto itself
199 // if (childTaxonNode.equals(targetTaxonNode)) {
200 // return Status.CANCEL_STATUS;
207 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
208 if (workspaceUndoContext
== null) {
209 logger
.error("Workspace undo context is null. DND operation cancelled");
210 return Status
.CANCEL_STATUS
;
213 AbstractPostOperation operation
= new MoveTaxonOperation
214 ("Move Taxon", workspaceUndoContext
, taxonNode
, targetITaxonTreeNode
, this, taxonNavigator
);
215 NavigationUtil
.executeOperation(operation
);
217 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
);
218 return Status
.OK_STATUS
;
222 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
226 public boolean postOperation(CdmBase objectAffectedByOperation
) {
236 public boolean onComplete() {
237 // TODO Auto-generated method stub