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
.Composite
;
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
, boolean isMultiLine
) {
44 super(formFactory
, parentElement
);
45 this.isMultiLine
= isMultiLine
;
48 protected TextWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String labelString
,
49 String initialText
, Integer textHeight
, int style
) {
50 this(formFactory
, parentElement
, labelString
, initialText
, textHeight
, null, false, style
);
53 protected TextWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String labelString
,
54 String initialText
, Integer textHeight
, boolean isMultiLine
, int style
) {
55 this(formFactory
, parentElement
, labelString
, initialText
, textHeight
, null, isMultiLine
, style
);
58 protected TextWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String labelString
,
59 String initialText
, Integer textHeight
, Integer textLimit
, int style
) {
60 this(formFactory
, parentElement
, labelString
, initialText
, textHeight
, textLimit
, false, style
);
65 * Constructor for TextWithLabelElement.
69 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
71 * @param parentElement
72 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
75 * a {@link java.lang.String} object.
77 * a {@link java.lang.String} object.
79 * a {@link java.lang.Integer} object.
80 * @param textLimit max characters allowed to enter
83 * @wbp.parser.entryPoint
85 protected TextWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String labelString
,
86 String initialText
, Integer textHeight
, Integer textLimit
, boolean isMultiLine
, int style
) {
87 super(formFactory
, parentElement
);
89 this.isMultiLine
= isMultiLine
;
91 init(formFactory
, labelString
, initialText
, textHeight
, textLimit
, isMultiLine
, style
, getLayoutComposite());
102 * @param layoutComposite
104 protected void init(CdmFormFactory formFactory
, String labelString
, String initialText
, Integer textHeight
,
105 Integer textLimit
, boolean isMultiLine
, int style
, Composite layoutComposite
) {
106 if (labelString
!= null) {
107 label
= formFactory
.createLabel(layoutComposite
, CdmUtils
.Nz(labelString
), SWT
.NULL
);
110 label
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
113 label
.setLayoutData(LayoutConstants
.LEFT());
117 int scrollStyle
= textHeight
== null ? SWT
.NULL
: (SWT
.V_SCROLL
| SWT
.MULTI
);
119 int combinedStyle
= style
| SWT
.BORDER
| scrollStyle
;
121 // SWT.PASSWORD does not work when SWT.WRAP is set.
122 if (style
!= SWT
.PASSWORD
) {
123 combinedStyle
= combinedStyle
| SWT
.WRAP
;
126 text
= formFactory
.createText(layoutComposite
, "", combinedStyle
);
127 text
.setTextLimit(textLimit
!=null?textLimit
:Text
.LIMIT
);
133 if (textHeight
== null) {
134 text
.addKeyListener(new KeyAdapter() {
136 public void keyPressed(KeyEvent e
) {
137 if (e
.character
== SWT
.CR
) {
138 // Don't accept carriage returns as input when in single
141 } else if (e
.character
== SWT
.TAB
) {
142 // traverse is not working for wrapped text widgets so
143 // we reintroduce it here
145 TextWithLabelElement
.this.text
.traverse(SWT
.TRAVERSE_TAB_NEXT
);
151 TableWrapData layoutData
;
153 layoutData
= LayoutConstants
.FILL_HORIZONTALLY(2, 1);
156 layoutData
= LayoutConstants
.FILL();
157 layoutData
.grabHorizontal
= true;
159 if (textHeight
!= null && textHeight
> 0) {
160 (layoutData
).heightHint
= textHeight
;
163 text
.setLayoutData(layoutData
);
165 text
.addModifyListener(this);
167 setText(initialText
);
171 * Get the text of this composites text composite
173 * @return a {@link java.lang.String} object.
175 public String
getText() {
176 return text
.getText();
180 * Set the text of this composites text composite
183 * a {@link java.lang.String} object.
185 public void setText(String string
) {
186 Listener
[] listeners
= text
.getListeners(SWT
.Modify
);
188 for (Listener listener
: listeners
) {
189 text
.removeListener(SWT
.Modify
, listener
);
192 text
.setText(CdmUtils
.Nz(string
));
194 for (Listener listener
: listeners
) {
195 text
.addListener(SWT
.Modify
, listener
);
203 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
208 public void modifyText(ModifyEvent e
) {
209 if(e
.widget
==text
&& !isMultiLine
){
210 Text text
= (Text
) e
.widget
;
211 boolean hasControlCharacters
= false;
212 String textString
= text
.getText();
213 int stringLength
= textString
.length();
214 for (int i
= 0; i
< stringLength
; i
++) {
215 if (Character
.isISOControl(textString
.charAt(i
))) {
216 hasControlCharacters
= true;
220 if(hasControlCharacters
){
221 //remove control character such as line breaks etc.
222 setText(text
.getText().replaceAll("\\p{C}", new Character((char)9608).toString()));
225 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
230 public void setEnabled(boolean enabled
) {
231 text
.setEnabled(enabled
);
232 String symbolicName
= enabled ? Resources
.COLOR_FONT_DEFAULT
: Resources
.COLOR_TEXT_DISABLED
;
233 text
.setForeground(getColor(symbolicName
));
237 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
240 public boolean isEnabled() {
241 return text
.isEnabled();
246 public void setIrrelevant(boolean irrelevant
) {
247 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
249 Color color
= getColor(colorId
);
250 text
.setBackground(color
);
255 public void setBackground(Color color
) {
257 label
.setBackground(color
);
262 public void setSelected(boolean selected
) {
263 setBackground(selected ? SELECTED
: getPersistentBackground());
269 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
273 public void setFocus() {
282 * @return a {@link org.eclipse.swt.widgets.Control} object.
284 public Control
getMainControl() {
296 public void setTextLimit(int limit
) {
297 text
.setTextLimit(limit
);