package eu.etaxonomy.taxeditor.editor.view.derivate;
+import java.util.ArrayList;
import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
import org.eclipse.jface.viewers.ITreeContentProvider;
+import org.eclipse.jface.viewers.TreeNode;
import org.eclipse.jface.viewers.Viewer;
+import eu.etaxonomy.cdm.model.molecular.DnaSample;
+import eu.etaxonomy.cdm.model.molecular.Sequence;
+import eu.etaxonomy.cdm.model.molecular.SingleRead;
+import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
+import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.taxeditor.editor.EditorUtil;
+
public class DerivateContentProvider implements ITreeContentProvider {
@Override
@Override
public Object[] getElements(Object inputElement) {
- return Collections.singleton(inputElement).toArray();
+ SpecimenOrObservationBase specimen = null;
+ if(inputElement instanceof DerivateViewEditorInput){
+ specimen = ((DerivateViewEditorInput) inputElement).getDerivate();
+ }
+
+ specimen = EditorUtil.getTopMostDerivate(specimen);
+
+ if(specimen!=null){
+ //show topmost element in the derivate hierarchy if no field unit was found
+ TreeNode node = new TreeNode(specimen);
+ return Collections.singleton(node).toArray();
+ }
+ return null;
}
@Override
- public Object[] getChildren(Object parentElement) {
- return null;
+ public Object[] getChildren(Object element) {
+ TreeNode currentnode = null;
+ Object value = null;
+ if(element instanceof TreeNode){
+ currentnode = (TreeNode) element;
+ value = ((TreeNode) element).getValue();
+ }
+ Set<Object> children = new LinkedHashSet<Object>();
+ if(value instanceof DnaSample){
+ DnaSample dnaSample = (DnaSample)value;
+ Set<Sequence> sequences = dnaSample.getSequences();
+ for (Sequence sequence : sequences) {
+ TreeNode node = new TreeNode(sequence);
+ node.setParent(currentnode);
+ children.add(node);
+ }
+ return children.toArray();
+ }
+ else if(value instanceof Sequence){
+ Set<SingleRead> singleReads = ((Sequence) value).getSingleReads();
+ for(SingleRead singleRead:singleReads){
+ TreeNode node = new TreeNode(singleRead);
+ node.setParent(currentnode);
+ children.add(node);
+ }
+ return children.toArray();
+ }
+ else if(value instanceof SpecimenOrObservationBase){
+ List<DerivedUnit> derivates = getDerivates((SpecimenOrObservationBase<?>) value);
+ for (DerivedUnit derivedUnit:derivates) {
+ TreeNode node = new TreeNode(derivedUnit);
+ node.setParent(currentnode);
+ children.add(node);
+ }
+ return children.toArray();
+ }
+ return null;
}
@Override
public Object getParent(Object element) {
- // TODO Auto-generated method stub
+ if(element instanceof TreeNode){
+ return ((TreeNode) element).getParent();
+ }
return null;
}
@Override
public boolean hasChildren(Object element) {
- // TODO Auto-generated method stub
+ if(element instanceof TreeNode){
+ element = ((TreeNode) element).getValue();
+ }
+
+ if(element instanceof DnaSample){
+ DnaSample dnaSample = (DnaSample)element;
+ if(!dnaSample.getSequences().isEmpty()){// || !dnaSample.getAmplifications().isEmpty()){
+ return true;
+ }
+ }
+ else if(element instanceof Sequence){
+ return !((Sequence)element).getSingleReads().isEmpty();
+ }
+ else if(element instanceof SpecimenOrObservationBase){
+ SpecimenOrObservationBase specimenOrObservation = (SpecimenOrObservationBase)element;
+ for(DerivationEvent event:(Set<DerivationEvent>)specimenOrObservation.getDerivationEvents()){
+ if(!event.getDerivatives().isEmpty()){
+ return true;
+ }
+ }
+ }
return false;
}
+ private List<DerivedUnit> getDerivates(SpecimenOrObservationBase specimenOrObservation){
+ List<DerivedUnit> derivates = new ArrayList<DerivedUnit>();
+ for(DerivationEvent derivationEvent:(Set<DerivationEvent>)specimenOrObservation.getDerivationEvents()){
+ derivates.addAll(derivationEvent.getDerivatives());
+ }
+ return derivates;
+ }
+
}