2 * Copyright (C) 2015 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
.e4
;
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
.IUndoContext
;
20 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
21 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
22 import org
.eclipse
.jface
.viewers
.ISelection
;
23 import org
.eclipse
.jface
.viewers
.TreeSelection
;
24 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
25 import org
.eclipse
.swt
.dnd
.DND
;
26 import org
.eclipse
.swt
.dnd
.DropTargetEvent
;
27 import org
.eclipse
.swt
.dnd
.TransferData
;
29 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonNodeService
;
30 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
32 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDto
;
33 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
34 import eu
.etaxonomy
.taxeditor
.navigation
.l10n
.Messages
;
35 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
36 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
42 public class TreeNodeDropAdapterE4
extends ViewerDropAdapter
{
44 protected static final String TREE_NODE_DROP_ADAPTER_UNSAVED_PARENT_MESSAGE
= Messages
.TreeNodeDropAdapter_UNSAVED_PARENT_MESSAGE
;
45 protected static final String TREE_NODE_DROP_ADAPTER_UNSAVED_PARENT
= Messages
.TreeNodeDropAdapter_UNSAVED_PARENT
;
46 protected static final String TREE_NODE_DROP_ADAPTER_MOVE_TAXON
= Messages
.TreeNodeDropAdapter_MOVE_TAXON
;
47 protected static final String TREE_NODE_DROP_ADAPTER_CANCEL
= Messages
.TreeNodeDropAdapter_CANCEL
;
48 protected static final String TREE_NODE_DROP_ADAPTER_BEHIND
= Messages
.TreeNodeDropAdapter_BEHIND
;
49 protected static final String TREE_NODE_DROP_ADAPTER_CHILD
= Messages
.TreeNodeDropAdapter_CHILD
;
50 protected static final String DO_YOU_WANT_TO_MOVE_THE_TAXONNODE_AS_CHILD_OR_BEHIND_THE_TARGET_NODE
= Messages
.TreeNodeDropAdapter_MOVE_BEHIND
;
51 protected static final String TARGET_NODE
= Messages
.TreeNodeDropAdapter_TARGET_NODE
;
53 private static final Logger logger
= Logger
.getLogger(TreeNodeDropAdapterE4
.class);
55 private TaxonNavigatorE4 taxonNavigator
;
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
);
62 public enum MovingType
{
63 CHILD
, PREVIOUS
, BEHIND
66 protected TreeNodeDropAdapterE4(TaxonNavigatorE4 navigator
) {
67 super(navigator
.getViewer());
68 this.taxonNavigator
= navigator
;
73 public boolean performDrop(Object data
) {
74 Object target
= getCurrentTarget();
75 if (getCurrentTarget() instanceof TaxonNodeDto
) {
76 Set
<TaxonNodeDto
> taxonNodes
= getSelectedTaxa();
77 TaxonNodeDto targetTreeNode
= (TaxonNodeDto
) target
;
78 // if (targetTreeNode.getType() != null && targetTreeNode.getType().equals(Classification.class)){
79 // targetTreeNode = ((Classification)targetTreeNode).getRootNode();
80 // targetTreeNode = HibernateProxyHelper.deproxy(targetTreeNode, TaxonNode.class);
82 if(taxonNodes
!= null) {
83 if (taxonNodes
.size() == 1){
84 boolean success
= moveTaxon(taxonNodes
, targetTreeNode
);
86 taxonNavigator
.refresh();
90 if( MessageDialog
.openConfirm(null, Messages
.TreeNodeDropAdapter_MOVING
, Messages
.TreeNodeDropAdapter_MOVING_MESSAGE
)){
99 private Set
<TaxonNodeDto
> getSelectedTaxa(){
100 HashSet
<TaxonNodeDto
> taxonNodes
= new HashSet();
102 ISelection selection
= LocalSelectionTransfer
.getTransfer().getSelection();
103 if (selection
instanceof TreeSelection
) {
105 Iterator
<?
> selectionIterator
= ((TreeSelection
) selection
).iterator();
107 while (selectionIterator
.hasNext()){
108 Object object
= selectionIterator
.next();
109 if(object
instanceof TaxonNodeDto
){
110 TaxonNodeDto taxonNode
= (TaxonNodeDto
) object
;
111 taxonNodes
.add(taxonNode
);
120 public boolean validateDrop(Object target
, int operation
,
121 TransferData transferType
) {
123 if (target
instanceof TaxonNodeDto
) {
125 // check users permissions with target taxonnode and taxon
126 if (target
instanceof TaxonNodeDto
) {
127 TaxonNodeDto targetNode
= (TaxonNodeDto
)target
;
128 TaxonNode node
= CdmStore
.getService(ITaxonNodeService
.class).load(targetNode
.getUuid());
129 boolean hasTargetNodePermission
= CdmStore
.currentAuthentiationHasPermission(node
, UPDATE
);
130 boolean hasTargetTaxonPermission
= node
.getTaxon() == null ?
132 CdmStore
.currentAuthentiationHasPermission(node
.getTaxon(), UPDATE
);
134 if(logger
.isDebugEnabled()){
135 logger
.debug("target: " + node
.getTaxon() == null?
"-" : node
.getTaxon().getTitleCache()); //$NON-NLS-1$
138 if(!hasTargetNodePermission
|| ! hasTargetNodePermission
){
139 if(logger
.isDebugEnabled()){
140 logger
.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission
+ " " + hasTargetTaxonPermission
+ " "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
146 // do not allow to drop onto itself and
147 // check users permissions with all selected taxon nodes and taxa
148 for(TaxonNodeDto taxonNode
: getSelectedTaxa()){
149 if (logger
.isDebugEnabled()){
150 logger
.debug("selectedTaxa: " + taxonNode
.getTaxonUuid() == null?
"-" : taxonNode
.getTitleCache()); //$NON-NLS-1$
152 boolean isClassification
= taxonNode
.getTaxonUuid()== null;
153 if (isClassification
) {
154 if(logger
.isDebugEnabled()){
155 logger
.debug("CANCEL_STATUS for selected classification " + taxonNode
.getClassificationUUID()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
159 boolean isSameTaxonNode
= taxonNode
.equals(target
);
160 TaxonNode node
= CdmStore
.getService(ITaxonNodeService
.class).load(taxonNode
.getUuid());
161 boolean hasTaxonNodePermission
= CdmStore
.currentAuthentiationHasPermission(node
, UPDATE
);
163 boolean hasTaxonPermission
=
164 CdmStore
.currentAuthentiationHasPermission(node
.getTaxon(), UPDATE
);CdmStore
.currentAuthentiationHasPermission(node
.getTaxon(), UPDATE
);
167 || !hasTaxonNodePermission
168 || !hasTaxonPermission
170 if(logger
.isDebugEnabled()){
171 logger
.debug("CANCEL_STATUS for selected " + isSameTaxonNode
+ Messages
.TreeNodeDropAdapter_10
+ hasTaxonNodePermission
+ " " + hasTaxonPermission
+ " "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
176 logger
.debug("OK_STATUS"); //$NON-NLS-1$
179 logger
.debug("CANCEL_STATUS"); //$NON-NLS-1$
184 private boolean moveTaxon(Set
<TaxonNodeDto
> taxonNodes
, TaxonNodeDto targetITaxonTreeNode
) {
185 Iterator
<TaxonNodeDto
> taxIterator
= taxonNodes
.iterator();
186 Set
<UUID
> uuids
= new HashSet
<UUID
>();
187 TaxonNodeDto node
= null;
188 while(taxIterator
.hasNext()){
189 node
= taxIterator
.next();
190 uuids
.add(node
.getUuid());
192 IUndoContext workspaceUndoContext
= taxonNavigator
.getUndoContext();
193 if (!PreferencesUtil
.getSortNodesNaturally()){
194 if (workspaceUndoContext
== null) {
195 logger
.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
198 result
=CdmStore
.getService(ITaxonNodeService
.class).moveTaxonNodes(uuids
,targetITaxonTreeNode
.getUuid(), 0);
200 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
); //$NON-NLS-1$
203 String
[] buttonLables
= {TREE_NODE_DROP_ADAPTER_CHILD
, TREE_NODE_DROP_ADAPTER_BEHIND
,TREE_NODE_DROP_ADAPTER_CANCEL
};
204 MessageDialog dialog
= new MessageDialog(null, TARGET_NODE
, null, DO_YOU_WANT_TO_MOVE_THE_TAXONNODE_AS_CHILD_OR_BEHIND_THE_TARGET_NODE
, MessageDialog
.QUESTION_WITH_CANCEL
, buttonLables
, 0);
206 int returnCode
= dialog
.getReturnCode();
208 if (returnCode
== 0){
209 if (workspaceUndoContext
== null) {
210 logger
.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
214 result
= CdmStore
.getService(ITaxonNodeService
.class).moveTaxonNodes(uuids
,targetITaxonTreeNode
.getUuid(), 0);
216 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
); //$NON-NLS-1$
218 }else if (returnCode
== 1){
219 if (workspaceUndoContext
== null) {
220 logger
.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
225 CdmStore
.getService(ITaxonNodeService
.class).moveTaxonNodes(uuids
,targetITaxonTreeNode
.getUuid(), 2);
226 logger
.info("Moved taxa to new parent " + targetITaxonTreeNode
); //$NON-NLS-1$
229 // } else if (returnCode == 2){
230 // IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
231 // if (workspaceUndoContext == null) {
232 // logger.error("Workspace undo context is null. DND operation cancelled");
235 // TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
237 // AbstractPostOperation operation = new MoveTaxonOperation
238 // ("Move Taxon", workspaceUndoContext, uuids, targetNode, this, taxonNavigator, MovingType.BEHIND);
239 // NavigationUtil.executeOperation(operation);
241 // logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
253 public void dragOver(DropTargetEvent event
) {
254 super.dragOver(event
);
255 event
.feedback
= DND
.FEEDBACK_SELECT
| DND
.FEEDBACK_INSERT_AFTER
;