2 * Copyright (C) 2007 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
.combo
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.Collection
;
14 import java
.util
.Collections
;
15 import java
.util
.Comparator
;
16 import java
.util
.LinkedHashMap
;
17 import java
.util
.List
;
18 import java
.util
.Map
.Entry
;
20 import org
.eclipse
.swt
.events
.SelectionEvent
;
21 import org
.eclipse
.swt
.widgets
.Combo
;
22 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
24 import eu
.etaxonomy
.cdm
.model
.media
.ExternalLinkType
;
25 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
27 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCodeEdition
;
28 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
29 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
30 import eu
.etaxonomy
.cdm
.model
.reference
.OriginalSourceType
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeStatus
;
33 import eu
.etaxonomy
.cdm
.model
.term
.IEnumTerm
;
34 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
35 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
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
.section
.name
.NameRelationshipDetailElement
;
40 import eu
.etaxonomy
.taxeditor
.ui
.section
.name
.NomenclaturalStatusElement
;
41 import eu
.etaxonomy
.taxeditor
.ui
.section
.name
.NomenclaturalStatusSection
;
45 * @created Mar 16, 2010
47 public class EnumComboElement
<T
extends IEnumTerm
<T
>>
48 extends AbstractComboElement
<T
> {
51 public void setVisible(boolean b
){
55 private static CdmEnumDataHolder
[] cdmEnumDataHolders
= {
56 new CdmEnumDataHolder
<NomenclaturalCode
>(){
59 public String
getName() {
60 return "Nomenclatural Code";
64 public Collection
<NomenclaturalCode
> getElements() {
65 Collection
<NomenclaturalCode
> result
= new ArrayList
<>(Arrays
.asList(NomenclaturalCode
.values()));
66 result
.remove(NomenclaturalCode
.ICVCN
);
71 public Class
<NomenclaturalCode
> getClazz() {
72 return NomenclaturalCode
.class;
75 new CdmEnumDataHolder
<ReferenceType
>(){
78 public String
getName() {
79 return "Reference Type";
83 public Collection
<ReferenceType
> getElements() {
84 List
<ReferenceType
> list
= Arrays
.asList(ReferenceType
.values());
85 Collections
.sort(list
, new Comparator
<ReferenceType
>() {
87 public int compare(ReferenceType rt1
, ReferenceType rt2
){
88 return rt1
.getLabel().compareTo(rt2
.getLabel());
95 public Class
<ReferenceType
> getClazz() {
96 return ReferenceType
.class;
100 new CdmEnumDataHolder
<SpecimenOrObservationType
>(){
103 public String
getName() {
104 return "Specimen / Observation Type";
108 public Collection
<SpecimenOrObservationType
> getElements() {
109 return Arrays
.asList(SpecimenOrObservationType
.values());
113 public Class
<SpecimenOrObservationType
> getClazz() {
114 return SpecimenOrObservationType
.class;
118 new CdmEnumDataHolder
<ExternalLinkType
>(){
121 public String
getName() {
126 public Collection
<ExternalLinkType
> getElements() {
127 return Arrays
.asList(ExternalLinkType
.values());
131 public Class
<ExternalLinkType
> getClazz() {
132 return ExternalLinkType
.class;
136 new CdmEnumDataHolder
<OriginalSourceType
>(){
139 public String
getName() {
140 return "Original Source Type";
144 public Collection
<OriginalSourceType
> getElements() {
145 return Arrays
.asList(OriginalSourceType
.values());
149 public Class
<OriginalSourceType
> getClazz() {
150 return OriginalSourceType
.class;
154 new CdmEnumDataHolder
<NomenclaturalCodeEdition
>(){
157 public String
getName() {
158 return "Nomenclatural Code Edition";
162 public Collection
<NomenclaturalCodeEdition
> getElements() {
163 return Arrays
.asList(NomenclaturalCodeEdition
.values());
167 public Class
<NomenclaturalCodeEdition
> getClazz() {
168 return NomenclaturalCodeEdition
.class;
172 new CdmEnumDataHolder
<TaxonNodeStatus
>(){
175 public String
getName() {
176 return "Taxon Node Status";
180 public Collection
<TaxonNodeStatus
> getElements() {
181 return Arrays
.asList(TaxonNodeStatus
.values());
185 public Class
<TaxonNodeStatus
> getClazz() {
186 return TaxonNodeStatus
.class;
192 protected List
<T
> elementTypeList
= new ArrayList
<>();
194 private final Class
<T
> enumType
;
196 public EnumComboElement(CdmFormFactory formFactory
,
197 ICdmFormElement parentElement
, Class
<T
> enumType
, Comparator
<T
> comparator
, int style
, boolean hasNullValue
) {
198 super(formFactory
, parentElement
, hasNullValue
);
199 this.enumType
= enumType
;
200 label
.setText(getDefaultLabelString());
201 populateTypes(comparator
);
202 combo
.addSelectionListener(this);
203 addContentProposalAdapter();
206 public void setIndent(int indent
) {
207 TableWrapData layoutData
= new TableWrapData();
208 layoutData
.indent
= indent
;
209 label
.setLayoutData(layoutData
);
211 getLayoutComposite().layout();
214 public Combo
getCombo(){
218 private String
getDefaultLabelString() {
219 if(enumType
.equals(ReferenceType
.class)){
220 return "Reference Type";
221 } else if (enumType
.equals(NomenclaturalCode
.class)){
222 return "Nomenclatural Code";
223 } else if(enumType
.equals(OriginalSourceType
.class)) {
224 return "Orig. Source Type";
225 } else if(enumType
.equals(SpecimenOrObservationType
.class)) {
226 return "Spec./Obs. Type";
227 } else if(enumType
.equals(ExternalLinkType
.class)) {
229 } else if(enumType
.equals(NomenclaturalCodeEdition
.class)) {
230 return "Code Edition";
231 }else if(enumType
.equals(TaxonNodeStatus
.class)) {
232 return Messages
.EnumCombo_Placement_status
;
237 private void populateTypes(Comparator
<T
> comparator
){
238 List
<T
> elementsForClass
= new ArrayList
<T
>(getElementsForClass(enumType
));
239 if(comparator
!=null){
240 Collections
.sort(elementsForClass
, comparator
);
244 combo
.add("", index
);
247 // special hierarchical order for specimens
248 if(enumType
.equals(SpecimenOrObservationType
.class)){
249 LinkedHashMap
<T
, String
> orderedTerms
= AbstractUtility
.orderTerms(elementsForClass
);
250 for(Entry
<T
, String
> keyLabelPair
: orderedTerms
.entrySet()){
251 elementTypeList
.add(keyLabelPair
.getKey());
252 combo
.add(keyLabelPair
.getValue(), index
);
255 }else if(enumType
.equals(NomenclaturalCodeEdition
.class)){
256 NomenclaturalCode code
= null;
258 ICdmFormElement parentElement
= getParentElement();
259 if (parentElement
instanceof NameRelationshipDetailElement
){
260 NameRelationship rel
= ((NameRelationshipDetailElement
)parentElement
).getEntity();
261 code
= rel
.getToName().getNameType();
262 }else if (parentElement
instanceof NomenclaturalStatusElement
){
263 ICdmFormElement parent
= ((NomenclaturalStatusElement
)parentElement
).getParentElement();
264 TaxonName name
= ((NomenclaturalStatusSection
)parent
).getEntity();
265 code
= name
.getNameType();
267 List
<NomenclaturalCodeEdition
> elements
= NomenclaturalCodeEdition
.forCode(code
);
268 LinkedHashMap
<NomenclaturalCodeEdition
, String
> orderedTerms
= AbstractUtility
.orderTerms(elements
);
269 for (NomenclaturalCodeEdition codeEdition
: elements
){
270 elementTypeList
.add((T
)codeEdition
);
271 combo
.add(codeEdition
.getLabel(), index
);
277 for(T enumElement
: elementsForClass
){
278 elementTypeList
.add(enumElement
);
279 if (enumElement
instanceof IEnumTerm
) {
280 combo
.add(((IEnumTerm
) enumElement
).getLabel(), index
);
283 combo
.add(((Enum
) enumElement
).name(), index
);
291 public void setSelection(T selection
) {
292 this.selection
= selection
;
293 int index
= elementTypeList
.indexOf(selection
);
294 if (hasNullValue
&& selection
!= null){
301 public void widgetSelected(SelectionEvent e
) {
303 int index
= combo
.getSelectionIndex();
313 selection
= elementTypeList
.get(index
);
316 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
319 private Collection
<T
> getElementsForClass(Class
<T
> clazz
){
320 CdmEnumDataHolder
<T
> dataHolder
= getCdmEnumDataHolderForClass(clazz
);
321 if (dataHolder
!= null) {
322 return dataHolder
.getElements();
327 private CdmEnumDataHolder
<T
> getCdmEnumDataHolderForClass(Class
<T
> clazz
){
328 for (CdmEnumDataHolder dataHolder
: cdmEnumDataHolders
) {
329 if (dataHolder
.getClazz().equals(clazz
)){
336 private interface CdmEnumDataHolder
<T
> {
339 Collection
<T
> getElements();