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
.IStructuredSelection
;
14 import org
.eclipse
.jface
.viewers
.TreeNode
;
15 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
16 import org
.eclipse
.swt
.dnd
.TransferData
;
18 import eu
.etaxonomy
.cdm
.model
.molecular
.Amplification
;
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
.DerivationEvent
;
23 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
24 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
25 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
32 public class DerivateDropListener
extends ViewerDropAdapter
{
34 private final DerivateView derivateView
;
39 protected DerivateDropListener(DerivateView derivateView
) {
40 super(derivateView
.viewer
);
41 this.derivateView
= derivateView
;
45 * @see org.eclipse.jface.viewers.ViewerDropAdapter#performDrop(java.lang.Object)
48 public boolean performDrop(Object data
) {
49 Object target
= getCurrentTarget();
50 if(target
instanceof TreeNode
&& data
instanceof IStructuredSelection
&& ((IStructuredSelection
) data
).size()==1 && ((IStructuredSelection
) data
).getFirstElement() instanceof TreeNode
){
51 TreeNode targetNode
= (TreeNode
) target
;
52 TreeNode draggednode
= (TreeNode
) ((IStructuredSelection
) data
).getFirstElement();
53 if(moveDropTarget(draggednode
, targetNode
)){
54 derivateView
.changed(null);
62 * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
65 public boolean validateDrop(Object target
, int operation
, TransferData transferType
) {
66 return LocalSelectionTransfer
.getTransfer().isSupportedType(transferType
) && target
instanceof TreeNode
;
73 private boolean moveDropTarget(TreeNode draggedObject
, TreeNode target
) {
74 Object dragValue
= draggedObject
.getValue();
75 Object targetValue
= target
.getValue();
76 TreeNode dragParentNode
= draggedObject
.getParent();
77 Object dragParentSpecimen
= dragParentNode
.getValue();
79 // drag'n'drop for SpecimenOrObservationBase
80 if(dragValue
instanceof DerivedUnit
&& targetValue
instanceof SpecimenOrObservationBase
<?
>){
81 DerivedUnit draggedSpecimen
= (DerivedUnit
) dragValue
;
82 SpecimenOrObservationBase
<?
> targetSpecimen
= (SpecimenOrObservationBase
<?
>) targetValue
;
83 SpecimenOrObservationType dragType
= draggedSpecimen
.getRecordBasis();
84 SpecimenOrObservationType targetType
= targetSpecimen
.getRecordBasis();
85 //check if type is a sub derivate type
86 if(!dragParentNode
.equals(target
) //don't drag on direct parent node
87 && (targetType
==SpecimenOrObservationType
.FieldUnit
//dragging on FieldUnit always works (except when direct parent)
88 || (dragType
.isKindOf(targetType
) && targetType
!=dragType
))){ //dragging only on parent derivate type
89 //remove derivation event from parent specimen of dragged object
90 DerivationEvent eventToRemove
= null;
91 if(dragParentSpecimen
instanceof SpecimenOrObservationBase
<?
>){
92 SpecimenOrObservationBase
<?
> parentDragSpecimen
= (SpecimenOrObservationBase
<?
>) dragParentSpecimen
;
93 for(DerivationEvent event
:parentDragSpecimen
.getDerivationEvents()){
94 if(event
.getDerivatives().contains(draggedSpecimen
)){
95 eventToRemove
= event
;
99 parentDragSpecimen
.removeDerivationEvent(eventToRemove
);
101 //add new derivation event to target
102 targetSpecimen
.addDerivationEvent(DerivationEvent
.NewSimpleInstance(targetSpecimen
, draggedSpecimen
, eventToRemove
==null?
null:eventToRemove
.getType()));
107 // drag'n'drop for SingleRead
108 else if(dragValue
instanceof SingleRead
&& (targetValue
instanceof Amplification
|| targetValue
instanceof Sequence
)){
109 SingleRead singleRead
= (SingleRead
) dragValue
;
110 if(dragParentSpecimen
instanceof Amplification
){
111 Amplification amplification
= (Amplification
) dragParentSpecimen
;
112 if(amplification
.getSingleReads().size()<2){
116 //remove dragged single read (if not the last one)
117 amplification
.removeSingleRead(singleRead
);
121 if(targetValue
instanceof Sequence
){
122 Sequence sequence
= (Sequence
)targetValue
;
123 sequence
.addSingleRead(singleRead
);
124 //remove dragged single read
125 if(dragParentSpecimen
instanceof Sequence
){
126 ((Sequence
) dragParentSpecimen
).removeSingleRead(singleRead
);
129 //drop on Amplification
130 else if(targetValue
instanceof Amplification
){
131 Amplification amplification
= (Amplification
)targetValue
;
132 amplification
.addSingleRead(singleRead
);
137 // drag'n'drop for Sequence
138 else if(dragValue
instanceof Sequence
&& targetValue
instanceof DnaSample
){
139 Sequence sequence
= (Sequence
)dragValue
;
140 //remove dragged sequence
141 if(dragParentSpecimen
instanceof DnaSample
){
142 ((DnaSample
) dragParentSpecimen
).removeSequence(sequence
);
145 ((DnaSample
) targetValue
).addSequence(sequence
);