Merge branch 'release/5.42.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / occurrence / association / TaxonAssociationDetailElement.java
index 4ff63d538b054474d860e11497154cc29f6843fa..8b1f8a543f2101d1a427974d9a43347f12d88593 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
  * Copyright (C) 2014 EDIT
  * European Distributed Institute of Taxonomy
@@ -11,35 +10,30 @@ package eu.etaxonomy.taxeditor.ui.section.occurrence.association;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.Set;
 
-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.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.TableViewer;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.dnd.Clipboard;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
 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.swt.widgets.MenuItem;
+import org.eclipse.ui.forms.widgets.TableWrapLayout;
 
-import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
+import eu.etaxonomy.cdm.facade.DerivedUnitFacade;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 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.name.TaxonName;
+import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.store.CdmStore;
@@ -47,65 +41,113 @@ 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
  * @date Dec 1, 2014
- *
  */
 public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<DerivedUnitFacade> implements IDoubleClickListener{
 
-
-    private TableViewer associationsViewer;
-    private TableViewer typeDesignationViewer;
-
     public TaxonAssociationDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
         super(formFactory, formElement);
     }
 
-    /** {@inheritDoc} */
     @Override
     protected void createControls(ICdmFormElement formElement, DerivedUnitFacade entity, int style) {
+       boolean includeUnpublished = true;
+        Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listIndividualsAssociationTaxa(entity.innerDerivedUnit(), includeUnpublished, null, null, null, null);
+        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);
 
-        //TODO add context menu for deleting associations
+        Collection<CdmBase> associations = new ArrayList<>();
+        associations.addAll(associatedTaxa);
+        associations.addAll(typeDesignations);
+        associations.addAll(determinationEvents);
 
-        Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listAssociatedTaxa(entity.innerDerivedUnit(), null, null, null, null);
-        Collection<SpecimenTypeDesignation> typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(entity.innerDerivedUnit(), null, null, null, null);
+        TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
+        layout.bottomMargin = 0;
+        layout.topMargin = 0;
 
-        if(!associatedTaxa.isEmpty()){
-            associationsViewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
-            associationsViewer.getTable().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
-            associationsViewer.setContentProvider(new ArrayContentProvider());
-            associationsViewer.setInput(associatedTaxa);
-            associationsViewer.addDoubleClickListener(this);
-        }
-        else{
+        if(associatedTaxa.isEmpty() && typeDesignations.isEmpty() && determinationEvents.isEmpty()){
             Label label = formFactory.createLabel(getLayoutComposite(), "No associations");
             label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+            return;
         }
-
-        //TODO implement service method for this which is just used in the label provider
-        Collection<TaxonBase<?>> typedTaxa = new HashSet<TaxonBase<?>>();
-        for (SpecimenTypeDesignation specimenTypeDesignation : typeDesignations) {
-            for (TaxonNameBase taxonNameBase : specimenTypeDesignation.getTypifiedNames()) {
-                Set taxa = taxonNameBase.getTaxa();
-                for (Object taxon : taxa) {
-                    if(taxon instanceof CdmBase && ((CdmBase)taxon).isInstanceOf(TaxonBase.class)){
-                        typedTaxa.add(HibernateProxyHelper.deproxy(taxon, TaxonBase.class));
-                    }
+        for (CdmBase cdmBase : associations) {
+            final StyledText styledTextWidget = new StyledText(getLayoutComposite(), SWT.WRAP);
+            styledTextWidget.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+            styledTextWidget.setText(getLabelText(cdmBase));
+            styledTextWidget.setBackground(getLayoutComposite().getBackground());
+            styledTextWidget.setEditable(false);
+            //Set caret null this will hide caret
+            styledTextWidget.setCaret(null);
+
+            //TODO add context menu for deleting associations
+            //context menu
+            Menu menu = new Menu(styledTextWidget);
+            //copy menu
+            MenuItem copyItem = new MenuItem(menu, SWT.PUSH);
+            copyItem.setText("Copy");
+            copyItem.addSelectionListener(new SelectionListener() {
+
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    Object[] data = new Object[]{styledTextWidget.getText()};
+                    Clipboard clipboard = new Clipboard(styledTextWidget.getDisplay());
+                    clipboard.setContents(data, new Transfer[]{TextTransfer.getInstance()});
+                }
+                @Override
+                public void widgetDefaultSelected(SelectionEvent e) {
                 }
+            });
+            //"Open in..." menu
+            MenuItem openInItem = new MenuItem(menu, SWT.PUSH);
+            openInItem.setText("Open");
+            if(cdmBase.isInstanceOf(SpecimenTypeDesignation.class)){
+               openInItem.setData(HibernateProxyHelper.deproxy(cdmBase, SpecimenTypeDesignation.class));
             }
+            openInItem.setData(cdmBase);
+            openInItem.addSelectionListener(new SelectionListener() {
+
+               @Override
+               public void widgetSelected(SelectionEvent e) {
+                       new CdmViewerChooser(TaxonAssociationDetailElement.this.getLayoutComposite().getShell()).chooseViewer(e.widget.getData());
+               }
+               @Override
+               public void widgetDefaultSelected(SelectionEvent e) {
+               }
+            });
+            styledTextWidget.setMenu(menu);
         }
-        if(!typedTaxa.isEmpty()){
-            Label typeLabel = formFactory.createLabel(getLayoutComposite(), "Type Designations");
-            typeLabel.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
-
-            typeDesignationViewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
-            typeDesignationViewer.getTable().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
-            typeDesignationViewer.setContentProvider(new ArrayContentProvider());
-            typeDesignationViewer.setInput(typedTaxa);
-            typeDesignationViewer.addDoubleClickListener(this);
+    }
+
+    private String getLabelText(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<TaxonName> typifiedNames = typeDesignation.getTypifiedNames();
+            for (TaxonName taxonNameBase : typifiedNames) {
+                label += taxonNameBase+", ";
+            }
+            if(label.endsWith(", ")){
+                label = label.substring(0, label.length()-2);
+            }
+            return label;
+        }
+        return "";
     }
 
     /** {@inheritDoc} */
@@ -118,46 +160,31 @@ public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<Deri
     public void doubleClick(DoubleClickEvent event) {
         if(event.getSelection() instanceof IStructuredSelection){
             Object firstElement = ((IStructuredSelection) event.getSelection()).getFirstElement();
-            if(firstElement instanceof TaxonBase<?>){
-                TaxonBase<?> taxonBase = (TaxonBase<?>)firstElement;
-                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);
+            if(firstElement instanceof SpecimenTypeDesignation){
+               SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)firstElement;
+               Set<TaxonName> 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){
+                    firstElement = ((DeterminationEvent) firstElement).getTaxon();
                 }
-                Parameterization params = new Parameterization(iparam, (taxonBase).getUuid().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);
+                else if(((DeterminationEvent) firstElement).getTaxonName()!=null){
+                       firstElement = ((DeterminationEvent) firstElement).getTaxonName();
                 }
             }
+            new CdmViewerChooser(event.getViewer().getControl().getShell()).chooseViewer(firstElement);
         }
     }
 
+       @Override
+       public void fillFields() {
+               // TODO Auto-generated method stub
+
+       }
 }