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