d1ad70eac4ed661f35769d54f51e9bafeb4ad82e
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / derivateSearch / DerivateContentProvider.java
1 package eu.etaxonomy.taxeditor.view.derivateSearch;
2
3 import java.util.ArrayList;
4 import java.util.LinkedHashSet;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.eclipse.jface.viewers.ITreeContentProvider;
9 import org.eclipse.jface.viewers.TreeNode;
10 import org.eclipse.jface.viewers.Viewer;
11
12 import eu.etaxonomy.cdm.model.molecular.DnaSample;
13 import eu.etaxonomy.cdm.model.molecular.Sequence;
14 import eu.etaxonomy.cdm.model.molecular.SingleRead;
15 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
16 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
17 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
18 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
19
20 public class DerivateContentProvider implements ITreeContentProvider {
21
22 List<TreeNode> rootNodes;
23
24 @Override
25 public void dispose() {
26 // TODO Auto-generated method stub
27
28 }
29
30 public List<SpecimenOrObservationBase> getRootElements() {
31 List<SpecimenOrObservationBase> elements = new ArrayList<SpecimenOrObservationBase>();
32
33 if(rootNodes != null) {
34 for(TreeNode node : rootNodes) {
35 elements.add((SpecimenOrObservationBase)node.getValue());
36 }
37 }
38 return elements;
39 }
40
41 @Override
42 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
43 }
44
45 @Override
46 public Object[] getElements(Object inputElement) {
47 rootNodes = new ArrayList<TreeNode>();
48 if(inputElement instanceof Set){
49 for (Object o: (Set)inputElement) {
50 if(o instanceof SpecimenOrObservationBase){
51 SpecimenOrObservationBase<?> rootElement = (SpecimenOrObservationBase<?>)o;
52 if(rootElement!=null){
53 rootNodes.add(new TreeNode(rootElement));
54 }
55 }
56 }
57 }
58 return rootNodes.toArray();
59 }
60
61 @Override
62 public Object[] getChildren(Object element) {
63 TreeNode currentnode = null;
64 Object value = null;
65 if(element instanceof TreeNode){
66 currentnode = (TreeNode) element;
67 value = ((TreeNode) element).getValue();
68 }
69 Set<Object> children = new LinkedHashSet<Object>();
70 if(value instanceof DnaSample && ((DnaSample) value).getRecordBasis()==SpecimenOrObservationType.DnaSample){
71 DnaSample dnaSample = (DnaSample)value;
72 Set<Sequence> sequences = dnaSample.getSequences();
73 for (Sequence sequence : sequences) {
74 TreeNode node = new TreeNode(sequence);
75 node.setParent(currentnode);
76 children.add(node);
77 }
78 return children.toArray();
79 }
80 else if(value instanceof Sequence){
81 Set<SingleRead> singleReads = ((Sequence) value).getSingleReads();
82 for(SingleRead singleRead:singleReads){
83 TreeNode node = new TreeNode(singleRead);
84 node.setParent(currentnode);
85 children.add(node);
86 }
87 return children.toArray();
88 }
89 else if(value instanceof SpecimenOrObservationBase){
90 List<DerivedUnit> derivates = getDerivates((SpecimenOrObservationBase<?>) value);
91 for (DerivedUnit derivedUnit:derivates) {
92 TreeNode node = new TreeNode(derivedUnit);
93 node.setParent(currentnode);
94 children.add(node);
95 }
96 return children.toArray();
97 }
98 return null;
99 }
100
101 @Override
102 public Object getParent(Object element) {
103 if(element instanceof TreeNode){
104 return ((TreeNode) element).getParent();
105 }
106 return null;
107 }
108
109 @Override
110 public boolean hasChildren(Object element) {
111 if(element instanceof TreeNode){
112 element = ((TreeNode) element).getValue();
113 }
114
115 if(element instanceof DnaSample && ((DnaSample) element).getRecordBasis()==SpecimenOrObservationType.DnaSample){
116 DnaSample dnaSample = (DnaSample)element;
117 if(!dnaSample.getSequences().isEmpty()){// || !dnaSample.getAmplifications().isEmpty()){
118 return true;
119 }
120 }
121 else if(element instanceof Sequence){
122 return !((Sequence)element).getSingleReads().isEmpty();
123 }
124 else if(element instanceof SpecimenOrObservationBase){
125 SpecimenOrObservationBase<?> specimenOrObservation = (SpecimenOrObservationBase<?>)element;
126 for(DerivationEvent event:specimenOrObservation.getDerivationEvents()){
127 if(!event.getDerivatives().isEmpty()){
128 return true;
129 }
130 }
131 }
132 return false;
133 }
134
135 private List<DerivedUnit> getDerivates(SpecimenOrObservationBase<?> specimenOrObservation){
136 List<DerivedUnit> derivates = new ArrayList<DerivedUnit>();
137 for(DerivationEvent derivationEvent:specimenOrObservation.getDerivationEvents()){
138 derivates.addAll(derivationEvent.getDerivatives());
139 }
140 return derivates;
141 }
142
143 }