4 package eu
.etaxonomy
.taxeditor
.ui
.element
;
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
.layout
.GridData
;
13 import org
.eclipse
.swt
.widgets
.Composite
;
14 import org
.eclipse
.swt
.widgets
.Control
;
15 import org
.eclipse
.swt
.widgets
.Label
;
16 import org
.eclipse
.swt
.widgets
.Listener
;
17 import org
.eclipse
.swt
.widgets
.Text
;
18 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
20 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
21 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
27 public class TextWithLabelElement
extends AbstractCdmFormElement
implements ModifyListener
, IEnableableFormElement
,
33 private final boolean isMultiLine
;
35 public static final int MAX_HEIGHT
= 0;
36 public static final int SINGLE
= -1;
38 protected TextWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, boolean isMultiLine
) {
39 super(formFactory
, parentElement
);
40 this.isMultiLine
= isMultiLine
;
43 protected TextWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String labelString
,
44 String initialText
, Integer textHeight
, int style
) {
45 this(formFactory
, parentElement
, labelString
, initialText
, textHeight
, null, false, style
);
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
);
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
);
58 protected TextWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String labelString
,
59 String initialText
, Integer textHeight
, Integer textLimit
, boolean isMultiLine
, int style
) {
60 super(formFactory
, parentElement
);
62 this.isMultiLine
= isMultiLine
;
64 initLabel(formFactory
, labelString
, isMultiLine
, getLayoutComposite());
66 initText(formFactory
, initialText
, textHeight
, textLimit
, isMultiLine
, style
, getLayoutComposite());
69 protected void initText(CdmFormFactory formFactory
, String initialText
, Integer textHeight
, Integer textLimit
,
70 boolean isMultiLine
, int style
, Composite layoutComposite
) {
71 int scrollStyle
= textHeight
== null ? SWT
.NULL
: (SWT
.V_SCROLL
| SWT
.MULTI
);
73 int combinedStyle
= style
| SWT
.BORDER
| scrollStyle
;
75 // SWT.PASSWORD does not work when SWT.WRAP is set.
76 if (style
!= SWT
.PASSWORD
) {
77 combinedStyle
= combinedStyle
| SWT
.WRAP
;
80 text
= formFactory
.createText(layoutComposite
, "", combinedStyle
);
81 text
.setTextLimit(textLimit
!=null?textLimit
:Text
.LIMIT
);
87 if (textHeight
== null) {
88 text
.addKeyListener(new KeyAdapter() {
90 public void keyPressed(KeyEvent e
) {
91 if (e
.character
== SWT
.CR
) {
92 // Don't accept carriage returns as input when in single
95 } else if (e
.character
== SWT
.TAB
) {
96 // traverse is not working for wrapped text widgets so
97 // we reintroduce it here
99 TextWithLabelElement
.this.text
.traverse(SWT
.TRAVERSE_TAB_NEXT
);
105 TableWrapData layoutData
;
107 layoutData
= LayoutConstants
.FILL_HORIZONTALLY(2, 1);
110 layoutData
= LayoutConstants
.FILL();
112 if (textHeight
!= null && textHeight
> 0) {
113 (layoutData
).heightHint
= textHeight
;
116 text
.setLayoutData(layoutData
);
118 text
.addModifyListener(this);
120 setText(initialText
);
123 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
);
129 label
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
130 ((TableWrapData
)label
.getLayoutData()).valign
= TableWrapData
.MIDDLE
;
133 label
.setLayoutData(LayoutConstants
.LEFT());
134 ((TableWrapData
)label
.getLayoutData()).valign
= TableWrapData
.MIDDLE
;
140 * Get the text of this composites text composite
142 * @return a {@link java.lang.String} object.
144 public String
getText() {
146 return CdmUtils
.Nb(text
.getText());
150 * Set the text of this composites text composite
153 * a {@link java.lang.String} object.
155 public void setText(String string
) {
156 Listener
[] listeners
= text
.getListeners(SWT
.Modify
);
158 for (Listener listener
: listeners
) {
159 text
.removeListener(SWT
.Modify
, listener
);
162 text
.setText(CdmUtils
.Nz(string
));
164 for (Listener listener
: listeners
) {
165 text
.addListener(SWT
.Modify
, listener
);
171 public void modifyText(ModifyEvent e
) {
172 if(e
.widget
==text
&& !isMultiLine
){
173 Text text
= (Text
) e
.widget
;
174 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;
183 if(hasControlCharacters
){
184 //remove control character such as line breaks etc.
185 setText(text
.getText().replaceAll("\\p{C}", new Character((char)9608).toString()));
188 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
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
));
200 public boolean isEnabled() {
201 return text
.isEnabled();
206 public void setIrrelevant(boolean irrelevant
) {
207 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
209 Color color
= getColor(colorId
);
210 text
.setBackground(color
);
215 public void setBackground(Color color
) {
217 label
.setBackground(color
);
222 public void setSelected(boolean selected
) {
223 setBackground(selected ? SELECTED
: getPersistentBackground());
228 public void setFocus() {
232 public Control
getMainControl() {
236 public void setTextLimit(int limit
) {
237 text
.setTextLimit(limit
);