d50920b87a5f13b38fc2a1b98efe9645532b083e
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / forms / TextWithLabelElement.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.ui.forms;
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 {
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.forms.CdmFormFactory}
47 * object.
48 * @param parentElement
49 * a {@link eu.etaxonomy.taxeditor.ui.forms.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(CdmFormFactory.LEFT());
70 }
71
72 int scrollStyle = textHeight == null ? SWT.NULL
73 : (SWT.V_SCROLL | SWT.MULTI);
74 text = formFactory.createText(getLayoutComposite(), "", SWT.BORDER
75 | SWT.WRAP | scrollStyle);
76
77 addControl(text);
78
79 // text.setWO
80
81 if (textHeight == null) {
82 text.addKeyListener(new KeyAdapter() {
83 @Override
84 public void keyPressed(KeyEvent e) {
85 if (e.character == SWT.CR) {
86 // Don't accept carriage returns as input when in single
87 // line mode
88 e.doit = false;
89 } else if (e.character == SWT.TAB) {
90 // traverse is not working for wrapped text widgets so
91 // we reintroduce it here
92 e.doit = false;
93 TextWithLabelElement.this.text
94 .traverse(SWT.TRAVERSE_TAB_NEXT);
95 }
96 }
97 });
98 }
99
100 TableWrapData layoutData = CdmFormFactory.FILL();
101 if (textHeight != null && textHeight > 0) {
102 (layoutData).heightHint = textHeight;
103 }
104
105 text.setLayoutData(layoutData);
106
107 text.addModifyListener(this);
108
109 setText(initialText);
110 }
111
112 /**
113 * Get the text of this composites text composite
114 *
115 * @return a {@link java.lang.String} object.
116 */
117 public String getText() {
118 return text.getText();
119 }
120
121 /**
122 * Set the text of this composites text composite
123 *
124 * @param string
125 * a {@link java.lang.String} object.
126 */
127 public void setText(String string) {
128 Listener[] listeners = text.getListeners(SWT.Modify);
129
130 for (Listener listener : listeners) {
131 text.removeListener(SWT.Modify, listener);
132 }
133
134 text.setText(CdmUtils.Nz(string));
135
136 for (Listener listener : listeners) {
137 text.addListener(SWT.Modify, listener);
138 }
139 }
140
141 /*
142 * (non-Javadoc)
143 *
144 * @see
145 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
146 * .ModifyEvent)
147 */
148 /** {@inheritDoc} */
149 public void modifyText(ModifyEvent e) {
150 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
151 }
152
153 /** {@inheritDoc} */
154 public void setSelected(boolean selected) {
155 setBackground(getColor(selected));
156 }
157
158 /** {@inheritDoc} */
159 public void setEnabled(boolean enabled) {
160 text.setEnabled(enabled);
161 String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT
162 : Resources.COLOR_TEXT_DISABLED;
163 text.setForeground(getColor(symbolicName));
164 }
165
166 /** {@inheritDoc} */
167 public void setIrrelevant(boolean irrelevant) {
168 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT
169 : 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 * (non-Javadoc)
184 *
185 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
186 */
187 /** {@inheritDoc} */
188 @Override
189 public void setFocus() {
190 text.setFocus();
191 }
192
193 /**
194 * <p>
195 * getMainControl
196 * </p>
197 *
198 * @return a {@link org.eclipse.swt.widgets.Control} object.
199 */
200 public Control getMainControl() {
201 return text;
202 }
203
204 /**
205 * <p>
206 * setTextLimit
207 * </p>
208 *
209 * @param limit
210 * a int.
211 */
212 public void setTextLimit(int limit) {
213 text.setTextLimit(limit);
214 }
215 }