ref #6190 removing svn property place holder in first line of code - java files
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / mvc / element / TextWithLabelElementController.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.ui.mvc.element;
5
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.Listener;
14 import org.eclipse.swt.widgets.Text;
15
16 import eu.etaxonomy.cdm.common.CdmUtils;
17 import eu.etaxonomy.taxeditor.preference.Resources;
18 import eu.etaxonomy.taxeditor.ui.element.AbstractCdmFormElement;
19 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
20 import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
21 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
22 import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
23 import eu.etaxonomy.taxeditor.ui.element.ISelectable;
24 import eu.etaxonomy.taxeditor.ui.mvc.interfaces.CdmCompositeController;
25
26 /**
27 * <p>
28 * TextWithLabelElement class.
29 * </p>
30 *
31 * @author n.hoffmann
32 * @version $Id: $
33 */
34 public class TextWithLabelElementController extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement,
35 ISelectable, CdmCompositeController {
36
37 protected Text text;
38 // private Label label;
39
40 /** Constant <code>MAX_HEIGHT=0</code> */
41 public static final int MAX_HEIGHT = 0;
42 /** Constant <code>SINGLE=-1</code> */
43 public static final int SINGLE = -1;
44
45 /**
46 * <p>
47 * Constructor for TextWithLabelElement.
48 * </p>
49 *
50 * @param formFactory
51 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
52 * object.
53 * @param parentElement
54 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
55 * object.
56 * @param labelString
57 * a {@link java.lang.String} object.
58 * @param initialText
59 * a {@link java.lang.String} object.
60 * @param textHeight
61 * a {@link java.lang.Integer} object.
62 * @param style
63 * a int.
64 * @wbp.parser.entryPoint
65 */
66 public TextWithLabelElementController(TextWithLabelElementMVC textWithLabelElement, CdmFormFactory formFactory, ICdmFormElement parentElement,
67 String initialText, Integer textHeight, int style) {
68 super(formFactory, parentElement);
69
70 int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
71
72 int combinedStyle = style | SWT.BORDER | scrollStyle;
73
74 // SWT.PASSWORD does not work when SWT.WRAP is set.
75 if (style != SWT.PASSWORD) {
76 combinedStyle = combinedStyle | SWT.WRAP;
77 }
78
79 text = textWithLabelElement.getText();
80
81 addControl(text);
82
83 if (textHeight == null) {
84 text.addKeyListener(new KeyAdapter() {
85 @Override
86 public void keyPressed(KeyEvent e) {
87 if (e.character == SWT.CR) {
88 // Don't accept carriage returns as input when in single
89 // line mode
90 e.doit = false;
91 } else if (e.character == SWT.TAB) {
92 // traverse is not working for wrapped text widgets so
93 // we reintroduce it here
94 e.doit = false;
95 TextWithLabelElementController.this.text.traverse(SWT.TRAVERSE_TAB_NEXT);
96 }
97 }
98 });
99 }
100
101 text.addModifyListener(this);
102 }
103
104 /**
105 * Get the text of this composites text composite
106 *
107 * @return a {@link java.lang.String} object.
108 */
109 public String getText() {
110 return text.getText();
111 }
112
113 /**
114 * Set the text of this composites text composite
115 *
116 * @param string
117 * a {@link java.lang.String} object.
118 */
119 public void setText(String string) {
120 Listener[] listeners = text.getListeners(SWT.Modify);
121
122 for (Listener listener : listeners) {
123 text.removeListener(SWT.Modify, listener);
124 }
125
126 text.setText(CdmUtils.Nz(string));
127
128 for (Listener listener : listeners) {
129 text.addListener(SWT.Modify, listener);
130 }
131 }
132
133 /*
134 * (non-Javadoc)
135 *
136 * @see
137 * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
138 * .ModifyEvent)
139 */
140 /** {@inheritDoc} */
141 @Override
142 public void modifyText(ModifyEvent e) {
143 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
144 }
145
146 /** {@inheritDoc} */
147 @Override
148 public void setEnabled(boolean enabled) {
149 text.setEnabled(enabled);
150 String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
151 text.setForeground(getColor(symbolicName));
152 }
153
154 /* (non-Javadoc)
155 * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
156 */
157 @Override
158 public boolean isEnabled() {
159 return text.isEnabled();
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 /** {@inheritDoc} */
172 @Override
173 public void setBackground(Color color) {
174 // if (label != null) {
175 // label.setBackground(color);
176 // }
177 }
178
179 @Override
180 public void setSelected(boolean selected) {
181 setBackground(selected ? SELECTED : getPersistentBackground());
182 }
183
184 /*
185 * (non-Javadoc)
186 *
187 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
188 */
189 /** {@inheritDoc} */
190 @Override
191 public void setFocus() {
192 text.setFocus();
193 }
194
195 /**
196 * <p>
197 * getMainControl
198 * </p>
199 *
200 * @return a {@link org.eclipse.swt.widgets.Control} object.
201 */
202 public Control getMainControl() {
203 return text;
204 }
205
206 /**
207 * <p>
208 * setTextLimit
209 * </p>
210 *
211 * @param limit
212 * a int.
213 */
214 public void setTextLimit(int limit) {
215 text.setTextLimit(limit);
216 }
217 }