AT: commiting latest changes to the Palm Use data extension and merger
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / combo / TermComboElement.java
index ac7e878a55c3ec55f31aca868aa6a235685d229c..17e8eb8e8c7aa2c648ded5fd011eb26c6ca9cd6f 100644 (file)
@@ -63,6 +63,8 @@ public class TermComboElement<T extends DefinedTermBase>
 
        private Class<T> termClass;
 
+       private List<T> customPreferredTerms;
+
 
        /**
         * <p>
@@ -135,28 +137,39 @@ public class TermComboElement<T extends DefinedTermBase>
         *            a T object or <code>null</code> to clear the selection
         */
        public void setSelection(T selection) {                 
+               this.selection = selection;
+               
+               this.selection = selection;
+               
                Listener[] listeners = combo.getListeners(SWT.Selection);
 
                for (Listener listener : listeners) {
                        combo.removeListener(SWT.Selection, listener);
                }
-               int index;
+               int selectedIndex;
                if(selection == null){
-                       index = 0;
+                       // set selection to the emptyElement
+                       selectedIndex = 0;
                }else{
-                       index = terms.indexOf(selection);
-                       if (index == -1) {
+                       selectedIndex = terms.indexOf(selection);
+                       if (selectedIndex == -1) {
                                createTermNotInPreferredTerms(selection);
-                               index = terms.indexOf(selection);
+                               selectedIndex = terms.indexOf(selection);
                        }
                }
-               combo.select(index);
+               combo.select(selectedIndex);
 
                for (Listener listener : listeners) {
                        combo.addListener(SWT.Selection, listener);
                }
        }
 
+       /**
+        * Fills the combo with elements and sets up the convenience functions
+        * for selection index
+        * 
+        * @param preferredTerms
+        */
        private void populateTerms(List<T> preferredTerms) {
 
                combo.removeAll();
@@ -178,9 +191,9 @@ public class TermComboElement<T extends DefinedTermBase>
                        if (label == null) {
                                if (term.getTitleCache() != null) {
                                        label = term.getTitleCache();
-                                       StoreUtil.error(getClass(),
+                                       StoreUtil.warn(getClass(),
                                                        "Term does not have a representation: " + term
-                                                                       + ", " + term.getUuid(), null);
+                                                                       + ", " + term.getUuid());
                                } else {
                                        label = "Unknown";
                                        StoreUtil.error(getClass(),
@@ -227,6 +240,9 @@ public class TermComboElement<T extends DefinedTermBase>
         * @return a {@link java.util.List} object.
         */
        protected List<T> getPreferredTerms(){
+               if (customPreferredTerms != null){
+                       return customPreferredTerms;
+               }
                return getTermManager().getPreferredTerms(termClass);
        }
 
@@ -242,6 +258,11 @@ public class TermComboElement<T extends DefinedTermBase>
                return term.getLabel(CdmStore.getDefaultLanguage());
        }
 
+       /**
+        * 
+        * 
+        * @param term
+        */
        private void createTermNotInPreferredTerms(T term) {
                List<T> preferredTerms = getPreferredTerms();
 
@@ -341,15 +362,34 @@ public class TermComboElement<T extends DefinedTermBase>
                return CdmStore.getTermManager();
        }
        
+       /**
+        * 
+        * @return
+        */
        public int getVisibleItemCount(){
                return combo.getVisibleItemCount();
        }
        
+       /**
+        * 
+        * @param count
+        */
        public void setVisibleItemCount(int count){
                combo.setVisibleItemCount(count);
        }
 
+       /**
+        * <p>A {@link List} of term objects may be passed to this combo box. In this case, the default behaviour
+        * of displaying the preferred terms for the T type will be overridden and the combo will only display the 
+        * given terms. Also, any previous selection will be reseted.</p>
+        * 
+        * <p>To return to the default of displaying the preferred terms, simply pass <code>null</code>.</p>
+        * 
+        * @param terms a {@link List} of T objects or <code>null</code> for default preferred terms
+        */
        public void setTerms(List<T> terms) {
-               populateTerms(terms);
+               setSelection(null);
+               customPreferredTerms = terms;
+               populateTerms(customPreferredTerms);
        }
 }