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