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