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
.widgets
.Control
;
13 import org
.eclipse
.swt
.widgets
.Label
;
14 import org
.eclipse
.swt
.widgets
.Listener
;
15 import org
.eclipse
.swt
.widgets
.Text
;
16 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
18 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
19 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
23 * TextWithLabelElement class.
29 public class TextWithLabelElement
extends AbstractCdmFormElement
implements ModifyListener
, IEnableableFormElement
,
35 private final boolean isMultiLine
;
37 /** Constant <code>MAX_HEIGHT=0</code> */
38 public static final int MAX_HEIGHT
= 0;
39 /** Constant <code>SINGLE=-1</code> */
40 public static final int SINGLE
= -1;
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
);
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
);
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
);
59 * Constructor for TextWithLabelElement.
63 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
65 * @param parentElement
66 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
69 * a {@link java.lang.String} object.
71 * a {@link java.lang.String} object.
73 * a {@link java.lang.Integer} object.
74 * @param textLimit max characters allowed to enter
77 * @wbp.parser.entryPoint
79 protected TextWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String labelString
,
80 String initialText
, Integer textHeight
, Integer textLimit
, boolean isMultiLine
, int style
) {
81 super(formFactory
, parentElement
);
83 this.isMultiLine
= isMultiLine
;
85 if (labelString
!= null) {
86 label
= formFactory
.createLabel(getLayoutComposite(), CdmUtils
.Nz(labelString
), SWT
.NULL
);
89 label
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
92 label
.setLayoutData(LayoutConstants
.LEFT());
96 int scrollStyle
= textHeight
== null ? SWT
.NULL
: (SWT
.V_SCROLL
| SWT
.MULTI
);
98 int combinedStyle
= style
| SWT
.BORDER
| scrollStyle
;
100 // SWT.PASSWORD does not work when SWT.WRAP is set.
101 if (style
!= SWT
.PASSWORD
) {
102 combinedStyle
= combinedStyle
| SWT
.WRAP
;
105 text
= formFactory
.createText(getLayoutComposite(), "", combinedStyle
);
106 text
.setTextLimit(textLimit
!=null?textLimit
:Text
.LIMIT
);
112 if (textHeight
== null) {
113 text
.addKeyListener(new KeyAdapter() {
115 public void keyPressed(KeyEvent e
) {
116 if (e
.character
== SWT
.CR
) {
117 // Don't accept carriage returns as input when in single
120 } else if (e
.character
== SWT
.TAB
) {
121 // traverse is not working for wrapped text widgets so
122 // we reintroduce it here
124 TextWithLabelElement
.this.text
.traverse(SWT
.TRAVERSE_TAB_NEXT
);
130 TableWrapData layoutData
;
132 layoutData
= LayoutConstants
.FILL_HORIZONTALLY(2, 1);
135 layoutData
= LayoutConstants
.FILL();
137 if (textHeight
!= null && textHeight
> 0) {
138 (layoutData
).heightHint
= textHeight
;
141 text
.setLayoutData(layoutData
);
143 text
.addModifyListener(this);
145 setText(initialText
);
149 * Get the text of this composites text composite
151 * @return a {@link java.lang.String} object.
153 public String
getText() {
154 return text
.getText();
158 * Set the text of this composites text composite
161 * a {@link java.lang.String} object.
163 public void setText(String string
) {
164 Listener
[] listeners
= text
.getListeners(SWT
.Modify
);
166 for (Listener listener
: listeners
) {
167 text
.removeListener(SWT
.Modify
, listener
);
170 text
.setText(CdmUtils
.Nz(string
));
172 for (Listener listener
: listeners
) {
173 text
.addListener(SWT
.Modify
, listener
);
181 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
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;
198 if(hasControlCharacters
){
199 //remove control character such as line breaks etc.
200 setText(text
.getText().replaceAll("\\p{C}", new Character((char)9608).toString()));
203 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
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
));
215 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
218 public boolean isEnabled() {
219 return text
.isEnabled();
224 public void setIrrelevant(boolean irrelevant
) {
225 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
227 Color color
= getColor(colorId
);
228 text
.setBackground(color
);
233 public void setBackground(Color color
) {
235 label
.setBackground(color
);
240 public void setSelected(boolean selected
) {
241 setBackground(selected ? SELECTED
: getPersistentBackground());
247 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
251 public void setFocus() {
260 * @return a {@link org.eclipse.swt.widgets.Control} object.
262 public Control
getMainControl() {
274 public void setTextLimit(int limit
) {
275 text
.setTextLimit(limit
);