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
.section
.campanula
;
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
.TableWrapData
;
23 import org
.eclipse
.wb
.swt
.SWTResourceManager
;
25 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
26 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
27 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmFormElement
;
28 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.IEnableableFormElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.ISelectable
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
40 public class TextFieldController
extends AbstractCdmFormElement
implements ModifyListener
, IEnableableFormElement
, ISelectable
{
44 /** Constant <code>MAX_HEIGHT=0</code> */
45 public static final int MAX_HEIGHT
= 0;
46 /** Constant <code>SINGLE=-1</code> */
47 public static final int SINGLE
= -1;
50 * Create the composite.
56 public TextFieldController(Composite parent
, Text controlledText
, CdmFormFactory formFactory
, ICdmFormElement parentFormElement
, String initialText
, Integer textHeight
, int style
) {
57 super(formFactory
, parentFormElement
);
58 setBackground(SWTResourceManager
.getColor(SWT
.COLOR_WHITE
));
62 setPropertyChangeListeners(formFactory
.getPropertyChangeListeners());
64 initialText
= initialText
==null?
"":initialText
;
66 int scrollStyle
= textHeight
== null ? SWT
.NULL
: (SWT
.V_SCROLL
| SWT
.MULTI
);
68 int combinedStyle
= style
| SWT
.BORDER
| scrollStyle
;
70 // SWT.PASSWORD does not work when SWT.WRAP is set.
71 if (style
!= SWT
.PASSWORD
) {
72 combinedStyle
= combinedStyle
| SWT
.WRAP
;
76 this.text
= controlledText
;
77 TableWrapData twd_text_1
= new TableWrapData(TableWrapData
.LEFT
, TableWrapData
.TOP
);
78 twd_text_1
.grabHorizontal
= true;
79 text
.setText(initialText
);
80 text
.setLayoutData(twd_text_1
);
84 if (textHeight
== null) {
85 text
.addKeyListener(new KeyAdapter() {
87 public void keyPressed(KeyEvent e
) {
88 if (e
.character
== SWT
.CR
) {
89 // Don't accept carriage returns as input when in single
92 } else if (e
.character
== SWT
.TAB
) {
93 // traverse is not working for wrapped text widgets so
94 // we reintroduce it here
96 TextFieldController
.this.text
.traverse(SWT
.TRAVERSE_TAB_NEXT
);
102 TableWrapData layoutData
= LayoutConstants
.FILL();
103 if (textHeight
!= null && textHeight
> 0) {
104 (layoutData
).heightHint
= textHeight
;
107 text
.setLayoutData(layoutData
);
109 text
.addModifyListener(this);
116 * Get the text of this composites text composite
118 * @return a {@link java.lang.String} object.
120 public String
getText() {
121 return text
.getText();
125 * Set the text of this composites text composite
128 * a {@link java.lang.String} object.
130 public void setText(String string
) {
131 Listener
[] listeners
= text
.getListeners(SWT
.Modify
);
133 for (Listener listener
: listeners
) {
134 text
.removeListener(SWT
.Modify
, listener
);
137 text
.setText(CdmUtils
.Nz(string
));
139 for (Listener listener
: listeners
) {
140 text
.addListener(SWT
.Modify
, listener
);
148 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
153 public void modifyText(ModifyEvent e
) {
154 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e
));
159 public void setEnabled(boolean enabled
) {
160 text
.setEnabled(enabled
);
161 String symbolicName
= enabled ? Resources
.COLOR_FONT_DEFAULT
: Resources
.COLOR_TEXT_DISABLED
;
162 text
.setForeground(getColor(symbolicName
));
167 public void setIrrelevant(boolean irrelevant
) {
168 String colorId
= irrelevant ? Resources
.COLOR_COMPOSITE_IRRELEVANT
: Resources
.COLOR_COMPOSITE_BACKGROUND
;
170 Color color
= getColor(colorId
);
171 text
.setBackground(color
);
175 public void setSelected(boolean selected
) {
176 setBackground(selected ? SELECTED
: getPersistentBackground());
182 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
187 public void setFocus() {
196 * @return a {@link org.eclipse.swt.widgets.Control} object.
198 public Control
getMainControl() {
210 public void setTextLimit(int limit
) {
211 text
.setTextLimit(limit
);