efe1877a287fd27098b4bd04e4941c6109c7f69e
[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 * @author n.hoffmann
24 * @version $Id: $
25 */
26 public class TextWithLabelElement extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement,
27 ISelectable {
28
29 protected Text text;
30 private Label label;
31
32 private final boolean isMultiLine;
33
34 public static final int MAX_HEIGHT = 0;
35 public static final int SINGLE = -1;
36
37 protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, boolean isMultiLine) {
38 super(formFactory, parentElement);
39 this.isMultiLine = isMultiLine;
40 }
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 protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
58 String initialText, Integer textHeight, Integer textLimit, boolean isMultiLine, int style) {
59 super(formFactory, parentElement);
60
61 this.isMultiLine = isMultiLine;
62
63 init(formFactory, labelString, initialText, textHeight, textLimit, isMultiLine, style, getLayoutComposite());
64 }
65
66 protected void init(CdmFormFactory formFactory, String labelString, String initialText, Integer textHeight,
67 Integer textLimit, boolean isMultiLine, int style, Composite layoutComposite) {
68 if (labelString != null) {
69 label = formFactory.createLabel(layoutComposite, CdmUtils.Nz(labelString), SWT.NULL);
70 addControl(label);
71 if(isMultiLine){
72 label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
73 }
74 else{
75 label.setLayoutData(LayoutConstants.LEFT());
76 }
77 }
78
79 int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
80
81 int combinedStyle = style | SWT.BORDER | scrollStyle;
82
83 // SWT.PASSWORD does not work when SWT.WRAP is set.
84 if (style != SWT.PASSWORD) {
85 combinedStyle = combinedStyle | SWT.WRAP;
86 }
87
88 text = formFactory.createText(layoutComposite, "", combinedStyle);
89 text.setTextLimit(textLimit!=null?textLimit:Text.LIMIT);
90
91 addControl(text);
92
93 // text.setWO
94
95 if (textHeight == null) {
96 text.addKeyListener(new KeyAdapter() {
97 @Override
98 public void keyPressed(KeyEvent e) {
99 if (e.character == SWT.CR) {
100 // Don't accept carriage returns as input when in single
101 // line mode
102 e.doit = false;
103 } else if (e.character == SWT.TAB) {
104 // traverse is not working for wrapped text widgets so
105 // we reintroduce it here
106 e.doit = false;
107 TextWithLabelElement.this.text.traverse(SWT.TRAVERSE_TAB_NEXT);
108 }
109 }
110 });
111 }
112
113 TableWrapData layoutData;
114 if(isMultiLine){
115 layoutData = LayoutConstants.FILL_HORIZONTALLY(2, 1);
116 }
117 else{
118 layoutData = LayoutConstants.FILL();
119 }
120 if (textHeight != null && textHeight > 0) {
121 (layoutData).heightHint = textHeight;
122 }
123
124 text.setLayoutData(layoutData);
125
126 text.addModifyListener(this);
127
128 setText(initialText);
129 }
130
131 /**
132 * Get the text of this composites text composite
133 *
134 * @return a {@link java.lang.String} object.
135 */
136 public String getText() {
137 return text.getText();
138 }
139
140 /**
141 * Set the text of this composites text composite
142 *
143 * @param string
144 * a {@link java.lang.String} object.
145 */
146 public void setText(String string) {
147 Listener[] listeners = text.getListeners(SWT.Modify);
148
149 for (Listener listener : listeners) {
150 text.removeListener(SWT.Modify, listener);
151 }
152
153 text.setText(CdmUtils.Nz(string));
154
155 for (Listener listener : listeners) {
156 text.addListener(SWT.Modify, listener);
157 }
158 }
159
160 /** {@inheritDoc} */
161 @Override
162 public void modifyText(ModifyEvent e) {
163 if(e.widget==text && !isMultiLine){
164 Text text = (Text) e.widget;
165 boolean hasControlCharacters = false;
166 String textString = text.getText();
167 int stringLength = textString.length();
168 for (int i = 0; i < stringLength; i++) {
169 if (Character.isISOControl(textString.charAt(i))) {
170 hasControlCharacters = true;
171 break;
172 }
173 }
174 if(hasControlCharacters){
175 //remove control character such as line breaks etc.
176 setText(text.getText().replaceAll("\\p{C}", new Character((char)9608).toString()));
177 }
178 }
179 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
180 }
181
182 /** {@inheritDoc} */
183 @Override
184 public void setEnabled(boolean enabled) {
185 text.setEnabled(enabled);
186 String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
187 text.setForeground(getColor(symbolicName));
188 }
189
190 @Override
191 public boolean isEnabled() {
192 return text.isEnabled();
193 }
194
195 /** {@inheritDoc} */
196 @Override
197 public void setIrrelevant(boolean irrelevant) {
198 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
199
200 Color color = getColor(colorId);
201 text.setBackground(color);
202 }
203
204 /** {@inheritDoc} */
205 @Override
206 public void setBackground(Color color) {
207 if (label != null) {
208 label.setBackground(color);
209 }
210 }
211
212 @Override
213 public void setSelected(boolean selected) {
214 setBackground(selected ? SELECTED : getPersistentBackground());
215 }
216
217 /** {@inheritDoc} */
218 @Override
219 public void setFocus() {
220 text.setFocus();
221 }
222
223 public Control getMainControl() {
224 return text;
225 }
226
227 public void setTextLimit(int limit) {
228 text.setTextLimit(limit);
229 }
230
231 }