3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.ui
.combo
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.Collection
;
16 import java
.util
.LinkedHashMap
;
17 import java
.util
.List
;
18 import java
.util
.Map
.Entry
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.SelectionEvent
;
22 import org
.eclipse
.swt
.events
.SelectionListener
;
23 import org
.eclipse
.swt
.graphics
.Color
;
24 import org
.eclipse
.swt
.widgets
.Combo
;
25 import org
.eclipse
.swt
.widgets
.Label
;
27 import eu
.etaxonomy
.cdm
.model
.common
.IEnumTerm
;
28 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceType
;
29 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
30 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
32 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
33 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
34 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
37 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
40 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
41 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
45 * Abstract AbstractEnumComboElement class.
49 * @created Mar 16, 2010
52 public class EnumComboElement
<T
extends IEnumTerm
<T
>> extends
53 AbstractCdmFormElement
implements SelectionListener
,
54 IEnableableFormElement
, ISelectable
{
57 public void setVisible(boolean b
){
62 private static CdmEnumDataHolder
[] cdmEnumDataHolders
= {
63 new CdmEnumDataHolder
<NomenclaturalCode
>(){
66 public String
getName() {
67 return "Nomenclatural Code";
71 public Collection
<NomenclaturalCode
> getElements() {
72 return Arrays
.asList(NomenclaturalCode
.values());
76 public Class
<NomenclaturalCode
> getClazz() {
77 return NomenclaturalCode
.class;
81 new CdmEnumDataHolder
<ReferenceType
>(){
84 public String
getName() {
85 return "Reference Type";
89 public Collection
<ReferenceType
> getElements() {
90 return Arrays
.asList(ReferenceType
.values());
94 public Class
<ReferenceType
> getClazz() {
95 return ReferenceType
.class;
99 new CdmEnumDataHolder
<SpecimenOrObservationType
>(){
102 public String
getName() {
103 return "Specimen / Observation Type";
107 public Collection
<SpecimenOrObservationType
> getElements() {
108 return Arrays
.asList(SpecimenOrObservationType
.values());
112 public Class
<SpecimenOrObservationType
> getClazz() {
113 return SpecimenOrObservationType
.class;
117 new CdmEnumDataHolder
<OriginalSourceType
>(){
120 public String
getName() {
121 return "Original Source Type";
125 public Collection
<OriginalSourceType
> getElements() {
126 return Arrays
.asList(OriginalSourceType
.values());
130 public Class
<OriginalSourceType
> getClazz() {
131 return OriginalSourceType
.class;
138 private static final int DEFAULT_VISIBLE_ITEMS
= 10;
140 protected T selection
;
142 protected List
<T
> elementTypeList
= new ArrayList
<T
>();
144 private final Label label
;
145 protected Combo combo
;
147 private final Class
<T
> enumType
;
151 * Constructor for AbstractEnumComboElement.
155 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
157 * @param parentElement
158 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
161 * a {@link java.lang.String} object.
165 public EnumComboElement(CdmFormFactory formFactory
,
166 ICdmFormElement parentElement
, Class
<T
> enumType
, int style
) {
167 super(formFactory
, parentElement
);
168 this.enumType
= enumType
;
169 label
= formFactory
.createLabel(getLayoutComposite(), getDefaultLabelString());
173 combo
= new Combo(getLayoutComposite(), SWT
.BORDER
| SWT
.READ_ONLY
);
175 combo
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY());
176 setVisibleItemCount(DEFAULT_VISIBLE_ITEMS
);
180 combo
.addSelectionListener(this);
183 private String
getDefaultLabelString() {
184 if(enumType
.equals(ReferenceType
.class)){
185 return "Reference Type";
186 } else if (enumType
.equals(NomenclaturalCode
.class)){
187 return "Nomenclatural Code";
188 } else if(enumType
.equals(OriginalSourceType
.class)) {
189 return "Orig. Source Type";
190 } else if(enumType
.equals(SpecimenOrObservationType
.class)) {
191 return "Spec./Obs. Type";
201 private void populateTypes(){
202 Collection
<T
> elementsForClass
= getElementsForClass(enumType
);
203 // special hierarchical order for specimens
204 if(enumType
.equals(SpecimenOrObservationType
.class)){
205 LinkedHashMap
<T
, String
> orderedTerms
= AbstractUtility
.orderTerms(elementsForClass
);
206 for(Entry
<T
, String
> keyLabelPair
: orderedTerms
.entrySet()){
207 elementTypeList
.add(keyLabelPair
.getKey());
208 combo
.add(keyLabelPair
.getValue());
213 for(T enumElement
: getElementsForClass(enumType
)){
214 elementTypeList
.add(enumElement
);
215 combo
.add(((Enum
) enumElement
).name());
222 * Setter for the field <code>selection</code>.
226 * the selection to set
228 public void setSelection(T selection
) {
229 this.selection
= selection
;
230 combo
.select(elementTypeList
.indexOf(selection
));
235 * addSelectionListener
239 * a {@link org.eclipse.swt.events.SelectionListener} object.
241 public void addSelectionListener(SelectionListener listener
) {
242 combo
.addSelectionListener(listener
);
247 * removeSelectionListener
251 * a {@link org.eclipse.swt.events.SelectionListener} object.
253 public void removeSelectionListener(SelectionListener listener
) {
254 combo
.removeSelectionListener(listener
);
259 public void setSelected(boolean selected
) {
260 setBackground(selected ? SELECTED
: getPersistentBackground());
265 * Getter for the field <code>selection</code>.
268 * @return the selection
270 public T
getSelection() {
276 public void setEnabled(boolean enabled
) {
277 combo
.setEnabled(enabled
);
281 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
284 public boolean isEnabled() {
285 return combo
.isEnabled();
292 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt
293 * .events.SelectionEvent)
297 public void widgetSelected(SelectionEvent e
) {
298 selection
= elementTypeList
.get(combo
.getSelectionIndex());
299 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
304 public void setIrrelevant(boolean irrelevant
) {
305 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
306 : Resources
.COLOR_COMPOSITE_BACKGROUND
;
308 Color color
= StoreUtil
.getColor(colorId
);
309 combo
.setBackground(color
);
314 public void setBackground(Color color
) {
315 label
.setBackground(color
);
320 public void widgetDefaultSelected(SelectionEvent e
) {
323 public void setVisibleItemCount(int visibleItems
){
324 combo
.setVisibleItemCount(visibleItems
);
327 private Collection
<T
> getElementsForClass(Class
<T
> clazz
){
328 CdmEnumDataHolder
<T
> dataHolder
= getCdmEnumDataHolderForClass(clazz
);
329 if (dataHolder
!= null) {
330 return dataHolder
.getElements();
335 private CdmEnumDataHolder
<T
> getCdmEnumDataHolderForClass(Class
<T
> clazz
){
336 for (CdmEnumDataHolder dataHolder
: cdmEnumDataHolders
) {
337 if (dataHolder
.getClazz().equals(clazz
)){
344 private interface CdmEnumDataHolder
<T
> {
347 Collection
<T
> getElements();