Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / description / detail / DistributionDetailElement.java
index 879dad83f8d59d74e90a763be895c3e47f4fa63f..bb8579b15a0bc22eeb546040fe791534e2cc8f4b 100644 (file)
@@ -6,13 +6,14 @@
 * 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.section.description.detail;
 
 import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 
+import org.apache.commons.lang3.StringUtils;
+
 import eu.etaxonomy.cdm.model.description.Distribution;
 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
 import eu.etaxonomy.cdm.model.location.NamedArea;
@@ -21,12 +22,15 @@ import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
 import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
+import eu.etaxonomy.taxeditor.editor.IDistributionEditorPart;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
-import eu.etaxonomy.taxeditor.ui.combo.TermComboElement;
+import eu.etaxonomy.taxeditor.ui.combo.term.TermComboElement;
 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
+import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
 
 /**
@@ -43,6 +47,8 @@ public class DistributionDetailElement extends
        private TermComboElement<DefinedTermBase> combo_presenceAbsence;
        private boolean namedAreaEditable = true;
 
+       private TextWithLabelElement taxonOfDistribtion;
+
 
        /**
         * <p>Constructor for DistributionDetailElement.</p>
@@ -63,16 +69,28 @@ public class DistributionDetailElement extends
        @Override
        protected void createControls(ICdmFormElement formElement,
                        Distribution entity, int style) {
-               selection_namedArea = formFactory.createSelectionElement(NamedArea.class, //getConversationHolder(),
-                               formElement, "Area",
-                               entity.getArea(), EntitySelectionElement.NOTHING, style);
+
+           if (AbstractUtility.getActivePart() instanceof IDistributionEditorPart){
+               String nameCache = null;
+               nameCache = ((IDistributionEditorPart)AbstractUtility.getActivePart()).getEditor().getActualNameCache();
+               taxonOfDistribtion = formFactory.createTextWithLabelElement(formElement, "Taxon", nameCache, style);
+               taxonOfDistribtion.setEnabled(false);
+           }
+
+           if (PreferencesUtil.getBooleanValue(PreferencePredicate.NamedAreaWithIdInVoc.getKey())) {
+            selection_namedArea = formFactory.createSelectionElementWithIdInVocabulary(NamedArea.class,
+                       formElement, "Area",
+                       entity.getArea(), EntitySelectionElement.NOTHING, style);
+        }else{
+            selection_namedArea = formFactory.createSelectionElement(NamedArea.class,
+                    formElement, "Area",
+                    entity.getArea(), EntitySelectionElement.NOTHING, style);
+        }
 
                ICdmFormElement parent = this.getParentElement();
                NamedArea area = entity.getArea();
                List<DefinedTermBase> inputList = getStatusListForArea(area);
         combo_presenceAbsence = formFactory.createDefinedTermComboElement(inputList, this, "Distribution Status", entity.getStatus(), style);
-
-        //combo_presenceAbsence = formFactory.createTermComboElement(PresenceAbsenceTerm.class, this, "Distribution Status", entity.getStatus(), style);
         createTimePeriod(formElement, entity, style);
         if (entity.getFeature().getRecommendedModifierEnumeration() != null && !entity.getFeature().getRecommendedModifierEnumeration().isEmpty()){
             createModifier(formElement, entity, style);
@@ -90,16 +108,18 @@ public class DistributionDetailElement extends
                }
         CdmPreference pref = CdmPreferenceCache.instance().get(key);
         // if there is no preference for the area, look for the general
-        if (pref == null){
+        if (pref == null || pref.isAllowOverride()){
             pref = CdmPreference.NewTaxEditorInstance(PreferencePredicate.AvailableDistributionStatus, PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionStatus.getKey()));
         }
 
         String [] statusArray = null;
-        List<UUID> uuidList = new ArrayList();
+        List<UUID> uuidList = new ArrayList<>();
         if (pref.getValue() != null){
             statusArray = pref.getValue().split(";");
             for (String status: statusArray){
-                uuidList.add(UUID.fromString(status));
+                if (StringUtils.isNotBlank(status)){
+                       uuidList.add(UUID.fromString(status));
+                }
             }
         }
 
@@ -118,8 +138,13 @@ public class DistributionDetailElement extends
 
                if (selection_namedArea.getEntity()!= null){
                    List<DefinedTermBase> inputList = getStatusListForArea(selection_namedArea.getEntity());
-                   //combo_presenceAbsence.removeElements();
-                   combo_presenceAbsence.setTerms(inputList);
+                   DefinedTermBase selectedTerm = combo_presenceAbsence.getSelection();
+                   if (!inputList.equals(combo_presenceAbsence.getTerms())){
+                       combo_presenceAbsence.setTerms(inputList);
+                       if (inputList.contains(selectedTerm)){
+                           combo_presenceAbsence.setSelection(selectedTerm);
+                       }
+                   }
                }
 
                }else if(eventSource == combo_presenceAbsence){
@@ -127,5 +152,8 @@ public class DistributionDetailElement extends
                }
        }
 
+       public void setName(String nameCache){
+           taxonOfDistribtion.setText(nameCache);
+       }
 
 }