ref #7040 Fix addition of empty element in combos
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / combo / TermComboElement.java
index c2f5a806fef4c5ae3996bae2492e8478bffbb5bf..7bd6f3b71497dd9de4618b301d626ad173d26147 100644 (file)
@@ -42,6 +42,18 @@ public class TermComboElement<T extends DefinedTermBase>
 
        private Comparator<T> termComparator;
 
+       public Comparator<T> getTermComparator() {
+               return termComparator;
+       }
+
+       public void setTermComparator(Comparator<T> termComparator) {
+               this.termComparator = termComparator;
+               List<T> termsWithoutNull = terms.subList(1, terms.size());
+
+               populateTerms(termsWithoutNull);
+
+       }
+
        private final TermType termType;
        private final TermVocabulary termVocabulary;
        private final Class<T> termClass;
@@ -53,31 +65,31 @@ public class TermComboElement<T extends DefinedTermBase>
 
        public TermComboElement(CdmFormFactory formFactory,
                        ICdmFormElement parentElement, TermType termType, String labelString, T selection, boolean addEmptyElement,
-                       int style, boolean useAbbrevLabel) {
-               this(formFactory, parentElement, null, termType, null, labelString, selection, addEmptyElement, style, useAbbrevLabel);
+                       int style, boolean useAbbrevLabel, Comparator<T> comparator) {
+               this(formFactory, parentElement, null, termType, null, labelString, selection, addEmptyElement, style, useAbbrevLabel, comparator);
        }
 
        public TermComboElement(CdmFormFactory formFactory,
                ICdmFormElement parentElement, TermVocabulary<?> termVocabulary, String labelString, T selection, boolean addEmptyElement,
-               int style) {
-           this(formFactory, parentElement, null, null, termVocabulary, labelString, selection, addEmptyElement, style, false);
+               int style, boolean useAbbrevLabel, Comparator<T> comparator) {
+           this(formFactory, parentElement, null, null, termVocabulary, labelString, selection, addEmptyElement, style, useAbbrevLabel, comparator);
        }
 
     public TermComboElement(CdmFormFactory formFactory,
             ICdmFormElement parentElement, Class<T> termClass, String labelString, T selection, boolean addEmptyElement,
             int style) {
-        this(formFactory, parentElement, termClass, null, null, labelString, selection, addEmptyElement, style, false);
+        this(formFactory, parentElement, termClass, null, null, labelString, selection, addEmptyElement, style, false, null);
     }
     public TermComboElement(CdmFormFactory formFactory,
             ICdmFormElement parentElement, Class<T> termClass, String labelString, T selection, boolean addEmptyElement,
             int style, boolean useAbbrevLabel) {
-        this(formFactory, parentElement, termClass, null, null, labelString, selection, addEmptyElement, style, useAbbrevLabel);
-       
+        this(formFactory, parentElement, termClass, null, null, labelString, selection, addEmptyElement, style, useAbbrevLabel, null);
+
     }
 
        private TermComboElement(CdmFormFactory formFactory,
                ICdmFormElement parentElement, Class<T> termClass, TermType termType, TermVocabulary<?> termVocabulary, String labelString, T selection, boolean addEmptyElement,
-               int style, boolean useAbbrevLabel) {
+               int style, boolean useAbbrevLabel, Comparator<T> comparator) {
         super(formFactory, parentElement);
 
         this.termType = termType;
@@ -85,6 +97,7 @@ public class TermComboElement<T extends DefinedTermBase>
         this.termClass = termClass;
         this.addEmptyElement = addEmptyElement;
         this.useAbbrevLabel = useAbbrevLabel;
+        this.termComparator = comparator;
         if (labelString != null) {
             label.setText(labelString);
         }
@@ -159,15 +172,12 @@ public class TermComboElement<T extends DefinedTermBase>
                int i = 1;
                int index = 0;
 
-               if(addEmptyElement){
-                   // Add an empty element for when nothing was selected yet
-                   combo.add(EMPTY_ELEMENT_LABEL);
-                   terms.add(emptyElement);
-               }
-
                if (termComparator != null) {
                        Collections.sort(preferredTerms, termComparator);
                }
+               terms.addAll(preferredTerms);
+
+               List<String> labels = new ArrayList<>();
                for (T term : preferredTerms) {
                        String label = getLabel(term);
                        if (label == null) {
@@ -185,8 +195,7 @@ public class TermComboElement<T extends DefinedTermBase>
 
                        }
 
-                       combo.add(label);
-                       terms.add(term);
+                       labels.add(label);
 
                        i++;
                        if (selection != null) {
@@ -196,11 +205,21 @@ public class TermComboElement<T extends DefinedTermBase>
                        }
                }
 
-               if (selection != null && index == 0) {
+               String[] items = labels.toArray(new String[0]);
+        combo.setItems(items);
+
+        if(addEmptyElement){
+            // Add an empty element for when nothing was selected yet
+            combo.add(EMPTY_ELEMENT_LABEL, 0);
+            terms.add(0, emptyElement);
+        }
+
+        if (selection != null && index == 0) {
                        createTermNotInPreferredTerms(selection);
                }
 
                combo.select(index);
+
        }
 
        protected List<T> getPreferredTerms(){