had to rename the packages to make them compliant with buckminster
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / forms / TextWithLabelElement.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.ui.forms;
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.Label;
14 import org.eclipse.swt.widgets.Listener;
15 import org.eclipse.swt.widgets.Text;
16 import org.eclipse.ui.forms.widgets.TableWrapData;
17
18 import eu.etaxonomy.cdm.common.CdmUtils;
19 import eu.etaxonomy.taxeditor.preference.Resources;
20
21 /**
22 * <p>TextWithLabelElement class.</p>
23 *
24 * @author n.hoffmann
25 * @version $Id: $
26 */
27 public class TextWithLabelElement extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement{
28
29 protected Text text;
30 private Label label;
31
32 /** Constant <code>MAX_HEIGHT=0</code> */
33 public static final int MAX_HEIGHT = 0;
34 /** Constant <code>SINGLE=-1</code> */
35 public static final int SINGLE = -1;
36
37 /**
38 * <p>Constructor for TextWithLabelElement.</p>
39 *
40 * @param formFactory a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory} object.
41 * @param parentElement a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement} object.
42 * @param labelString a {@link java.lang.String} object.
43 * @param initialText a {@link java.lang.String} object.
44 * @param textHeight a {@link java.lang.Integer} object.
45 * @param style a int.
46 */
47 protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString, String initialText, Integer textHeight, int style)
48 {
49 super(formFactory, parentElement);
50
51 if(labelString != null && ! labelString.equals("")){
52 label = formFactory.createLabel(getLayoutComposite(), CdmUtils.Nz(labelString), SWT.NULL);
53 addControl(label);
54 label.setLayoutData(CdmFormFactory.LEFT());
55 }
56
57 int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
58 text = formFactory.createText(getLayoutComposite(), "", SWT.BORDER | SWT.WRAP | scrollStyle);
59
60 addControl(text);
61
62 // text.setWO
63
64 if(textHeight == null){
65 text.addKeyListener( new KeyAdapter(){
66 @Override
67 public void keyPressed(KeyEvent e) {
68 if(e.character == SWT.CR){
69 // Don't accept carriage returns as input when in single line mode
70 e.doit = false;
71 }else if(e.character == SWT.TAB){
72 // traverse is not working for wrapped text widgets so
73 // we reintroduce it here
74 e.doit = false;
75 TextWithLabelElement.this.text.traverse(SWT.TRAVERSE_TAB_NEXT);
76 }
77 }
78 });
79 }
80
81 TableWrapData layoutData = CdmFormFactory.FILL();
82 if (textHeight != null && textHeight > 0){
83 ((TableWrapData) layoutData).heightHint = textHeight;
84 }
85
86 text.setLayoutData(layoutData);
87
88 text.addModifyListener(this);
89
90 setText(initialText);
91 }
92
93 /**
94 * Get the text of this composites text composite
95 *
96 * @return a {@link java.lang.String} object.
97 */
98 public String getText() {
99 return text.getText();
100 }
101
102 /**
103 * Set the text of this composites text composite
104 *
105 * @param string a {@link java.lang.String} object.
106 */
107 public void setText(String string) {
108 Listener[] listeners = text.getListeners(SWT.Modify);
109
110 for(Listener listener : listeners){
111 text.removeListener(SWT.Modify, listener);
112 }
113
114 text.setText(CdmUtils.Nz(string));
115
116 for(Listener listener : listeners){
117 text.addListener(SWT.Modify, listener);
118 }
119 }
120
121 /* (non-Javadoc)
122 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
123 */
124 /** {@inheritDoc} */
125 public void modifyText(ModifyEvent e) {
126 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
127 }
128
129 /** {@inheritDoc} */
130 public void setSelected(boolean selected) {
131 setBackground(getColor(selected));
132 }
133
134 /** {@inheritDoc} */
135 public void setEnabled(boolean enabled) {
136 text.setEnabled(enabled);
137 String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
138 text.setForeground(getColor(symbolicName));
139 }
140
141 /** {@inheritDoc} */
142 public void setIrrelevant(boolean irrelevant) {
143 String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
144
145 Color color = getColor(colorId);
146 text.setBackground(color);
147 }
148
149 /** {@inheritDoc} */
150 @Override
151 public void setBackground(Color color) {
152 if(label != null)
153 label.setBackground(color);
154 }
155
156 /* (non-Javadoc)
157 * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
158 */
159 /** {@inheritDoc} */
160 @Override
161 public void setFocus() {
162 text.setFocus();
163 }
164
165 /**
166 * <p>getMainControl</p>
167 *
168 * @return a {@link org.eclipse.swt.widgets.Control} object.
169 */
170 public Control getMainControl() {
171 return text;
172 }
173
174 /**
175 * <p>setTextLimit</p>
176 *
177 * @param limit a int.
178 */
179 public void setTextLimit(int limit) {
180 text.setTextLimit(limit);
181 }
182 }