- created GatheringEventDetails element completely uncoupled to any controlling...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / 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.section.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.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;
24
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;
34
35 /**
36 * @author pplitzner
37 * @date 09.08.2013
38 *
39 */
40 public class TextFieldController extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement, ISelectable{
41
42 protected Text text;
43
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;
48
49 /**
50 * Create the composite.
51 * @param parent
52 * @param style
53 * @param initialText
54 * @param textHeight
55 */
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));
59
60 //vvvvvvvvvvvvv
61
62 setPropertyChangeListeners(formFactory.getPropertyChangeListeners());
63
64 initialText = initialText==null?"":initialText;
65
66 int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
67
68 int combinedStyle = style | SWT.BORDER | scrollStyle;
69
70 // SWT.PASSWORD does not work when SWT.WRAP is set.
71 if (style != SWT.PASSWORD) {
72 combinedStyle = combinedStyle | SWT.WRAP;
73 }
74 //^^^^^^^^^^^^^^
75
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);
81
82
83 //vvvvvvvvvvvvvvvvvv
84 if (textHeight == null) {
85 text.addKeyListener(new KeyAdapter() {
86 @Override
87 public void keyPressed(KeyEvent e) {
88 if (e.character == SWT.CR) {
89 // Don't accept carriage returns as input when in single
90 // line mode
91 e.doit = false;
92 } else if (e.character == SWT.TAB) {
93 // traverse is not working for wrapped text widgets so
94 // we reintroduce it here
95 e.doit = false;
96 TextFieldController.this.text.traverse(SWT.TRAVERSE_TAB_NEXT);
97 }
98 }
99 });
100 }
101
102 TableWrapData layoutData = LayoutConstants.FILL();
103 if (textHeight != null && textHeight > 0) {
104 (layoutData).heightHint = textHeight;
105 }
106
107 text.setLayoutData(layoutData);
108
109 text.addModifyListener(this);
110
111 addControl(text);
112
113 }
114
115 /**
116 * Get the text of this composites text composite
117 *
118 * @return a {@link java.lang.String} object.
119 */
120 public String getText() {
121 return text.getText();
122 }
123
124 /**
125 * Set the text of this composites text composite
126 *
127 * @param string
128 * a {@link java.lang.String} object.
129 */
130 public void setText(String string) {
131 Listener[] listeners = text.getListeners(SWT.Modify);
132
133 for (Listener listener : listeners) {
134 text.removeListener(SWT.Modify, listener);
135 }
136
137 text.setText(CdmUtils.Nz(string));
138
139 for (Listener listener : listeners) {
140 text.addListener(SWT.Modify, listener);
141 }
142 }
143
144 /*
145 * (non-Javadoc)
146 *
147 * @see
148 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
149 * .ModifyEvent)
150 */
151 /** {@inheritDoc} */
152 @Override
153 public void modifyText(ModifyEvent e) {
154 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
155 }
156
157 /** {@inheritDoc} */
158 @Override
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));
163 }
164
165 /** {@inheritDoc} */
166 @Override
167 public void setIrrelevant(boolean irrelevant) {
168 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
169
170 Color color = getColor(colorId);
171 text.setBackground(color);
172 }
173
174 @Override
175 public void setSelected(boolean selected) {
176 setBackground(selected ? SELECTED : getPersistentBackground());
177 }
178
179 /*
180 * (non-Javadoc)
181 *
182 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
183 */
184 /** {@inheritDoc}
185 * @return */
186 @Override
187 public void setFocus() {
188 text.setFocus();
189 }
190
191 /**
192 * <p>
193 * getMainControl
194 * </p>
195 *
196 * @return a {@link org.eclipse.swt.widgets.Control} object.
197 */
198 public Control getMainControl() {
199 return text;
200 }
201
202 /**
203 * <p>
204 * setTextLimit
205 * </p>
206 *
207 * @param limit
208 * a int.
209 */
210 public void setTextLimit(int limit) {
211 text.setTextLimit(limit);
212 }
213 }