4 package eu
.etaxonomy
.taxeditor
.ui
.element
;
6 import org
.apache
.commons
.lang
.StringUtils
;
7 import org
.eclipse
.swt
.SWT
;
8 import org
.eclipse
.swt
.events
.KeyAdapter
;
9 import org
.eclipse
.swt
.events
.KeyEvent
;
10 import org
.eclipse
.swt
.events
.ModifyEvent
;
11 import org
.eclipse
.swt
.events
.ModifyListener
;
12 import org
.eclipse
.swt
.graphics
.Color
;
13 import org
.eclipse
.swt
.widgets
.Control
;
14 import org
.eclipse
.swt
.widgets
.Label
;
15 import org
.eclipse
.swt
.widgets
.Listener
;
16 import org
.eclipse
.swt
.widgets
.Text
;
17 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
19 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
20 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
24 * TextWithLabelElement class.
30 public class TextWithLabelElement
extends AbstractCdmFormElement
implements ModifyListener
, IEnableableFormElement
,
36 private final boolean isMultiLine
;
38 /** Constant <code>MAX_HEIGHT=0</code> */
39 public static final int MAX_HEIGHT
= 0;
40 /** Constant <code>SINGLE=-1</code> */
41 public static final int SINGLE
= -1;
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
);
60 * Constructor for TextWithLabelElement.
64 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
66 * @param parentElement
67 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
70 * a {@link java.lang.String} object.
72 * a {@link java.lang.String} object.
74 * a {@link java.lang.Integer} object.
75 * @param textLimit max characters allowed to enter
78 * @wbp.parser.entryPoint
80 protected TextWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String labelString
,
81 String initialText
, Integer textHeight
, Integer textLimit
, boolean isMultiLine
, int style
) {
82 super(formFactory
, parentElement
);
84 this.isMultiLine
= isMultiLine
;
86 if (labelString
!= null) {
87 label
= formFactory
.createLabel(getLayoutComposite(), CdmUtils
.Nz(labelString
), SWT
.NULL
);
90 label
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
93 label
.setLayoutData(LayoutConstants
.LEFT());
97 int scrollStyle
= textHeight
== null ? SWT
.NULL
: (SWT
.V_SCROLL
| SWT
.MULTI
);
99 int combinedStyle
= style
| SWT
.BORDER
| scrollStyle
;
101 // SWT.PASSWORD does not work when SWT.WRAP is set.
102 if (style
!= SWT
.PASSWORD
) {
103 combinedStyle
= combinedStyle
| SWT
.WRAP
;
106 text
= formFactory
.createText(getLayoutComposite(), "", combinedStyle
);
107 text
.setTextLimit(textLimit
!=null?textLimit
:Text
.LIMIT
);
113 if (textHeight
== null) {
114 text
.addKeyListener(new KeyAdapter() {
116 public void keyPressed(KeyEvent e
) {
117 if (e
.character
== SWT
.CR
) {
118 // Don't accept carriage returns as input when in single
121 } else if (e
.character
== SWT
.TAB
) {
122 // traverse is not working for wrapped text widgets so
123 // we reintroduce it here
125 TextWithLabelElement
.this.text
.traverse(SWT
.TRAVERSE_TAB_NEXT
);
131 TableWrapData layoutData
;
133 layoutData
= LayoutConstants
.FILL_HORIZONTALLY(2, 1);
136 layoutData
= LayoutConstants
.FILL();
138 if (textHeight
!= null && textHeight
> 0) {
139 (layoutData
).heightHint
= textHeight
;
142 text
.setLayoutData(layoutData
);
144 text
.addModifyListener(this);
146 setText(initialText
);
150 * Get the text of this composites text composite
152 * @return a {@link java.lang.String} object.
154 public String
getText() {
155 if (StringUtils
.isBlank(text
.getText())){
158 return text
.getText();
163 * Set the text of this composites text composite
166 * a {@link java.lang.String} object.
168 public void setText(String string
) {
169 Listener
[] listeners
= text
.getListeners(SWT
.Modify
);
171 for (Listener listener
: listeners
) {
172 text
.removeListener(SWT
.Modify
, listener
);
175 text
.setText(CdmUtils
.Nz(string
));
177 for (Listener listener
: listeners
) {
178 text
.addListener(SWT
.Modify
, listener
);
186 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
191 public void modifyText(ModifyEvent e
) {
192 if(e
.widget
==text
&& !isMultiLine
){
193 Text text
= (Text
) e
.widget
;
194 boolean hasControlCharacters
= false;
195 String textString
= text
.getText();
196 int stringLength
= textString
.length();
197 for (int i
= 0; i
< stringLength
; i
++) {
198 if (Character
.isISOControl(textString
.charAt(i
))) {
199 hasControlCharacters
= true;
203 if(hasControlCharacters
){
204 //remove control character such as line breaks etc.
205 setText(text
.getText().replaceAll("\\p{C}", new Character((char)9608).toString()));
208 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
213 public void setEnabled(boolean enabled
) {
214 text
.setEnabled(enabled
);
215 String symbolicName
= enabled ? Resources
.COLOR_FONT_DEFAULT
: Resources
.COLOR_TEXT_DISABLED
;
216 text
.setForeground(getColor(symbolicName
));
220 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
223 public boolean isEnabled() {
224 return text
.isEnabled();
229 public void setIrrelevant(boolean irrelevant
) {
230 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
232 Color color
= getColor(colorId
);
233 text
.setBackground(color
);
238 public void setBackground(Color color
) {
240 label
.setBackground(color
);
245 public void setSelected(boolean selected
) {
246 setBackground(selected ? SELECTED
: getPersistentBackground());
252 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
256 public void setFocus() {
265 * @return a {@link org.eclipse.swt.widgets.Control} object.
267 public Control
getMainControl() {
279 public void setTextLimit(int limit
) {
280 text
.setTextLimit(limit
);