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
.PartInitException
;
25 import org
.eclipse
.ui
.navigator
.CommonDropAdapter
;
26 import org
.eclipse
.ui
.navigator
.CommonDropAdapterAssistant
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.ITreeNode
;
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
;
40 public class TaxonDropAdapterAssistant
extends CommonDropAdapterAssistant
implements IPostOperationEnabled
{
41 private static final Logger logger
= Logger
42 .getLogger(TaxonDropAdapterAssistant
.class);
44 public static final String ID
= "eu.etaxonomy.taxeditor.navigation.navigator.dropassistant"; //$NON-NLS-1$
46 private ITreeNode target
;
49 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#handleDrop(org.eclipse.ui.navigator.CommonDropAdapter, org.eclipse.swt.dnd.DropTargetEvent, java.lang.Object)
52 public IStatus
handleDrop(CommonDropAdapter dropAdapter
,
53 DropTargetEvent dropTargetEvent
, Object target
) {
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
;
63 return moveTaxon(childTaxonNode
, targetTreeNode
);
68 return Status
.CANCEL_STATUS
;
72 * @see org.eclipse.ui.navigator.CommonDropAdapterAssistant#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
75 public IStatus
validateDrop(Object target
, int operation
,
76 TransferData transferType
) {
77 if (target
instanceof ITreeNode
) {
78 return Status
.OK_STATUS
;
80 return Status
.CANCEL_STATUS
;
85 * @param childTaxonNode
89 private IStatus
moveTaxon(TaxonNode childTaxonNode
, ITreeNode targetTreeNode
) {
91 TaxonNavigator taxonNavigator
;
93 taxonNavigator
= (TaxonNavigator
) NavigationUtil
.getView(TaxonNavigator
.ID
);
94 } catch (PartInitException e
) {
96 throw new RuntimeException(e
);
99 this.target
= targetTreeNode
;
101 if(targetTreeNode
instanceof TaxonNode
){
103 TaxonNode targetTaxonNode
= (TaxonNode
) targetTreeNode
;
105 // Make sure parentTaxon is not the drop target
106 if (!childTaxonNode
.isTopmostNode() && childTaxonNode
.getParent().equals(targetTaxonNode
)){
107 return Status
.CANCEL_STATUS
;
110 // Make sure taxon is not being dropped onto itself
111 if (childTaxonNode
.equals(targetTaxonNode
)) {
112 return Status
.CANCEL_STATUS
;
115 // Make sure parent taxon does not have unsaved changes
116 if (NavigationUtil
.isDirty(targetTaxonNode
)){
117 MessageDialog
.openWarning(NavigationUtil
.getShell(), "Unsaved Parent Taxon", "There are unsaved " +
118 "changes in the parent taxon. Pleas save first.");
119 return Status
.CANCEL_STATUS
;
123 IUndoContext workspaceUndoContext
= NavigationUtil
.getWorkbenchUndoContext();
124 if (workspaceUndoContext
== null) {
125 logger
.error("Workspace undo context is null. DND operation cancelled");
126 return Status
.CANCEL_STATUS
;
131 IUndoableOperation operation
= new MoveTaxonOperation
132 ("Move Taxon", workspaceUndoContext
, childTaxonNode
, targetTreeNode
, this, taxonNavigator
);
133 NavigationUtil
.executeOperation(operation
);
137 logger
.info("Moved taxon " + childTaxonNode
+ " to new parent " + targetTreeNode
);
139 NavigationUtil
.selectInNavigator(childTaxonNode
, targetTreeNode
);
141 return Status
.OK_STATUS
;
145 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
147 public boolean postOperation(CdmBase objectAffectedByOperation
) {
148 NavigationUtil
.selectInNavigator(objectAffectedByOperation
, target
);