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