2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.navigation
.navigator
;
12 import java
.util
.EnumSet
;
13 import java
.util
.HashSet
;
14 import java
.util
.Iterator
;
16 import java
.util
.UUID
;
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
.TreeNodeDropAdapter
.MovingType
;
40 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.MoveTaxonOperation
;
41 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.operation
.RemotingMoveTaxonOperation
;
42 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
43 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
44 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
45 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
48 * <p>TaxonNodeDropAdapterAssistant class.</p>
54 public class TreeNodeDropAdapterAssistant
extends CommonDropAdapterAssistant
implements IPostOperationEnabled
{
56 private static final Logger logger
= Logger
.getLogger(TreeNodeDropAdapterAssistant
.class);
58 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
59 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
61 private static final EnumSet
<CRUD
> UPDATE
= EnumSet
.of(CRUD
.UPDATE
);
64 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
68 public IStatus
handleDrop(CommonDropAdapter dropAdapter
,
69 DropTargetEvent dropTargetEvent
, Object target
) {
72 if (target
instanceof ITaxonTreeNode
) {
73 Set
<TaxonNode
> taxonNodes
= getSelectedTaxa();
74 ITaxonTreeNode targetTreeNode
= (ITaxonTreeNode
) target
;
75 if (targetTreeNode
instanceof Classification
){
76 targetTreeNode
= ((Classification
)targetTreeNode
).getRootNode();
77 targetTreeNode
= HibernateProxyHelper
.deproxy(targetTreeNode
, TaxonNode
.class);
79 //if(taxonNodes != null) {
80 if (taxonNodes
.size() >= 1){
81 return moveTaxon(taxonNodes
, targetTreeNode
);
83 if( MessageDialog.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
90 return Status
.CANCEL_STATUS
;
93 private Set
<TaxonNode
> getSelectedTaxa(){
94 HashSet
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
96 ISelection selection
= LocalSelectionTransfer
.getTransfer().getSelection();
97 if (selection
instanceof TreeSelection
) {
99 Iterator selectionIterator
= ((TreeSelection
) selection
).iterator();
101 while (selectionIterator
.hasNext()){
102 Object object
= selectionIterator
.next();
103 if(object
instanceof TaxonNode
){
104 TaxonNode taxonNode
= (TaxonNode
) object
;
105 taxonNodes
.add(taxonNode
);
109 return taxonNodes
.size() > 0 ? taxonNodes
: null;
113 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
117 public IStatus
validateDrop(Object target
, int operation
,
118 TransferData transferType
) {
119 if (target
instanceof ITaxonTreeNode
) {
121 // check users permissions with target taxonnode and taxon
122 if (target
instanceof TaxonNode
) {
123 TaxonNode targetNode
= (TaxonNode
)target
;
124 Boolean hasTargetNodePermission
= CdmStore
.currentAuthentiationHasPermission(targetNode
, UPDATE
);
125 Boolean hasTargetTaxonPermission
= CdmStore
.currentAuthentiationHasPermission(targetNode
.getTaxon(), UPDATE
);
127 if(logger
.isDebugEnabled()){
128 logger
.debug("target: " + targetNode
.getTaxon().getTitleCache());
131 if(!hasTargetNodePermission
|| ! hasTargetNodePermission
){
132 if(logger
.isDebugEnabled()){
133 logger
.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission
.toString() + " " + hasTargetTaxonPermission
.toString() + " ");
135 return Status
.CANCEL_STATUS
;
139 // do not allow to drop onto itself and
140 // check users permissions with all selected taxon nodes and taxa
141 for(TaxonNode taxonNode
: getSelectedTaxa()){
142 logger
.debug("selectedTaxa: " + taxonNode
.getTaxon().getTitleCache());
143 Boolean isSameTaxonNode
= taxonNode
.equals(target
);
144 Boolean hasTaxonNodePermission
= CdmStore
.currentAuthentiationHasPermission(taxonNode
, UPDATE
);
145 Boolean hasTaxonPermission
= CdmStore
.currentAuthentiationHasPermission(taxonNode
.getTaxon(), UPDATE
);
148 || !hasTaxonNodePermission
149 || !hasTaxonPermission
151 if(logger
.isDebugEnabled()){
152 logger
.debug("CANCEL_STATUS for selected " + isSameTaxonNode
.toString() + " " + hasTaxonNodePermission
.toString() + " " + hasTaxonPermission
.toString() + " ");
154 return Status
.CANCEL_STATUS
;
157 logger
.debug("OK_STATUS");
158 return Status
.OK_STATUS
;
160 logger
.debug("CANCEL_STATUS");
161 return Status
.CANCEL_STATUS
;
166 * @param childTaxonNode
170 private IStatus
moveTaxon(Set
<TaxonNode
> taxonNodes
, ITaxonTreeNode targetITaxonTreeNode
) {
172 TaxonNavigator taxonNavigator
;
173 taxonNavigator
= (TaxonNavigator
) NavigationUtil
.showView(TaxonNavigator
.ID
);
175 if(targetITaxonTreeNode
instanceof TaxonNode
){
177 TaxonNode targetTaxonNode
= (TaxonNode
) targetITaxonTreeNode
;
178 // Make sure parent taxon does not have unsaved changes
179 if (NavigationUtil
.isDirty(targetTaxonNode
)){
180 MessageDialog
.openWarning(NavigationUtil
.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
181 "changes in the parent taxon. Pleas save first.");
182 return Status
.CANCEL_STATUS
;
186 Iterator
<TaxonNode
> taxIterator
= taxonNodes
.iterator();
187 Set
<UUID
> uuids
= new HashSet
<UUID
>();
188 TaxonNode node
= null;
189 while(taxIterator
.hasNext()){
190 node
= taxIterator
.next();
191 uuids
.add(node
.getUuid());
193 if (!PreferencesUtil
.getSortNodesNaturally()){
194 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
195 if (workspaceUndoContext
== null) {
196 logger
.error("Workspace undo context is null. DND operation cancelled");
197 return Status
.CANCEL_STATUS
;
200 AbstractOperation operation
= new RemotingMoveTaxonOperation(taxonNavigator
, false, uuids
, (TaxonNode
)targetITaxonTreeNode
, MovingType
.CHILD
);
201 NavigationUtil
.executeOperation(operation
, null);
204 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
);
205 return Status
.OK_STATUS
;
207 String
[] buttonLables
= {"Child", "Behind", "Cancel"};
208 MessageDialog dialog
= new MessageDialog(null, "Target node", null, "Do you want to move the taxon as child, before or behind the target.", MessageDialog
.QUESTION_WITH_CANCEL
, buttonLables
, 0);
210 int returnCode
= dialog
.getReturnCode();
211 if (returnCode
== 0){
212 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
213 if (workspaceUndoContext
== null) {
214 logger
.error("Workspace undo context is null. DND operation cancelled");
215 return Status
.CANCEL_STATUS
;
218 AbstractOperation operation
= new RemotingMoveTaxonOperation(taxonNavigator
, false, uuids
, (TaxonNode
)targetITaxonTreeNode
, MovingType
.CHILD
);
219 NavigationUtil
.executeOperation(operation
, null);
222 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
);
223 return Status
.OK_STATUS
;
224 }else if (returnCode
== 1){
225 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
226 if (workspaceUndoContext
== null) {
227 logger
.error("Workspace undo context is null. DND operation cancelled");
228 return Status
.CANCEL_STATUS
;
230 TaxonNode targetNode
= (TaxonNode
) targetITaxonTreeNode
;
232 AbstractOperation operation
= new RemotingMoveTaxonOperation(taxonNavigator
, false, uuids
, targetNode
, MovingType
.BEHIND
);
233 NavigationUtil
.executeOperation(operation
, null);
235 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
);
236 return Status
.OK_STATUS
;
237 // }else if (returnCode == 2){
238 // IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
239 // if (workspaceUndoContext == null) {
240 // logger.error("Workspace undo context is null. DND operation cancelled");
241 // return Status.CANCEL_STATUS;
243 // TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
245 // if(CdmStore.getCurrentSessionManager().isRemoting()) {
246 // AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode, MovingType.PREVIOUS);
247 // NavigationUtil.executeOperation(operation, null);
249 // AbstractPostOperation operation = new MoveTaxonOperation
250 // ("Move Taxon", workspaceUndoContext, uuids, targetNode, this, taxonNavigator, MovingType.PREVIOUS);
251 // NavigationUtil.executeOperation(operation);
253 // logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
254 // return Status.OK_STATUS;
256 return Status
.CANCEL_STATUS
;
264 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
268 public boolean postOperation(CdmBase objectAffectedByOperation
) {
278 public boolean onComplete() {
279 // TODO Auto-generated method stub