1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.ui.combo;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Arrays;
|
14
|
import java.util.Collection;
|
15
|
import java.util.Collections;
|
16
|
import java.util.Comparator;
|
17
|
import java.util.LinkedHashMap;
|
18
|
import java.util.List;
|
19
|
import java.util.Map.Entry;
|
20
|
|
21
|
import org.eclipse.swt.events.SelectionEvent;
|
22
|
import org.eclipse.swt.widgets.Combo;
|
23
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
24
|
|
25
|
import eu.etaxonomy.cdm.model.media.ExternalLinkType;
|
26
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
27
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
28
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCodeEdition;
|
29
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
30
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
31
|
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
|
32
|
import eu.etaxonomy.cdm.model.reference.ReferenceType;
|
33
|
import eu.etaxonomy.cdm.model.taxon.TaxonNodeStatus;
|
34
|
import eu.etaxonomy.cdm.model.term.IEnumTerm;
|
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;
|
42
|
|
43
|
/**
|
44
|
* @author n.hoffmann
|
45
|
* @created Mar 16, 2010
|
46
|
*/
|
47
|
public class EnumComboElement<T extends IEnumTerm<T>> extends
|
48
|
AbstractComboElement<T> {
|
49
|
|
50
|
//TODO RL
|
51
|
public void setVisible(boolean b){
|
52
|
combo.setVisible(b);
|
53
|
}
|
54
|
|
55
|
|
56
|
private static CdmEnumDataHolder[] cdmEnumDataHolders = {
|
57
|
new CdmEnumDataHolder<NomenclaturalCode>(){
|
58
|
|
59
|
@Override
|
60
|
public String getName() {
|
61
|
return "Nomenclatural Code";
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
public Collection<NomenclaturalCode> getElements() {
|
66
|
Collection<NomenclaturalCode> result = new ArrayList<>(Arrays.asList(NomenclaturalCode.values()));
|
67
|
result.remove(NomenclaturalCode.ICVCN);
|
68
|
return result;
|
69
|
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
public Class<NomenclaturalCode> getClazz() {
|
74
|
return NomenclaturalCode.class;
|
75
|
}
|
76
|
|
77
|
},
|
78
|
new CdmEnumDataHolder<ReferenceType>(){
|
79
|
|
80
|
@Override
|
81
|
public String getName() {
|
82
|
return "Reference Type";
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
public Collection<ReferenceType> getElements() {
|
87
|
return Arrays.asList(ReferenceType.values());
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
public Class<ReferenceType> getClazz() {
|
92
|
return ReferenceType.class;
|
93
|
}
|
94
|
|
95
|
},
|
96
|
new CdmEnumDataHolder<SpecimenOrObservationType>(){
|
97
|
|
98
|
@Override
|
99
|
public String getName() {
|
100
|
return "Specimen / Observation Type";
|
101
|
}
|
102
|
|
103
|
@Override
|
104
|
public Collection<SpecimenOrObservationType> getElements() {
|
105
|
return Arrays.asList(SpecimenOrObservationType.values());
|
106
|
}
|
107
|
|
108
|
@Override
|
109
|
public Class<SpecimenOrObservationType> getClazz() {
|
110
|
return SpecimenOrObservationType.class;
|
111
|
}
|
112
|
|
113
|
},
|
114
|
new CdmEnumDataHolder<ExternalLinkType>(){
|
115
|
|
116
|
@Override
|
117
|
public String getName() {
|
118
|
return "Type";
|
119
|
}
|
120
|
|
121
|
@Override
|
122
|
public Collection<ExternalLinkType> getElements() {
|
123
|
return Arrays.asList(ExternalLinkType.values());
|
124
|
}
|
125
|
|
126
|
@Override
|
127
|
public Class<ExternalLinkType> getClazz() {
|
128
|
return ExternalLinkType.class;
|
129
|
}
|
130
|
|
131
|
},
|
132
|
new CdmEnumDataHolder<OriginalSourceType>(){
|
133
|
|
134
|
@Override
|
135
|
public String getName() {
|
136
|
return "Original Source Type";
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
public Collection<OriginalSourceType> getElements() {
|
141
|
return Arrays.asList(OriginalSourceType.values());
|
142
|
}
|
143
|
|
144
|
@Override
|
145
|
public Class<OriginalSourceType> getClazz() {
|
146
|
return OriginalSourceType.class;
|
147
|
}
|
148
|
|
149
|
},
|
150
|
new CdmEnumDataHolder<NomenclaturalCodeEdition>(){
|
151
|
|
152
|
@Override
|
153
|
public String getName() {
|
154
|
return "Nomenclatural Code Edition";
|
155
|
}
|
156
|
|
157
|
@Override
|
158
|
public Collection<NomenclaturalCodeEdition> getElements() {
|
159
|
return Arrays.asList(NomenclaturalCodeEdition.values());
|
160
|
}
|
161
|
|
162
|
@Override
|
163
|
public Class<NomenclaturalCodeEdition> getClazz() {
|
164
|
return NomenclaturalCodeEdition.class;
|
165
|
}
|
166
|
|
167
|
},
|
168
|
new CdmEnumDataHolder<TaxonNodeStatus>(){
|
169
|
|
170
|
@Override
|
171
|
public String getName() {
|
172
|
return "Taxon Node Status";
|
173
|
}
|
174
|
|
175
|
@Override
|
176
|
public Collection<TaxonNodeStatus> getElements() {
|
177
|
return Arrays.asList(TaxonNodeStatus.values());
|
178
|
}
|
179
|
|
180
|
@Override
|
181
|
public Class<TaxonNodeStatus> getClazz() {
|
182
|
return TaxonNodeStatus.class;
|
183
|
}
|
184
|
|
185
|
}
|
186
|
|
187
|
};
|
188
|
|
189
|
protected List<T> elementTypeList = new ArrayList<>();
|
190
|
|
191
|
private final Class<T> enumType;
|
192
|
|
193
|
public EnumComboElement(CdmFormFactory formFactory,
|
194
|
ICdmFormElement parentElement, Class<T> enumType, Comparator<T> comparator, int style, boolean hasNullValue) {
|
195
|
super(formFactory, parentElement, hasNullValue);
|
196
|
this.enumType = enumType;
|
197
|
label.setText(getDefaultLabelString());
|
198
|
populateTypes(comparator);
|
199
|
combo.addSelectionListener(this);
|
200
|
addContentProposalAdapter();
|
201
|
}
|
202
|
|
203
|
public void setIndent(int indent) {
|
204
|
TableWrapData layoutData = new TableWrapData();
|
205
|
layoutData.indent = indent;
|
206
|
label.setLayoutData(layoutData);
|
207
|
|
208
|
getLayoutComposite().layout();
|
209
|
}
|
210
|
|
211
|
public Combo getCombo(){
|
212
|
return combo;
|
213
|
}
|
214
|
|
215
|
|
216
|
private String getDefaultLabelString() {
|
217
|
if(enumType.equals(ReferenceType.class)){
|
218
|
return "Reference Type";
|
219
|
} else if (enumType.equals(NomenclaturalCode.class)){
|
220
|
return "Nomenclatural Code";
|
221
|
} else if(enumType.equals(OriginalSourceType.class)) {
|
222
|
return "Orig. Source Type";
|
223
|
} else if(enumType.equals(SpecimenOrObservationType.class)) {
|
224
|
return "Spec./Obs. Type";
|
225
|
} else if(enumType.equals(ExternalLinkType.class)) {
|
226
|
return "Type";
|
227
|
} else if(enumType.equals(NomenclaturalCodeEdition.class)) {
|
228
|
return "Code Edition";
|
229
|
}else if(enumType.equals(TaxonNodeStatus.class)) {
|
230
|
return "Placement status";
|
231
|
}
|
232
|
return "No Label";
|
233
|
}
|
234
|
|
235
|
private void populateTypes(Comparator<T> comparator){
|
236
|
List<T> elementsForClass = new ArrayList<T>(getElementsForClass(enumType));
|
237
|
if(comparator!=null){
|
238
|
Collections.sort(elementsForClass, comparator);
|
239
|
}
|
240
|
int index = 0;
|
241
|
if (hasNullValue){
|
242
|
combo.add("", index);
|
243
|
index++;
|
244
|
}
|
245
|
// special hierarchical order for specimens
|
246
|
if(enumType.equals(SpecimenOrObservationType.class)){
|
247
|
LinkedHashMap<T, String> orderedTerms = AbstractUtility.orderTerms(elementsForClass);
|
248
|
for(Entry<T, String> keyLabelPair : orderedTerms.entrySet()){
|
249
|
elementTypeList.add(keyLabelPair.getKey());
|
250
|
combo.add(keyLabelPair.getValue(), index);
|
251
|
index++;
|
252
|
}
|
253
|
}else if(enumType.equals(NomenclaturalCodeEdition.class)){
|
254
|
NomenclaturalCode code = null;
|
255
|
|
256
|
ICdmFormElement parentElement = getParentElement();
|
257
|
if (parentElement instanceof NameRelationshipDetailElement){
|
258
|
NameRelationship rel = ((NameRelationshipDetailElement)parentElement).getEntity();
|
259
|
code = rel.getToName().getNomenclaturalCode();
|
260
|
}else if (parentElement instanceof NomenclaturalStatusElement){
|
261
|
ICdmFormElement parent = ((NomenclaturalStatusElement)parentElement).getParentElement();
|
262
|
TaxonName name = ((NomenclaturalStatusSection)parent).getEntity();
|
263
|
code = name.getNomenclaturalCode();
|
264
|
}
|
265
|
List<NomenclaturalCodeEdition> elements = NomenclaturalCodeEdition.forCode(code);
|
266
|
LinkedHashMap<NomenclaturalCodeEdition, String> orderedTerms = AbstractUtility.orderTerms(elements);
|
267
|
for (NomenclaturalCodeEdition codeEdition: elements){
|
268
|
elementTypeList.add((T)codeEdition);
|
269
|
combo.add(codeEdition.getMessage(), index);
|
270
|
index++;
|
271
|
}
|
272
|
}
|
273
|
// default order
|
274
|
else{
|
275
|
for(T enumElement : elementsForClass){
|
276
|
elementTypeList.add(enumElement);
|
277
|
if (enumElement instanceof IEnumTerm) {
|
278
|
combo.add(((IEnumTerm) enumElement).getMessage(), index);
|
279
|
index++;
|
280
|
}else{
|
281
|
combo.add(((Enum) enumElement).name(), index);
|
282
|
index++;
|
283
|
}
|
284
|
}
|
285
|
}
|
286
|
}
|
287
|
|
288
|
@Override
|
289
|
public void setSelection(T selection) {
|
290
|
this.selection = selection;
|
291
|
int index = elementTypeList.indexOf(selection);
|
292
|
if (hasNullValue && selection != null){
|
293
|
index++;
|
294
|
}
|
295
|
combo.select(index);
|
296
|
}
|
297
|
/** {@inheritDoc} */
|
298
|
@Override
|
299
|
public void widgetSelected(SelectionEvent e) {
|
300
|
|
301
|
int index = combo.getSelectionIndex();
|
302
|
if (index == -2){
|
303
|
return;
|
304
|
}
|
305
|
if (hasNullValue ){
|
306
|
index--;
|
307
|
}
|
308
|
if (index<0){
|
309
|
selection = null;
|
310
|
}else{
|
311
|
selection = elementTypeList.get(index);
|
312
|
}
|
313
|
|
314
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
315
|
}
|
316
|
|
317
|
private Collection<T> getElementsForClass(Class<T> clazz){
|
318
|
CdmEnumDataHolder<T> dataHolder = getCdmEnumDataHolderForClass(clazz);
|
319
|
if (dataHolder != null) {
|
320
|
return dataHolder.getElements();
|
321
|
}
|
322
|
return null;
|
323
|
}
|
324
|
|
325
|
private CdmEnumDataHolder<T> getCdmEnumDataHolderForClass(Class<T> clazz){
|
326
|
for (CdmEnumDataHolder dataHolder : cdmEnumDataHolders) {
|
327
|
if (dataHolder.getClazz().equals(clazz)){
|
328
|
return dataHolder;
|
329
|
}
|
330
|
}
|
331
|
return null;
|
332
|
}
|
333
|
|
334
|
private interface CdmEnumDataHolder<T> {
|
335
|
Class<T> getClazz();
|
336
|
String getName();
|
337
|
Collection<T> getElements();
|
338
|
}
|
339
|
}
|