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
.navigator
.CommonDropAdapter
;
29 import org
.eclipse
.ui
.navigator
.CommonDropAdapterAssistant
;
31 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
36 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
37 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.MoveTaxonOperation
;
39 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
40 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
41 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
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
) {
69 if (target
instanceof ITaxonTreeNode
) {
70 Set
<TaxonNode
> taxonNodes
= getSelectedTaxa();
71 ITaxonTreeNode targetTreeNode
= (ITaxonTreeNode
) target
;
72 if (targetTreeNode
instanceof Classification
){
73 targetTreeNode
= ((Classification
)targetTreeNode
).getRootNode();
74 targetTreeNode
= HibernateProxyHelper
.deproxy(targetTreeNode
, TaxonNode
.class);
76 if(taxonNodes
!= null) {
77 if (taxonNodes
.size() == 1){
78 return moveTaxon(taxonNodes
.iterator().next(), targetTreeNode
);
80 if( MessageDialog
.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
87 return Status
.CANCEL_STATUS
;
90 private Set
<TaxonNode
> getSelectedTaxa(){
91 HashSet
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
93 ISelection selection
= LocalSelectionTransfer
.getTransfer().getSelection();
94 if (selection
instanceof TreeSelection
) {
96 Iterator selectionIterator
= ((TreeSelection
) selection
).iterator();
98 while (selectionIterator
.hasNext()){
99 Object object
= selectionIterator
.next();
100 if(object
instanceof TaxonNode
){
101 TaxonNode taxonNode
= (TaxonNode
) object
;
102 taxonNodes
.add(taxonNode
);
106 return taxonNodes
.size() > 0 ? taxonNodes
: null;
110 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
114 public IStatus
validateDrop(Object target
, int operation
,
115 TransferData transferType
) {
116 if (target
instanceof ITaxonTreeNode
) {
118 // check users permissions with target taxonnode and taxon
119 if (target
instanceof TaxonNode
) {
120 TaxonNode targetNode
= (TaxonNode
)target
;
121 Boolean hasTargetNodePermission
= CdmStore
.currentAuthentiationHasPermission(targetNode
, UPDATE
);
122 Boolean hasTargetTaxonPermission
= CdmStore
.currentAuthentiationHasPermission(targetNode
.getTaxon(), UPDATE
);
124 if(logger
.isDebugEnabled()){
125 logger
.debug("target: " + targetNode
.getTaxon().getTitleCache());
128 if(!hasTargetNodePermission
|| ! hasTargetNodePermission
){
129 if(logger
.isDebugEnabled()){
130 logger
.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission
.toString() + " " + hasTargetTaxonPermission
.toString() + " ");
132 return Status
.CANCEL_STATUS
;
136 // do not allow to drop onto itself and
137 // check users permissions with all selected taxon nodes and taxa
138 for(TaxonNode taxonNode
: getSelectedTaxa()){
139 logger
.debug("selectedTaxa: " + taxonNode
.getTaxon().getTitleCache());
140 Boolean isSameTaxonNode
= taxonNode
.equals(target
);
141 Boolean hasTaxonNodePermission
= CdmStore
.currentAuthentiationHasPermission(taxonNode
, UPDATE
);
142 Boolean hasTaxonPermission
= CdmStore
.currentAuthentiationHasPermission(taxonNode
.getTaxon(), UPDATE
);
145 || !hasTaxonNodePermission
146 || !hasTaxonPermission
148 if(logger
.isDebugEnabled()){
149 logger
.debug("CANCEL_STATUS for selected " + isSameTaxonNode
.toString() + " " + hasTaxonNodePermission
.toString() + " " + hasTaxonPermission
.toString() + " ");
151 return Status
.CANCEL_STATUS
;
154 logger
.debug("OK_STATUS");
155 return Status
.OK_STATUS
;
157 logger
.debug("CANCEL_STATUS");
158 return Status
.CANCEL_STATUS
;
163 * @param childTaxonNode
167 private IStatus
moveTaxon(TaxonNode taxonNode
, ITaxonTreeNode targetITaxonTreeNode
) {
169 TaxonNavigator taxonNavigator
;
170 taxonNavigator
= (TaxonNavigator
) NavigationUtil
.showView(TaxonNavigator
.ID
);
172 if(targetITaxonTreeNode
instanceof TaxonNode
){
174 TaxonNode targetTaxonNode
= (TaxonNode
) targetITaxonTreeNode
;
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 if (!PreferencesUtil
.getSortNodesNaturally()){
184 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
185 if (workspaceUndoContext
== null) {
186 logger
.error("Workspace undo context is null. DND operation cancelled");
187 return Status
.CANCEL_STATUS
;
190 AbstractPostOperation operation
= new MoveTaxonOperation
191 ("Move Taxon", workspaceUndoContext
, taxonNode
, targetITaxonTreeNode
, this, taxonNavigator
, true);
192 NavigationUtil
.executeOperation(operation
);
194 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
);
195 return Status
.OK_STATUS
;
197 String
[] buttonLables
= {"Parent", "Predecessor", "Cancel"};
198 MessageDialog dialog
= new MessageDialog(null, "Target node", null, "Do you want to use the target node as parent or do you want to move the taxon below the target.", MessageDialog
.QUESTION_WITH_CANCEL
, buttonLables
, 0);
200 int returnCode
= dialog
.getReturnCode();
201 if (returnCode
== 0){
202 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
203 if (workspaceUndoContext
== null) {
204 logger
.error("Workspace undo context is null. DND operation cancelled");
205 return Status
.CANCEL_STATUS
;
208 AbstractPostOperation operation
= new MoveTaxonOperation
209 ("Move Taxon", workspaceUndoContext
, taxonNode
, targetITaxonTreeNode
, this, taxonNavigator
, true);
210 NavigationUtil
.executeOperation(operation
);
212 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
);
213 return Status
.OK_STATUS
;
214 }else if (returnCode
== 1){
215 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
216 if (workspaceUndoContext
== null) {
217 logger
.error("Workspace undo context is null. DND operation cancelled");
218 return Status
.CANCEL_STATUS
;
221 AbstractPostOperation operation
= new MoveTaxonOperation
222 ("Move Taxon", workspaceUndoContext
, taxonNode
, targetITaxonTreeNode
, this, taxonNavigator
, false);
223 NavigationUtil
.executeOperation(operation
);
225 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
);
226 return Status
.OK_STATUS
;
228 return Status
.CANCEL_STATUS
;
237 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
241 public boolean postOperation(CdmBase objectAffectedByOperation
) {
251 public boolean onComplete() {
252 // TODO Auto-generated method stub