Project

General

Profile

Download (7.86 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.layout.GridData;
13
import org.eclipse.swt.widgets.Composite;
14
import org.eclipse.swt.widgets.Control;
15
import org.eclipse.swt.widgets.Label;
16
import org.eclipse.swt.widgets.Listener;
17
import org.eclipse.swt.widgets.Text;
18
import org.eclipse.ui.forms.widgets.TableWrapData;
19

    
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.taxeditor.preference.Resources;
22

    
23
/**
24
 * @author n.hoffmann
25
 * @version $Id: $
26
 */
27
public class TextWithLabelElement extends AbstractCdmFormElement implements ModifyListener, IEnableableFormElement,
28
        ISelectable {
29

    
30
    protected Text text;
31
    private Label label;
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

    
146
      return CdmUtils.Nb(text.getText());
147
    }
148

    
149
    /**
150
     * Set the text of this composites text composite
151
     *
152
     * @param string
153
     *            a {@link java.lang.String} object.
154
     */
155
    public void setText(String string) {
156
        Listener[] listeners = text.getListeners(SWT.Modify);
157

    
158
        for (Listener listener : listeners) {
159
            text.removeListener(SWT.Modify, listener);
160
        }
161

    
162
        text.setText(CdmUtils.Nz(string));
163

    
164
        for (Listener listener : listeners) {
165
            text.addListener(SWT.Modify, listener);
166
        }
167
    }
168

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

    
191
    /** {@inheritDoc} */
192
    @Override
193
    public void setEnabled(boolean enabled) {
194
        text.setEnabled(enabled);
195
        String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
196
        text.setForeground(getColor(symbolicName));
197
    }
198

    
199
    @Override
200
    public boolean isEnabled() {
201
        return text.isEnabled();
202
    }
203

    
204
    /** {@inheritDoc} */
205
    @Override
206
    public void setIrrelevant(boolean irrelevant) {
207
        String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
208

    
209
        Color color = getColor(colorId);
210
        text.setBackground(color);
211
    }
212

    
213
    /** {@inheritDoc} */
214
    @Override
215
    public void setBackground(Color color) {
216
        if (label != null) {
217
            label.setBackground(color);
218
        }
219
    }
220

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

    
226
    /** {@inheritDoc} */
227
    @Override
228
    public void setFocus() {
229
        text.setFocus();
230
    }
231

    
232
    public Control getMainControl() {
233
        return text;
234
    }
235

    
236
    public void setTextLimit(int limit) {
237
        text.setTextLimit(limit);
238
    }
239

    
240
}
(41-41/45)