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.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
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
21
|
|
22
|
/**
|
23
|
* @author n.hoffmann
|
24
|
*/
|
25
|
public class TextWithLabelElement
|
26
|
extends AbstractRelevanceFormElement
|
27
|
implements ModifyListener, IEnableableFormElement, 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
|
initLabel(formFactory, labelString, isMultiLine, getLayoutComposite());
|
64
|
|
65
|
initText(formFactory, initialText, textHeight, textLimit, isMultiLine, style, getLayoutComposite());
|
66
|
}
|
67
|
|
68
|
protected void initText(CdmFormFactory formFactory, String initialText, Integer textHeight, Integer textLimit,
|
69
|
boolean isMultiLine, int style, Composite layoutComposite) {
|
70
|
int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
|
71
|
|
72
|
int combinedStyle = style | SWT.BORDER | scrollStyle;
|
73
|
|
74
|
// SWT.PASSWORD does not work when SWT.WRAP is set.
|
75
|
if (style != SWT.PASSWORD) {
|
76
|
combinedStyle = combinedStyle | SWT.WRAP;
|
77
|
}
|
78
|
|
79
|
text = formFactory.createText(layoutComposite, "", combinedStyle);
|
80
|
text.setTextLimit(textLimit!=null?textLimit:Text.LIMIT);
|
81
|
|
82
|
addControl(text);
|
83
|
|
84
|
// text.setWO
|
85
|
|
86
|
if (textHeight == null) {
|
87
|
text.addKeyListener(new KeyAdapter() {
|
88
|
@Override
|
89
|
public void keyPressed(KeyEvent e) {
|
90
|
if (e.character == SWT.CR) {
|
91
|
// Don't accept carriage returns as input when in single
|
92
|
// line mode
|
93
|
e.doit = false;
|
94
|
} else if (e.character == SWT.TAB) {
|
95
|
// traverse is not working for wrapped text widgets so
|
96
|
// we reintroduce it here
|
97
|
e.doit = false;
|
98
|
TextWithLabelElement.this.text.traverse(SWT.TRAVERSE_TAB_NEXT);
|
99
|
}
|
100
|
}
|
101
|
});
|
102
|
}
|
103
|
|
104
|
TableWrapData layoutData;
|
105
|
if(isMultiLine){
|
106
|
layoutData = LayoutConstants.FILL_HORIZONTALLY(2, 1);
|
107
|
}
|
108
|
else{
|
109
|
layoutData = LayoutConstants.FILL();
|
110
|
}
|
111
|
if (textHeight != null && textHeight > 0) {
|
112
|
(layoutData).heightHint = textHeight;
|
113
|
}
|
114
|
|
115
|
text.setLayoutData(layoutData);
|
116
|
|
117
|
text.addModifyListener(this);
|
118
|
|
119
|
setText(initialText);
|
120
|
}
|
121
|
|
122
|
protected void initLabel(CdmFormFactory formFactory, String labelString, boolean isMultiLine, Composite layoutComposite) {
|
123
|
if (labelString != null) {
|
124
|
label = formFactory.createLabel(layoutComposite, CdmUtils.Nz(labelString), SWT.NULL);
|
125
|
|
126
|
addControl(label);
|
127
|
if(isMultiLine){
|
128
|
label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
129
|
((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
|
130
|
}
|
131
|
else{
|
132
|
label.setLayoutData(LayoutConstants.LEFT());
|
133
|
((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
|
134
|
}
|
135
|
}
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Get the text of this composites text composite
|
140
|
*
|
141
|
* @return a {@link java.lang.String} object.
|
142
|
*/
|
143
|
public String getText() {
|
144
|
if (!text.isDisposed()){
|
145
|
return CdmUtils.Nb(text.getText());
|
146
|
}else{
|
147
|
return null;
|
148
|
}
|
149
|
}
|
150
|
|
151
|
/**
|
152
|
* Set the text of this composites text composite
|
153
|
*
|
154
|
* @param string
|
155
|
* a {@link java.lang.String} object.
|
156
|
*/
|
157
|
public void setText(String string) {
|
158
|
StoreUtil.setTextWithoutModifyListeners(text, string != null? string.trim(): string);
|
159
|
}
|
160
|
|
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
|
@Override
|
183
|
public void setEnabled(boolean enabled) {
|
184
|
text.setEnabled(enabled);
|
185
|
String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
|
186
|
text.setForeground(getColor(symbolicName));
|
187
|
}
|
188
|
|
189
|
@Override
|
190
|
public boolean isEnabled() {
|
191
|
return text.isEnabled();
|
192
|
}
|
193
|
|
194
|
public void setEditable(boolean editable) {
|
195
|
text.setEditable(editable);
|
196
|
String symbolicName = editable ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
|
197
|
text.setForeground(getColor(symbolicName));
|
198
|
}
|
199
|
|
200
|
|
201
|
|
202
|
|
203
|
@Override
|
204
|
public void updateCacheRelevance() {
|
205
|
Color color = cacheRelevance().getColor();
|
206
|
text.setBackground(color);
|
207
|
}
|
208
|
|
209
|
@Override
|
210
|
public void setBackground(Color color) {
|
211
|
if (label != null) {
|
212
|
if (!label.isDisposed()){
|
213
|
label.setBackground(color);
|
214
|
}
|
215
|
}
|
216
|
}
|
217
|
|
218
|
@Override
|
219
|
public void setSelected(boolean selected) {
|
220
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public void setFocus() {
|
225
|
text.setFocus();
|
226
|
}
|
227
|
|
228
|
public Control getMainControl() {
|
229
|
return text;
|
230
|
}
|
231
|
|
232
|
public void setTextLimit(int limit) {
|
233
|
text.setTextLimit(limit);
|
234
|
}
|
235
|
|
236
|
public void setIndent(int indent){
|
237
|
TableWrapData tableWrapData = (TableWrapData)label.getLayoutData();
|
238
|
tableWrapData.indent = indent;
|
239
|
|
240
|
label.setLayoutData(tableWrapData);
|
241
|
if (isMultiLine){
|
242
|
tableWrapData = (TableWrapData)text.getLayoutData();
|
243
|
tableWrapData.indent = indent;
|
244
|
|
245
|
text.setLayoutData(tableWrapData);
|
246
|
}
|
247
|
getLayoutComposite().layout();
|
248
|
}
|
249
|
|
250
|
public void setTextForeground(Color color){
|
251
|
text.setForeground(color);
|
252
|
}
|
253
|
|
254
|
@Override
|
255
|
public String toString() {
|
256
|
if (label != null){
|
257
|
return CdmUtils.concat("", "TextWithLabelElement[", label.getText(),"]");
|
258
|
}else{
|
259
|
return super.toString();
|
260
|
}
|
261
|
}
|
262
|
|
263
|
public void setMessage(String message) {
|
264
|
text.setMessage(message);
|
265
|
}
|
266
|
}
|