Project

General

Profile

Download (8.57 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.Composite;
13
import org.eclipse.swt.widgets.Control;
14
import org.eclipse.swt.widgets.Label;
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
import eu.etaxonomy.taxeditor.store.StoreUtil;
21

    
22
/**
23
 * @author n.hoffmann
24
 */
25
public class TextWithLabelElement extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement,
26
        ISelectable {
27

    
28
    protected Text text;
29
    private Label label;
30

    
31
    private final boolean isMultiLine;
32

    
33
    public static final int MAX_HEIGHT = 0;
34
    public static final int SINGLE = -1;
35

    
36
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, boolean isMultiLine) {
37
        super(formFactory, parentElement);
38
        this.isMultiLine = isMultiLine;
39
    }
40

    
41
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
42
            String initialText, Integer textHeight, int style) {
43
        this(formFactory, parentElement, labelString, initialText, textHeight, null, false, style);
44
    }
45

    
46
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
47
            String initialText, Integer textHeight, boolean isMultiLine, int style) {
48
        this(formFactory, parentElement, labelString, initialText, textHeight, null, isMultiLine, style);
49
    }
50

    
51
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
52
            String initialText, Integer textHeight, Integer textLimit, int style) {
53
        this(formFactory, parentElement, labelString, initialText, textHeight, textLimit, false, style);
54
    }
55

    
56
    protected TextWithLabelElement(CdmFormFactory formFactory, ICdmFormElement parentElement, String labelString,
57
            String initialText, Integer textHeight, Integer textLimit, boolean isMultiLine, int style) {
58
        super(formFactory, parentElement);
59

    
60
        this.isMultiLine = isMultiLine;
61

    
62
        initLabel(formFactory, labelString, isMultiLine, getLayoutComposite());
63

    
64
        initText(formFactory, initialText, textHeight, textLimit, isMultiLine, style, getLayoutComposite());
65
    }
66

    
67
    protected void initText(CdmFormFactory formFactory, String initialText, Integer textHeight, Integer textLimit,
68
            boolean isMultiLine, int style, Composite layoutComposite) {
69
        int scrollStyle = textHeight == null ? SWT.NULL : (SWT.V_SCROLL | SWT.MULTI);
70

    
71
        int combinedStyle = style | SWT.BORDER | scrollStyle;
72

    
73
        // SWT.PASSWORD does not work when SWT.WRAP is set.
74
        if (style != SWT.PASSWORD) {
75
            combinedStyle = combinedStyle | SWT.WRAP;
76
        }
77

    
78
        text = formFactory.createText(layoutComposite, "", combinedStyle);
79
        text.setTextLimit(textLimit!=null?textLimit:Text.LIMIT);
80

    
81
        addControl(text);
82

    
83
        // text.setWO
84

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

    
103
        TableWrapData layoutData;
104
        if(isMultiLine){
105
            layoutData = LayoutConstants.FILL_HORIZONTALLY(2, 1);
106
        }
107
        else{
108
            layoutData = LayoutConstants.FILL();
109
        }
110
        if (textHeight != null && textHeight > 0) {
111
            (layoutData).heightHint = textHeight;
112
        }
113

    
114
        text.setLayoutData(layoutData);
115

    
116
        text.addModifyListener(this);
117

    
118
        setText(initialText);
119
    }
120

    
121
    protected void initLabel(CdmFormFactory formFactory, String labelString, boolean isMultiLine, Composite layoutComposite) {
122
        if (labelString != null) {
123
            label = formFactory.createLabel(layoutComposite, CdmUtils.Nz(labelString), SWT.NULL);
124

    
125
            addControl(label);
126
            if(isMultiLine){
127
                label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
128
                ((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
129
            }
130
            else{
131
                label.setLayoutData(LayoutConstants.LEFT());
132
                ((TableWrapData)label.getLayoutData()).valign = TableWrapData.MIDDLE;
133
            }
134
        }
135
    }
136

    
137
    /**
138
     * Get the text of this composites text composite
139
     *
140
     * @return a {@link java.lang.String} object.
141
     */
142
    public String getText() {
143
        if (!text.isDisposed()){
144
            return CdmUtils.Nb(text.getText());
145
        }else{
146
            return null;
147
        }
148
    }
149

    
150
    /**
151
     * Set the text of this composites text composite
152
     *
153
     * @param string
154
     *            a {@link java.lang.String} object.
155
     */
156
    public void setText(String string) {
157
        StoreUtil.setTextWithoutModifyListeners(text, string != null? string.trim(): string);
158
    }
159

    
160
    @Override
161
    public void modifyText(ModifyEvent e) {
162
        if(e.widget==text && !isMultiLine){
163
            Text text = (Text) e.widget;
164
            boolean hasControlCharacters = false;
165
            String textString = text.getText();
166
            int stringLength = textString.length();
167
            for (int i = 0; i < stringLength; i++) {
168
                if (Character.isISOControl(textString.charAt(i))) {
169
                    hasControlCharacters = true;
170
                    break;
171
                }
172
            }
173
            if(hasControlCharacters){
174
                //remove control character such as line breaks etc.
175
                setText(text.getText().replaceAll("\\p{C}", new Character((char)9608).toString()));
176
            }
177
        }
178
        firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
179
    }
180

    
181
    @Override
182
    public void setEnabled(boolean enabled) {
183
        text.setEnabled(enabled);
184
        String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
185
        text.setForeground(getColor(symbolicName));
186
    }
187

    
188
    @Override
189
    public boolean isEnabled() {
190
        return text.isEnabled();
191
    }
192

    
193
    public void setEditable(boolean editable) {
194
        text.setEditable(editable);
195
        String symbolicName = editable ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
196
        text.setForeground(getColor(symbolicName));
197
    }
198

    
199

    
200

    
201

    
202
    @Override
203
    public void setIrrelevant(boolean irrelevant) {
204
        String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
205

    
206
        Color color = getColor(colorId);
207
        text.setBackground(color);
208
    }
209

    
210
    @Override
211
    public void setBackground(Color color) {
212
        if (label != null) {
213
            if (!label.isDisposed()){
214
                label.setBackground(color);
215
            }
216
        }
217
    }
218

    
219
    @Override
220
    public void setSelected(boolean selected) {
221
        setBackground(selected ? SELECTED : getPersistentBackground());
222
    }
223

    
224
    @Override
225
    public void setFocus() {
226
        text.setFocus();
227
    }
228

    
229
    public Control getMainControl() {
230
        return text;
231
    }
232

    
233
    public void setTextLimit(int limit) {
234
        text.setTextLimit(limit);
235
    }
236

    
237
    public void setIndent(int indent){
238
        TableWrapData tableWrapData = (TableWrapData)label.getLayoutData();
239
        tableWrapData.indent = indent;
240

    
241
        label.setLayoutData(tableWrapData);
242
        if (isMultiLine){
243
            tableWrapData = (TableWrapData)text.getLayoutData();
244
            tableWrapData.indent = indent;
245

    
246
            text.setLayoutData(tableWrapData);
247
        }
248
        getLayoutComposite().layout();
249
    }
250

    
251
    public void setTextForeground(Color color){
252
        text.setForeground(color);
253
    }
254

    
255
    @Override
256
    public String toString() {
257
        if (label != null){
258
            return CdmUtils.concat("", "TextWithLabelElement[", label.getText(),"]");
259
        }else{
260
            return super.toString();
261
        }
262
    }
263
}
(47-47/53)