59a34fbc3d4445be08cd973028d1eb90f57a202f
[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.TaxonNode;
29 import eu.etaxonomy.taxeditor.model.NameUtil;
30 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
31 import eu.etaxonomy.taxeditor.operations.IPostOperationEnabled;
32 import eu.etaxonomy.taxeditor.operations.MoveTaxonOperation;
33
34 /**
35 * @author p.ciardelli
36 * @created 03.06.2009
37 * @version 1.0
38 */
39 public class TaxonDropAdapterAssistant extends CommonDropAdapterAssistant implements IPostOperationEnabled{
40 private static final Logger logger = Logger
41 .getLogger(TaxonDropAdapterAssistant.class);
42
43 public static final String ID = "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
44
45 private TaxonNode newParentTaxonNode;
46
47 /* (non-Javadoc)
48 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
49 */
50 @Override
51 public IStatus handleDrop(CommonDropAdapter dropAdapter,
52 DropTargetEvent dropTargetEvent, Object target) {
53
54 ISelection selection = LocalSelectionTransfer.getTransfer().getSelection();
55 if (selection instanceof TreeSelection) {
56 Object element = ((TreeSelection) selection).getFirstElement();
57 if (element instanceof TaxonNode) {
58 TaxonNode childTaxonNode = (TaxonNode) element;
59 if (target instanceof TaxonNode) {
60 TaxonNode parentTaxonNode = (TaxonNode) target;
61
62 return moveTaxon(childTaxonNode, parentTaxonNode);
63 }
64 }
65 }
66
67 return Status.CANCEL_STATUS;
68 }
69
70 /* (non-Javadoc)
71 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
72 */
73 @Override
74 public IStatus validateDrop(Object target, int operation,
75 TransferData transferType) {
76 if (target instanceof TaxonNode) {
77 return Status.OK_STATUS;
78 }
79 return Status.CANCEL_STATUS;
80 }
81
82
83 /**
84 * @param childTaxonNode
85 * @param parentTaxon
86 * @return
87 */
88 private IStatus moveTaxon(TaxonNode childTaxonNode, TaxonNode newParentTaxonNode) {
89
90 this.newParentTaxonNode = newParentTaxonNode;
91
92 // Make sure parentTaxon is not a child
93 if (childTaxonNode.getParent().equals(newParentTaxonNode)){
94
95 MessageDialog.openError(NavigationUtil.getShell(), "Can't move taxon.",
96 "'" + NameUtil.getDisplayName(childTaxonNode.getTaxon()) + "' sits above " +
97 "'" + NameUtil.getDisplayName(newParentTaxonNode.getTaxon()) + "' " +
98 "in the taxonomic hierarchy.");
99
100 return Status.CANCEL_STATUS;
101 }
102
103 // Make sure taxon is not being dropped onto itself
104 if (childTaxonNode.equals(newParentTaxonNode)) {
105 return Status.CANCEL_STATUS;
106 }
107
108 // Make sure parent taxon does not have unsaved changes
109 if (NavigationUtil.isDirty(newParentTaxonNode)){
110 MessageDialog.openWarning(NavigationUtil.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
111 "changes in the parent taxon. Pleas save first.");
112 return Status.CANCEL_STATUS;
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 IUndoableOperation operation = new MoveTaxonOperation
122 ("Move Taxon", workspaceUndoContext, childTaxonNode, newParentTaxonNode, this);
123 NavigationUtil.executeOperation(operation);
124
125
126 logger.info("Moved taxon " + childTaxonNode + " to new parent " + newParentTaxonNode);
127
128 NavigationUtil.selectInNavigator(childTaxonNode, newParentTaxonNode);
129
130 return Status.OK_STATUS;
131 }
132
133 /* (non-Javadoc)
134 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
135 */
136 public boolean postOperation(CdmBase objectAffectedByOperation) {
137 NavigationUtil.selectInNavigator(objectAffectedByOperation, newParentTaxonNode);
138 return true;
139 }
140 }