1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 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
|
|
11
|
package eu.etaxonomy.taxeditor.ui.element;
|
12
|
|
13
|
import java.util.HashSet;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.eclipse.swt.SWT;
|
17
|
import org.eclipse.swt.events.KeyAdapter;
|
18
|
import org.eclipse.swt.events.KeyEvent;
|
19
|
import org.eclipse.swt.events.ModifyEvent;
|
20
|
import org.eclipse.swt.events.ModifyListener;
|
21
|
import org.eclipse.swt.events.SelectionEvent;
|
22
|
import org.eclipse.swt.events.SelectionListener;
|
23
|
import org.eclipse.swt.graphics.Color;
|
24
|
import org.eclipse.swt.widgets.Button;
|
25
|
import org.eclipse.swt.widgets.Composite;
|
26
|
import org.eclipse.swt.widgets.Label;
|
27
|
import org.eclipse.swt.widgets.Text;
|
28
|
|
29
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
30
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
31
|
|
32
|
/**
|
33
|
* When the button is pressed, this textfield may be edited.
|
34
|
*
|
35
|
* @author n.hoffmann
|
36
|
* @created Nov 18, 2009
|
37
|
* @version 1.0
|
38
|
*/
|
39
|
public class ToggleableTextElement extends AbstractCdmFormElement implements SelectionListener, ModifyListener, IEnableableFormElement, ISelectable {
|
40
|
|
41
|
private Text text_cache;
|
42
|
private Button button_toggle;
|
43
|
|
44
|
private boolean state;
|
45
|
private Set<SelectionListener> selectionListener = new HashSet<SelectionListener>();
|
46
|
private Label label;
|
47
|
private Composite container;
|
48
|
|
49
|
/**
|
50
|
* <p>Constructor for ToggleableTextElement.</p>
|
51
|
*
|
52
|
* @param style a int.
|
53
|
* @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
|
54
|
* @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
|
55
|
* @param labelString a {@link java.lang.String} object.
|
56
|
* @param initialText a {@link java.lang.String} object.
|
57
|
* @param initialState a boolean.
|
58
|
*/
|
59
|
protected ToggleableTextElement(CdmFormFactory formFactory, ICdmFormElement parentElement,
|
60
|
String labelString, String initialText, boolean initialState, int style) {
|
61
|
super(formFactory, parentElement);
|
62
|
|
63
|
label = formFactory.createLabel(getLayoutComposite(), labelString, style);
|
64
|
addControl(label);
|
65
|
|
66
|
container = formFactory.createComposite(getLayoutComposite(), SWT.WRAP);
|
67
|
container.setLayout(CdmFormFactory.LAYOUT(2, false));
|
68
|
container.setLayoutData(CdmFormFactory.FILL_HORIZONTALLY());
|
69
|
addControl(container);
|
70
|
|
71
|
text_cache = formFactory.createText(container, initialText, SWT.WRAP | SWT.MULTI);
|
72
|
addControl(text_cache);
|
73
|
text_cache.addModifyListener(this);
|
74
|
text_cache.setLayoutData(CdmFormFactory.FILL());
|
75
|
|
76
|
// Don't accept carriage returns as input
|
77
|
text_cache.addKeyListener( new KeyAdapter(){
|
78
|
@Override
|
79
|
public void keyPressed(KeyEvent e) {
|
80
|
if(e.character == SWT.CR)
|
81
|
e.doit = false;
|
82
|
}
|
83
|
});
|
84
|
|
85
|
button_toggle = formFactory.createButton(container, "Edit", SWT.TOGGLE);
|
86
|
addControl(button_toggle);
|
87
|
button_toggle.addSelectionListener(this);
|
88
|
|
89
|
setState(initialState);
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* <p>setText</p>
|
94
|
*
|
95
|
* @param text a {@link java.lang.String} object.
|
96
|
*/
|
97
|
public void setText(String text){
|
98
|
if(text != null){
|
99
|
// store current caret position
|
100
|
int caretPosition = text_cache.getCaretPosition();
|
101
|
|
102
|
text_cache.removeModifyListener(this);
|
103
|
text_cache.setText(text);
|
104
|
text_cache.addModifyListener(this);
|
105
|
|
106
|
// restore caret position
|
107
|
text_cache.setSelection(caretPosition);
|
108
|
}
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* <p>getText</p>
|
113
|
*
|
114
|
* @return a {@link java.lang.String} object.
|
115
|
*/
|
116
|
public String getText(){
|
117
|
return text_cache.getText();
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* <p>Setter for the field <code>state</code>.</p>
|
122
|
*
|
123
|
* @param state a boolean.
|
124
|
*/
|
125
|
public void setState(boolean state) {
|
126
|
this.state = state;
|
127
|
setEnabled(state);
|
128
|
}
|
129
|
|
130
|
/** {@inheritDoc} */
|
131
|
public void setEnabled(boolean enabled) {
|
132
|
text_cache.setEnabled(enabled);
|
133
|
String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
|
134
|
text_cache.setForeground(StoreUtil.getColor(symbolicName));
|
135
|
button_toggle.setSelection(enabled);
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* <p>Getter for the field <code>state</code>.</p>
|
140
|
*
|
141
|
* @return a boolean.
|
142
|
*/
|
143
|
public boolean getState(){
|
144
|
return state;
|
145
|
}
|
146
|
|
147
|
/* (non-Javadoc)
|
148
|
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
|
149
|
*/
|
150
|
/** {@inheritDoc} */
|
151
|
public void widgetSelected(SelectionEvent e) {
|
152
|
setState(button_toggle.getSelection());
|
153
|
for(SelectionListener listener : selectionListener){
|
154
|
listener.widgetSelected(e);
|
155
|
}
|
156
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* <p>addSelectionListener</p>
|
161
|
*
|
162
|
* @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
|
163
|
*/
|
164
|
public void addSelectionListener(SelectionListener listener){
|
165
|
selectionListener.add(listener);
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* <p>removeSelectionListener</p>
|
170
|
*
|
171
|
* @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
|
172
|
*/
|
173
|
public void removeSelectionListener(SelectionListener listener){
|
174
|
selectionListener.remove(listener);
|
175
|
}
|
176
|
|
177
|
/** {@inheritDoc} */
|
178
|
public void widgetDefaultSelected(SelectionEvent e) {}
|
179
|
|
180
|
/** {@inheritDoc} */
|
181
|
public void modifyText(ModifyEvent e) {
|
182
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
183
|
}
|
184
|
|
185
|
/** {@inheritDoc} */
|
186
|
public void setIrrelevant(boolean irrelevant) {
|
187
|
String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
|
188
|
|
189
|
Color color = getColor(colorId);
|
190
|
text_cache.setBackground(color);
|
191
|
}
|
192
|
|
193
|
/** {@inheritDoc} */
|
194
|
@Override
|
195
|
public void setBackground(Color color) {
|
196
|
label.setBackground(color);
|
197
|
container.setBackground(color);
|
198
|
}
|
199
|
|
200
|
@Override
|
201
|
public void setSelected(boolean selected) {
|
202
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
203
|
}
|
204
|
}
|