commit 4faee5bca667b1fde9551f2ca0f708e69009f292
Author: Katja Luther <k.luther@bgbm.org>
Date:   Wed Jan 5 13:01:32 2022 +0100

    ref #9894: add field primary collector and handle as described in ticket

diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/CollectorSelectionDialog.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/CollectorSelectionDialog.java
index 23b001e45..360de4da3 100755
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/CollectorSelectionDialog.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/CollectorSelectionDialog.java
@@ -8,8 +8,17 @@
 */
 package eu.etaxonomy.taxeditor.ui.dialog.selection;
 
+import java.lang.reflect.Member;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+
 import org.eclipse.jface.viewers.ILabelProvider;
 import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.swt.internal.win32.TCHITTESTINFO;
 import org.eclipse.swt.widgets.Shell;
 
 import eu.etaxonomy.cdm.api.service.IAgentService;
@@ -17,6 +26,7 @@ import eu.etaxonomy.cdm.api.service.dto.EntityDTOBase;
 import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
 import eu.etaxonomy.cdm.model.agent.AgentBase;
 import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.agent.Team;
 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
 import eu.etaxonomy.cdm.persistence.dto.TeamOrPersonUuidAndTitleCache;
@@ -29,6 +39,7 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
  * @since Jul 1, 2021
  */
 public class CollectorSelectionDialog extends AgentSelectionDialog<AgentBase<?>> {
+	Team collectorTeam = null;
 
     /**
      * @param shell
@@ -41,10 +52,15 @@ public class CollectorSelectionDialog extends AgentSelectionDialog<AgentBase<?>>
     protected CollectorSelectionDialog(Shell shell, String title, boolean multi, String settings,
             AgentBase<?> agent, boolean selectTeamMember) {
         super(shell, title, multi, settings, agent, selectTeamMember);
+        if (selectTeamMember && agent instanceof Team){
+    		this.collectorTeam = (Team)agent;
+    		
+    	}
 
     }
     public static <S extends AgentBase> S select(Shell shell,
             S entity, boolean selectTeamMember) {
+    	
         CollectorSelectionDialog dialog = new CollectorSelectionDialog(shell,
                 "Choose Collector", false, CollectorSelectionDialog.class.getCanonicalName(), entity, selectTeamMember);
         return  (S) getSelectionFromDialog(dialog);
@@ -93,8 +109,28 @@ public class CollectorSelectionDialog extends AgentSelectionDialog<AgentBase<?>>
         if (selectTeamMember){
             clazz = Person.class;
         }
-
+        Set<UUID> memberUuids = new HashSet<>();
+        if (collectorTeam != null && selectTeamMember){
+        	List<Person> teamMembers = this.collectorTeam.getTeamMembers();
+        	
+            teamMembers.forEach(member -> memberUuids.add(member.getUuid()));
+        }
+        
         model = CdmStore.getService(IAgentService.class).getUuidAndTitleCacheWithCollectorTitleCache(clazz, limitOfInitialElements, pattern);
+        if (collectorTeam == null){
+        	return;
+        }
+        //filter
+        Iterator<UuidAndTitleCache<AgentBase<?>>> modelIterator = model.iterator();
+        List<UuidAndTitleCache<AgentBase<?>>> tempModel = new ArrayList<>();
+        while(modelIterator.hasNext()){
+        	UuidAndTitleCache<AgentBase<?>> person = modelIterator.next();
+        	if (memberUuids.contains(person.getUuid())){
+        		tempModel.add(person);
+        	}
+        }
+        model.clear();
+        model.addAll(tempModel);
     }
 
     @Override
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/SelectionDialogFactory.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/SelectionDialogFactory.java
index d276f5f83..c64289154 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/SelectionDialogFactory.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/SelectionDialogFactory.java
@@ -61,6 +61,7 @@ import eu.etaxonomy.taxeditor.ui.section.reference.NomenclaturalSourceDetailElem
 import eu.etaxonomy.taxeditor.ui.section.reference.ReferenceDetailElement;
 import eu.etaxonomy.taxeditor.ui.section.user.GroupsByUserDetailElement;
 import eu.etaxonomy.taxeditor.ui.section.user.GroupsByUserDetailSection;
+import eu.etaxonomy.taxeditor.ui.selection.CollectorSelectionElement;
 
 /**
  * @author n.hoffmann
@@ -122,6 +123,9 @@ public class SelectionDialogFactory {
         }else if(clazz.equals(AgentBase.class) && (parentElement instanceof FieldUnitFacadeGeneralDetailElement || parentElement instanceof DerivedUnitGeneralDetailElement)){
 		    return (T) CollectorSelectionDialog.select(shell,
 		            (TeamOrPersonBase) currentSelection, false);
+		}else if (clazz.equals(AgentBase.class) && parentElement instanceof CollectorSelectionElement){
+			return (T) CollectorSelectionDialog.select(shell,
+		            (TeamOrPersonBase) ((CollectorSelectionElement)parentElement).getCollectorTeam(), true);
 		}else if(clazz.equals(TeamOrPersonBase.class)){
             //TODO: add TeamOrPersonBaseSelectionDialog (see ticket #4545)
             return (T) TeamOrPersonBaseSelectionDialog.select(shell,
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/CdmFormFactory.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/CdmFormFactory.java
index e83696d8e..b1ed7d73a 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/CdmFormFactory.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/CdmFormFactory.java
@@ -415,7 +415,6 @@ import eu.etaxonomy.taxeditor.ui.selection.CommonNameReferenceSelectionElement;
 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElementWithAbbreviatedTitle;
 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElementWithIdInVocabulary;
-import eu.etaxonomy.taxeditor.ui.selection.NomenclaturalAuthorTeamSelectionElement;
 import eu.etaxonomy.taxeditor.ui.selection.TaxonNodeSelectionElement;
 import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
 
@@ -3300,7 +3299,16 @@ public class CdmFormFactory extends FormToolkit {
       adapt(element);
       parentElement.addElement(element);
       return element;
-  }
+    }
+    
+    public CollectorSelectionElement createCollectorSelectionElement(ICdmFormElement parentElement, String labelString, AgentBase team, Person member, int mode,
+            int style) {
+      	CollectorSelectionElement element = new CollectorSelectionElement(this, parentElement, 
+                labelString, team, member, mode, style);
+        adapt(element);
+        parentElement.addElement(element);
+        return element;
+      }
 
     public CommonNameReferenceSelectionElement createCommonNameReferenceSelectionElement(ICdmFormElement parentElement, String labelString, Reference selection, int mode,
           int style) {
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/DerivedUnitGeneralDetailElement.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/DerivedUnitGeneralDetailElement.java
index 387a37eee..caec73529 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/DerivedUnitGeneralDetailElement.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/DerivedUnitGeneralDetailElement.java
@@ -11,6 +11,8 @@ package eu.etaxonomy.taxeditor.ui.section.occurrence;
 
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
 import eu.etaxonomy.cdm.model.agent.AgentBase;
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.agent.Team;
 import eu.etaxonomy.cdm.model.common.LanguageString;
 import eu.etaxonomy.cdm.model.location.NamedArea;
 import eu.etaxonomy.cdm.model.occurrence.Collection;
@@ -32,6 +34,7 @@ import eu.etaxonomy.taxeditor.ui.element.TimePeriodElement;
 import eu.etaxonomy.taxeditor.ui.element.ToggleableTextElement;
 import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
+import eu.etaxonomy.taxeditor.ui.selection.CollectorSelectionElement;
 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
 
 /**
@@ -54,7 +57,8 @@ public class DerivedUnitGeneralDetailElement extends
 	private PointElement element_point;
 	private NumberWithLabelElement number_elevation;
 	private TimePeriodElement element_date;
-	private EntitySelectionElement<AgentBase> selection_collector;
+	private CollectorSelectionElement selection_collector;
+	private CollectorSelectionElement selection_primary_collector;
 	private TextWithLabelElement text_collectingNumber;
 	private EntitySelectionElement<Collection> selection_collection;
 	private TextWithLabelElement text_accessionNumber;
@@ -103,6 +107,14 @@ public class DerivedUnitGeneralDetailElement extends
 		                    formElement, "Collector",
 		                    entity.getCollector(), EntitySelectionElement.ALL,
 		                    style);
+		    selection_primary_collector = formFactory
+		            .createCollectorSelectionElement(
+		                    formElement, "Primary collector",
+		                    entity.getCollector(), entity.getPrimaryCollector(), EntitySelectionElement.ALL,
+		                    style);
+	        if (!(entity.getCollector() instanceof Team)){
+		    	selection_primary_collector.setEnabled(false);
+		    }
 		    text_collectingNumber = formFactory.createTextWithLabelElement(formElement,
 		            "Collecting number", entity.getFieldNumber(), style);
 		}
@@ -138,7 +150,14 @@ public class DerivedUnitGeneralDetailElement extends
 			getEntity().setGatheringPeriod(element_date.getTimePeriod());
 		} else if (eventSource == selection_collector) {
 			getEntity().setCollector(selection_collector.getSelection());
-		} else if (eventSource == text_collectingNumber) {
+			 getEntity().setCollector(selection_collector.getSelection());
+	         if (getEntity().getCollector() instanceof Team){
+	        	 selection_primary_collector.setCollectorTeam((Team)getEntity().getCollector());
+	         }
+			selection_primary_collector.setEnabled(getEntity().getCollector() instanceof Team);
+		} else if (eventSource == selection_primary_collector) {
+			getEntity().setPrimaryCollector((Person)selection_primary_collector.getSelection());
+		}else if (eventSource == text_collectingNumber) {
 			getEntity().setFieldNumber(text_collectingNumber.getText());
 		}
 		  else if (eventSource == selection_collection) {
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/FieldUnitFacadeGeneralDetailElement.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/FieldUnitFacadeGeneralDetailElement.java
index c58b0e262..77a5ee61c 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/FieldUnitFacadeGeneralDetailElement.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/FieldUnitFacadeGeneralDetailElement.java
@@ -8,12 +8,19 @@
  */
 package eu.etaxonomy.taxeditor.ui.section.occurrence;
 
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
 import eu.etaxonomy.cdm.model.agent.AgentBase;
+import eu.etaxonomy.cdm.model.agent.Team;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.LanguageString;
 import eu.etaxonomy.cdm.model.location.NamedArea;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 import eu.etaxonomy.taxeditor.store.StoreUtil;
 import eu.etaxonomy.taxeditor.ui.combo.EnumComboElement;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
@@ -47,6 +54,7 @@ public class FieldUnitFacadeGeneralDetailElement extends AbstractCdmDetailElemen
     private NumberWithLabelElement number_elevation;
     private TimePeriodElement element_date;
     private CollectorSelectionElement selection_collector;
+    private CollectorSelectionElement selection_primary_collector;
     private TextWithLabelElement text_collectingNumber;
     private GatheringEventUnitElement element_elevation;
     private CheckboxElement checkIsPublish;
@@ -81,6 +89,14 @@ public class FieldUnitFacadeGeneralDetailElement extends AbstractCdmDetailElemen
 
         element_date = formFactory.createTimePeriodElement(formElement, "Date", entity.getGatheringPeriod(), style);
         selection_collector = formFactory.createCollectorSelectionElement(formElement, "Collector", entity.getCollector(), EntitySelectionElement.ALL, style);
+        selection_primary_collector = formFactory
+	            .createCollectorSelectionElement(
+	                    formElement, "Primary collector",
+	                    entity.getCollector(), entity.getPrimaryCollector(), EntitySelectionElement.ALL,
+	                    style);
+        if (!(entity.getCollector() instanceof Team)){
+	    	selection_primary_collector.setEnabled(false);
+	    }
         text_collectingNumber = formFactory.createTextWithLabelElement(formElement, "Collecting number",
                 entity.getFieldNumber(), style);
         checkIsPublish = formFactory.createCheckbox(formElement, "Publish", entity.innerFieldUnit().isPublish(), style);
@@ -104,6 +120,16 @@ public class FieldUnitFacadeGeneralDetailElement extends AbstractCdmDetailElemen
             getEntity().setGatheringPeriod(element_date.getTimePeriod());
         } else if (eventSource == selection_collector) {
             getEntity().setCollector(selection_collector.getSelection());
+            
+            if (getEntity().getCollector() instanceof Team){
+            	selection_primary_collector.setCollectorTeam((Team)getEntity().getCollector());
+            }else{
+            	selection_primary_collector.setEntity(null);
+            }
+            selection_primary_collector.setEnabled((getEntity().getCollector() instanceof Team));
+            
+        } else if (eventSource == selection_primary_collector) {
+            getEntity().setCollector(selection_primary_collector.getSelection());
         } else if (eventSource == text_collectingNumber) {
             getEntity().setFieldNumber(text_collectingNumber.getText());
         } else if (eventSource == combo_specorobstype) {
@@ -115,4 +141,18 @@ public class FieldUnitFacadeGeneralDetailElement extends AbstractCdmDetailElemen
         toggleableText_titleCache.setText(getEntity().getTitleCache());
 
     }
+    protected void updateControlStates(){
+        if (getEntity() != null ){
+            enabled = enabled && CdmStore.currentAuthentiationHasPermission(StoreUtil.getCdmEntity(getEntity()), requiredCrud);
+        }else{
+            enabled = true;
+        }
+        Set<Object> except = new HashSet<>(); 
+        if (!selection_primary_collector.isEnabled() && enabled){
+        	except.add(selection_primary_collector);
+        }
+        setEnabled(enabled, except);
+        
+    }
+
 }
\ No newline at end of file
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/selection/CollectorSelectionElement.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/selection/CollectorSelectionElement.java
index 7c0edde33..95cb2b600 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/selection/CollectorSelectionElement.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/selection/CollectorSelectionElement.java
@@ -1,13 +1,21 @@
 package eu.etaxonomy.taxeditor.ui.selection;
 
+import org.eclipse.swt.events.SelectionEvent;
+
 import eu.etaxonomy.cdm.model.agent.AgentBase;
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.agent.Team;
 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.taxeditor.store.StoreUtil;
+import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
 
 public class CollectorSelectionElement extends EntitySelectionElement<AgentBase> {
+	
+	Team collectorTeam;
 	/**
      * @param formFactory
      * @param conversation
@@ -24,6 +32,15 @@ public class CollectorSelectionElement extends EntitySelectionElement<AgentBase>
         parentElement, AgentBase.class,
         labelString, entity, mode, style);
     }
+    public CollectorSelectionElement(CdmFormFactory formFactory,// ConversationHolder conversation,
+            ICdmFormElement parentElement, String labelString, AgentBase entity, Person primaryCollector, int mode, int style) {
+    	super(formFactory, //conversation,
+        parentElement, AgentBase.class,
+        labelString, primaryCollector, mode, style);
+    	if (entity instanceof Team){
+    		collectorTeam = (Team)entity;
+    	}
+    }
 
     @Override
     protected String getTitle() {
@@ -36,4 +53,27 @@ public class CollectorSelectionElement extends EntitySelectionElement<AgentBase>
 		}
 		return "";
 	}
+	public Team getCollectorTeam() {
+		return collectorTeam;
+	}
+	public void setCollectorTeam(Team collectorTeam) {
+		this.collectorTeam = collectorTeam;
+	}
+	
+	public void widgetSelected(SelectionEvent e) {
+	    if (e.getSource().equals(button_selection) ){
+	    	AgentBase selection = null;
+	    	if (this.collectorTeam != null){
+	    		selection = SelectionDialogFactory.getSelectionFromDialog(AgentBase.class, getShell(),
+		                collectorTeam, this);
+	    	}else{	    	
+	    		selection = SelectionDialogFactory.getSelectionFromDialog(AgentBase.class, getShell(),
+	                getEntity(), getParentElement());
+	    	}
+            setSelectionInternal(selection);
+            if(!getLayoutComposite().isDisposed()){
+                StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
+            }
+	    }
+	}
 }
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/selection/NomenclaturalAuthorTeamSelectionElement.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/selection/NomenclaturalAuthorTeamSelectionElement.java
deleted file mode 100644
index 4602c051d..000000000
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/selection/NomenclaturalAuthorTeamSelectionElement.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/**
- * Copyright (C) 2007 EDIT
- * European Distributed Institute of Taxonomy
- * http://www.e-taxonomy.eu
- *
- * The contents of this file are subject to the Mozilla Public License Version 1.1
- * See LICENSE.TXT at the top of this package for the full license terms.
- */
-
-package eu.etaxonomy.taxeditor.ui.selection;
-
-import org.eclipse.swt.events.SelectionEvent;
-
-import eu.etaxonomy.cdm.model.agent.Team;
-import eu.etaxonomy.taxeditor.ui.dialog.selection.NomenclaturalAuthorTeamSelectionDialog;
-import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
-import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
-
-/**
- * <p>
- * NomenclaturalAuthorTeamSelectionElement class.
- * </p>
- *
- * @author n.hoffmann
- * @created Jun 22, 2010
- * @version 1.0
- */
-public class NomenclaturalAuthorTeamSelectionElement extends
-		EntitySelectionElement<Team> {
-
-	public static final int DEFAULT = EDITABLE | SELECTABLE | DELETABLE;
-
-	/**
-	 * <p>
-	 * Constructor for NomenclaturalAuthorTeamSelectionElement.
-	 * </p>
-	 *
-	 * @param formFactory
-	 *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
-	 *            object.
-	 * @param conversation
-	 *            a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
-	 *            object.
-	 * @param parentElement
-	 *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
-	 *            object.
-	 * @param labelString
-	 *            a {@link java.lang.String} object.
-	 * @param entity
-	 *            a {@link eu.etaxonomy.cdm.model.agent.Team} object.
-	 * @param style
-	 *            a int.
-	 */
-	public NomenclaturalAuthorTeamSelectionElement(CdmFormFactory formFactory,//ConversationHolder conversation,
-	        ICdmFormElement parentElement,
-			String labelString, Team entity, int mode, int style) {
-		super(formFactory,// conversation,
-		        parentElement, Team.class, labelString, entity,
-				mode, style);
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	protected String getTitle() {
-		return (entity != null) ? entity.getNomenclaturalTitleCache() : "";
-	}
-
-	/** {@inheritDoc} */
-	@Override
-	public void widgetSelected(SelectionEvent e) {
-		Team newSelection = NomenclaturalAuthorTeamSelectionDialog.select(
-				getShell(), //getConversationHolder(),
-				entity);
-		setSelectionInternal(newSelection);
-	}
-}
