- moved campanula package to parent package
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / campanula / TextFieldController.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.taxeditor.ui.campanula;
11
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.Control;
19 import org.eclipse.swt.widgets.Listener;
20 import org.eclipse.swt.widgets.Text;
21 import org.eclipse.wb.swt.SWTResourceManager;
22
23 import eu.etaxonomy.cdm.common.CdmUtils;
24 import eu.etaxonomy.taxeditor.preference.Resources;
25 import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
26 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
28 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
29 import eu.etaxonomy.taxeditor.ui.element.ISelectable;
30 import eu.etaxonomy.taxeditor.ui.section.campanula.compatibility.ICdmFormElement;
31
32 /**
33 * @author pplitzner
34 * @date 09.08.2013
35 *
36 */
37 public class TextFieldController extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement, ISelectable{
38
39 protected Text text;
40
41 /** Constant <code>MAX_HEIGHT=0</code> */
42 public static final int MAX_HEIGHT = 0;
43 /** Constant <code>SINGLE=-1</code> */
44 public static final int SINGLE = -1;
45
46 /**
47 * Create the composite.
48 * @param parent
49 * @param style
50 * @param initialText
51 * @param textHeight
52 */
53 public TextFieldController(Text controlledText, CdmFormFactory formFactory, ICdmFormElement parentFormElement, String initialText, Integer textHeight) {
54 super(formFactory, parentFormElement);
55 setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
56
57 //vvvvvvvvvvvvv
58
59 setPropertyChangeListeners(formFactory.getPropertyChangeListeners());
60 formFactory.addPropertyChangeListener(this);
61
62 initialText = initialText==null?"":initialText;
63 //
64 // int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
65 //
66 // int combinedStyle = style | SWT.BORDER | scrollStyle;
67 //
68 // // SWT.PASSWORD does not work when SWT.WRAP is set.
69 // if (style != SWT.PASSWORD) {
70 // combinedStyle = combinedStyle | SWT.WRAP;
71 // }
72 //^^^^^^^^^^^^^^
73
74 this.text = controlledText;
75 // TableWrapData twd_text_1 = new TableWrapData(TableWrapData.LEFT, TableWrapData.TOP);
76 // twd_text_1.grabHorizontal = true;
77 text.setText(initialText);
78 // text.setLayoutData(twd_text_1);
79
80
81 //vvvvvvvvvvvvvvvvvv
82 if (textHeight == null) {
83 text.addKeyListener(new KeyAdapter() {
84 @Override
85 public void keyPressed(KeyEvent e) {
86 if (e.character == SWT.CR) {
87 // Don't accept carriage returns as input when in single
88 // line mode
89 e.doit = false;
90 } else if (e.character == SWT.TAB) {
91 // traverse is not working for wrapped text widgets so
92 // we reintroduce it here
93 e.doit = false;
94 TextFieldController.this.text.traverse(SWT.TRAVERSE_TAB_NEXT);
95 }
96 }
97 });
98 }
99
100 // if (textHeight != null && textHeight > 0) {
101 // TableWrapData layoutData = LayoutConstants.FILL();
102 // (layoutData).heightHint = textHeight;
103 // text.setLayoutData(layoutData);
104 // }
105
106 text.addModifyListener(this);
107
108 addControl(text);
109
110 }
111
112 /**
113 * Get the text of this composites text composite
114 *
115 * @return a {@link java.lang.String} object.
116 */
117 public String getText() {
118 return text.getText();
119 }
120
121 /**
122 * Set the text of this composites text composite
123 *
124 * @param string
125 * a {@link java.lang.String} object.
126 */
127 public void setText(String string) {
128 Listener[] listeners = text.getListeners(SWT.Modify);
129
130 for (Listener listener : listeners) {
131 text.removeListener(SWT.Modify, listener);
132 }
133
134 text.setText(CdmUtils.Nz(string));
135
136 for (Listener listener : listeners) {
137 text.addListener(SWT.Modify, listener);
138 }
139 }
140
141 /*
142 * (non-Javadoc)
143 *
144 * @see
145 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
146 * .ModifyEvent)
147 */
148 /** {@inheritDoc} */
149 @Override
150 public void modifyText(ModifyEvent e) {
151 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
152 }
153
154 /** {@inheritDoc} */
155 @Override
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));
160 }
161
162 /** {@inheritDoc} */
163 @Override
164 public void setIrrelevant(boolean irrelevant) {
165 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
166
167 Color color = getColor(colorId);
168 text.setBackground(color);
169 }
170
171 @Override
172 public void setSelected(boolean selected) {
173 setBackground(selected ? SELECTED : getPersistentBackground());
174 }
175
176 /*
177 * (non-Javadoc)
178 *
179 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
180 */
181 /** {@inheritDoc}
182 * @return */
183 @Override
184 public void setFocus() {
185 text.setFocus();
186 }
187
188 /**
189 * <p>
190 * getMainControl
191 * </p>
192 *
193 * @return a {@link org.eclipse.swt.widgets.Control} object.
194 */
195 public Control getMainControl() {
196 return text;
197 }
198
199 /**
200 * <p>
201 * setTextLimit
202 * </p>
203 *
204 * @param limit
205 * a int.
206 */
207 public void setTextLimit(int limit) {
208 text.setTextLimit(limit);
209 }
210 }