- activated filter in factual data view for displaying only IndividualsAssociations
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / DescriptiveContentProvider.java
index cee881704e5668cdc8fe1ceebcfc0638f340ac0c..ddbb3525abc1c795239678ee843aa39b4dae0983 100644 (file)
@@ -15,11 +15,11 @@ import eu.etaxonomy.cdm.api.service.ITermService;
 import eu.etaxonomy.cdm.model.common.Marker;
 import eu.etaxonomy.cdm.model.common.MarkerType;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
+import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
 import eu.etaxonomy.cdm.model.description.Feature;
 import eu.etaxonomy.cdm.model.description.FeatureTree;
-import eu.etaxonomy.cdm.model.description.TaxonDescription;
-import eu.etaxonomy.cdm.model.taxon.Taxon;
-import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
+import eu.etaxonomy.cdm.model.description.IDescribable;
+import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
 import eu.etaxonomy.taxeditor.editor.UsageTermCollection;
 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
 import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
@@ -37,15 +37,20 @@ import eu.etaxonomy.taxeditor.store.TermStore;
 public class DescriptiveContentProvider implements ITreeContentProvider {
 
        protected static final Object[] NO_CHILDREN = new Object[0];
-       protected Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache;
+       protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache;
+       private boolean showOnlyIndividualAssociations;
 
+       public DescriptiveContentProvider(Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache) {
+           this(featureNodeContainerCache, false);
+       }
        /**
         * <p>Constructor for DescriptiveContentProvider.</p>
         *
         * @param featureNodeContainerCache a {@link java.util.Map} object.
         */
-       public DescriptiveContentProvider(Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache) {
+       public DescriptiveContentProvider(Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache, boolean showOnlyIndividualAssociations) {
                this.featureNodeContainerCache = featureNodeContainerCache;
+               this.showOnlyIndividualAssociations = showOnlyIndividualAssociations;
        }
 
        /* (non-Javadoc)
@@ -54,22 +59,39 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
        /** {@inheritDoc} */
        @Override
     public Object[] getChildren(Object parentElement) {
-               if (parentElement instanceof TaxonEditorInput) {
-                       return getDescriptions((TaxonEditorInput) parentElement).toArray();
+               if (parentElement instanceof IDescribable<?>) {
+                       return getDescriptions((IDescribable<?>) parentElement).toArray();
                }
-               else if (parentElement instanceof TaxonDescription) {
-                       if ( ! ((TaxonDescription) parentElement).isImageGallery()) {
-                               TaxonDescription description = (TaxonDescription) parentElement;
+               else if (parentElement instanceof DescriptionBase<?>) {
+                       if ( ! ((DescriptionBase<?>) parentElement).isImageGallery()) {
+                           DescriptionBase<?> description = (DescriptionBase<?>) parentElement;
 
                                FeatureNodeContainerTree containerTree = getContainerTreeForDesription(description);
-
-                               return containerTree.getRoot().getChildren().toArray();
+                               List<FeatureNodeContainer> children = containerTree.getRoot().getChildren();
+                               //filter out containers with no children
+                               List<FeatureNodeContainer> childrenWithChildren = new ArrayList<FeatureNodeContainer>();
+                               for (FeatureNodeContainer featureNodeContainer : children) {
+                                   if(getChildren(featureNodeContainer).length>0){
+                                       childrenWithChildren.add(featureNodeContainer);
+                                   }
+                }
+                               return childrenWithChildren.toArray();
                        }
                }
                else if (parentElement instanceof FeatureNodeContainer){
                        FeatureNodeContainer container = (FeatureNodeContainer) parentElement;
                        if(container.isLeaf()){
-                               return container.getDescriptionElements().toArray();
+                               List<DescriptionElementBase> descriptionElements = container.getDescriptionElements();
+                               if(showOnlyIndividualAssociations){
+                                   List<DescriptionElementBase> filteredDescriptionElements = new ArrayList<DescriptionElementBase>();
+                                   for (DescriptionElementBase descriptionElement : descriptionElements) {
+                                       if(descriptionElement instanceof IndividualsAssociation){
+                                           filteredDescriptionElements.add(descriptionElement);
+                                       }
+                                   }
+                                   descriptionElements = filteredDescriptionElements;
+                               }
+                return descriptionElements.toArray();
                        }else{
                                return container.getChildren().toArray();
                        }
@@ -78,7 +100,7 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
                return NO_CHILDREN;
        }
 
-       private FeatureNodeContainerTree getContainerTreeForDesription(TaxonDescription description){
+       private FeatureNodeContainerTree getContainerTreeForDesription(DescriptionBase<?> description){
                if(! featureNodeContainerCache.containsKey(description)){
                        FeatureNodeContainerTree containerTree = new FeatureNodeContainerTree(description, getFeatureTree(description));
                        featureNodeContainerCache.put(description, containerTree);
@@ -89,8 +111,8 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
        /** {@inheritDoc} */
        @Override
        public boolean hasChildren(Object element) {
-               if (element instanceof TaxonDescription){
-                       TaxonDescription description = (TaxonDescription) element;
+               if (element instanceof DescriptionBase<?>){
+                   DescriptionBase<?> description = (DescriptionBase<?>) element;
                        FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
                        if(containerTree != null && containerTree.getRoot() != null){
                                return containerTree.getRoot().getChildren().size() != 0;
@@ -126,34 +148,32 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
                return featureTree;
        }
 
-       /**
-        * Get all descriptions associated with the given TaxonEditorInput
-        *
-        * @param parentElement
-        * @return
-        */
-       protected List<DescriptionBase> getDescriptions(TaxonEditorInput parentElement) {
-               Taxon taxon = parentElement.getTaxon();
-               List<DescriptionBase> descriptions = new ArrayList<DescriptionBase>();
-               for(DescriptionBase description : taxon.getDescriptions()){
+    /**
+     * Get all descriptions associated with the given object
+     * @param parentElement
+     * @return
+     */
+    protected List<DescriptionBase<?>> getDescriptions(IDescribable<?> parentElement) {
+        Set<? extends DescriptionBase<?>> elementDescriptions = parentElement.getDescriptions();
+        List<DescriptionBase<?>> resultDescriptions = new ArrayList<DescriptionBase<?>>();
+        for(DescriptionBase<?> description : elementDescriptions){
                        if(! description.isImageGallery()){
                                MarkerType useMarkertype = (MarkerType) CdmStore.getService(ITermService.class).find(UsageTermCollection.uuidUseMarkerType);
                                Set<Marker> descriptionMarkers = description.getMarkers();
                                if(descriptionMarkers != null && !descriptionMarkers.isEmpty()) {
                                        for (Marker marker: descriptionMarkers) {
                                                if(!(marker.getMarkerType().equals(useMarkertype))) {
-                                                       descriptions.add(description);
+                                                       resultDescriptions.add(description);
                                                }
                                        }
                                }
                                else {
-                                       descriptions.add(description);
+                                       resultDescriptions.add(description);
                                }
                        }
-
                }
-               return descriptions;
-       }
+        return resultDescriptions;
+    }
 
        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
@@ -191,4 +211,8 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
        @Override
     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
 
+       public void toggleShowOnlyIndividualAssociations() {
+           showOnlyIndividualAssociations = !showOnlyIndividualAssociations;
+    }
+
 }