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 private final TermType termType
;
46 private final TermVocabulary termVocabulary
;
47 private final Class
<T
> termClass
;
49 private List
<T
> customPreferredTerms
;
51 private boolean addEmptyElement
;
53 public TermComboElement(CdmFormFactory formFactory
,
54 ICdmFormElement parentElement
, TermType termType
, String labelString
, T selection
, boolean addEmptyElement
,
56 this(formFactory
, parentElement
, null, termType
, null, labelString
, selection
, addEmptyElement
, style
);
59 public TermComboElement(CdmFormFactory formFactory
,
60 ICdmFormElement parentElement
, TermVocabulary
<?
> termVocabulary
, String labelString
, T selection
, boolean addEmptyElement
,
62 this(formFactory
, parentElement
, null, null, termVocabulary
, labelString
, selection
, addEmptyElement
, style
);
65 public TermComboElement(CdmFormFactory formFactory
,
66 ICdmFormElement parentElement
, Class
<T
> termClass
, String labelString
, T selection
, boolean addEmptyElement
,
68 this(formFactory
, parentElement
, termClass
, null, null, labelString
, selection
, addEmptyElement
, style
);
71 private TermComboElement(CdmFormFactory formFactory
,
72 ICdmFormElement parentElement
, Class
<T
> termClass
, TermType termType
, TermVocabulary
<?
> termVocabulary
, String labelString
, T selection
, boolean addEmptyElement
,
74 super(formFactory
, parentElement
);
76 this.termType
= termType
;
77 this.termVocabulary
= termVocabulary
;
78 this.termClass
= termClass
;
79 this.addEmptyElement
= addEmptyElement
;
81 if (labelString
!= null) {
82 label
.setText(labelString
);
86 //TODO try to remove generic T and avoid classes to be used
87 populateTerms((List
<T
>) getTermManager().getPreferredTerms(termType
));
89 else if(termVocabulary
!=null){
90 populateTerms((List
<T
>) getTermManager().getPreferredTerms(termVocabulary
));
92 else if(this.termClass
!=null){
93 populateTerms(getPreferredTerms());
96 combo
.addSelectionListener(this);
97 combo
.addDisposeListener(this);
98 PreferencesUtil
.getPreferenceStore().addPropertyChangeListener(this);
100 if (selection
!= null) {
101 setSelection(selection
);
106 * <p>Sets the selection of the combo to the given T object.</p>
107 * <p>Passing <code>null</code> to this method will set the selection to
108 * the empty element and effectively clear the selection</p>
111 * a T object or <code>null</code> to clear the selection
114 public void setSelection(T selection
) {
115 this.selection
= selection
;
117 Listener
[] listeners
= combo
.getListeners(SWT
.Selection
);
119 for (Listener listener
: listeners
) {
120 combo
.removeListener(SWT
.Selection
, listener
);
123 if(selection
== null){
124 // set selection to the emptyElement
127 selectedIndex
= terms
.indexOf(selection
);
128 if (selectedIndex
== -1) {
129 createTermNotInPreferredTerms(selection
);
130 selectedIndex
= terms
.indexOf(selection
);
133 combo
.select(selectedIndex
);
135 for (Listener listener
: listeners
) {
136 combo
.addListener(SWT
.Selection
, listener
);
141 * Fills the combo with elements and sets up the convenience functions
142 * for selection index
144 * @param preferredTerms
146 private void populateTerms(List
<T
> preferredTerms
) {
150 terms
= new ArrayList
<T
>();
156 // Add an empty element for when nothing was selected yet
157 combo
.add(EMPTY_ELEMENT_LABEL
);
158 terms
.add(emptyElement
);
161 if (termComparator
!= null) {
162 Collections
.sort(preferredTerms
, termComparator
);
164 for (T term
: preferredTerms
) {
165 String label
= getLabel(term
);
167 if (term
.getTitleCache() != null) {
168 label
= term
.getTitleCache();
169 MessagingUtils
.warn(getClass(),
170 "Term does not have a default language representation: " + label
171 + ", " + term
.getUuid());
174 MessagingUtils
.warn(getClass(),
175 "Representation Label and TitleCache empty for term: "
176 + term
+ ", " + term
.getUuid());
185 if (selection
!= null) {
186 if (selection
.equals(term
)) {
192 if (selection
!= null && index
== 0) {
193 createTermNotInPreferredTerms(selection
);
199 protected List
<T
> getPreferredTerms(){
200 List
<T
> preferredTerms
= new ArrayList
<T
>();
201 if (customPreferredTerms
!= null){
202 return customPreferredTerms
;
204 else if(termType
!=null){
205 preferredTerms
= getTermManager().getPreferredTerms(termType
);
207 else if(termVocabulary
!=null){
208 preferredTerms
= getTermManager().getPreferredTerms(termVocabulary
);
211 preferredTerms
= getTermManager().getPreferredTerms(termClass
);
213 return preferredTerms
;
217 * May be overridden by derived classes if the desired label string does not
218 * reside in term.getLabel();
222 * @return a {@link java.lang.String} object.
224 protected String
getLabel(T term
) {
228 String termLabel
= term
.getLabel(CdmStore
.getDefaultLanguage());
229 if (termLabel
== null){
230 termLabel
= term
.getLabel();
232 if(PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.SHOW_VOCABULARY_ID_FOR_TERM_LABELS
)
233 && term
.getVocabulary()!=null){
234 String vocLabel
= term
.getVocabulary().getLabel(CdmStore
.getDefaultLanguage());
235 if (vocLabel
== null){
236 vocLabel
= term
.getVocabulary().getLabel();
238 termLabel
+= " ["+vocLabel
+"]";
249 private void createTermNotInPreferredTerms(T term
) {
250 List
<T
> preferredTerms
= getPreferredTerms();
252 preferredTerms
.add(term
);
254 populateTerms(preferredTerms
);
259 public void widgetSelected(SelectionEvent e
) {
260 selection
= terms
.get(combo
.getSelectionIndex());
261 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
266 public void propertyChange(PropertyChangeEvent event
) {
267 super.propertyChange(event
);
269 && PreferencesUtil
.PREFERRED_TERMS_CHANGE
.equals(event
271 populateTerms(getPreferredTerms());
275 protected TermManager
getTermManager() {
276 return CdmStore
.getTermManager();
279 public int getVisibleItemCount(){
280 return combo
.getVisibleItemCount();
284 * <p>A {@link List} of term objects may be passed to this combo box. In this case, the default behaviour
285 * of displaying the preferred terms for the T type will be overridden and the combo will only display the
286 * given terms. Also, any previous selection will be reseted.</p>
288 * <p>To return to the default of displaying the preferred terms, simply pass <code>null</code>.</p>
290 * @param terms a {@link List} of T objects or <code>null</code> for default preferred terms
292 public void setTerms(List
<T
> terms
) {
294 customPreferredTerms
= terms
;
295 populateTerms(customPreferredTerms
);
298 public void removeEmptyElement(){
300 if(terms
.contains(emptyElement
)){
301 terms
.remove(emptyElement
);
303 if(Arrays
.asList(combo
.getItems()).contains(EMPTY_ELEMENT_LABEL
)){
304 combo
.remove(EMPTY_ELEMENT_LABEL
);