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
.AbstractOperation
;
20 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
21 import org
.eclipse
.core
.runtime
.IStatus
;
22 import org
.eclipse
.core
.runtime
.Status
;
23 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
24 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
25 import org
.eclipse
.jface
.viewers
.ISelection
;
26 import org
.eclipse
.jface
.viewers
.TreeSelection
;
27 import org
.eclipse
.swt
.dnd
.DropTargetEvent
;
28 import org
.eclipse
.swt
.dnd
.TransferData
;
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
.navigation
.navigator
.operation
.RemotingMoveTaxonOperation
;
41 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
42 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
43 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
44 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
47 * <p>TaxonNodeDropAdapterAssistant class.</p>
53 public class TreeNodeDropAdapterAssistant
extends CommonDropAdapterAssistant
implements IPostOperationEnabled
{
55 private static final Logger logger
= Logger
.getLogger(TreeNodeDropAdapterAssistant
.class);
57 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
58 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
60 private static final EnumSet
<CRUD
> UPDATE
= EnumSet
.of(CRUD
.UPDATE
);
63 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
67 public IStatus
handleDrop(CommonDropAdapter dropAdapter
,
68 DropTargetEvent dropTargetEvent
, Object target
) {
71 if (target
instanceof ITaxonTreeNode
) {
72 Set
<TaxonNode
> taxonNodes
= getSelectedTaxa();
73 ITaxonTreeNode targetTreeNode
= (ITaxonTreeNode
) target
;
74 if (targetTreeNode
instanceof Classification
){
75 targetTreeNode
= ((Classification
)targetTreeNode
).getRootNode();
76 targetTreeNode
= HibernateProxyHelper
.deproxy(targetTreeNode
, TaxonNode
.class);
78 if(taxonNodes
!= null) {
79 if (taxonNodes
.size() == 1){
80 return moveTaxon(taxonNodes
.iterator().next(), targetTreeNode
);
82 if( MessageDialog
.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
89 return Status
.CANCEL_STATUS
;
92 private Set
<TaxonNode
> getSelectedTaxa(){
93 HashSet
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
95 ISelection selection
= LocalSelectionTransfer
.getTransfer().getSelection();
96 if (selection
instanceof TreeSelection
) {
98 Iterator selectionIterator
= ((TreeSelection
) selection
).iterator();
100 while (selectionIterator
.hasNext()){
101 Object object
= selectionIterator
.next();
102 if(object
instanceof TaxonNode
){
103 TaxonNode taxonNode
= (TaxonNode
) object
;
104 taxonNodes
.add(taxonNode
);
108 return taxonNodes
.size() > 0 ? taxonNodes
: null;
112 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
116 public IStatus
validateDrop(Object target
, int operation
,
117 TransferData transferType
) {
118 if (target
instanceof ITaxonTreeNode
) {
120 // check users permissions with target taxonnode and taxon
121 if (target
instanceof TaxonNode
) {
122 TaxonNode targetNode
= (TaxonNode
)target
;
123 Boolean hasTargetNodePermission
= CdmStore
.currentAuthentiationHasPermission(targetNode
, UPDATE
);
124 Boolean hasTargetTaxonPermission
= CdmStore
.currentAuthentiationHasPermission(targetNode
.getTaxon(), UPDATE
);
126 if(logger
.isDebugEnabled()){
127 logger
.debug("target: " + targetNode
.getTaxon().getTitleCache());
130 if(!hasTargetNodePermission
|| ! hasTargetNodePermission
){
131 if(logger
.isDebugEnabled()){
132 logger
.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission
.toString() + " " + hasTargetTaxonPermission
.toString() + " ");
134 return Status
.CANCEL_STATUS
;
138 // do not allow to drop onto itself and
139 // check users permissions with all selected taxon nodes and taxa
140 for(TaxonNode taxonNode
: getSelectedTaxa()){
141 logger
.debug("selectedTaxa: " + taxonNode
.getTaxon().getTitleCache());
142 Boolean isSameTaxonNode
= taxonNode
.equals(target
);
143 Boolean hasTaxonNodePermission
= CdmStore
.currentAuthentiationHasPermission(taxonNode
, UPDATE
);
144 Boolean hasTaxonPermission
= CdmStore
.currentAuthentiationHasPermission(taxonNode
.getTaxon(), UPDATE
);
147 || !hasTaxonNodePermission
148 || !hasTaxonPermission
150 if(logger
.isDebugEnabled()){
151 logger
.debug("CANCEL_STATUS for selected " + isSameTaxonNode
.toString() + " " + hasTaxonNodePermission
.toString() + " " + hasTaxonPermission
.toString() + " ");
153 return Status
.CANCEL_STATUS
;
156 logger
.debug("OK_STATUS");
157 return Status
.OK_STATUS
;
159 logger
.debug("CANCEL_STATUS");
160 return Status
.CANCEL_STATUS
;
165 * @param childTaxonNode
169 private IStatus
moveTaxon(TaxonNode taxonNode
, ITaxonTreeNode targetITaxonTreeNode
) {
171 TaxonNavigator taxonNavigator
;
172 taxonNavigator
= (TaxonNavigator
) NavigationUtil
.showView(TaxonNavigator
.ID
);
174 if(targetITaxonTreeNode
instanceof TaxonNode
){
176 TaxonNode targetTaxonNode
= (TaxonNode
) targetITaxonTreeNode
;
177 // Make sure parent taxon does not have unsaved changes
178 if (NavigationUtil
.isDirty(targetTaxonNode
)){
179 MessageDialog
.openWarning(NavigationUtil
.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
180 "changes in the parent taxon. Pleas save first.");
181 return Status
.CANCEL_STATUS
;
185 if (!PreferencesUtil
.getSortNodesNaturally()){
186 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
187 if (workspaceUndoContext
== null) {
188 logger
.error("Workspace undo context is null. DND operation cancelled");
189 return Status
.CANCEL_STATUS
;
192 if(CdmStore
.getCurrentSessionManager().isRemoting()) {
193 AbstractOperation operation
= new RemotingMoveTaxonOperation(taxonNavigator
, false, taxonNode
, (TaxonNode
)targetITaxonTreeNode
, true);
194 NavigationUtil
.executeOperation(operation
, null);
196 AbstractPostOperation operation
= new MoveTaxonOperation
197 ("Move Taxon", workspaceUndoContext
, taxonNode
, targetITaxonTreeNode
, this, taxonNavigator
, true);
198 NavigationUtil
.executeOperation(operation
);
200 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
);
201 return Status
.OK_STATUS
;
203 String
[] buttonLables
= {"Parent", "Predecessor", "Cancel"};
204 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);
206 int returnCode
= dialog
.getReturnCode();
207 if (returnCode
== 0){
208 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
209 if (workspaceUndoContext
== null) {
210 logger
.error("Workspace undo context is null. DND operation cancelled");
211 return Status
.CANCEL_STATUS
;
215 if(CdmStore
.getCurrentSessionManager().isRemoting()) {
216 AbstractOperation operation
= new RemotingMoveTaxonOperation(taxonNavigator
, false, taxonNode
, (TaxonNode
)targetITaxonTreeNode
, true);
217 NavigationUtil
.executeOperation(operation
, null);
219 AbstractPostOperation operation
= new MoveTaxonOperation
220 ("Move Taxon", workspaceUndoContext
, taxonNode
, targetITaxonTreeNode
, this, taxonNavigator
, true);
221 NavigationUtil
.executeOperation(operation
);
224 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
);
225 return Status
.OK_STATUS
;
226 }else if (returnCode
== 1){
227 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
228 if (workspaceUndoContext
== null) {
229 logger
.error("Workspace undo context is null. DND operation cancelled");
230 return Status
.CANCEL_STATUS
;
233 if(CdmStore
.getCurrentSessionManager().isRemoting()) {
234 AbstractOperation operation
= new RemotingMoveTaxonOperation(taxonNavigator
, false, taxonNode
, (TaxonNode
)targetITaxonTreeNode
, true);
235 NavigationUtil
.executeOperation(operation
, null);
237 AbstractPostOperation operation
= new MoveTaxonOperation
238 ("Move Taxon", workspaceUndoContext
, taxonNode
, targetITaxonTreeNode
, this, taxonNavigator
, false);
239 NavigationUtil
.executeOperation(operation
);
241 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
);
242 return Status
.OK_STATUS
;
244 return Status
.CANCEL_STATUS
;
253 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
257 public boolean postOperation(CdmBase objectAffectedByOperation
) {
267 public boolean onComplete() {
268 // TODO Auto-generated method stub