4 package eu
.etaxonomy
.taxeditor
.ui
.combo
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Arrays
;
8 import java
.util
.Collections
;
9 import java
.util
.Comparator
;
10 import java
.util
.List
;
12 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.events
.SelectionEvent
;
15 import org
.eclipse
.swt
.widgets
.Listener
;
17 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
18 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
19 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
20 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
21 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
22 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
23 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
24 import eu
.etaxonomy
.taxeditor
.store
.TermManager
;
25 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
26 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
27 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
31 * @created Nov 5, 2009
35 public class TermComboElement
<T
extends DefinedTermBase
>
36 extends AbstractComboElement
<T
> {
38 private T emptyElement
;
39 private static String EMPTY_ELEMENT_LABEL
= "";
41 private ArrayList
<T
> terms
;
43 private Comparator
<T
> termComparator
;
45 public Comparator
<T
> getTermComparator() {
46 return termComparator
;
49 public void setTermComparator(Comparator
<T
> termComparator
) {
50 this.termComparator
= termComparator
;
51 List
<T
> termsWithoutNull
= terms
.subList(1, terms
.size());
53 populateTerms(termsWithoutNull
);
57 private final TermType termType
;
58 private final TermVocabulary termVocabulary
;
59 private final Class
<T
> termClass
;
61 private List
<T
> customPreferredTerms
;
63 private boolean useAbbrevLabel
= false;
64 private boolean addEmptyElement
;
66 public TermComboElement(CdmFormFactory formFactory
,
67 ICdmFormElement parentElement
, TermType termType
, String labelString
, T selection
, boolean addEmptyElement
,
68 int style
, boolean useAbbrevLabel
, Comparator
<T
> comparator
) {
69 this(formFactory
, parentElement
, null, termType
, null, labelString
, selection
, addEmptyElement
, style
, useAbbrevLabel
, comparator
);
72 public TermComboElement(CdmFormFactory formFactory
,
73 ICdmFormElement parentElement
, TermVocabulary
<?
> termVocabulary
, String labelString
, T selection
, boolean addEmptyElement
,
74 int style
, boolean useAbbrevLabel
, Comparator
<T
> comparator
) {
75 this(formFactory
, parentElement
, null, null, termVocabulary
, labelString
, selection
, addEmptyElement
, style
, useAbbrevLabel
, comparator
);
78 public TermComboElement(CdmFormFactory formFactory
,
79 ICdmFormElement parentElement
, Class
<T
> termClass
, String labelString
, T selection
, boolean addEmptyElement
,
81 this(formFactory
, parentElement
, termClass
, null, null, labelString
, selection
, addEmptyElement
, style
, false, null);
83 public TermComboElement(CdmFormFactory formFactory
,
84 ICdmFormElement parentElement
, Class
<T
> termClass
, String labelString
, T selection
, boolean addEmptyElement
,
85 int style
, boolean useAbbrevLabel
) {
86 this(formFactory
, parentElement
, termClass
, null, null, labelString
, selection
, addEmptyElement
, style
, useAbbrevLabel
, null);
90 private TermComboElement(CdmFormFactory formFactory
,
91 ICdmFormElement parentElement
, Class
<T
> termClass
, TermType termType
, TermVocabulary
<?
> termVocabulary
, String labelString
, T selection
, boolean addEmptyElement
,
92 int style
, boolean useAbbrevLabel
, Comparator
<T
> comparator
) {
93 super(formFactory
, parentElement
);
95 this.termType
= termType
;
96 this.termVocabulary
= termVocabulary
;
97 this.termClass
= termClass
;
98 this.addEmptyElement
= addEmptyElement
;
99 this.useAbbrevLabel
= useAbbrevLabel
;
100 this.termComparator
= comparator
;
101 if (labelString
!= null) {
102 label
.setText(labelString
);
106 //TODO try to remove generic T and avoid classes to be used
107 populateTerms((List
<T
>) getTermManager().getPreferredTerms(termType
));
109 else if(termVocabulary
!=null){
110 populateTerms((List
<T
>) getTermManager().getPreferredTerms(termVocabulary
));
112 else if(this.termClass
!=null){
113 populateTerms(getPreferredTerms());
116 combo
.addSelectionListener(this);
117 combo
.addDisposeListener(this);
118 PreferencesUtil
.getPreferenceStore().addPropertyChangeListener(this);
120 if (selection
!= null) {
121 setSelection(selection
);
126 * <p>Sets the selection of the combo to the given T object.</p>
127 * <p>Passing <code>null</code> to this method will set the selection to
128 * the empty element and effectively clear the selection</p>
131 * a T object or <code>null</code> to clear the selection
134 public void setSelection(T selection
) {
135 this.selection
= selection
;
137 Listener
[] listeners
= combo
.getListeners(SWT
.Selection
);
139 for (Listener listener
: listeners
) {
140 combo
.removeListener(SWT
.Selection
, listener
);
143 if(selection
== null){
144 // set selection to the emptyElement
147 selectedIndex
= terms
.indexOf(selection
);
148 if (selectedIndex
== -1) {
149 createTermNotInPreferredTerms(selection
);
150 selectedIndex
= terms
.indexOf(selection
);
153 combo
.select(selectedIndex
);
155 for (Listener listener
: listeners
) {
156 combo
.addListener(SWT
.Selection
, listener
);
161 * Fills the combo with elements and sets up the convenience functions
162 * for selection index
164 * @param preferredTerms
166 private void populateTerms(List
<T
> preferredTerms
) {
170 terms
= new ArrayList
<T
>();
176 // Add an empty element for when nothing was selected yet
177 combo
.add(EMPTY_ELEMENT_LABEL
);
178 terms
.add(emptyElement
);
181 if (termComparator
!= null) {
182 Collections
.sort(preferredTerms
, termComparator
);
184 for (T term
: preferredTerms
) {
185 String label
= getLabel(term
);
187 if (term
.getTitleCache() != null) {
188 label
= term
.getTitleCache();
189 MessagingUtils
.warn(getClass(),
190 "Term does not have a default language representation: " + label
191 + ", " + term
.getUuid());
194 MessagingUtils
.warn(getClass(),
195 "Representation Label and TitleCache empty for term: "
196 + term
+ ", " + term
.getUuid());
205 if (selection
!= null) {
206 if (selection
.equals(term
)) {
212 if (selection
!= null && index
== 0) {
213 createTermNotInPreferredTerms(selection
);
220 protected List
<T
> getPreferredTerms(){
221 List
<T
> preferredTerms
= new ArrayList
<T
>();
222 if (customPreferredTerms
!= null){
223 return customPreferredTerms
;
225 else if(termType
!=null){
226 preferredTerms
= getTermManager().getPreferredTerms(termType
);
228 else if(termVocabulary
!=null){
229 preferredTerms
= getTermManager().getPreferredTerms(termVocabulary
);
232 preferredTerms
= getTermManager().getPreferredTerms(termClass
);
234 return preferredTerms
;
238 * May be overridden by derived classes if the desired label string does not
239 * reside in term.getLabel();
243 * @return a {@link java.lang.String} object.
245 protected String
getLabel(T term
) {
249 String termLabel
= null;
251 termLabel
= term
.getIdInVocabulary();
253 termLabel
= term
.getLabel(CdmStore
.getDefaultLanguage());
255 if (termLabel
== null){
256 termLabel
= term
.getLabel();
258 if(PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOW_VOCABULARY_ID_FOR_TERM_LABELS
)
259 && term
.getVocabulary()!=null){
260 String vocLabel
= term
.getVocabulary().getLabel(CdmStore
.getDefaultLanguage());
261 if (vocLabel
== null){
262 vocLabel
= term
.getVocabulary().getLabel();
264 termLabel
+= " ["+vocLabel
+"]";
275 private void createTermNotInPreferredTerms(T term
) {
276 List
<T
> preferredTerms
= getPreferredTerms();
278 preferredTerms
.add(term
);
280 populateTerms(preferredTerms
);
285 public void widgetSelected(SelectionEvent e
) {
286 selection
= terms
.get(combo
.getSelectionIndex());
287 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
292 public void propertyChange(PropertyChangeEvent event
) {
293 super.propertyChange(event
);
295 && PreferencesUtil
.PREFERRED_TERMS_CHANGE
.equals(event
297 populateTerms(getPreferredTerms());
301 protected TermManager
getTermManager() {
302 return CdmStore
.getTermManager();
305 public int getVisibleItemCount(){
306 return combo
.getVisibleItemCount();
310 * <p>A {@link List} of term objects may be passed to this combo box. In this case, the default behaviour
311 * of displaying the preferred terms for the T type will be overridden and the combo will only display the
312 * given terms. Also, any previous selection will be reseted.</p>
314 * <p>To return to the default of displaying the preferred terms, simply pass <code>null</code>.</p>
316 * @param terms a {@link List} of T objects or <code>null</code> for default preferred terms
318 public void setTerms(List
<T
> terms
) {
320 customPreferredTerms
= terms
;
321 populateTerms(customPreferredTerms
);
324 public void removeEmptyElement(){
326 if(terms
.contains(emptyElement
)){
327 terms
.remove(emptyElement
);
329 if(Arrays
.asList(combo
.getItems()).contains(EMPTY_ELEMENT_LABEL
)){
330 combo
.remove(EMPTY_ELEMENT_LABEL
);