363629cc281822a8bd272174320d508b49d3b8ac
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / combo / EnumComboElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.combo;
12
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;
19
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;
26
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;
42
43 /**
44 * <p>
45 * Abstract AbstractEnumComboElement class.
46 * </p>
47 *
48 * @author n.hoffmann
49 * @created Mar 16, 2010
50 * @version 1.0
51 */
52 public class EnumComboElement<T extends IEnumTerm<T>> extends
53 AbstractCdmFormElement implements SelectionListener,
54 IEnableableFormElement, ISelectable {
55
56 //TODO RL
57 public void setVisible(boolean b){
58 combo.setVisible(b);
59 }
60
61
62 private static CdmEnumDataHolder[] cdmEnumDataHolders = {
63 new CdmEnumDataHolder<NomenclaturalCode>(){
64
65 @Override
66 public String getName() {
67 return "Nomenclatural Code";
68 }
69
70 @Override
71 public Collection<NomenclaturalCode> getElements() {
72 return Arrays.asList(NomenclaturalCode.values());
73 }
74
75 @Override
76 public Class<NomenclaturalCode> getClazz() {
77 return NomenclaturalCode.class;
78 }
79
80 },
81 new CdmEnumDataHolder<ReferenceType>(){
82
83 @Override
84 public String getName() {
85 return "Reference Type";
86 }
87
88 @Override
89 public Collection<ReferenceType> getElements() {
90 return Arrays.asList(ReferenceType.values());
91 }
92
93 @Override
94 public Class<ReferenceType> getClazz() {
95 return ReferenceType.class;
96 }
97
98 },
99 new CdmEnumDataHolder<SpecimenOrObservationType>(){
100
101 @Override
102 public String getName() {
103 return "Specimen / Observation Type";
104 }
105
106 @Override
107 public Collection<SpecimenOrObservationType> getElements() {
108 return Arrays.asList(SpecimenOrObservationType.values());
109 }
110
111 @Override
112 public Class<SpecimenOrObservationType> getClazz() {
113 return SpecimenOrObservationType.class;
114 }
115
116 },
117 new CdmEnumDataHolder<OriginalSourceType>(){
118
119 @Override
120 public String getName() {
121 return "Original Source Type";
122 }
123
124 @Override
125 public Collection<OriginalSourceType> getElements() {
126 return Arrays.asList(OriginalSourceType.values());
127 }
128
129 @Override
130 public Class<OriginalSourceType> getClazz() {
131 return OriginalSourceType.class;
132 }
133
134 }
135
136 };
137
138 private static final int DEFAULT_VISIBLE_ITEMS = 10;
139
140 protected T selection;
141
142 protected List<T> elementTypeList = new ArrayList<T>();
143
144 private final Label label;
145 protected Combo combo;
146
147 private final Class<T> enumType;
148
149 /**
150 * <p>
151 * Constructor for AbstractEnumComboElement.
152 * </p>
153 *
154 * @param formFactory
155 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
156 * object.
157 * @param parentElement
158 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
159 * object.
160 * @param labelString
161 * a {@link java.lang.String} object.
162 * @param <T>
163 * a T object.
164 */
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());
170 addControl(label);
171
172 // create combo
173 combo = new Combo(getLayoutComposite(), SWT.BORDER | SWT.READ_ONLY);
174 addControl(combo);
175 combo.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
176 setVisibleItemCount(DEFAULT_VISIBLE_ITEMS);
177
178 populateTypes();
179
180 combo.addSelectionListener(this);
181 }
182
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";
192 }
193 return "No Label";
194 }
195
196 /**
197 * <p>
198 * populateTypes
199 * </p>
200 */
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());
209 }
210 }
211 // default order
212 else{
213 for(T enumElement : getElementsForClass(enumType)){
214 elementTypeList.add(enumElement);
215 combo.add(((Enum) enumElement).name());
216 }
217 }
218 }
219
220 /**
221 * <p>
222 * Setter for the field <code>selection</code>.
223 * </p>
224 *
225 * @param selection
226 * the selection to set
227 */
228 public void setSelection(T selection) {
229 this.selection = selection;
230 combo.select(elementTypeList.indexOf(selection));
231 }
232
233 /**
234 * <p>
235 * addSelectionListener
236 * </p>
237 *
238 * @param listener
239 * a {@link org.eclipse.swt.events.SelectionListener} object.
240 */
241 public void addSelectionListener(SelectionListener listener) {
242 combo.addSelectionListener(listener);
243 }
244
245 /**
246 * <p>
247 * removeSelectionListener
248 * </p>
249 *
250 * @param listener
251 * a {@link org.eclipse.swt.events.SelectionListener} object.
252 */
253 public void removeSelectionListener(SelectionListener listener) {
254 combo.removeSelectionListener(listener);
255 }
256
257 /** {@inheritDoc} */
258 @Override
259 public void setSelected(boolean selected) {
260 setBackground(selected ? SELECTED : getPersistentBackground());
261 }
262
263 /**
264 * <p>
265 * Getter for the field <code>selection</code>.
266 * </p>
267 *
268 * @return the selection
269 */
270 public T getSelection() {
271 return selection;
272 }
273
274 /** {@inheritDoc} */
275 @Override
276 public void setEnabled(boolean enabled) {
277 combo.setEnabled(enabled);
278 }
279
280 /* (non-Javadoc)
281 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
282 */
283 @Override
284 public boolean isEnabled() {
285 return combo.isEnabled();
286 }
287
288 /*
289 * (non-Javadoc)
290 *
291 * @see
292 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt
293 * .events.SelectionEvent)
294 */
295 /** {@inheritDoc} */
296 @Override
297 public void widgetSelected(SelectionEvent e) {
298 selection = elementTypeList.get(combo.getSelectionIndex());
299 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
300 }
301
302 /** {@inheritDoc} */
303 @Override
304 public void setIrrelevant(boolean irrelevant) {
305 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
306 : Resources.COLOR_COMPOSITE_BACKGROUND;
307
308 Color color = StoreUtil.getColor(colorId);
309 combo.setBackground(color);
310 }
311
312 /** {@inheritDoc} */
313 @Override
314 public void setBackground(Color color) {
315 label.setBackground(color);
316 }
317
318 /** {@inheritDoc} */
319 @Override
320 public void widgetDefaultSelected(SelectionEvent e) {
321 }
322
323 public void setVisibleItemCount(int visibleItems){
324 combo.setVisibleItemCount(visibleItems);
325 }
326
327 private Collection<T> getElementsForClass(Class<T> clazz){
328 CdmEnumDataHolder<T> dataHolder = getCdmEnumDataHolderForClass(clazz);
329 if (dataHolder != null) {
330 return dataHolder.getElements();
331 }
332 return null;
333 }
334
335 private CdmEnumDataHolder<T> getCdmEnumDataHolderForClass(Class<T> clazz){
336 for (CdmEnumDataHolder dataHolder : cdmEnumDataHolders) {
337 if (dataHolder.getClazz().equals(clazz)){
338 return dataHolder;
339 }
340 }
341 return null;
342 }
343
344 private interface CdmEnumDataHolder<T> {
345 Class<T> getClazz();
346 String getName();
347 Collection<T> getElements();
348 }
349 }