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