simplify taxon association section code
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 10 Aug 2016 08:10:18 +0000 (10:10 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 10 Aug 2016 08:10:18 +0000 (10:10 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/association/TaxonAssociationDetailElement.java

index 7a92974453c546d0ae31a7d267f8e7fafd8b25c0..34da746194fc0e6ea975b6606d2b577b1747c203 100644 (file)
@@ -9,9 +9,11 @@
  */
 package eu.etaxonomy.taxeditor.ui.section.occurrence.association;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Set;
 
+import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.DoubleClickEvent;
 import org.eclipse.jface.viewers.IDoubleClickListener;
@@ -19,10 +21,14 @@ import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.jface.viewers.ListViewer;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.ui.handlers.HandlerUtil;
 
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
@@ -56,65 +62,52 @@ public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<Deri
         Collection<SpecimenTypeDesignation> typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(entity.innerDerivedUnit(), null, null, null, null);
         Collection<DeterminationEvent> determinationEvents = CdmStore.getService(IOccurrenceService.class).listDeterminationEvents(entity.innerDerivedUnit(), null, null, null, null);
 
+        Collection<CdmBase> associations = new ArrayList<>();
+        associations.addAll(associatedTaxa);
+        associations.addAll(typeDesignations);
+        associations.addAll(determinationEvents);
+        
         if(associatedTaxa.isEmpty() && typeDesignations.isEmpty() && determinationEvents.isEmpty()){
             Label label = formFactory.createLabel(getLayoutComposite(), "No associations");
             label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
             return;
         }
-        if(!associatedTaxa.isEmpty()){
-            ListViewer viewer = new ListViewer(getLayoutComposite(), SWT.FULL_SELECTION);
-            viewer.getList().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
-            viewer.setContentProvider(new ArrayContentProvider());
-            viewer.setLabelProvider(new LabelProvider(){
-                @Override
-                public String getText(Object element) {
-                    return "Associated with "+element.toString();
-                }
-            });
-            viewer.setInput(associatedTaxa);
-            viewer.addDoubleClickListener(this);
-        }
-        if(!typeDesignations.isEmpty()){
-               ListViewer viewer = new ListViewer(getLayoutComposite(), SWT.FULL_SELECTION);
-            viewer.getList().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
-            viewer.setContentProvider(new ArrayContentProvider());
-            viewer.setLabelProvider(new LabelProvider(){
-                @Override
-                public String getText(Object element) {
-                    SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)element;
-                    String label = typeDesignation.getTypeStatus()!=null?typeDesignation.getTypeStatus().getLabel()+" of ":"Type of ";
-                    Set<TaxonNameBase> typifiedNames = typeDesignation.getTypifiedNames();
-                    for (TaxonNameBase taxonNameBase : typifiedNames) {
-                        label += taxonNameBase+", ";
-                    }
-                    if(label.endsWith(", ")){
-                        label = label.substring(0, label.length()-2);
-                    }
-                    return label;
-                }
-            });
-            viewer.setInput(typeDesignations);
-            viewer.addDoubleClickListener(this);
-        }
-        if(!determinationEvents.isEmpty()){
-               ListViewer viewer = new ListViewer(getLayoutComposite(), SWT.FULL_SELECTION);
-            viewer.getList().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
-            viewer.setContentProvider(new ArrayContentProvider());
-            viewer.setLabelProvider(new LabelProvider(){
-                @Override
-                public String getText(Object element) {
-                    DeterminationEvent determinationEvent = (DeterminationEvent)element;
-                    if(determinationEvent.getTaxon()!=null){
-                        return "Determined as taxon "+determinationEvent.getTaxon();
-                    }
-                    if(determinationEvent.getTaxonName()!=null){
-                        return "Determined as name "+determinationEvent.getTaxonName();
-                    }
-                    return element.toString();
-                }
-            });
-            viewer.setInput(determinationEvents);
-            viewer.addDoubleClickListener(this);
+        if(!associations.isEmpty()){
+               ListViewer viewer = new ListViewer(getLayoutComposite(), SWT.SINGLE);
+               viewer.getList().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+               viewer.setContentProvider(new ArrayContentProvider());
+               viewer.setLabelProvider(new LabelProvider(){
+                       @Override
+                       public String getText(Object element) {
+                               if(element instanceof TaxonBase){
+                                       return "Associated with "+element.toString();
+                               }
+                               else if(element instanceof DeterminationEvent){
+                        DeterminationEvent determinationEvent = (DeterminationEvent)element;
+                        if(determinationEvent.getTaxon()!=null){
+                            return "Determined as taxon "+determinationEvent.getTaxon();
+                        }
+                        if(determinationEvent.getTaxonName()!=null){
+                            return "Determined as name "+determinationEvent.getTaxonName();
+                        }
+                               }
+                               else if(element instanceof SpecimenTypeDesignation){
+                        SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)element;
+                        String label = typeDesignation.getTypeStatus()!=null?typeDesignation.getTypeStatus().getLabel()+" of ":"Type of ";
+                        Set<TaxonNameBase> typifiedNames = typeDesignation.getTypifiedNames();
+                        for (TaxonNameBase taxonNameBase : typifiedNames) {
+                            label += taxonNameBase+", ";
+                        }
+                        if(label.endsWith(", ")){
+                            label = label.substring(0, label.length()-2);
+                        }
+                        return label;
+                               }
+                               return "";
+                       }
+               });
+               viewer.setInput(associations);
+               viewer.addDoubleClickListener(this);
         }
     }