33263074f1bbd20b5b06ab5c2c113dc4c065710a
[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
30 ModifyListener, IEnableableFormElement, 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 */
60 protected TextWithLabelElement(CdmFormFactory formFactory,
61 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(),
67 CdmUtils.Nz(labelString), SWT.NULL);
68 addControl(label);
69 label.setLayoutData(LayoutConstants.LEFT());
70 }
71
72 int scrollStyle = textHeight == null ? SWT.NULL
73 : (SWT.V_SCROLL | SWT.MULTI);
74
75 int combinedStyle = style | SWT.BORDER | scrollStyle;
76
77 //SWT.PASSWORD does not work when SWT.WRAP is set.
78 if(style != SWT.PASSWORD){
79 combinedStyle = combinedStyle | SWT.WRAP;
80 }
81
82 text = formFactory.createText(getLayoutComposite(), "", combinedStyle);
83
84 addControl(text);
85
86 // text.setWO
87
88 if (textHeight == null) {
89 text.addKeyListener(new KeyAdapter() {
90 @Override
91 public void keyPressed(KeyEvent e) {
92 if (e.character == SWT.CR) {
93 // Don't accept carriage returns as input when in single
94 // line mode
95 e.doit = false;
96 } else if (e.character == SWT.TAB) {
97 // traverse is not working for wrapped text widgets so
98 // we reintroduce it here
99 e.doit = false;
100 TextWithLabelElement.this.text
101 .traverse(SWT.TRAVERSE_TAB_NEXT);
102 }
103 }
104 });
105 }
106
107 TableWrapData layoutData = LayoutConstants.FILL();
108 if (textHeight != null && textHeight > 0) {
109 (layoutData).heightHint = textHeight;
110 }
111
112 text.setLayoutData(layoutData);
113
114 text.addModifyListener(this);
115
116 setText(initialText);
117 }
118
119 /**
120 * Get the text of this composites text composite
121 *
122 * @return a {@link java.lang.String} object.
123 */
124 public String getText() {
125 return text.getText();
126 }
127
128 /**
129 * Set the text of this composites text composite
130 *
131 * @param string
132 * a {@link java.lang.String} object.
133 */
134 public void setText(String string) {
135 Listener[] listeners = text.getListeners(SWT.Modify);
136
137 for (Listener listener : listeners) {
138 text.removeListener(SWT.Modify, listener);
139 }
140
141 text.setText(CdmUtils.Nz(string));
142
143 for (Listener listener : listeners) {
144 text.addListener(SWT.Modify, listener);
145 }
146 }
147
148 /*
149 * (non-Javadoc)
150 *
151 * @see
152 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
153 * .ModifyEvent)
154 */
155 /** {@inheritDoc} */
156 public void modifyText(ModifyEvent e) {
157 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
158 }
159
160 /** {@inheritDoc} */
161 public void setEnabled(boolean enabled) {
162 text.setEnabled(enabled);
163 String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT
164 : Resources.COLOR_TEXT_DISABLED;
165 text.setForeground(getColor(symbolicName));
166 }
167
168 /** {@inheritDoc} */
169 public void setIrrelevant(boolean irrelevant) {
170 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
171 : Resources.COLOR_COMPOSITE_BACKGROUND;
172
173 Color color = getColor(colorId);
174 text.setBackground(color);
175 }
176
177 /** {@inheritDoc} */
178 @Override
179 public void setBackground(Color color) {
180 if (label != null)
181 label.setBackground(color);
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 }