4 package eu
.etaxonomy
.taxeditor
.ui
.combo
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Collections
;
8 import java
.util
.Comparator
;
11 import org
.eclipse
.jface
.util
.PropertyChangeEvent
;
12 import org
.eclipse
.swt
.SWT
;
13 import org
.eclipse
.swt
.events
.DisposeEvent
;
14 import org
.eclipse
.swt
.events
.DisposeListener
;
15 import org
.eclipse
.swt
.events
.SelectionEvent
;
16 import org
.eclipse
.swt
.events
.SelectionListener
;
17 import org
.eclipse
.swt
.graphics
.Color
;
18 import org
.eclipse
.swt
.widgets
.Combo
;
19 import org
.eclipse
.swt
.widgets
.Label
;
20 import org
.eclipse
.swt
.widgets
.Listener
;
22 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTerm
;
23 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
24 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
25 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
26 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
27 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
28 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
29 import eu
.etaxonomy
.taxeditor
.store
.TermManager
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
40 * Abstract AbstractTermComboElement class.
44 * @created Nov 5, 2009
48 public class TermComboElement
<T
extends DefinedTermBase
>
49 extends AbstractCdmFormElement
implements SelectionListener
,
50 DisposeListener
, IEnableableFormElement
, ISelectable
{
52 private static final int DEFAULT_VISIBLE_ITEMS
= 10;
56 private T emptyElement
;
57 private static String EMPTY_ELEMENT_LABEL
= "";
59 protected Label label
;
62 private ArrayList
<T
> terms
;
64 private Comparator
<T
> termComparator
;
66 private Class
<T
> termClass
;
68 private List
<T
> customPreferredTerms
;
72 * Constructor for AbstractTermComboElement.
75 * @param parentElement
76 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
81 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
84 * a {@link java.lang.String} object.
92 public TermComboElement(CdmFormFactory formFactory
,
93 ICdmFormElement parentElement
, TermType termType
, String labelString
, T selection
,
95 super(formFactory
, parentElement
);
97 this.termClass
= (Class
<T
>) DefinedTerm
.class;
99 if (labelString
!= null) {
100 label
= formFactory
.createLabel(getLayoutComposite(), labelString
);
105 combo
= new Combo(getLayoutComposite(), SWT
.BORDER
| SWT
.READ_ONLY
);
107 combo
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
108 combo
.setVisibleItemCount(DEFAULT_VISIBLE_ITEMS
);
110 populateTerms(filterDefinedTermByTermType(getPreferredTerms(), termType
));
112 combo
.addSelectionListener(this);
113 combo
.addDisposeListener(this);
114 PreferencesUtil
.getPreferenceStore().addPropertyChangeListener(this);
116 if (selection
!= null) {
117 setSelection(selection
);
123 * Constructor for AbstractTermComboElement.
126 * @param parentElement
127 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
132 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
135 * a {@link java.lang.String} object.
138 * @param visibleItems
143 public TermComboElement(CdmFormFactory formFactory
,
144 ICdmFormElement parentElement
, Class
<T
> termClass
, String labelString
, T selection
,
146 super(formFactory
, parentElement
);
148 this.termClass
= termClass
;
150 if (labelString
!= null) {
151 label
= formFactory
.createLabel(getLayoutComposite(), labelString
);
156 combo
= new Combo(getLayoutComposite(), SWT
.BORDER
| SWT
.READ_ONLY
);
158 combo
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
159 combo
.setVisibleItemCount(DEFAULT_VISIBLE_ITEMS
);
161 populateTerms(getPreferredTerms());
163 combo
.addSelectionListener(this);
164 combo
.addDisposeListener(this);
165 PreferencesUtil
.getPreferenceStore().addPropertyChangeListener(this);
167 if (selection
!= null) {
168 setSelection(selection
);
174 * Getter for the field <code>selection</code>.
177 * @return a T object.
179 public T
getSelection() {
184 * <p>Sets the selection of the combo to the given T object.</p>
185 * <p>Passing <code>null</code> to this method will set the selection to
186 * the empty element and effectively clear the selection</p>
189 * a T object or <code>null</code> to clear the selection
191 public void setSelection(T selection
) {
192 this.selection
= selection
;
194 this.selection
= selection
;
196 Listener
[] listeners
= combo
.getListeners(SWT
.Selection
);
198 for (Listener listener
: listeners
) {
199 combo
.removeListener(SWT
.Selection
, listener
);
202 if(selection
== null){
203 // set selection to the emptyElement
206 selectedIndex
= terms
.indexOf(selection
);
207 if (selectedIndex
== -1) {
208 createTermNotInPreferredTerms(selection
);
209 selectedIndex
= terms
.indexOf(selection
);
212 combo
.select(selectedIndex
);
214 for (Listener listener
: listeners
) {
215 combo
.addListener(SWT
.Selection
, listener
);
220 * Fills the combo with elements and sets up the convenience functions
221 * for selection index
223 * @param preferredTerms
225 private void populateTerms(List
<T
> preferredTerms
) {
229 terms
= new ArrayList
<T
>();
234 // Add an empty element for when nothing was selected yet
235 combo
.add(EMPTY_ELEMENT_LABEL
);
236 terms
.add(emptyElement
);
238 if (termComparator
!= null) {
239 Collections
.sort(preferredTerms
, termComparator
);
241 for (T term
: preferredTerms
) {
242 String label
= getLabel(term
);
244 if (term
.getTitleCache() != null) {
245 label
= term
.getTitleCache();
246 StoreUtil
.warn(getClass(),
247 "Term does not have a representation: " + term
248 + ", " + term
.getUuid());
251 StoreUtil
.warn(getClass(),
252 "Representation Label and TitleCache empty for term: "
253 + term
+ ", " + term
.getUuid());
262 if (selection
!= null) {
263 if (selection
.equals(term
)) {
269 if (selection
!= null && index
== 0) {
270 createTermNotInPreferredTerms(selection
);
280 * eu.etaxonomy.taxeditor.forms.IEnableableFormElement#setEnabled(boolean)
283 public void setEnabled(boolean enabled
) {
284 combo
.setEnabled(enabled
);
292 * @return a {@link java.util.List} object.
294 protected List
<T
> getPreferredTerms(){
295 if (customPreferredTerms
!= null){
296 return customPreferredTerms
;
298 return getTermManager().getPreferredTerms(termClass
);
302 * May be overridden by derived classes if the desired label string does not
303 * reside in term.getLabel();
307 * @return a {@link java.lang.String} object.
309 protected String
getLabel(T term
) {
310 return term
.getLabel(CdmStore
.getDefaultLanguage());
318 private void createTermNotInPreferredTerms(T term
) {
319 List
<T
> preferredTerms
= getPreferredTerms();
321 preferredTerms
.add(term
);
323 populateTerms(preferredTerms
);
328 * addSelectionListener
332 * a {@link org.eclipse.swt.events.SelectionListener} object.
334 public void addSelectionListener(SelectionListener listener
) {
335 combo
.addSelectionListener(listener
);
340 * removeSelectionListener
344 * a {@link org.eclipse.swt.events.SelectionListener} object.
346 public void removeSelectionListener(SelectionListener listener
) {
347 combo
.removeSelectionListener(listener
);
354 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt
355 * .events.SelectionEvent)
358 public void widgetSelected(SelectionEvent e
) {
359 selection
= terms
.get(combo
.getSelectionIndex());
360 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
366 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.
367 * PropertyChangeEvent)
370 public void propertyChange(PropertyChangeEvent event
) {
371 super.propertyChange(event
);
373 && PreferencesUtil
.PREFERRED_TERMS_CHANGE
.equals(event
375 populateTerms(getPreferredTerms());
380 public void setSelected(boolean selected
) {
381 setBackground(selected ? SELECTED
: getPersistentBackground());
385 public void widgetDisposed(DisposeEvent e
) {
386 PreferencesUtil
.getPreferenceStore().removePropertyChangeListener(this);
391 public void widgetDefaultSelected(SelectionEvent e
) {
395 public void setIrrelevant(boolean irrelevant
) {
396 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
397 : Resources
.COLOR_COMPOSITE_BACKGROUND
;
399 Color color
= StoreUtil
.getColor(colorId
);
400 combo
.setBackground(color
);
405 public void setBackground(Color color
) {
407 label
.setBackground(color
);
413 protected TermManager
getTermManager() {
414 return CdmStore
.getTermManager();
421 public int getVisibleItemCount(){
422 return combo
.getVisibleItemCount();
429 public void setVisibleItemCount(int count
){
430 combo
.setVisibleItemCount(count
);
434 * <p>A {@link List} of term objects may be passed to this combo box. In this case, the default behaviour
435 * of displaying the preferred terms for the T type will be overridden and the combo will only display the
436 * given terms. Also, any previous selection will be reseted.</p>
438 * <p>To return to the default of displaying the preferred terms, simply pass <code>null</code>.</p>
440 * @param terms a {@link List} of T objects or <code>null</code> for default preferred terms
442 public void setTerms(List
<T
> terms
) {
444 customPreferredTerms
= terms
;
445 populateTerms(customPreferredTerms
);
448 private List
<T
> filterDefinedTermByTermType(List
<T
> terms
, TermType termType
) {
449 List
<T
> definedTerms
= new ArrayList
<T
>();
451 if(term
.getTermType().equals(termType
)) {
452 definedTerms
.add(term
);