Project

General

Profile

Download (6.36 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2013 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.taxeditor.ui.element;
11

    
12
import org.eclipse.swt.SWT;
13
import org.eclipse.swt.events.KeyAdapter;
14
import org.eclipse.swt.events.KeyEvent;
15
import org.eclipse.swt.events.ModifyEvent;
16
import org.eclipse.swt.events.ModifyListener;
17
import org.eclipse.swt.graphics.Color;
18
import org.eclipse.swt.widgets.Composite;
19
import org.eclipse.swt.widgets.Control;
20
import org.eclipse.swt.widgets.Label;
21
import org.eclipse.swt.widgets.Listener;
22
import org.eclipse.swt.widgets.Text;
23
import org.eclipse.ui.forms.widgets.FormToolkit;
24
import org.eclipse.ui.forms.widgets.TableWrapData;
25
import org.eclipse.wb.swt.SWTResourceManager;
26

    
27
import eu.etaxonomy.cdm.common.CdmUtils;
28
import eu.etaxonomy.taxeditor.preference.Resources;
29

    
30
/**
31
 * @author pplitzner
32
 * @date 23.07.2013
33
 *
34
 */
35
public class TextWithLabelComposite extends AbstractCdmFormElementComposite implements ModifyListener, IEnableableFormElement, ISelectable {
36

    
37
    protected Text text;
38
    private final Label label;
39

    
40
    /** Constant <code>MAX_HEIGHT=0</code> */
41
    public static final int MAX_HEIGHT = 0;
42
    /** Constant <code>SINGLE=-1</code> */
43
    public static final int SINGLE = -1;
44

    
45
    /**
46
     *
47
     *
48
     */
49
    public TextWithLabelComposite(Composite parent, FormToolkit formFactory, ICdmFormElement parentFormElement, String labelString,
50
            String initialText, Integer textHeight, int style) {
51
        super(parent, formFactory, parentFormElement, style);
52
        setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
53

    
54
        //vvvvvvvvvvvvv
55

    
56
        initialText = initialText==null?"":initialText;
57

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

    
60
        int combinedStyle = style | SWT.BORDER | scrollStyle;
61

    
62
        // SWT.PASSWORD does not work when SWT.WRAP is set.
63
        if (style != SWT.PASSWORD) {
64
            combinedStyle = combinedStyle | SWT.WRAP;
65
        }
66
        //^^^^^^^^^^^^^^
67

    
68
        label = new Label(this, SWT.NONE);
69
        TableWrapData twd_lblNewLabel = new TableWrapData(TableWrapData.RIGHT, TableWrapData.TOP);
70
        twd_lblNewLabel.grabHorizontal = true;
71
        twd_lblNewLabel.align = TableWrapData.LEFT;
72
        label.setLayoutData(twd_lblNewLabel);
73
        label.setText(labelString);
74

    
75
        text = new Text(this, SWT.NONE);
76
        TableWrapData twd_text_1 = new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP);
77
        twd_text_1.align = TableWrapData.RIGHT;
78
        twd_text_1.grabHorizontal = false;
79
        text.setText(initialText);
80
        text.setLayoutData(twd_text_1);
81

    
82

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

    
102
        TableWrapData layoutData = LayoutConstants.FILL();
103
        if (textHeight != null && textHeight > 0) {
104
            (layoutData).heightHint = textHeight;
105
        }
106

    
107
        text.setLayoutData(layoutData);
108

    
109
        text.addModifyListener(this);
110

    
111
        addControl(label);
112
        addControl(text);
113
    }
114

    
115

    
116
    /**
117
     * Get the text of this composites text composite
118
     *
119
     * @return a {@link java.lang.String} object.
120
     */
121
    public String getText() {
122
        return text.getText();
123
    }
124

    
125
    /**
126
     * Set the text of this composites text composite
127
     *
128
     * @param string
129
     *            a {@link java.lang.String} object.
130
     */
131
    public void setText(String string) {
132
        Listener[] listeners = text.getListeners(SWT.Modify);
133

    
134
        for (Listener listener : listeners) {
135
            text.removeListener(SWT.Modify, listener);
136
        }
137

    
138
        text.setText(CdmUtils.Nz(string));
139

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

    
145
    /*
146
     * (non-Javadoc)
147
     *
148
     * @see
149
     * org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events
150
     * .ModifyEvent)
151
     */
152
    /** {@inheritDoc} */
153
    @Override
154
    public void modifyText(ModifyEvent e) {
155
        firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
156
    }
157

    
158
    /** {@inheritDoc} */
159
    @Override
160
    public void setEnabled(boolean enabled) {
161
        text.setEnabled(enabled);
162
        String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
163
        text.setForeground(getColor(symbolicName));
164
    }
165

    
166
    /** {@inheritDoc} */
167
    @Override
168
    public void setIrrelevant(boolean irrelevant) {
169
        String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
170

    
171
        Color color = getColor(colorId);
172
        text.setBackground(color);
173
    }
174

    
175
    /** {@inheritDoc} */
176
    @Override
177
    public void setBackground(Color color) {
178
        super.setBackground(color);
179
        if (label != null) {
180
            label.setBackground(color);
181
        }
182
    }
183

    
184
    @Override
185
    public void setSelected(boolean selected) {
186
        setBackground(selected ? SELECTED : getPersistentBackground());
187
    }
188

    
189
    /*
190
     * (non-Javadoc)
191
     *
192
     * @see eu.etaxonomy.taxeditor.forms.AbstractCdmFormElement#setFocus()
193
     */
194
    /** {@inheritDoc}
195
     * @return */
196
    @Override
197
    public boolean setFocus() {
198
        return text.setFocus();
199
    }
200

    
201
    /**
202
     * <p>
203
     * getMainControl
204
     * </p>
205
     *
206
     * @return a {@link org.eclipse.swt.widgets.Control} object.
207
     */
208
    public Control getMainControl() {
209
        return text;
210
    }
211

    
212
    /**
213
     * <p>
214
     * setTextLimit
215
     * </p>
216
     *
217
     * @param limit
218
     *            a int.
219
     */
220
    public void setTextLimit(int limit) {
221
        text.setTextLimit(limit);
222
    }
223
}
(36-36/40)