Project

General

Profile

Download (8.72 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
26
        extends AbstractRelevanceFormElement
27
        implements ModifyListener, IEnableableFormElement, ISelectable {
28

    
29
    protected Text text;
30
    private Label label;
31
    private boolean isEnabled;
32

    
33
    private final boolean isMultiLine;
34

    
35
    public static final int MAX_HEIGHT = 0;
36
    public static final int SINGLE = -1;
37

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

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

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

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

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

    
62
        this.isMultiLine = isMultiLine;
63

    
64
        initLabel(formFactory, labelString, isMultiLine, getLayoutComposite());
65

    
66
        initText(formFactory, initialText, textHeight, textLimit, isMultiLine, style, getLayoutComposite());
67
    }
68

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

    
73
        int combinedStyle = style | SWT.BORDER | scrollStyle;
74

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

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

    
83
        addControl(text);
84

    
85
        // text.setWO
86

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

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

    
116
        text.setLayoutData(layoutData);
117

    
118
        text.addModifyListener(this);
119

    
120
        setText(initialText);
121
    }
122

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

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

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

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

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

    
183
    @Override
184
    public void setEnabled(boolean enabled) {
185
        this.isEnabled = enabled;
186
        if (text.isDisposed()){
187
        	return;
188
        }
189
        text.setEditable(enabled);
190
        String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
191
        text.setForeground(getColor(symbolicName));
192
    }
193

    
194
    @Override
195
    public boolean isEnabled() {
196
        return isEnabled;
197
    }
198

    
199
    public void setEditable(boolean editable) {
200
    	if (text.isDisposed()){
201
    		return;
202
    	}
203
        text.setEditable(editable);
204
        String symbolicName = editable ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
205
        text.setForeground(getColor(symbolicName));
206
    }
207

    
208

    
209

    
210

    
211
    @Override
212
    public void updateCacheRelevance() {
213
        Color color = cacheRelevance().getColor();
214
        text.setBackground(color);
215
    }
216

    
217
    @Override
218
    public void setBackground(Color color) {
219
        if (label != null) {
220
            if (!label.isDisposed()){
221
                label.setBackground(color);
222
            }
223
        }
224
    }
225

    
226
    @Override
227
    public void setSelected(boolean selected) {
228
        setBackground(selected ? SELECTED : getPersistentBackground());
229
    }
230

    
231
    @Override
232
    public void setFocus() {
233
        text.setFocus();
234
    }
235

    
236
    public Control getMainControl() {
237
        return text;
238
    }
239

    
240
    public void setTextLimit(int limit) {
241
        text.setTextLimit(limit);
242
    }
243

    
244
    public void setIndent(int indent){
245
        TableWrapData tableWrapData = (TableWrapData)label.getLayoutData();
246
        tableWrapData.indent = indent;
247

    
248
        label.setLayoutData(tableWrapData);
249
        if (isMultiLine){
250
            tableWrapData = (TableWrapData)text.getLayoutData();
251
            tableWrapData.indent = indent;
252

    
253
            text.setLayoutData(tableWrapData);
254
        }
255
        getLayoutComposite().layout();
256
    }
257

    
258
    public void setTextForeground(Color color){
259
        text.setForeground(color);
260
    }
261

    
262
    @Override
263
    public String toString() {
264
        if (label != null){
265
            return CdmUtils.concat("", "TextWithLabelElement[", label.getText(),"]");
266
        }else{
267
            return super.toString();
268
        }
269
    }
270

    
271
    public void setMessage(String message) {
272
        text.setMessage(message);
273
    }
274
}
(51-51/57)