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
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
21 import org
.eclipse
.jface
.viewers
.ISelection
;
22 import org
.eclipse
.jface
.viewers
.TreeSelection
;
23 import org
.eclipse
.swt
.dnd
.DropTargetEvent
;
24 import org
.eclipse
.swt
.dnd
.TransferData
;
25 import org
.eclipse
.ui
.navigator
.CommonDropAdapter
;
26 import org
.eclipse
.ui
.navigator
.CommonDropAdapterAssistant
;
28 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.ITaxonTreeNode
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
33 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
34 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
35 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
38 * <p>TaxonNodeDropAdapterAssistant class.</p>
43 public class TreeNodeDropAdapterAssistant
extends CommonDropAdapterAssistant
implements IPostOperationEnabled
{
45 private static final Logger logger
= Logger
.getLogger(TreeNodeDropAdapterAssistant
.class);
47 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
48 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
50 private static final EnumSet
<CRUD
> UPDATE
= EnumSet
.of(CRUD
.UPDATE
);
54 public IStatus
handleDrop(CommonDropAdapter dropAdapter
,
55 DropTargetEvent dropTargetEvent
, Object target
) {
58 if (target
instanceof ITaxonTreeNode
) {
59 Set
<TaxonNode
> taxonNodes
= getSelectedTaxa();
60 ITaxonTreeNode targetTreeNode
= (ITaxonTreeNode
) target
;
61 if (targetTreeNode
instanceof Classification
){
62 targetTreeNode
= ((Classification
)targetTreeNode
).getRootNode();
63 targetTreeNode
= HibernateProxyHelper
.deproxy(targetTreeNode
, TaxonNode
.class);
65 //if(taxonNodes != null) {
66 if (taxonNodes
.size() >= 1){
67 return moveTaxon(taxonNodes
, targetTreeNode
);
69 if( MessageDialog.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
76 return Status
.CANCEL_STATUS
;
79 private Set
<TaxonNode
> getSelectedTaxa(){
80 HashSet
<TaxonNode
> taxonNodes
= new HashSet
<TaxonNode
>();
82 ISelection selection
= LocalSelectionTransfer
.getTransfer().getSelection();
83 if (selection
instanceof TreeSelection
) {
85 Iterator
<?
> selectionIterator
= ((TreeSelection
) selection
).iterator();
87 while (selectionIterator
.hasNext()){
88 Object object
= selectionIterator
.next();
89 if(object
instanceof TaxonNode
){
90 TaxonNode taxonNode
= (TaxonNode
) object
;
91 taxonNodes
.add(taxonNode
);
95 return taxonNodes
.size() > 0 ? taxonNodes
: null;
100 public IStatus
validateDrop(Object target
, int operation
,
101 TransferData transferType
) {
102 if (target
instanceof ITaxonTreeNode
) {
104 // check users permissions with target taxonnode and taxon
105 if (target
instanceof TaxonNode
) {
106 TaxonNode targetNode
= (TaxonNode
)target
;
107 boolean hasTargetNodePermission
= CdmStore
.currentAuthentiationHasPermission(targetNode
, UPDATE
);
108 boolean hasTargetTaxonPermission
= targetNode
.getTaxon() == null ?
110 CdmStore
.currentAuthentiationHasPermission(targetNode
.getTaxon(), UPDATE
);
112 if(logger
.isDebugEnabled()){
113 logger
.debug("target: " + targetNode
.getTaxon() == null?
"-" : targetNode
.getTaxon().getTitleCache()); //$NON-NLS-1$
116 if(!hasTargetNodePermission
|| ! hasTargetNodePermission
){
117 if(logger
.isDebugEnabled()){
118 logger
.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission
+ " " + hasTargetTaxonPermission
+ " "); //$NON-NLS-1$
120 return Status
.CANCEL_STATUS
;
124 // do not allow to drop onto itself and
125 // check users permissions with all selected taxon nodes and taxa
126 for(TaxonNode taxonNode
: getSelectedTaxa()){
127 if (logger
.isDebugEnabled()){
128 logger
.debug("selectedTaxa: " + taxonNode
.getTaxon() == null?
"-" : taxonNode
.getTaxon().getTitleCache());
130 boolean isSameTaxonNode
= taxonNode
.equals(target
);
131 boolean hasTaxonNodePermission
= CdmStore
.currentAuthentiationHasPermission(taxonNode
, UPDATE
);
132 boolean hasTaxonPermission
= taxonNode
.getTaxon() == null ?
134 CdmStore
.currentAuthentiationHasPermission(taxonNode
.getTaxon(), UPDATE
);
137 || !hasTaxonNodePermission
138 || !hasTaxonPermission
140 if(logger
.isDebugEnabled()){
141 logger
.debug("CANCEL_STATUS for selected " + isSameTaxonNode
+ " " + hasTaxonNodePermission
+ " " + hasTaxonPermission
+ " "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
143 return Status
.CANCEL_STATUS
;
146 if (logger
.isDebugEnabled()){logger
.debug("OK_STATUS");} //$NON-NLS-1$
147 return Status
.OK_STATUS
;
149 if (logger
.isDebugEnabled()){logger
.debug("CANCEL_STATUS");} //$NON-NLS-1$
150 return Status
.CANCEL_STATUS
;
155 * @param childTaxonNode
159 private IStatus
moveTaxon(Set
<TaxonNode
> taxonNodes
, ITaxonTreeNode targetITaxonTreeNode
) {
161 // TaxonNavigator taxonNavigator;
162 // taxonNavigator = (TaxonNavigator) AbstractUtility.showView(TaxonNavigator.ID);
164 // if(targetITaxonTreeNode instanceof TaxonNode){
166 // TaxonNode targetTaxonNode = (TaxonNode) targetITaxonTreeNode;
167 // // Make sure parent taxon does not have unsaved changes
168 //// if (NavigationUtil.isDirty(targetTaxonNode)){
169 //// MessageDialog.openWarning(NavigationUtil.getShell(), TreeNodeDropAdapter.TREE_NODE_DROP_ADAPTER_UNSAVED_PARENT, TreeNodeDropAdapter.TREE_NODE_DROP_ADAPTER_UNSAVED_PARENT_MESSAGE);
170 //// return Status.CANCEL_STATUS;
174 // Iterator<TaxonNode> taxIterator = taxonNodes.iterator();
175 // Set<UUID> uuids = new HashSet<>();
176 // TaxonNode node = null;
177 // while(taxIterator.hasNext()){
178 // node = taxIterator.next();
179 // uuids.add(node.getUuid());
181 // if (!PreferencesUtil.getSortNodesNaturally()){
182 // IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
183 // if (workspaceUndoContext == null) {
184 // logger.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
185 // return Status.CANCEL_STATUS;
188 // AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
189 // AbstractUtility.executeOperation(operation, (RemotingCdmHandlerE4)null);
192 // logger.info("Moved taxa to new parent " + targetITaxonTreeNode); //$NON-NLS-1$
193 // return Status.OK_STATUS;
195 // String[] buttonLables = {TreeNodeDropAdapter.TREE_NODE_DROP_ADAPTER_CHILD, TreeNodeDropAdapter.TREE_NODE_DROP_ADAPTER_BEHIND, TreeNodeDropAdapter.TREE_NODE_DROP_ADAPTER_CANCEL};
196 // MessageDialog dialog = new MessageDialog(null, TreeNodeDropAdapter.TARGET_NODE, null, TreeNodeDropAdapter.DO_YOU_WANT_TO_MOVE_THE_TAXONNODE_AS_CHILD_OR_BEHIND_THE_TARGET_NODE, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
198 // int returnCode = dialog.getReturnCode();
199 // if (returnCode == 0){
200 // IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
201 // if (workspaceUndoContext == null) {
202 // logger.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
203 // return Status.CANCEL_STATUS;
206 // AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
207 // AbstractUtility.executeOperation(operation, (RemotingCdmHandlerE4)null);
210 // logger.info("Moved taxa to new parent " + targetITaxonTreeNode); //$NON-NLS-1$
211 // return Status.OK_STATUS;
212 // }else if (returnCode == 1){
213 // IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
214 // if (workspaceUndoContext == null) {
215 // logger.error("Workspace undo context is null. DND operation cancelled"); //$NON-NLS-1$
216 // return Status.CANCEL_STATUS;
218 // TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
220 // AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode, MovingType.BEHIND);
221 // AbstractUtility.executeOperation(operation, (RemotingCdmHandlerE4)null);
223 // logger.info("Moved taxa to new parent " + targetITaxonTreeNode); //$NON-NLS-1$
224 // return Status.OK_STATUS;
225 //// }else if (returnCode == 2){
226 //// IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
227 //// if (workspaceUndoContext == null) {
228 //// logger.error("Workspace undo context is null. DND operation cancelled");
229 //// return Status.CANCEL_STATUS;
231 //// TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
233 //// if(CdmStore.getCurrentSessionManager().isRemoting()) {
234 //// AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode, MovingType.PREVIOUS);
235 //// NavigationUtil.executeOperation(operation, null);
237 //// AbstractPostOperation operation = new MoveTaxonOperation
238 //// ("Move Taxon", workspaceUndoContext, uuids, targetNode, this, taxonNavigator, MovingType.PREVIOUS);
239 //// NavigationUtil.executeOperation(operation);
241 //// logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
242 //// return Status.OK_STATUS;
244 return Status
.CANCEL_STATUS
;
251 public boolean postOperation(CdmBase objectAffectedByOperation
) {
261 public boolean onComplete() {
262 // TODO Auto-generated method stub