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 /** Constant <code>MAX_HEIGHT=0</code> */
36 public static final int MAX_HEIGHT
= 0;
37 /** Constant <code>SINGLE=-1</code> */
38 public static final int SINGLE
= -1;
40 protected TextWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String labelString
,
41 String initialText
, Integer textHeight
, int style
) {
42 this(formFactory
, parentElement
, labelString
, initialText
, textHeight
, null, style
);
46 * Constructor for TextWithLabelElement.
50 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
52 * @param parentElement
53 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
56 * a {@link java.lang.String} object.
58 * a {@link java.lang.String} object.
60 * a {@link java.lang.Integer} object.
61 * @param textLimit max characters allowed to enter
64 * @wbp.parser.entryPoint
66 protected TextWithLabelElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, String labelString
,
67 String initialText
, Integer textHeight
, Integer textLimit
, int style
) {
68 super(formFactory
, parentElement
);
70 if (labelString
!= null) {
71 label
= formFactory
.createLabel(getLayoutComposite(), CdmUtils
.Nz(labelString
), SWT
.NULL
);
73 label
.setLayoutData(LayoutConstants
.LEFT());
76 int scrollStyle
= textHeight
== null ? SWT
.NULL
: (SWT
.V_SCROLL
| SWT
.MULTI
);
78 int combinedStyle
= style
| SWT
.BORDER
| scrollStyle
;
80 // SWT.PASSWORD does not work when SWT.WRAP is set.
81 if (style
!= SWT
.PASSWORD
) {
82 combinedStyle
= combinedStyle
| SWT
.WRAP
;
85 text
= formFactory
.createText(getLayoutComposite(), "", combinedStyle
);
86 text
.setTextLimit(textLimit
!=null?textLimit
:Text
.LIMIT
);
92 if (textHeight
== null) {
93 text
.addKeyListener(new KeyAdapter() {
95 public void keyPressed(KeyEvent e
) {
96 if (e
.character
== SWT
.CR
) {
97 // Don't accept carriage returns as input when in single
100 } else if (e
.character
== SWT
.TAB
) {
101 // traverse is not working for wrapped text widgets so
102 // we reintroduce it here
104 TextWithLabelElement
.this.text
.traverse(SWT
.TRAVERSE_TAB_NEXT
);
110 TableWrapData layoutData
= LayoutConstants
.FILL();
111 if (textHeight
!= null && textHeight
> 0) {
112 (layoutData
).heightHint
= textHeight
;
115 text
.setLayoutData(layoutData
);
117 text
.addModifyListener(this);
119 setText(initialText
);
123 * Get the text of this composites text composite
125 * @return a {@link java.lang.String} object.
127 public String
getText() {
128 return text
.getText();
132 * Set the text of this composites text composite
135 * a {@link java.lang.String} object.
137 public void setText(String string
) {
138 Listener
[] listeners
= text
.getListeners(SWT
.Modify
);
140 for (Listener listener
: listeners
) {
141 text
.removeListener(SWT
.Modify
, listener
);
144 text
.setText(CdmUtils
.Nz(string
));
146 for (Listener listener
: listeners
) {
147 text
.addListener(SWT
.Modify
, listener
);
155 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
160 public void modifyText(ModifyEvent e
) {
161 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
166 public void setEnabled(boolean enabled
) {
167 text
.setEnabled(enabled
);
168 String symbolicName
= enabled ? Resources
.COLOR_FONT_DEFAULT
: Resources
.COLOR_TEXT_DISABLED
;
169 text
.setForeground(getColor(symbolicName
));
173 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
176 public boolean isEnabled() {
177 return text
.isEnabled();
182 public void setIrrelevant(boolean irrelevant
) {
183 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
185 Color color
= getColor(colorId
);
186 text
.setBackground(color
);
191 public void setBackground(Color color
) {
193 label
.setBackground(color
);
198 public void setSelected(boolean selected
) {
199 setBackground(selected ? SELECTED
: getPersistentBackground());
205 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
209 public void setFocus() {
218 * @return a {@link org.eclipse.swt.widgets.Control} object.
220 public Control
getMainControl() {
232 public void setTextLimit(int limit
) {
233 text
.setTextLimit(limit
);
236 public void setVisible(boolean visible
){
237 text
.setVisible(visible
);
238 label
.setVisible(visible
);