a8828cbc7d8a167f6e4cab1a6c89e7debf29c6c9
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / TreeNodeDropAdapterAssistant.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.navigation.navigator;
12
13 import java.util.EnumSet;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.Set;
17
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.handlers.HandlerUtil;
29 import org.eclipse.ui.navigator.CommonDropAdapter;
30 import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
31
32 import eu.etaxonomy.cdm.model.common.CdmBase;
33 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
34 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
35 import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
36 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
37 import eu.etaxonomy.taxeditor.navigation.navigator.operation.MoveTaxonOperation;
38 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
39 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
40 import eu.etaxonomy.taxeditor.store.CdmStore;
41
42 /**
43 * <p>TaxonNodeDropAdapterAssistant class.</p>
44 *
45 * @author p.ciardelli
46 * @created 03.06.2009
47 * @version 1.0
48 */
49 public class TreeNodeDropAdapterAssistant extends CommonDropAdapterAssistant implements IPostOperationEnabled {
50
51 private static final Logger logger = Logger.getLogger(TreeNodeDropAdapterAssistant.class);
52
53 /** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.navig"{trunked}</code> */
54 public static final String ID = "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
55
56 private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
57
58 /* (non-Javadoc)
59 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
60 */
61 /** {@inheritDoc} */
62 @Override
63 public IStatus handleDrop(CommonDropAdapter dropAdapter,
64 DropTargetEvent dropTargetEvent, Object target) {
65
66 if (target instanceof ITaxonTreeNode) {
67 Set<TaxonNode> taxonNodes = getSelectedTaxa();
68 ITaxonTreeNode targetTreeNode = (ITaxonTreeNode) target;
69 if(taxonNodes != null) {
70 if (taxonNodes.size() == 1){
71 return moveTaxon(taxonNodes.iterator().next(), targetTreeNode);
72 } else{
73 if( MessageDialog.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
74 return null;
75 }
76 }
77 }
78 }
79
80 return Status.CANCEL_STATUS;
81 }
82
83 private Set<TaxonNode> getSelectedTaxa(){
84 HashSet<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
85
86 ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
87 if (selection instanceof TreeSelection) {
88
89 Iterator selectionIterator = ((TreeSelection) selection).iterator();
90
91 while (selectionIterator.hasNext()){
92 Object object = selectionIterator.next();
93 if(object instanceof TaxonNode){
94 TaxonNode taxonNode = (TaxonNode) object;
95 taxonNodes.add(taxonNode);
96 }
97 }
98 }
99 return taxonNodes.size() > 0 ? taxonNodes : null;
100 }
101
102 /* (non-Javadoc)
103 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
104 */
105 /** {@inheritDoc} */
106 @Override
107 public IStatus validateDrop(Object target, int operation,
108 TransferData transferType) {
109 if (target instanceof ITaxonTreeNode) {
110
111 // check users permissions with target taxonnode and taxon
112 if (target instanceof TaxonNode) {
113 TaxonNode targetNode = (TaxonNode)target;
114 Boolean hasTargetNodePermission = CdmStore.currentAuthentiationHasPermission(targetNode, UPDATE);
115 Boolean hasTargetTaxonPermission = CdmStore.currentAuthentiationHasPermission(targetNode.getTaxon(), UPDATE);
116
117 if(logger.isDebugEnabled()){
118 logger.debug("target: " + targetNode.getTaxon().getTitleCache());
119 }
120
121 if(!hasTargetNodePermission || ! hasTargetNodePermission){
122 if(logger.isDebugEnabled()){
123 logger.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission.toString() + " " + hasTargetTaxonPermission.toString() + " ");
124 }
125 return Status.CANCEL_STATUS;
126 }
127 }
128
129 // do not allow to drop onto itself and
130 // check users permissions with all selected taxon nodes and taxa
131 for(TaxonNode taxonNode : getSelectedTaxa()){
132 logger.debug("selectedTaxa: " + taxonNode.getTaxon().getTitleCache());
133 Boolean isSameTaxonNode = taxonNode.equals(target);
134 Boolean hasTaxonNodePermission = CdmStore.currentAuthentiationHasPermission(taxonNode, UPDATE);
135 Boolean hasTaxonPermission = CdmStore.currentAuthentiationHasPermission(taxonNode.getTaxon(), UPDATE);
136 if (
137 isSameTaxonNode
138 || !hasTaxonNodePermission
139 || !hasTaxonPermission
140 ) {
141 if(logger.isDebugEnabled()){
142 logger.debug("CANCEL_STATUS for selected " + isSameTaxonNode.toString() + " " + hasTaxonNodePermission.toString() + " " + hasTaxonPermission.toString() + " ");
143 }
144 return Status.CANCEL_STATUS;
145 }
146 }
147 logger.debug("OK_STATUS");
148 return Status.OK_STATUS;
149 }
150 logger.debug("CANCEL_STATUS");
151 return Status.CANCEL_STATUS;
152 }
153
154
155 /**
156 * @param childTaxonNode
157 * @param parentTaxon
158 * @return
159 */
160 private IStatus moveTaxon(TaxonNode taxonNode, ITaxonTreeNode targetITaxonTreeNode) {
161
162 TaxonNavigator taxonNavigator;
163 taxonNavigator = (TaxonNavigator) NavigationUtil.showView(TaxonNavigator.ID);
164
165 if(targetITaxonTreeNode instanceof TaxonNode){
166
167 TaxonNode targetTaxonNode = (TaxonNode) targetITaxonTreeNode;
168
169 // for(TaxonNode taxonNode : taxonNodes){
170 // if (taxonNode.equals(targetTaxonNode)) {
171 // return Status.CANCEL_STATUS;
172 // }
173 // }
174
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;
180 }
181
182
183 // Make sure parentTaxon is not the drop target
184 // if (!childTaxonNode.isTopmostNode() && childTaxonNode.getParent().equals(targetTaxonNode)){
185 // return Status.CANCEL_STATUS;
186 // }
187
188 // Make sure taxon is not being dropped onto itself
189 // if (childTaxonNode.equals(targetTaxonNode)) {
190 // return Status.CANCEL_STATUS;
191 // }
192
193
194
195 }
196
197 IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
198 if (workspaceUndoContext == null) {
199 logger.error("Workspace undo context is null. DND operation cancelled");
200 return Status.CANCEL_STATUS;
201 }
202
203 AbstractPostOperation operation = new MoveTaxonOperation
204 ("Move Taxon", workspaceUndoContext, taxonNode, targetITaxonTreeNode, this, taxonNavigator);
205 NavigationUtil.executeOperation(operation);
206
207 logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
208 return Status.OK_STATUS;
209 }
210
211 /* (non-Javadoc)
212 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
213 */
214 /** {@inheritDoc} */
215 @Override
216 public boolean postOperation(CdmBase objectAffectedByOperation) {
217 return true;
218 }
219
220 /**
221 * <p>onComplete</p>
222 *
223 * @return a boolean.
224 */
225 @Override
226 public boolean onComplete() {
227 // TODO Auto-generated method stub
228 return false;
229 }
230
231 }