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
.api
.service
.IAmplificationService
;
19 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
20 import eu
.etaxonomy
.cdm
.api
.service
.ISequenceService
;
21 import eu
.etaxonomy
.cdm
.model
.molecular
.Amplification
;
22 import eu
.etaxonomy
.cdm
.model
.molecular
.DnaSample
;
23 import eu
.etaxonomy
.cdm
.model
.molecular
.Sequence
;
24 import eu
.etaxonomy
.cdm
.model
.molecular
.SingleRead
;
25 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
26 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
27 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
28 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
35 public class DerivateDropListener
extends ViewerDropAdapter
{
37 private final DerivateView derivateView
;
42 protected DerivateDropListener(DerivateView derivateView
) {
43 super(derivateView
.viewer
);
44 this.derivateView
= derivateView
;
48 * @see org.eclipse.jface.viewers.ViewerDropAdapter#performDrop(java.lang.Object)
51 public boolean performDrop(Object data
) {
52 if(derivateView
.isDirty()){
53 AbstractUtility
.warningDialog("View has unsaved changes", this, "You need to save before performing this action");
56 Object target
= getCurrentTarget();
57 if(target
instanceof TreeNode
&& data
instanceof IStructuredSelection
&& ((IStructuredSelection
) data
).size()==1 && ((IStructuredSelection
) data
).getFirstElement() instanceof TreeNode
){
58 TreeNode targetNode
= (TreeNode
) target
;
59 TreeNode draggednode
= (TreeNode
) ((IStructuredSelection
) data
).getFirstElement();
60 if(moveDropTarget(draggednode
, targetNode
)){
61 derivateView
.viewer
.setInput(derivateView
.getEditorInput());
69 * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
72 public boolean validateDrop(Object target
, int operation
, TransferData transferType
) {
73 System
.out
.println("VAL");
74 return LocalSelectionTransfer
.getTransfer().isSupportedType(transferType
) && target
instanceof TreeNode
;
81 private boolean moveDropTarget(TreeNode draggedObject
, TreeNode target
) {
82 Object dragValue
= draggedObject
.getValue();
83 Object targetValue
= target
.getValue();
84 TreeNode dragParentNode
= draggedObject
.getParent();
85 Object dragParentSpecimen
= dragParentNode
.getValue();
87 // drag'n'drop for SpecimenOrObservationBase
88 if(dragValue
instanceof DerivedUnit
&& targetValue
instanceof SpecimenOrObservationBase
<?
>){
89 DerivedUnit draggedSpecimen
= (DerivedUnit
) dragValue
;
90 SpecimenOrObservationBase
<?
> targetSpecimen
= (SpecimenOrObservationBase
<?
>) targetValue
;
91 //check if type is a sub derivate type
92 if(!dragParentNode
.equals(target
)//don't drag on direct parent node
93 && dragParentSpecimen
instanceof SpecimenOrObservationBase
<?
>){
94 CdmStore
.getService(IOccurrenceService
.class).moveDerivate((SpecimenOrObservationBase
<?
>)dragParentSpecimen
, targetSpecimen
, draggedSpecimen
);
99 // drag'n'drop for SingleRead
100 else if(dragValue
instanceof SingleRead
&& (targetValue
instanceof Amplification
|| targetValue
instanceof Sequence
)){
101 SingleRead singleRead
= (SingleRead
) dragValue
;
102 //amplification -> amplification
103 if(dragParentSpecimen
instanceof Amplification
&& targetValue
instanceof Amplification
){
104 if(((Amplification
) dragParentSpecimen
).getSingleReads().size()>1){
105 CdmStore
.getService(IAmplificationService
.class).moveSingleRead((Amplification
)dragParentSpecimen
, (Amplification
)targetValue
, singleRead
);
108 //amplification -> sequence
109 else if(dragParentSpecimen
instanceof Amplification
&& targetValue
instanceof Sequence
){
110 //TODO: what does the user expect?
112 //sequence -> sequence
113 else if(dragParentSpecimen
instanceof Sequence
&& targetValue
instanceof Sequence
){
114 CdmStore
.getService(ISequenceService
.class).moveSingleRead((Sequence
)dragParentSpecimen
, (Sequence
)targetValue
, singleRead
);
116 //sequence -> amplification
117 else if(dragParentSpecimen
instanceof Sequence
&& targetValue
instanceof Amplification
){
118 //TODO: what does the user expect?
123 // drag'n'drop for Sequence
124 else if(dragValue
instanceof Sequence
&& targetValue
instanceof DnaSample
){
125 Sequence sequence
= (Sequence
)dragValue
;
126 if(dragParentSpecimen
instanceof DnaSample
){
127 CdmStore
.getService(IOccurrenceService
.class).moveSequence((DnaSample
)dragParentSpecimen
, (DnaSample
)targetValue
, sequence
);