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