Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / combo / AbstractComboElement.java
1 /**
2 * Copyright (C) 2016 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 package eu.etaxonomy.taxeditor.ui.combo;
10
11 import org.eclipse.jface.viewers.ComboViewer;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.events.DisposeEvent;
14 import org.eclipse.swt.events.DisposeListener;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.graphics.Color;
18 import org.eclipse.swt.widgets.Combo;
19 import org.eclipse.swt.widgets.Label;
20 import org.eclipse.ui.forms.widgets.TableWrapData;
21
22 import eu.etaxonomy.taxeditor.model.AbstractUtility;
23 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
24 import eu.etaxonomy.taxeditor.preference.Resources;
25 import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
26 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
28 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
29 import eu.etaxonomy.taxeditor.ui.element.ISelectable;
30 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
31
32 /**
33 * @author pplitzner
34 * @date Aug 11, 2016
35 *
36 */
37 public abstract class AbstractComboElement<T> extends
38 AbstractCdmFormElement implements SelectionListener,
39 IEnableableFormElement, ISelectable,
40 DisposeListener {
41
42 protected static final int DEFAULT_VISIBLE_ITEMS = 10;
43
44 protected T selection;
45
46 protected Label label;
47
48 protected final Combo combo;
49
50
51 public AbstractComboElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
52 super(formFactory, formElement);
53
54 label = formFactory.createLabel(getLayoutComposite(), "");
55 addControl(label);
56
57 // create combo
58 ComboViewer viewer = new ComboViewer(getLayoutComposite(), SWT.BORDER | SWT.READ_ONLY );
59 combo = viewer.getCombo();
60 addControl(combo);
61 TableWrapData fill_HORIZONTALLY = LayoutConstants.FILL_HORIZONTALLY();
62 combo.setLayoutData(fill_HORIZONTALLY);
63 fill_HORIZONTALLY.maxWidth = 50;
64 combo.setVisibleItemCount(DEFAULT_VISIBLE_ITEMS);
65 }
66
67 /** {@inheritDoc} */
68 @Override
69 public void setBackground(Color color) {
70 if (label != null) {
71 label.setBackground(color);
72 }
73 }
74
75 /** {@inheritDoc} */
76 @Override
77 public void setIrrelevant(boolean irrelevant) {
78 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
79 : Resources.COLOR_COMPOSITE_BACKGROUND;
80
81 Color color = AbstractUtility.getColor(colorId);
82 combo.setBackground(color);
83 if (label != null) {
84 label.setBackground(color);
85 }
86 colorId = !irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
87 : Resources.COLOR_COMPOSITE_BACKGROUND;
88 color = AbstractUtility.getColor(colorId);
89 combo.setForeground(color);
90
91 }
92
93 public void setVisibleItemCount(int count){
94 combo.setVisibleItemCount(count);
95 }
96
97 /** {@inheritDoc} */
98 @Override
99 public void setSelected(boolean selected) {
100 setBackground(selected ? SELECTED : getPersistentBackground());
101 }
102
103 public T getSelection() {
104 return selection;
105 }
106
107 public void addSelectionListener(SelectionListener listener) {
108 combo.addSelectionListener(listener);
109 }
110
111 public void removeSelectionListener(SelectionListener listener) {
112 combo.removeSelectionListener(listener);
113 }
114
115 /** {@inheritDoc} */
116 @Override
117 public void widgetDisposed(DisposeEvent e) {
118 PreferencesUtil.getPreferenceStore().removePropertyChangeListener(this);
119 }
120
121 @Override
122 public void widgetDefaultSelected(SelectionEvent e) {
123 }
124
125 @Override
126 public boolean isEnabled() {
127 return combo.isEnabled();
128 }
129
130 /** {@inheritDoc} */
131 @Override
132 public void setEnabled(boolean enabled) {
133 combo.setEnabled(enabled);
134 }
135
136 public abstract void setSelection(T selection);
137 }