2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.ui
.mvc
.combo
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.Collection
;
14 import java
.util
.LinkedHashMap
;
15 import java
.util
.List
;
16 import java
.util
.Map
.Entry
;
18 import org
.eclipse
.swt
.events
.SelectionEvent
;
19 import org
.eclipse
.swt
.events
.SelectionListener
;
20 import org
.eclipse
.swt
.graphics
.Color
;
21 import org
.eclipse
.swt
.widgets
.Combo
;
22 import org
.eclipse
.swt
.widgets
.Composite
;
24 import eu
.etaxonomy
.cdm
.model
.term
.IEnumTerm
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.OriginalSourceType
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
27 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
28 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
29 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
30 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
31 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
37 import eu
.etaxonomy
.taxeditor
.ui
.mvc
.interfaces
.CdmCompositeController
;
44 public class EnumTermComboController
<T
extends IEnumTerm
<T
>> extends AbstractCdmFormElement
implements SelectionListener
,
45 IEnableableFormElement
, ISelectable
, CdmCompositeController
{
47 private static CdmEnumDataHolder
[] cdmEnumDataHolders
= {
48 new CdmEnumDataHolder
<NomenclaturalCode
>(){
51 public String
getName() {
52 return "Nomenclatural Code";
56 public Collection
<NomenclaturalCode
> getElements() {
57 Collection
<NomenclaturalCode
> result
= Arrays
.asList(NomenclaturalCode
.values());
58 result
.remove(NomenclaturalCode
.ICVCN
);
63 public Class
<NomenclaturalCode
> getClazz() {
64 return NomenclaturalCode
.class;
68 new CdmEnumDataHolder
<ReferenceType
>(){
71 public String
getName() {
72 return "Reference Type";
76 public Collection
<ReferenceType
> getElements() {
77 return Arrays
.asList(ReferenceType
.values());
81 public Class
<ReferenceType
> getClazz() {
82 return ReferenceType
.class;
86 new CdmEnumDataHolder
<SpecimenOrObservationType
>(){
89 public String
getName() {
90 return "Specimen / Observation Type";
94 public Collection
<SpecimenOrObservationType
> getElements() {
95 return Arrays
.asList(SpecimenOrObservationType
.values());
99 public Class
<SpecimenOrObservationType
> getClazz() {
100 return SpecimenOrObservationType
.class;
104 new CdmEnumDataHolder
<OriginalSourceType
>(){
107 public String
getName() {
108 return "Original Source Type";
112 public Collection
<OriginalSourceType
> getElements() {
113 return Arrays
.asList(OriginalSourceType
.values());
117 public Class
<OriginalSourceType
> getClazz() {
118 return OriginalSourceType
.class;
125 private static final int DEFAULT_VISIBLE_ITEMS
= 10;
127 protected T selection
;
129 protected List
<T
> elementTypeList
= new ArrayList
<T
>();
131 protected Combo combo
;
133 private final Class
<T
> enumType
;
137 * Constructor for AbstractEnumComboElement.
141 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
143 * @param parentElement
144 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
147 * a {@link java.lang.String} object.
151 public EnumTermComboController(Combo combo
, CdmFormFactory formFactory
,
152 ICdmFormElement parentElement
, Class
<T
> enumType
, int style
) {
153 super(formFactory
, (Composite
)null);
154 this.enumType
= enumType
;
158 setVisibleItemCount(DEFAULT_VISIBLE_ITEMS
);
162 combo
.addSelectionListener(this);
166 private String
getDefaultLabelString() {
167 if(enumType
.equals(ReferenceType
.class)){
168 return "Reference Type";
169 } else if (enumType
.equals(NomenclaturalCode
.class)){
170 return "Nomenclatural Code";
171 } else if(enumType
.equals(OriginalSourceType
.class)) {
172 return "Orig. Source Type";
173 } else if(enumType
.equals(SpecimenOrObservationType
.class)) {
174 return "Spec./Obs. Type";
184 private void populateTypes(){
185 Collection
<T
> elementsForClass
= getElementsForClass(enumType
);
186 // special hierarchical order for specimens
187 if(enumType
.equals(SpecimenOrObservationType
.class)){
188 LinkedHashMap
<T
, String
> orderedTerms
= AbstractUtility
.orderTerms(elementsForClass
);
189 for(Entry
<T
, String
> keyLabelPair
: orderedTerms
.entrySet()){
190 elementTypeList
.add(keyLabelPair
.getKey());
191 combo
.add(keyLabelPair
.getValue());
196 for(T enumElement
: getElementsForClass(enumType
)){
197 elementTypeList
.add(enumElement
);
198 combo
.add(((Enum
) enumElement
).name());
205 * Setter for the field <code>selection</code>.
209 * the selection to set
211 public void setSelection(T selection
) {
212 this.selection
= selection
;
213 combo
.select(elementTypeList
.indexOf(selection
));
218 * addSelectionListener
222 * a {@link org.eclipse.swt.events.SelectionListener} object.
224 public void addSelectionListener(SelectionListener listener
) {
225 combo
.addSelectionListener(listener
);
230 * removeSelectionListener
234 * a {@link org.eclipse.swt.events.SelectionListener} object.
236 public void removeSelectionListener(SelectionListener listener
) {
237 combo
.removeSelectionListener(listener
);
242 public void setSelected(boolean selected
) {
243 setBackground(selected ? SELECTED
: getPersistentBackground());
248 * Getter for the field <code>selection</code>.
251 * @return the selection
253 public T
getSelection() {
259 public void setEnabled(boolean enabled
) {
260 combo
.setEnabled(enabled
);
264 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
267 public boolean isEnabled() {
268 return combo
.isEnabled();
275 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt
276 * .events.SelectionEvent)
280 public void widgetSelected(SelectionEvent e
) {
281 selection
= elementTypeList
.get(combo
.getSelectionIndex());
286 public void setIrrelevant(boolean irrelevant
) {
287 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
288 : Resources
.COLOR_COMPOSITE_BACKGROUND
;
290 Color color
= StoreUtil
.getColor(colorId
);
291 combo
.setBackground(color
);
296 public void setBackground(Color color
) {
297 // label.setBackground(color);
302 public void widgetDefaultSelected(SelectionEvent e
) {
305 public void setVisibleItemCount(int visibleItems
){
306 combo
.setVisibleItemCount(visibleItems
);
309 private Collection
<T
> getElementsForClass(Class
<T
> clazz
){
310 CdmEnumDataHolder
<T
> dataHolder
= getCdmEnumDataHolderForClass(clazz
);
311 if (dataHolder
!= null) {
312 return dataHolder
.getElements();
317 private CdmEnumDataHolder
<T
> getCdmEnumDataHolderForClass(Class
<T
> clazz
){
318 for (CdmEnumDataHolder dataHolder
: cdmEnumDataHolders
) {
319 if (dataHolder
.getClazz().equals(clazz
)){
326 private interface CdmEnumDataHolder
<T
> {
329 Collection
<T
> getElements();