merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / TextWithLabelElement.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.ui.element;
5
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.events.KeyAdapter;
8 import org.eclipse.swt.events.KeyEvent;
9 import org.eclipse.swt.events.ModifyEvent;
10 import org.eclipse.swt.events.ModifyListener;
11 import org.eclipse.swt.graphics.Color;
12 import org.eclipse.swt.widgets.Control;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Listener;
15 import org.eclipse.swt.widgets.Text;
16 import org.eclipse.ui.forms.widgets.TableWrapData;
17
18 import eu.etaxonomy.cdm.common.CdmUtils;
19 import eu.etaxonomy.taxeditor.preference.Resources;
20 import eu.etaxonomy.taxeditor.ui.campanula.compatibility.ICdmFormElement;
21
22 /**
23 * <p>
24 * TextWithLabelElement class.
25 * </p>
26 *
27 * @author n.hoffmann
28 * @version $Id: $
29 */
30 public class TextWithLabelElement extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement,
31 ISelectable {
32
33 protected Text text;
34 private Label label;
35
36 /** Constant <code>MAX_HEIGHT=0</code> */
37 public static final int MAX_HEIGHT = 0;
38 /** Constant <code>SINGLE=-1</code> */
39 public static final int SINGLE = -1;
40
41 /**
42 * <p>
43 * Constructor for TextWithLabelElement.
44 * </p>
45 *
46 * @param formFactory
47 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
48 * object.
49 * @param parentElement
50 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
51 * object.
52 * @param labelString
53 * a {@link java.lang.String} object.
54 * @param initialText
55 * a {@link java.lang.String} object.
56 * @param textHeight
57 * a {@link java.lang.Integer} object.
58 * @param style
59 * a int.
60 * @wbp.parser.entryPoint
61 */
62 protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
63 String initialText, Integer textHeight, int style) {
64 super(formFactory, parentElement);
65
66 if (labelString != null) {
67 label = formFactory.createLabel(getLayoutComposite(), CdmUtils.Nz(labelString), SWT.NULL);
68 addControl(label);
69 label.setLayoutData(LayoutConstants.LEFT());
70 }
71
72 int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
73
74 int combinedStyle = style | SWT.BORDER | scrollStyle;
75
76 // SWT.PASSWORD does not work when SWT.WRAP is set.
77 if (style != SWT.PASSWORD) {
78 combinedStyle = combinedStyle | SWT.WRAP;
79 }
80
81 text = formFactory.createText(getLayoutComposite(), "", combinedStyle);
82
83 addControl(text);
84
85 // text.setWO
86
87 if (textHeight == null) {
88 text.addKeyListener(new KeyAdapter() {
89 @Override
90 public void keyPressed(KeyEvent e) {
91 if (e.character == SWT.CR) {
92 // Don't accept carriage returns as input when in single
93 // line mode
94 e.doit = false;
95 } else if (e.character == SWT.TAB) {
96 // traverse is not working for wrapped text widgets so
97 // we reintroduce it here
98 e.doit = false;
99 TextWithLabelElement.this.text.traverse(SWT.TRAVERSE_TAB_NEXT);
100 }
101 }
102 });
103 }
104
105 TableWrapData layoutData = LayoutConstants.FILL();
106 if (textHeight != null && textHeight > 0) {
107 (layoutData).heightHint = textHeight;
108 }
109
110 text.setLayoutData(layoutData);
111
112 text.addModifyListener(this);
113
114 setText(initialText);
115 }
116
117 /**
118 * Get the text of this composites text composite
119 *
120 * @return a {@link java.lang.String} object.
121 */
122 public String getText() {
123 return text.getText();
124 }
125
126 /**
127 * Set the text of this composites text composite
128 *
129 * @param string
130 * a {@link java.lang.String} object.
131 */
132 public void setText(String string) {
133 Listener[] listeners = text.getListeners(SWT.Modify);
134
135 for (Listener listener : listeners) {
136 text.removeListener(SWT.Modify, listener);
137 }
138
139 text.setText(CdmUtils.Nz(string));
140
141 for (Listener listener : listeners) {
142 text.addListener(SWT.Modify, listener);
143 }
144 }
145
146 /*
147 * (non-Javadoc)
148 *
149 * @see
150 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
151 * .ModifyEvent)
152 */
153 /** {@inheritDoc} */
154 @Override
155 public void modifyText(ModifyEvent e) {
156 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
157 }
158
159 /** {@inheritDoc} */
160 @Override
161 public void setEnabled(boolean enabled) {
162 text.setEnabled(enabled);
163 String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
164 text.setForeground(getColor(symbolicName));
165 }
166
167 /** {@inheritDoc} */
168 @Override
169 public void setIrrelevant(boolean irrelevant) {
170 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
171
172 Color color = getColor(colorId);
173 text.setBackground(color);
174 }
175
176 /** {@inheritDoc} */
177 @Override
178 public void setBackground(Color color) {
179 if (label != null) {
180 label.setBackground(color);
181 }
182 }
183
184 @Override
185 public void setSelected(boolean selected) {
186 setBackground(selected ? SELECTED : getPersistentBackground());
187 }
188
189 /*
190 * (non-Javadoc)
191 *
192 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
193 */
194 /** {@inheritDoc} */
195 @Override
196 public void setFocus() {
197 text.setFocus();
198 }
199
200 /**
201 * <p>
202 * getMainControl
203 * </p>
204 *
205 * @return a {@link org.eclipse.swt.widgets.Control} object.
206 */
207 public Control getMainControl() {
208 return text;
209 }
210
211 /**
212 * <p>
213 * setTextLimit
214 * </p>
215 *
216 * @param limit
217 * a int.
218 */
219 public void setTextLimit(int limit) {
220 text.setTextLimit(limit);
221 }
222 }