fixes #1089
[taxeditor.git] / taxeditor-navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / TaxonDropAdapterAssistant.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 org.apache.log4j.Logger;
14 import org.eclipse.core.commands.operations.IUndoContext;
15 import org.eclipse.core.commands.operations.IUndoableOperation;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.util.LocalSelectionTransfer;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.TreeSelection;
22 import org.eclipse.swt.dnd.DropTargetEvent;
23 import org.eclipse.swt.dnd.TransferData;
24 import org.eclipse.ui.navigator.CommonDropAdapter;
25 import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
26
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.taxon.ITreeNode;
29 import eu.etaxonomy.cdm.model.taxon.IllegalAncestryException;
30 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
32 import eu.etaxonomy.taxeditor.operations.IPostOperationEnabled;
33 import eu.etaxonomy.taxeditor.operations.MoveTaxonOperation;
34
35 /**
36 * @author p.ciardelli
37 * @created 03.06.2009
38 * @version 1.0
39 */
40 public class TaxonDropAdapterAssistant extends CommonDropAdapterAssistant implements IPostOperationEnabled{
41 private static final Logger logger = Logger
42 .getLogger(TaxonDropAdapterAssistant.class);
43
44 public static final String ID = "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
45
46 private ITreeNode target;
47
48 /* (non-Javadoc)
49 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
50 */
51 @Override
52 public IStatus handleDrop(CommonDropAdapter dropAdapter,
53 DropTargetEvent dropTargetEvent, Object target) {
54
55 ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
56 if (selection instanceof TreeSelection) {
57 Object element = ((TreeSelection) selection).getFirstElement();
58 if (element instanceof TaxonNode) {
59 TaxonNode childTaxonNode = (TaxonNode) element;
60 if (target instanceof ITreeNode) {
61 ITreeNode targetTreeNode = (ITreeNode) target;
62
63 return moveTaxon(childTaxonNode, targetTreeNode);
64 }
65 }
66 }
67
68 return Status.CANCEL_STATUS;
69 }
70
71 /* (non-Javadoc)
72 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
73 */
74 @Override
75 public IStatus validateDrop(Object target, int operation,
76 TransferData transferType) {
77 if (target instanceof ITreeNode) {
78 return Status.OK_STATUS;
79 }
80 return Status.CANCEL_STATUS;
81 }
82
83
84 /**
85 * @param childTaxonNode
86 * @param parentTaxon
87 * @return
88 */
89 private IStatus moveTaxon(TaxonNode childTaxonNode, ITreeNode targetTreeNode) {
90
91 this.target = targetTreeNode;
92
93 if(targetTreeNode instanceof TaxonNode){
94
95 TaxonNode targetTaxonNode = (TaxonNode) targetTreeNode;
96
97 // Make sure parentTaxon is not the drop target
98 if (!childTaxonNode.isTopmostNode() && childTaxonNode.getParent().equals(targetTaxonNode)){
99 return Status.CANCEL_STATUS;
100 }
101
102 // Make sure taxon is not being dropped onto itself
103 if (childTaxonNode.equals(targetTaxonNode)) {
104 return Status.CANCEL_STATUS;
105 }
106
107 // Make sure parent taxon does not have unsaved changes
108 if (NavigationUtil.isDirty(targetTaxonNode)){
109 MessageDialog.openWarning(NavigationUtil.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
110 "changes in the parent taxon. Pleas save first.");
111 return Status.CANCEL_STATUS;
112 }
113 }
114
115 IUndoContext workspaceUndoContext = NavigationUtil.getWorkbenchUndoContext();
116 if (workspaceUndoContext == null) {
117 logger.error("Workspace undo context is null. DND operation cancelled");
118 return Status.CANCEL_STATUS;
119 }
120
121
122
123 IUndoableOperation operation = new MoveTaxonOperation
124 ("Move Taxon", workspaceUndoContext, childTaxonNode, targetTreeNode, this);
125 NavigationUtil.executeOperation(operation);
126
127
128
129 logger.info("Moved taxon " + childTaxonNode + " to new parent " + targetTreeNode);
130
131 NavigationUtil.selectInNavigator(childTaxonNode, targetTreeNode);
132
133 return Status.OK_STATUS;
134 }
135
136 /* (non-Javadoc)
137 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
138 */
139 public boolean postOperation(CdmBase objectAffectedByOperation) {
140 NavigationUtil.selectInNavigator(objectAffectedByOperation, target);
141 return true;
142 }
143 }