simplify taxon association section code
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / occurrence / association / TaxonAssociationDetailElement.java
index 697565e07800dcf182fc2e883cfb2aa549dbfd88..34da746194fc0e6ea975b6606d2b577b1747c203 100644 (file)
@@ -12,31 +12,23 @@ package eu.etaxonomy.taxeditor.ui.section.occurrence.association;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Set;
-import java.util.UUID;
 
-import org.eclipse.core.commands.Command;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.commands.IParameter;
-import org.eclipse.core.commands.NotEnabledException;
-import org.eclipse.core.commands.NotHandledException;
-import org.eclipse.core.commands.Parameterization;
-import org.eclipse.core.commands.ParameterizedCommand;
-import org.eclipse.core.commands.common.NotDefinedException;
+import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.DoubleClickEvent;
 import org.eclipse.jface.viewers.IDoubleClickListener;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.LabelProvider;
-import org.eclipse.jface.viewers.TableViewer;
+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.ui.IWorkbenchWindow;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.commands.ICommandService;
-import org.eclipse.ui.handlers.IHandlerService;
+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;
@@ -47,6 +39,7 @@ import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
+import eu.etaxonomy.taxeditor.view.CdmViewerChooser;
 
 /**
  * @author pplitzner
@@ -69,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()){
-            TableViewer viewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
-            viewer.getTable().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()){
-            TableViewer viewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
-            viewer.getTable().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()){
-            TableViewer viewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
-            viewer.getTable().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);
         }
     }
 
@@ -141,59 +121,25 @@ public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<Deri
     public void doubleClick(DoubleClickEvent event) {
         if(event.getSelection() instanceof IStructuredSelection){
             Object firstElement = ((IStructuredSelection) event.getSelection()).getFirstElement();
-            UUID taxonToOpenUuid = null;
-            if(firstElement instanceof TaxonBase<?>){
-                taxonToOpenUuid = ((TaxonBase<?>)firstElement).getUuid();
-            }
-            else if(firstElement instanceof SpecimenTypeDesignation){
-                //TODO how to open an editor for all typed names?
+            if(firstElement instanceof SpecimenTypeDesignation){
+               SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)firstElement;
+               Set<TaxonNameBase> typifiedNames = typeDesignation.getTypifiedNames();
+               if(typifiedNames.size()==1){
+                       firstElement = typifiedNames.iterator().next();
+               }
+               if(typifiedNames.size()==0 || typifiedNames.size()>=2){
+                       MessagingUtils.informationDialog("Could not open name", "Type designation has either 0 or more than 1 names associated");
+               }
             }
             else if(firstElement instanceof DeterminationEvent){
                 if(((DeterminationEvent) firstElement).getTaxon()!=null){
-                    taxonToOpenUuid = ((DeterminationEvent) firstElement).getTaxon().getUuid();
+                    firstElement = ((DeterminationEvent) firstElement).getTaxon();
                 }
                 else if(((DeterminationEvent) firstElement).getTaxonName()!=null){
-                    //TODO how to open editor for taxon name
-                }
-            }
-            if(taxonToOpenUuid!=null){
-                String commandId = "eu.etaxonomy.taxeditor.editor.openTaxonEditor";
-
-
-                ArrayList parameters = new ArrayList();
-                IParameter iparam = null;
-
-                //get the command from plugin.xml
-                IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-                ICommandService cmdService = (ICommandService)window.getService(ICommandService.class);
-                Command cmd = cmdService.getCommand(commandId);
-
-                //get the parameter
-                try {
-                    iparam = cmd.getParameter("eu.etaxonomy.taxeditor.editor.taxonParameter");
-                } catch (NotDefinedException e1) {
-                    MessagingUtils.error(this.getClass(), "Command not defined", e1);
-                }
-                Parameterization params = new Parameterization(iparam, taxonToOpenUuid.toString());
-                parameters.add(params);
-
-                //build the parameterized command
-                ParameterizedCommand pc = new ParameterizedCommand(cmd, (Parameterization[]) parameters.toArray(new Parameterization[parameters.size()]));
-
-                //execute the command
-                IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class);
-                try {
-                    handlerService.executeCommand(pc, null);
-                } catch (ExecutionException e) {
-                    MessagingUtils.error(TaxonAssociationDetailElement.class, e);
-                } catch (NotDefinedException e) {
-                    MessagingUtils.error(TaxonAssociationDetailElement.class, e);
-                } catch (NotEnabledException e) {
-                    MessagingUtils.error(TaxonAssociationDetailElement.class, e);
-                } catch (NotHandledException e) {
-                    MessagingUtils.error(TaxonAssociationDetailElement.class, e);
+                       firstElement = ((DeterminationEvent) firstElement).getTaxonName();
                 }
             }
+            new CdmViewerChooser(event.getViewer().getControl().getShell()).chooseViewer(firstElement);
         }
     }
 }