3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.navigation
.navigator
;
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
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
28 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
29 import eu
.etaxonomy
.taxeditor
.store
.model
.NameUtil
;
30 import eu
.etaxonomy
.taxeditor
.store
.model
.TaxonUtil
;
31 import eu
.etaxonomy
.taxeditor
.store
.operations
.MoveTaxonOperation
;
38 public class TaxonDropAdapterAssistant
extends CommonDropAdapterAssistant
{
39 private static final Logger logger
= Logger
40 .getLogger(TaxonDropAdapterAssistant
.class);
42 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
45 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
48 public IStatus
handleDrop(CommonDropAdapter dropAdapter
,
49 DropTargetEvent dropTargetEvent
, Object target
) {
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
;
59 return moveTaxon(childTaxon
, parentTaxon
);
64 // IContainer target = getActualTarget((IResource) dropAdapter
65 // .getCurrentTarget());
66 // IContainer target =
67 // if (target != null && target.isAccessible()) {
69 // target.refreshLocal(IResource.DEPTH_ONE, null);
70 // } catch (CoreException e) {
74 return Status
.CANCEL_STATUS
;
78 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
81 public IStatus
validateDrop(Object target
, int operation
,
82 TransferData transferType
) {
83 if (target
instanceof Taxon
) {
84 return Status
.OK_STATUS
;
86 return Status
.CANCEL_STATUS
;
95 private IStatus
moveTaxon(Taxon childTaxon
, Taxon newParentTaxon
) {
97 // Make sure parentTaxon is not a child
98 if (TaxonUtil
.isTaxonChildOfTaxon(newParentTaxon
, childTaxon
)) {
100 MessageDialog
.openError(NavigationUtil
.getShell(), "Can't move taxon.",
101 "'" + NameUtil
.getDisplayName(childTaxon
) + "' sits above " +
102 "'" + NameUtil
.getDisplayName(newParentTaxon
) + "' " +
103 "in the taxonomic hierarchy.");
105 return Status
.CANCEL_STATUS
;
108 // Make sure taxon is not being dropped onto itself
109 if (childTaxon
.equals(newParentTaxon
)) {
110 return Status
.CANCEL_STATUS
;
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
;
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
;
125 IUndoableOperation operation
= new MoveTaxonOperation
126 ("Move Taxon", workspaceUndoContext
, childTaxon
, newParentTaxon
);
127 NavigationUtil
.executeOperation(operation
);
129 logger
.info("Moved taxon " + childTaxon
+ " to new parent " + newParentTaxon
);
131 NavigationUtil
.selectInNavigator(childTaxon
, newParentTaxon
);
133 return Status
.OK_STATUS
;