fixing #4808
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / DescriptiveContentProvider.java
index ddbb3525abc1c795239678ee843aa39b4dae0983..33626cb75ca5c6ad6a7f206bbd56ee81f3271ac6 100644 (file)
@@ -4,6 +4,8 @@
 package eu.etaxonomy.taxeditor.editor.view.descriptive;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -11,21 +13,18 @@ import java.util.Set;
 import org.eclipse.jface.viewers.ITreeContentProvider;
 import org.eclipse.jface.viewers.Viewer;
 
-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.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;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
-import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.store.TermStore;
+import eu.etaxonomy.taxeditor.store.UsageTermCollection;
 
 /**
  * <p>DescriptiveContentProvider class.</p>
@@ -39,6 +38,7 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
        protected static final Object[] NO_CHILDREN = new Object[0];
        protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache;
        private boolean showOnlyIndividualAssociations;
+       private static FeatureTree defaultFeatureTree = null;
 
        public DescriptiveContentProvider(Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache) {
            this(featureNodeContainerCache, false);
@@ -82,14 +82,28 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
                        FeatureNodeContainer container = (FeatureNodeContainer) parentElement;
                        if(container.isLeaf()){
                                List<DescriptionElementBase> descriptionElements = container.getDescriptionElements();
-                               if(showOnlyIndividualAssociations){
-                                   List<DescriptionElementBase> filteredDescriptionElements = new ArrayList<DescriptionElementBase>();
-                                   for (DescriptionElementBase descriptionElement : descriptionElements) {
-                                       if(descriptionElement instanceof IndividualsAssociation){
-                                           filteredDescriptionElements.add(descriptionElement);
-                                       }
+                               List<IndividualsAssociation> individualAssociations = new ArrayList<IndividualsAssociation>();
+                               for (DescriptionElementBase descriptionElement : descriptionElements) {
+                                   if(descriptionElement instanceof IndividualsAssociation){
+                                       individualAssociations.add((IndividualsAssociation) descriptionElement);
                                    }
-                                   descriptionElements = filteredDescriptionElements;
+                               }
+                               //sort individual associations by title cache of associated specimens
+                               Collections.sort(individualAssociations, new Comparator<IndividualsAssociation>() {
+
+                    @Override
+                    public int compare(IndividualsAssociation o1, IndividualsAssociation o2) {
+                        if(o1==null || o1.getAssociatedSpecimenOrObservation()==null){
+                            return -1;
+                        }
+                        if(o2==null || o2.getAssociatedSpecimenOrObservation()==null){
+                            return 1;
+                        }
+                        return o1.getAssociatedSpecimenOrObservation().compareTo(o2.getAssociatedSpecimenOrObservation());
+                    }
+                });
+                               if(showOnlyIndividualAssociations){
+                                   descriptionElements = new ArrayList<DescriptionElementBase>(individualAssociations);
                                }
                 return descriptionElements.toArray();
                        }else{
@@ -131,8 +145,11 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
         * @return
         */
        private FeatureTree getFeatureTree(DescriptionBase description){
-               FeatureTree featureTree = null;
 
+           if(defaultFeatureTree == null) {
+               defaultFeatureTree = FeatureTree.NewInstance(TermStore.getTerms(Feature.class));
+           }
+           FeatureTree featureTree;
                // TODO change this to the feature tree associated with this taxon description
                if (description.hasStructuredData()){
                        featureTree = PreferencesUtil.getDefaultFeatureTreeForStructuredDescription();
@@ -142,7 +159,7 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
 
                // create a transient tree with all features if none was selected
                if(featureTree == null){
-                       featureTree = FeatureTree.NewInstance(TermStore.getTerms(Feature.class));
+                       featureTree = defaultFeatureTree;
                }
 
                return featureTree;
@@ -158,11 +175,10 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
         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))) {
+                                               if(marker.getMarkerType() != null && !(marker.getMarkerType().getUuid().equals(UsageTermCollection.uuidUseMarkerType))) {
                                                        resultDescriptions.add(description);
                                                }
                                        }
@@ -175,27 +191,18 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
         return resultDescriptions;
     }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
-        */
        /** {@inheritDoc} */
        @Override
     public Object getParent(Object element) {
                return null;
        }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
-        */
        /** {@inheritDoc} */
        @Override
     public Object[] getElements(Object inputElement) {
                return getChildren(inputElement);
        }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.jface.viewers.IContentProvider#dispose()
-        */
        /**
         * <p>dispose</p>
         */
@@ -204,9 +211,6 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
                featureNodeContainerCache.clear();
        }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
-        */
        /** {@inheritDoc} */
        @Override
     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}