3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.ui
.element
;
12 import org
.eclipse
.swt
.SWT
;
13 import org
.eclipse
.swt
.events
.KeyAdapter
;
14 import org
.eclipse
.swt
.events
.KeyEvent
;
15 import org
.eclipse
.swt
.events
.ModifyEvent
;
16 import org
.eclipse
.swt
.events
.ModifyListener
;
17 import org
.eclipse
.swt
.graphics
.Color
;
18 import org
.eclipse
.swt
.widgets
.Composite
;
19 import org
.eclipse
.swt
.widgets
.Control
;
20 import org
.eclipse
.swt
.widgets
.Listener
;
21 import org
.eclipse
.swt
.widgets
.Text
;
22 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
23 import org
.eclipse
.ui
.forms
.widgets
.TableWrapData
;
24 import org
.eclipse
.wb
.swt
.SWTResourceManager
;
26 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
27 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
34 public class CdmTextField
extends AbstractCdmFormElementComposite
implements ModifyListener
, IEnableableFormElement
, ISelectable
{
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;
44 * Create the composite.
50 public CdmTextField(Composite parent
, FormToolkit formFactory
, ICdmFormElement parentFormElement
, String initialText
, Integer textHeight
, int style
) {
51 super(parent
, formFactory
, parentFormElement
, style
);
52 setBackground(SWTResourceManager
.getColor(SWT
.COLOR_WHITE
));
56 initialText
= initialText
==null?
"":initialText
;
58 int scrollStyle
= textHeight
== null ? SWT
.NULL
: (SWT
.V_SCROLL
| SWT
.MULTI
);
60 int combinedStyle
= style
| SWT
.BORDER
| scrollStyle
;
62 // SWT.PASSWORD does not work when SWT.WRAP is set.
63 if (style
!= SWT
.PASSWORD
) {
64 combinedStyle
= combinedStyle
| SWT
.WRAP
;
68 text
= new Text(this, SWT
.NONE
);
69 TableWrapData twd_text_1
= new TableWrapData(TableWrapData
.LEFT
, TableWrapData
.TOP
);
70 twd_text_1
.grabHorizontal
= true;
71 text
.setText(initialText
);
72 text
.setLayoutData(twd_text_1
);
76 if (textHeight
== null) {
77 text
.addKeyListener(new KeyAdapter() {
79 public void keyPressed(KeyEvent e
) {
80 if (e
.character
== SWT
.CR
) {
81 // Don't accept carriage returns as input when in single
84 } else if (e
.character
== SWT
.TAB
) {
85 // traverse is not working for wrapped text widgets so
86 // we reintroduce it here
88 CdmTextField
.this.text
.traverse(SWT
.TRAVERSE_TAB_NEXT
);
94 TableWrapData layoutData
= LayoutConstants
.FILL();
95 if (textHeight
!= null && textHeight
> 0) {
96 (layoutData
).heightHint
= textHeight
;
99 text
.setLayoutData(layoutData
);
101 text
.addModifyListener(this);
108 protected void checkSubclass() {
109 // Disable the check that prevents subclassing of SWT components
113 * Get the text of this composites text composite
115 * @return a {@link java.lang.String} object.
117 public String
getText() {
118 return text
.getText();
122 * Set the text of this composites text composite
125 * a {@link java.lang.String} object.
127 public void setText(String string
) {
128 Listener
[] listeners
= text
.getListeners(SWT
.Modify
);
130 for (Listener listener
: listeners
) {
131 text
.removeListener(SWT
.Modify
, listener
);
134 text
.setText(CdmUtils
.Nz(string
));
136 for (Listener listener
: listeners
) {
137 text
.addListener(SWT
.Modify
, listener
);
145 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
150 public void modifyText(ModifyEvent e
) {
151 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
156 public void setEnabled(boolean enabled
) {
157 text
.setEnabled(enabled
);
158 String symbolicName
= enabled ? Resources
.COLOR_FONT_DEFAULT
: Resources
.COLOR_TEXT_DISABLED
;
159 text
.setForeground(getColor(symbolicName
));
164 public void setIrrelevant(boolean irrelevant
) {
165 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
167 Color color
= getColor(colorId
);
168 text
.setBackground(color
);
172 public void setSelected(boolean selected
) {
173 setBackground(selected ? SELECTED
: getPersistentBackground());
179 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
184 public boolean setFocus() {
185 return text
.setFocus();
193 * @return a {@link org.eclipse.swt.widgets.Control} object.
195 public Control
getMainControl() {
207 public void setTextLimit(int limit
) {
208 text
.setTextLimit(limit
);