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.events.SelectionAdapter;
|
12
|
import org.eclipse.swt.events.SelectionEvent;
|
13
|
import org.eclipse.swt.graphics.Color;
|
14
|
import org.eclipse.swt.graphics.Point;
|
15
|
import org.eclipse.swt.layout.GridData;
|
16
|
import org.eclipse.swt.widgets.Button;
|
17
|
import org.eclipse.swt.widgets.Composite;
|
18
|
import org.eclipse.swt.widgets.Control;
|
19
|
import org.eclipse.swt.widgets.Label;
|
20
|
import org.eclipse.swt.widgets.Listener;
|
21
|
import org.eclipse.swt.widgets.Text;
|
22
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
23
|
|
24
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
25
|
import eu.etaxonomy.cdm.model.common.IntextReference;
|
26
|
import eu.etaxonomy.cdm.model.common.IntextReferenceHelper;
|
27
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
28
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
29
|
import eu.etaxonomy.taxeditor.ui.section.description.IntextReferenceDetailElement;
|
30
|
import eu.etaxonomy.taxeditor.ui.section.description.IntextReferenceSection;
|
31
|
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
|
32
|
|
33
|
/**
|
34
|
* @author n.hoffmann
|
35
|
* @version $Id: $
|
36
|
*/
|
37
|
public class TextWithLabelElement extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement,
|
38
|
ISelectable {
|
39
|
|
40
|
protected Text text;
|
41
|
private Label label;
|
42
|
|
43
|
|
44
|
|
45
|
private final boolean isMultiLine;
|
46
|
|
47
|
public static final int MAX_HEIGHT = 0;
|
48
|
public static final int SINGLE = -1;
|
49
|
|
50
|
protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, boolean isMultiLine) {
|
51
|
super(formFactory, parentElement);
|
52
|
this.isMultiLine = isMultiLine;
|
53
|
}
|
54
|
|
55
|
protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
56
|
String initialText, Integer textHeight, int style) {
|
57
|
this(formFactory, parentElement, labelString, initialText, textHeight, null, false, style);
|
58
|
}
|
59
|
|
60
|
protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
61
|
String initialText, Integer textHeight, boolean isMultiLine, int style) {
|
62
|
this(formFactory, parentElement, labelString, initialText, textHeight, null, isMultiLine, style);
|
63
|
}
|
64
|
|
65
|
protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
66
|
String initialText, Integer textHeight, Integer textLimit, int style) {
|
67
|
this(formFactory, parentElement, labelString, initialText, textHeight, textLimit, false, style);
|
68
|
}
|
69
|
|
70
|
protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
|
71
|
String initialText, Integer textHeight, Integer textLimit, boolean isMultiLine, int style) {
|
72
|
super(formFactory, parentElement);
|
73
|
|
74
|
this.isMultiLine = isMultiLine;
|
75
|
|
76
|
initLabel(formFactory, labelString, isMultiLine, getLayoutComposite());
|
77
|
|
78
|
initText(formFactory, initialText, textHeight, textLimit, isMultiLine, style, getLayoutComposite());
|
79
|
}
|
80
|
|
81
|
protected void initText(CdmFormFactory formFactory, String initialText, Integer textHeight, Integer textLimit,
|
82
|
boolean isMultiLine, int style, Composite layoutComposite) {
|
83
|
int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
|
84
|
|
85
|
int combinedStyle = style | SWT.BORDER | scrollStyle;
|
86
|
|
87
|
// SWT.PASSWORD does not work when SWT.WRAP is set.
|
88
|
if (style != SWT.PASSWORD) {
|
89
|
combinedStyle = combinedStyle | SWT.WRAP;
|
90
|
}
|
91
|
|
92
|
text = formFactory.createText(layoutComposite, "", combinedStyle);
|
93
|
text.setTextLimit(textLimit!=null?textLimit:Text.LIMIT);
|
94
|
|
95
|
addControl(text);
|
96
|
|
97
|
// text.setWO
|
98
|
|
99
|
if (textHeight == null) {
|
100
|
text.addKeyListener(new KeyAdapter() {
|
101
|
@Override
|
102
|
public void keyPressed(KeyEvent e) {
|
103
|
if (e.character == SWT.CR) {
|
104
|
// Don't accept carriage returns as input when in single
|
105
|
// line mode
|
106
|
e.doit = false;
|
107
|
} else if (e.character == SWT.TAB) {
|
108
|
// traverse is not working for wrapped text widgets so
|
109
|
// we reintroduce it here
|
110
|
e.doit = false;
|
111
|
TextWithLabelElement.this.text.traverse(SWT.TRAVERSE_TAB_NEXT);
|
112
|
}
|
113
|
}
|
114
|
});
|
115
|
}
|
116
|
|
117
|
TableWrapData layoutData;
|
118
|
if(isMultiLine){
|
119
|
layoutData = LayoutConstants.FILL_HORIZONTALLY(2, 1);
|
120
|
}
|
121
|
else{
|
122
|
layoutData = LayoutConstants.FILL();
|
123
|
}
|
124
|
if (textHeight != null && textHeight > 0) {
|
125
|
(layoutData).heightHint = textHeight;
|
126
|
}
|
127
|
|
128
|
text.setLayoutData(layoutData);
|
129
|
|
130
|
text.addModifyListener(this);
|
131
|
|
132
|
setText(initialText);
|
133
|
|
134
|
|
135
|
|
136
|
}
|
137
|
|
138
|
protected void initLabel(CdmFormFactory formFactory, String labelString, boolean isMultiLine, Composite layoutComposite) {
|
139
|
if (labelString != null) {
|
140
|
label = formFactory.createLabel(layoutComposite, CdmUtils.Nz(labelString), SWT.NULL);
|
141
|
|
142
|
addControl(label);
|
143
|
if(isMultiLine){
|
144
|
label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
145
|
((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
|
146
|
}
|
147
|
else{
|
148
|
label.setLayoutData(LayoutConstants.LEFT());
|
149
|
((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
|
150
|
}
|
151
|
}
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* Get the text of this composites text composite
|
156
|
*
|
157
|
* @return a {@link java.lang.String} object.
|
158
|
*/
|
159
|
public String getText() {
|
160
|
|
161
|
return CdmUtils.Nb(text.getText());
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Set the text of this composites text composite
|
166
|
*
|
167
|
* @param string
|
168
|
* a {@link java.lang.String} object.
|
169
|
*/
|
170
|
public void setText(String string) {
|
171
|
Listener[] listeners = text.getListeners(SWT.Modify);
|
172
|
|
173
|
for (Listener listener : listeners) {
|
174
|
text.removeListener(SWT.Modify, listener);
|
175
|
}
|
176
|
|
177
|
text.setText(CdmUtils.Nz(string));
|
178
|
|
179
|
for (Listener listener : listeners) {
|
180
|
text.addListener(SWT.Modify, listener);
|
181
|
}
|
182
|
}
|
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
|
@Override
|
215
|
public boolean isEnabled() {
|
216
|
return text.isEnabled();
|
217
|
}
|
218
|
|
219
|
/** {@inheritDoc} */
|
220
|
@Override
|
221
|
public void setIrrelevant(boolean irrelevant) {
|
222
|
String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
|
223
|
|
224
|
Color color = getColor(colorId);
|
225
|
text.setBackground(color);
|
226
|
}
|
227
|
|
228
|
/** {@inheritDoc} */
|
229
|
@Override
|
230
|
public void setBackground(Color color) {
|
231
|
if (label != null) {
|
232
|
label.setBackground(color);
|
233
|
}
|
234
|
}
|
235
|
|
236
|
@Override
|
237
|
public void setSelected(boolean selected) {
|
238
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
239
|
}
|
240
|
|
241
|
/** {@inheritDoc} */
|
242
|
@Override
|
243
|
public void setFocus() {
|
244
|
text.setFocus();
|
245
|
}
|
246
|
|
247
|
public Control getMainControl() {
|
248
|
return text;
|
249
|
}
|
250
|
|
251
|
public void setTextLimit(int limit) {
|
252
|
text.setTextLimit(limit);
|
253
|
}
|
254
|
|
255
|
public Point getSelection(){
|
256
|
return text.getSelection();
|
257
|
}
|
258
|
|
259
|
}
|