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