Merge branch 'develop' into wset
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / TreeNodeDropAdapterAssistant.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.navigation.navigator;
11
12 import java.util.EnumSet;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.Set;
16
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;
27
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;
36
37 /**
38 * <p>TaxonNodeDropAdapterAssistant class.</p>
39 *
40 * @author p.ciardelli
41 * @created 03.06.2009
42 */
43 public class TreeNodeDropAdapterAssistant extends CommonDropAdapterAssistant implements IPostOperationEnabled {
44
45 private static final Logger logger = Logger.getLogger(TreeNodeDropAdapterAssistant.class);
46
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$
49
50 private static final EnumSet<CRUD> UPDATE = EnumSet.of(CRUD.UPDATE);
51
52 /** {@inheritDoc} */
53 @Override
54 public IStatus handleDrop(CommonDropAdapter dropAdapter,
55 DropTargetEvent dropTargetEvent, Object target) {
56
57
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);
64 }
65 //if(taxonNodes != null) {
66 if (taxonNodes.size() >= 1){
67 return moveTaxon(taxonNodes, targetTreeNode);
68 /*} else{
69 if( MessageDialog.openConfirm(null, "Moving taxon", "The operation move accepted taxon to other parent is available only for a single taxon.")){
70 return null;
71 }
72 }*/
73 }
74 }
75
76 return Status.CANCEL_STATUS;
77 }
78
79 private Set<TaxonNode> getSelectedTaxa(){
80 HashSet<TaxonNode> taxonNodes = new HashSet<TaxonNode>();
81
82 ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
83 if (selection instanceof TreeSelection) {
84
85 Iterator<?> selectionIterator = ((TreeSelection) selection).iterator();
86
87 while (selectionIterator.hasNext()){
88 Object object = selectionIterator.next();
89 if(object instanceof TaxonNode){
90 TaxonNode taxonNode = (TaxonNode) object;
91 taxonNodes.add(taxonNode);
92 }
93 }
94 }
95 return taxonNodes.size() > 0 ? taxonNodes : null;
96 }
97
98 /** {@inheritDoc} */
99 @Override
100 public IStatus validateDrop(Object target, int operation,
101 TransferData transferType) {
102 if (target instanceof ITaxonTreeNode) {
103
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 ?
109 true :
110 CdmStore.currentAuthentiationHasPermission(targetNode.getTaxon(), UPDATE);
111
112 if(logger.isDebugEnabled()){
113 logger.debug("target: " + targetNode.getTaxon() == null? "-" : targetNode.getTaxon().getTitleCache()); //$NON-NLS-1$
114 }
115
116 if(!hasTargetNodePermission || ! hasTargetNodePermission){
117 if(logger.isDebugEnabled()){
118 logger.debug("CANCEL_STATUS for target node: " + hasTargetNodePermission + " " + hasTargetTaxonPermission + " "); //$NON-NLS-1$
119 }
120 return Status.CANCEL_STATUS;
121 }
122 }
123
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());
129 }
130 boolean isSameTaxonNode = taxonNode.equals(target);
131 boolean hasTaxonNodePermission = CdmStore.currentAuthentiationHasPermission(taxonNode, UPDATE);
132 boolean hasTaxonPermission = taxonNode.getTaxon() == null ?
133 true :
134 CdmStore.currentAuthentiationHasPermission(taxonNode.getTaxon(), UPDATE);
135 if (
136 isSameTaxonNode
137 || !hasTaxonNodePermission
138 || !hasTaxonPermission
139 ) {
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$
142 }
143 return Status.CANCEL_STATUS;
144 }
145 }
146 if (logger.isDebugEnabled()){logger.debug("OK_STATUS");} //$NON-NLS-1$
147 return Status.OK_STATUS;
148 }
149 if (logger.isDebugEnabled()){logger.debug("CANCEL_STATUS");} //$NON-NLS-1$
150 return Status.CANCEL_STATUS;
151 }
152
153
154 /**
155 * @param childTaxonNode
156 * @param parentTaxon
157 * @return
158 */
159 private IStatus moveTaxon(Set<TaxonNode> taxonNodes, ITaxonTreeNode targetITaxonTreeNode) {
160
161 // TaxonNavigator taxonNavigator;
162 // taxonNavigator = (TaxonNavigator) AbstractUtility.showView(TaxonNavigator.ID);
163 //
164 // if(targetITaxonTreeNode instanceof TaxonNode){
165 //
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;
171 //// }
172 //
173 // }
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());
180 // }
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;
186 // }
187 //
188 // AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
189 // AbstractUtility.executeOperation(operation, (RemotingCdmHandlerE4)null);
190 //
191 //
192 // logger.info("Moved taxa to new parent " + targetITaxonTreeNode); //$NON-NLS-1$
193 // return Status.OK_STATUS;
194 // }else{
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);
197 // dialog.open();
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;
204 // }
205 //
206 // AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, (TaxonNode)targetITaxonTreeNode, MovingType.CHILD);
207 // AbstractUtility.executeOperation(operation, (RemotingCdmHandlerE4)null);
208 //
209 //
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;
217 // }
218 // TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
219 //
220 // AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode, MovingType.BEHIND);
221 // AbstractUtility.executeOperation(operation, (RemotingCdmHandlerE4)null);
222 //
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;
230 //// }
231 //// TaxonNode targetNode = (TaxonNode) targetITaxonTreeNode;
232 ////
233 //// if(CdmStore.getCurrentSessionManager().isRemoting()) {
234 //// AbstractOperation operation = new RemotingMoveTaxonOperation(taxonNavigator, false, uuids, targetNode, MovingType.PREVIOUS);
235 //// NavigationUtil.executeOperation(operation, null);
236 //// } else {
237 //// AbstractPostOperation operation = new MoveTaxonOperation
238 //// ("Move Taxon", workspaceUndoContext, uuids, targetNode, this, taxonNavigator, MovingType.PREVIOUS);
239 //// NavigationUtil.executeOperation(operation);
240 //// }
241 //// logger.info("Moved taxa to new parent " + targetITaxonTreeNode);
242 //// return Status.OK_STATUS;
243 // } else{
244 return Status.CANCEL_STATUS;
245 // }
246 // }
247 }
248
249 /** {@inheritDoc} */
250 @Override
251 public boolean postOperation(CdmBase objectAffectedByOperation) {
252 return true;
253 }
254
255 /**
256 * <p>onComplete</p>
257 *
258 * @return a boolean.
259 */
260 @Override
261 public boolean onComplete() {
262 // TODO Auto-generated method stub
263 return false;
264 }
265
266 }