merged campanula branch to trunk. Main features are: BioCase Query via Imports, Deriv...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / DescriptiveContentProvider.java
index fce0faf8e592fc5ec687eb383a97af673547b092..8bd9fa6f4386c3156e888ca49a1262b3bc9eede0 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * 
+ *
  */
 package eu.etaxonomy.taxeditor.editor.view.descriptive;
 
@@ -17,9 +17,7 @@ import eu.etaxonomy.cdm.model.common.MarkerType;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
 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.taxeditor.editor.UsageTermCollection;
 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
 import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
@@ -35,33 +33,34 @@ import eu.etaxonomy.taxeditor.store.TermStore;
  * @version $Id: $
  */
 public class DescriptiveContentProvider implements ITreeContentProvider {
-       
+
        protected static final Object[] NO_CHILDREN = new Object[0];
-       protected Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache;
-       
+       protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache;
+
        /**
         * <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) {
                this.featureNodeContainerCache = featureNodeContainerCache;
        }
-       
+
        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
         */
        /** {@inheritDoc} */
-       public Object[] getChildren(Object parentElement) {
-               if (parentElement instanceof TaxonEditorInput) {
-                       return getDescriptions((TaxonEditorInput) parentElement).toArray(); 
+       @Override
+    public Object[] getChildren(Object parentElement) {
+               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();
                        }
                }
@@ -73,11 +72,11 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
                                return container.getChildren().toArray();
                        }
                }
-               
+
                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);
@@ -88,77 +87,76 @@ 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;
                        }
-               } 
+               }
                return getChildren(element).length != 0;
        }
-       
+
        /**
         * Retrieves the feature tree associated with the given description
-        * 
-        * TODO as of now this is always the same thing because feature trees may not be associated 
+        *
+        * TODO as of now this is always the same thing because feature trees may not be associated
         * to descriptions yet.
-        * 
+        *
         * @param description
         * @return
         */
        private FeatureTree getFeatureTree(DescriptionBase description){
                FeatureTree featureTree = null;
-               
+
                // TODO change this to the feature tree associated with this taxon description
-               if (description.hasStructuredData()){                                   
+               if (description.hasStructuredData()){
                        featureTree = PreferencesUtil.getDefaultFeatureTreeForStructuredDescription();
                }else{
                        featureTree = PreferencesUtil.getDefaultFeatureTreeForTextualDescription();
                }
-               
+
                // create a transient tree with all features if none was selected
                if(featureTree == null){
                        featureTree = FeatureTree.NewInstance(TermStore.getTerms(Feature.class));
                }
-               
+
                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)
         */
        /** {@inheritDoc} */
-       public Object getParent(Object element) {
+       @Override
+    public Object getParent(Object element) {
                return null;
        }
 
@@ -166,17 +164,19 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
         * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
         */
        /** {@inheritDoc} */
-       public Object[] getElements(Object inputElement) {
+       @Override
+    public Object[] getElements(Object inputElement) {
                return getChildren(inputElement);
        }
-       
+
        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.IContentProvider#dispose()
         */
        /**
         * <p>dispose</p>
         */
-       public void dispose() {
+       @Override
+    public void dispose() {
                featureNodeContainerCache.clear();
        }
 
@@ -184,6 +184,7 @@ public class DescriptiveContentProvider implements ITreeContentProvider {
         * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
         */
        /** {@inheritDoc} */
-       public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}    
-       
+       @Override
+    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
+
 }