3 * Copyright (C) 2014 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.
10 package eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
;
12 import org
.eclipse
.jface
.util
.LocalSelectionTransfer
;
13 import org
.eclipse
.jface
.viewers
.TreeNode
;
14 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
15 import org
.eclipse
.swt
.dnd
.TransferData
;
17 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
18 import eu
.etaxonomy
.cdm
.api
.service
.molecular
.ISequenceService
;
19 import eu
.etaxonomy
.cdm
.model
.molecular
.DnaSample
;
20 import eu
.etaxonomy
.cdm
.model
.molecular
.Sequence
;
21 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
22 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
23 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
24 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
25 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
26 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
27 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateLabelProvider
;
34 public class DerivateDropListener
extends ViewerDropAdapter
{
36 private final DerivateView derivateView
;
41 protected DerivateDropListener(DerivateView derivateView
) {
42 super(derivateView
.getViewer());
43 this.derivateView
= derivateView
;
47 * @see org.eclipse.jface.viewers.ViewerDropAdapter#performDrop(java.lang.Object)
50 public boolean performDrop(Object data
) {
51 if(derivateView
.isDirty()){
52 MessagingUtils
.warningDialog("View has unsaved changes", this, "You need to save before performing this action");
55 Object target
= getCurrentTarget();
56 if(target
instanceof TreeNode
&& data
instanceof DerivateViewSelection
){
57 DerivateViewSelection derivateViewSelection
= (DerivateViewSelection
)data
;
58 TreeNode targetNode
= (TreeNode
) target
;
59 TreeNode draggednode
= EditorUtil
.getTreeNodeOfSelection(derivateViewSelection
.getSelection());
60 if(draggednode
!=null){
61 if(moveTreeNode(draggednode
, targetNode
)){
63 derivateView
.getConversationHolder().commit();
64 derivateView
.refreshTree();
66 // update source DerivateView if dragging between different views occured
67 DerivateView sourceDerivateView
= derivateViewSelection
.getDerivateView();
68 if(sourceDerivateView
!=derivateView
){
69 sourceDerivateView
.getConversationHolder().commit();
70 sourceDerivateView
.refreshTree();
73 //FIXME: way too much overhead for just refreshing the other DerivateView
74 // CdmStore.getContextManager().notifyContextRefresh();
75 LocalSelectionTransfer
.getTransfer().setSelection(null);
79 MessagingUtils
.warningDialog("Moving derivate not possible!", derivateView
, "Moving \""+DerivateLabelProvider
.getDerivateText(draggednode
)+"\" to \""+DerivateLabelProvider
.getDerivateText(targetNode
)+"\" is not possible!");
87 * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
90 public boolean validateDrop(Object target
, int operation
, TransferData transferType
) {
91 return LocalSelectionTransfer
.getTransfer().isSupportedType(transferType
) && target
instanceof TreeNode
;
98 private boolean moveTreeNode(TreeNode draggedNode
, TreeNode targetNode
) {
99 Object draggedNodeValue
= draggedNode
.getValue();
100 Object targetNodeValue
= targetNode
.getValue();
101 TreeNode fromParentNode
= draggedNode
.getParent();
102 Object fromParentSpecimen
= fromParentNode
.getValue();
104 // drag'n'drop for SpecimenOrObservationBase
105 IOccurrenceService occurrenceService
= CdmStore
.getService(IOccurrenceService
.class);
106 if(draggedNodeValue
instanceof DerivedUnit
&& targetNodeValue
instanceof SpecimenOrObservationBase
<?
>){
107 DerivedUnit draggedSpecimen
= (DerivedUnit
) draggedNodeValue
;
108 SpecimenOrObservationBase
<?
> targetSpecimen
= (SpecimenOrObservationBase
<?
>) targetNodeValue
;
109 //check if type is a sub derivate type
110 if(!fromParentNode
.equals(targetNode
)//don't drag on direct parent node
111 && fromParentSpecimen
instanceof SpecimenOrObservationBase
<?
>){
112 return occurrenceService
.moveDerivate((SpecimenOrObservationBase
<?
>)fromParentSpecimen
, targetSpecimen
, draggedSpecimen
);
116 // drag'n'drop for SingleRead
117 else if(draggedNodeValue
instanceof SingleRead
&& targetNodeValue
instanceof Sequence
){
118 SingleRead singleRead
= (SingleRead
) draggedNodeValue
;
119 if(fromParentSpecimen
instanceof Sequence
){
120 return CdmStore
.getService(ISequenceService
.class).moveSingleRead((Sequence
)fromParentSpecimen
, (Sequence
)targetNodeValue
, singleRead
);
124 // drag'n'drop for Sequence
125 else if(draggedNodeValue
instanceof Sequence
&& targetNodeValue
instanceof DnaSample
){
126 Sequence sequence
= (Sequence
)draggedNodeValue
;
127 if(fromParentSpecimen
instanceof DnaSample
){
128 return occurrenceService
.moveSequence((DnaSample
)fromParentSpecimen
, (DnaSample
)targetNodeValue
, sequence
);