Project

General

Profile

Download (7.63 KB) Statistics
| Branch: | Tag: | Revision:
1 729887cf n.hoffmann
/**
2 20326c1c Patric Plitzner
 *
3 729887cf n.hoffmann
 */
4 78222507 n.hoffmann
package eu.etaxonomy.taxeditor.ui.element;
5 729887cf n.hoffmann
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 a9b5194c Patrick Plitzner
import org.eclipse.swt.widgets.Composite;
13 27b59ab1 n.hoffmann
import org.eclipse.swt.widgets.Control;
14 729887cf n.hoffmann
import org.eclipse.swt.widgets.Label;
15
import org.eclipse.swt.widgets.Listener;
16 46fbd183 n.hoffmann
import org.eclipse.swt.widgets.Text;
17 729887cf n.hoffmann
import org.eclipse.ui.forms.widgets.TableWrapData;
18
19
import eu.etaxonomy.cdm.common.CdmUtils;
20
import eu.etaxonomy.taxeditor.preference.Resources;
21
22
/**
23 3be6ef3e n.hoffmann
 * @author n.hoffmann
24
 * @version $Id: $
25 729887cf n.hoffmann
 */
26 20326c1c Patric Plitzner
public class TextWithLabelElement extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement,
27
        ISelectable {
28
29
    protected Text text;
30
    private Label label;
31
32 170726d7 Patric Plitzner
    private final boolean isMultiLine;
33
34 20326c1c Patric Plitzner
    public static final int MAX_HEIGHT = 0;
35
    public static final int SINGLE = -1;
36
37 a9b5194c Patrick Plitzner
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, boolean isMultiLine) {
38
        super(formFactory, parentElement);
39
        this.isMultiLine = isMultiLine;
40
    }
41
42 82dfc775 Patric Plitzner
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
43
            String initialText, Integer textHeight, int style) {
44 170726d7 Patric Plitzner
        this(formFactory, parentElement, labelString, initialText, textHeight, null, false, style);
45
    }
46
47
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
48
            String initialText, Integer textHeight, boolean isMultiLine, int style) {
49
        this(formFactory, parentElement, labelString, initialText, textHeight, null, isMultiLine, style);
50
    }
51
52
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
53
            String initialText, Integer textHeight, Integer textLimit, int style) {
54
        this(formFactory, parentElement, labelString, initialText, textHeight, textLimit, false, style);
55 82dfc775 Patric Plitzner
    }
56 170726d7 Patric Plitzner
57 20326c1c Patric Plitzner
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
58 170726d7 Patric Plitzner
            String initialText, Integer textHeight, Integer textLimit, boolean isMultiLine, int style) {
59 20326c1c Patric Plitzner
        super(formFactory, parentElement);
60
61 170726d7 Patric Plitzner
        this.isMultiLine = isMultiLine;
62
63 1fbf88c8 Patrick Plitzner
        initLabel(formFactory, labelString, isMultiLine, getLayoutComposite());
64 a9b5194c Patrick Plitzner
65 1fbf88c8 Patrick Plitzner
        initText(formFactory, initialText, textHeight, textLimit, isMultiLine, style, getLayoutComposite());
66
    }
67 20326c1c Patric Plitzner
68 1fbf88c8 Patrick Plitzner
    protected void initText(CdmFormFactory formFactory, String initialText, Integer textHeight, Integer textLimit,
69
            boolean isMultiLine, int style, Composite layoutComposite) {
70 20326c1c Patric Plitzner
        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 a9b5194c Patrick Plitzner
        text = formFactory.createText(layoutComposite, "", combinedStyle);
80 82dfc775 Patric Plitzner
        text.setTextLimit(textLimit!=null?textLimit:Text.LIMIT);
81 20326c1c Patric Plitzner
82
        addControl(text);
83
84
        // text.setWO
85
86
        if (textHeight == null) {
87
            text.addKeyListener(new KeyAdapter() {
88
                @Override
89
                public void keyPressed(KeyEvent e) {
90
                    if (e.character == SWT.CR) {
91
                        // Don't accept carriage returns as input when in single
92
                        // line mode
93
                        e.doit = false;
94
                    } else if (e.character == SWT.TAB) {
95
                        // traverse is not working for wrapped text widgets so
96
                        // we reintroduce it here
97
                        e.doit = false;
98
                        TextWithLabelElement.this.text.traverse(SWT.TRAVERSE_TAB_NEXT);
99
                    }
100
                }
101
            });
102
        }
103
104 170726d7 Patric Plitzner
        TableWrapData layoutData;
105
        if(isMultiLine){
106
            layoutData = LayoutConstants.FILL_HORIZONTALLY(2, 1);
107
        }
108
        else{
109
            layoutData = LayoutConstants.FILL();
110
        }
111 20326c1c Patric Plitzner
        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 1fbf88c8 Patrick Plitzner
    protected void initLabel(CdmFormFactory formFactory, String labelString, boolean isMultiLine, Composite layoutComposite) {
123
        if (labelString != null) {
124
            label = formFactory.createLabel(layoutComposite, CdmUtils.Nz(labelString), SWT.NULL);
125
            addControl(label);
126
            if(isMultiLine){
127
                label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
128
            }
129
            else{
130
                label.setLayoutData(LayoutConstants.LEFT());
131
            }
132
        }
133
    }
134
135 20326c1c Patric Plitzner
    /**
136
     * Get the text of this composites text composite
137
     *
138
     * @return a {@link java.lang.String} object.
139
     */
140
    public String getText() {
141 297bf978 Patrick Plitzner
        return text.getText();
142 20326c1c Patric Plitzner
    }
143
144
    /**
145
     * Set the text of this composites text composite
146
     *
147
     * @param string
148
     *            a {@link java.lang.String} object.
149
     */
150
    public void setText(String string) {
151
        Listener[] listeners = text.getListeners(SWT.Modify);
152
153
        for (Listener listener : listeners) {
154
            text.removeListener(SWT.Modify, listener);
155
        }
156
157
        text.setText(CdmUtils.Nz(string));
158
159
        for (Listener listener : listeners) {
160
            text.addListener(SWT.Modify, listener);
161
        }
162
    }
163
164
    /** {@inheritDoc} */
165
    @Override
166
    public void modifyText(ModifyEvent e) {
167 170726d7 Patric Plitzner
        if(e.widget==text && !isMultiLine){
168 f73eb844 Patric Plitzner
            Text text = (Text) e.widget;
169 6a822179 Patric Plitzner
            boolean hasControlCharacters = false;
170
            String textString = text.getText();
171
            int stringLength = textString.length();
172
            for (int i = 0; i < stringLength; i++) {
173
                if (Character.isISOControl(textString.charAt(i))) {
174
                    hasControlCharacters = true;
175
                    break;
176
                }
177
            }
178
            if(hasControlCharacters){
179
                //remove control character such as line breaks etc.
180 170726d7 Patric Plitzner
                setText(text.getText().replaceAll("\\p{C}", new Character((char)9608).toString()));
181 6a822179 Patric Plitzner
            }
182 f73eb844 Patric Plitzner
        }
183 20326c1c Patric Plitzner
        firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
184
    }
185
186
    /** {@inheritDoc} */
187
    @Override
188
    public void setEnabled(boolean enabled) {
189
        text.setEnabled(enabled);
190
        String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
191
        text.setForeground(getColor(symbolicName));
192
    }
193
194 aa2f3cbf Patric Plitzner
    @Override
195
    public boolean isEnabled() {
196
        return text.isEnabled();
197
    }
198
199 20326c1c Patric Plitzner
    /** {@inheritDoc} */
200
    @Override
201
    public void setIrrelevant(boolean irrelevant) {
202
        String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
203
204
        Color color = getColor(colorId);
205
        text.setBackground(color);
206
    }
207
208
    /** {@inheritDoc} */
209
    @Override
210
    public void setBackground(Color color) {
211
        if (label != null) {
212
            label.setBackground(color);
213
        }
214
    }
215
216
    @Override
217
    public void setSelected(boolean selected) {
218
        setBackground(selected ? SELECTED : getPersistentBackground());
219
    }
220
221
    /** {@inheritDoc} */
222
    @Override
223
    public void setFocus() {
224
        text.setFocus();
225
    }
226
227
    public Control getMainControl() {
228
        return text;
229
    }
230
231
    public void setTextLimit(int limit) {
232
        text.setTextLimit(limit);
233
    }
234 e9a2a0fa Patric Plitzner
235 729887cf n.hoffmann
}