Project

General

Profile

Download (6.78 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.taxeditor.ui.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.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>
23
 * TextWithLabelElement class.
24
 * </p>
25
 *
26
 * @author n.hoffmann
27
 * @version $Id: $
28
 */
29
public class TextWithLabelElement extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement,
30
        ISelectable {
31

    
32
    protected Text text;
33
    private Label label;
34

    
35
    /** Constant <code>MAX_HEIGHT=0</code> */
36
    public static final int MAX_HEIGHT = 0;
37
    /** Constant <code>SINGLE=-1</code> */
38
    public static final int SINGLE = -1;
39

    
40
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
41
            String initialText, Integer textHeight, int style) {
42
        this(formFactory, parentElement, labelString, initialText, textHeight, null, style);
43
    }
44
    /**
45
     * <p>
46
     * Constructor for TextWithLabelElement.
47
     * </p>
48
     *
49
     * @param formFactory
50
     *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
51
     *            object.
52
     * @param parentElement
53
     *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
54
     *            object.
55
     * @param labelString
56
     *            a {@link java.lang.String} object.
57
     * @param initialText
58
     *            a {@link java.lang.String} object.
59
     * @param textHeight
60
     *            a {@link java.lang.Integer} object.
61
     *            @param textLimit max characters allowed to enter
62
     * @param style
63
     *            a int.
64
     * @wbp.parser.entryPoint
65
     */
66
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
67
            String initialText, Integer textHeight, Integer textLimit, int style) {
68
        super(formFactory, parentElement);
69

    
70
        if (labelString != null) {
71
            label = formFactory.createLabel(getLayoutComposite(), CdmUtils.Nz(labelString), SWT.NULL);
72
            addControl(label);
73
            label.setLayoutData(LayoutConstants.LEFT());
74
        }
75

    
76
        int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
77

    
78
        int combinedStyle = style | SWT.BORDER | scrollStyle;
79

    
80
        // SWT.PASSWORD does not work when SWT.WRAP is set.
81
        if (style != SWT.PASSWORD) {
82
            combinedStyle = combinedStyle | SWT.WRAP;
83
        }
84

    
85
        text = formFactory.createText(getLayoutComposite(), "", combinedStyle);
86
        text.setTextLimit(textLimit!=null?textLimit:Text.LIMIT);
87

    
88
        addControl(text);
89

    
90
        // text.setWO
91

    
92
        if (textHeight == null) {
93
            text.addKeyListener(new KeyAdapter() {
94
                @Override
95
                public void keyPressed(KeyEvent e) {
96
                    if (e.character == SWT.CR) {
97
                        // Don't accept carriage returns as input when in single
98
                        // line mode
99
                        e.doit = false;
100
                    } else if (e.character == SWT.TAB) {
101
                        // traverse is not working for wrapped text widgets so
102
                        // we reintroduce it here
103
                        e.doit = false;
104
                        TextWithLabelElement.this.text.traverse(SWT.TRAVERSE_TAB_NEXT);
105
                    }
106
                }
107
            });
108
        }
109

    
110
        TableWrapData layoutData = LayoutConstants.FILL();
111
        if (textHeight != null && textHeight > 0) {
112
            (layoutData).heightHint = textHeight;
113
        }
114

    
115
        text.setLayoutData(layoutData);
116

    
117
        text.addModifyListener(this);
118

    
119
        setText(initialText);
120
    }
121

    
122
    /**
123
     * Get the text of this composites text composite
124
     *
125
     * @return a {@link java.lang.String} object.
126
     */
127
    public String getText() {
128
        return text.getText();
129
    }
130

    
131
    /**
132
     * Set the text of this composites text composite
133
     *
134
     * @param string
135
     *            a {@link java.lang.String} object.
136
     */
137
    public void setText(String string) {
138
        Listener[] listeners = text.getListeners(SWT.Modify);
139

    
140
        for (Listener listener : listeners) {
141
            text.removeListener(SWT.Modify, listener);
142
        }
143

    
144
        text.setText(CdmUtils.Nz(string));
145

    
146
        for (Listener listener : listeners) {
147
            text.addListener(SWT.Modify, listener);
148
        }
149
    }
150

    
151
    /*
152
     * (non-Javadoc)
153
     *
154
     * @see
155
     * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
156
     * .ModifyEvent)
157
     */
158
    /** {@inheritDoc} */
159
    @Override
160
    public void modifyText(ModifyEvent e) {
161
        firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
162
    }
163

    
164
    /** {@inheritDoc} */
165
    @Override
166
    public void setEnabled(boolean enabled) {
167
        text.setEnabled(enabled);
168
        String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
169
        text.setForeground(getColor(symbolicName));
170
    }
171

    
172
    /* (non-Javadoc)
173
     * @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
174
     */
175
    @Override
176
    public boolean isEnabled() {
177
        return text.isEnabled();
178
    }
179

    
180
    /** {@inheritDoc} */
181
    @Override
182
    public void setIrrelevant(boolean irrelevant) {
183
        String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
184

    
185
        Color color = getColor(colorId);
186
        text.setBackground(color);
187
    }
188

    
189
    /** {@inheritDoc} */
190
    @Override
191
    public void setBackground(Color color) {
192
        if (label != null) {
193
            label.setBackground(color);
194
        }
195
    }
196

    
197
    @Override
198
    public void setSelected(boolean selected) {
199
        setBackground(selected ? SELECTED : getPersistentBackground());
200
    }
201

    
202
    /*
203
     * (non-Javadoc)
204
     *
205
     * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
206
     */
207
    /** {@inheritDoc} */
208
    @Override
209
    public void setFocus() {
210
        text.setFocus();
211
    }
212

    
213
    /**
214
     * <p>
215
     * getMainControl
216
     * </p>
217
     *
218
     * @return a {@link org.eclipse.swt.widgets.Control} object.
219
     */
220
    public Control getMainControl() {
221
        return text;
222
    }
223

    
224
    /**
225
     * <p>
226
     * setTextLimit
227
     * </p>
228
     *
229
     * @param limit
230
     *            a int.
231
     */
232
    public void setTextLimit(int limit) {
233
        text.setTextLimit(limit);
234
    }
235

    
236
    public void setVisible(boolean visible){
237
        text.setVisible(visible);
238
        label.setVisible(visible);
239
    }
240
}
(36-36/39)