d5f7b310542dc1212a42e43036f53a09078d8417
[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 private final boolean isMultiLine;
36
37 /** Constant <code>MAX_HEIGHT=0</code> */
38 public static final int MAX_HEIGHT = 0;
39 /** Constant <code>SINGLE=-1</code> */
40 public static final int SINGLE = -1;
41
42 protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
43 String initialText, Integer textHeight, int style) {
44 this(formFactory, parentElement, labelString, initialText, textHeight, null, false, style);
45 }
46
47 protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
48 String initialText, Integer textHeight, boolean isMultiLine, int style) {
49 this(formFactory, parentElement, labelString, initialText, textHeight, null, isMultiLine, style);
50 }
51
52 protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
53 String initialText, Integer textHeight, Integer textLimit, int style) {
54 this(formFactory, parentElement, labelString, initialText, textHeight, textLimit, false, style);
55 }
56
57 /**
58 * <p>
59 * Constructor for TextWithLabelElement.
60 * </p>
61 *
62 * @param formFactory
63 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
64 * object.
65 * @param parentElement
66 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
67 * object.
68 * @param labelString
69 * a {@link java.lang.String} object.
70 * @param initialText
71 * a {@link java.lang.String} object.
72 * @param textHeight
73 * a {@link java.lang.Integer} object.
74 * @param textLimit max characters allowed to enter
75 * @param style
76 * a int.
77 * @wbp.parser.entryPoint
78 */
79 protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
80 String initialText, Integer textHeight, Integer textLimit, boolean isMultiLine, int style) {
81 super(formFactory, parentElement);
82
83 this.isMultiLine = isMultiLine;
84
85 if (labelString != null) {
86 label = formFactory.createLabel(getLayoutComposite(), CdmUtils.Nz(labelString), SWT.NULL);
87 addControl(label);
88 if(isMultiLine){
89 label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
90 }
91 else{
92 label.setLayoutData(LayoutConstants.LEFT());
93 }
94 }
95
96 int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
97
98 int combinedStyle = style | SWT.BORDER | scrollStyle;
99
100 // SWT.PASSWORD does not work when SWT.WRAP is set.
101 if (style != SWT.PASSWORD) {
102 combinedStyle = combinedStyle | SWT.WRAP;
103 }
104
105 text = formFactory.createText(getLayoutComposite(), "", combinedStyle);
106 text.setTextLimit(textLimit!=null?textLimit:Text.LIMIT);
107
108 addControl(text);
109
110 // text.setWO
111
112 if (textHeight == null) {
113 text.addKeyListener(new KeyAdapter() {
114 @Override
115 public void keyPressed(KeyEvent e) {
116 if (e.character == SWT.CR) {
117 // Don't accept carriage returns as input when in single
118 // line mode
119 e.doit = false;
120 } else if (e.character == SWT.TAB) {
121 // traverse is not working for wrapped text widgets so
122 // we reintroduce it here
123 e.doit = false;
124 TextWithLabelElement.this.text.traverse(SWT.TRAVERSE_TAB_NEXT);
125 }
126 }
127 });
128 }
129
130 TableWrapData layoutData;
131 if(isMultiLine){
132 layoutData = LayoutConstants.FILL_HORIZONTALLY(2, 1);
133 }
134 else{
135 layoutData = LayoutConstants.FILL();
136 }
137 if (textHeight != null && textHeight > 0) {
138 (layoutData).heightHint = textHeight;
139 }
140
141 text.setLayoutData(layoutData);
142
143 text.addModifyListener(this);
144
145 setText(initialText);
146 }
147
148 /**
149 * Get the text of this composites text composite
150 *
151 * @return a {@link java.lang.String} object.
152 */
153 public String getText() {
154 return text.getText();
155 }
156
157 /**
158 * Set the text of this composites text composite
159 *
160 * @param string
161 * a {@link java.lang.String} object.
162 */
163 public void setText(String string) {
164 Listener[] listeners = text.getListeners(SWT.Modify);
165
166 for (Listener listener : listeners) {
167 text.removeListener(SWT.Modify, listener);
168 }
169
170 text.setText(CdmUtils.Nz(string));
171
172 for (Listener listener : listeners) {
173 text.addListener(SWT.Modify, listener);
174 }
175 }
176
177 /*
178 * (non-Javadoc)
179 *
180 * @see
181 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
182 * .ModifyEvent)
183 */
184 /** {@inheritDoc} */
185 @Override
186 public void modifyText(ModifyEvent e) {
187 if(e.widget==text && !isMultiLine){
188 Text text = (Text) e.widget;
189 boolean hasControlCharacters = false;
190 String textString = text.getText();
191 int stringLength = textString.length();
192 for (int i = 0; i < stringLength; i++) {
193 if (Character.isISOControl(textString.charAt(i))) {
194 hasControlCharacters = true;
195 break;
196 }
197 }
198 if(hasControlCharacters){
199 //remove control character such as line breaks etc.
200 setText(text.getText().replaceAll("\\p{C}", new Character((char)9608).toString()));
201 }
202 }
203 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
204 }
205
206 /** {@inheritDoc} */
207 @Override
208 public void setEnabled(boolean enabled) {
209 text.setEnabled(enabled);
210 String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
211 text.setForeground(getColor(symbolicName));
212 }
213
214 /* (non-Javadoc)
215 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
216 */
217 @Override
218 public boolean isEnabled() {
219 return text.isEnabled();
220 }
221
222 /** {@inheritDoc} */
223 @Override
224 public void setIrrelevant(boolean irrelevant) {
225 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
226
227 Color color = getColor(colorId);
228 text.setBackground(color);
229 }
230
231 /** {@inheritDoc} */
232 @Override
233 public void setBackground(Color color) {
234 if (label != null) {
235 label.setBackground(color);
236 }
237 }
238
239 @Override
240 public void setSelected(boolean selected) {
241 setBackground(selected ? SELECTED : getPersistentBackground());
242 }
243
244 /*
245 * (non-Javadoc)
246 *
247 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
248 */
249 /** {@inheritDoc} */
250 @Override
251 public void setFocus() {
252 text.setFocus();
253 }
254
255 /**
256 * <p>
257 * getMainControl
258 * </p>
259 *
260 * @return a {@link org.eclipse.swt.widgets.Control} object.
261 */
262 public Control getMainControl() {
263 return text;
264 }
265
266 /**
267 * <p>
268 * setTextLimit
269 * </p>
270 *
271 * @param limit
272 * a int.
273 */
274 public void setTextLimit(int limit) {
275 text.setTextLimit(limit);
276 }
277
278 }