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
.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
;
39 public class TaxonDropAdapterAssistant
extends CommonDropAdapterAssistant
implements IPostOperationEnabled
{
40 private static final Logger logger
= Logger
41 .getLogger(TaxonDropAdapterAssistant
.class);
43 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
45 private TaxonNode newParentTaxonNode
;
48 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
51 public IStatus
handleDrop(CommonDropAdapter dropAdapter
,
52 DropTargetEvent dropTargetEvent
, Object target
) {
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
;
62 return moveTaxon(childTaxonNode
, parentTaxonNode
);
67 return Status
.CANCEL_STATUS
;
71 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
74 public IStatus
validateDrop(Object target
, int operation
,
75 TransferData transferType
) {
76 if (target
instanceof TaxonNode
) {
77 return Status
.OK_STATUS
;
79 return Status
.CANCEL_STATUS
;
84 * @param childTaxonNode
88 private IStatus
moveTaxon(TaxonNode childTaxonNode
, TaxonNode newParentTaxonNode
) {
90 this.newParentTaxonNode
= newParentTaxonNode
;
92 // Make sure parentTaxon is not a child
93 if (childTaxonNode
.getParent().equals(newParentTaxonNode
)){
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.");
100 return Status
.CANCEL_STATUS
;
103 // Make sure taxon is not being dropped onto itself
104 if (childTaxonNode
.equals(newParentTaxonNode
)) {
105 return Status
.CANCEL_STATUS
;
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
;
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
;
121 IUndoableOperation operation
= new MoveTaxonOperation
122 ("Move Taxon", workspaceUndoContext
, childTaxonNode
, newParentTaxonNode
, this);
123 NavigationUtil
.executeOperation(operation
);
126 logger
.info("Moved taxon " + childTaxonNode
+ " to new parent " + newParentTaxonNode
);
128 NavigationUtil
.selectInNavigator(childTaxonNode
, newParentTaxonNode
);
130 return Status
.OK_STATUS
;
134 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
136 public boolean postOperation(CdmBase objectAffectedByOperation
) {
137 NavigationUtil
.selectInNavigator(objectAffectedByOperation
, newParentTaxonNode
);