1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.ui.element;
|
11
|
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Set;
|
14
|
|
15
|
import org.eclipse.swt.SWT;
|
16
|
import org.eclipse.swt.events.KeyAdapter;
|
17
|
import org.eclipse.swt.events.KeyEvent;
|
18
|
import org.eclipse.swt.events.ModifyEvent;
|
19
|
import org.eclipse.swt.events.ModifyListener;
|
20
|
import org.eclipse.swt.events.SelectionEvent;
|
21
|
import org.eclipse.swt.events.SelectionListener;
|
22
|
import org.eclipse.swt.graphics.Color;
|
23
|
import org.eclipse.swt.widgets.Button;
|
24
|
import org.eclipse.swt.widgets.Composite;
|
25
|
import org.eclipse.swt.widgets.Label;
|
26
|
import org.eclipse.swt.widgets.Text;
|
27
|
|
28
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
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 final Text text_cache;
|
42
|
private final Button button_toggle;
|
43
|
private boolean state;
|
44
|
private final Set<SelectionListener> selectionListener = new HashSet<SelectionListener>();
|
45
|
private final Label label;
|
46
|
private final Composite container;
|
47
|
|
48
|
//TODO for RL
|
49
|
public void setVisible(boolean b) {
|
50
|
text_cache.setVisible(b);
|
51
|
button_toggle.setVisible(b);
|
52
|
label.setVisible(b);
|
53
|
container.setVisible(b);
|
54
|
}
|
55
|
|
56
|
|
57
|
/**
|
58
|
* <p>Constructor for ToggleableTextElement.</p>
|
59
|
*
|
60
|
* @param style a int.
|
61
|
* @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
|
62
|
* @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object.
|
63
|
* @param labelString a {@link java.lang.String} object.
|
64
|
* @param initialText a {@link java.lang.String} object.
|
65
|
* @param initialState a boolean.
|
66
|
*/
|
67
|
protected ToggleableTextElement(CdmFormFactory formFactory, ICdmFormElement parentElement,
|
68
|
String labelString, String initialText, boolean initialState, int style) {
|
69
|
super(formFactory, parentElement);
|
70
|
|
71
|
label = formFactory.createLabel(getLayoutComposite(), labelString, style);
|
72
|
addControl(label);
|
73
|
|
74
|
container = formFactory.createComposite(getLayoutComposite(), SWT.WRAP);
|
75
|
container.setLayout(LayoutConstants.LAYOUT(2, false));
|
76
|
container.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
|
77
|
addControl(container);
|
78
|
|
79
|
text_cache = formFactory.createText(container, initialText, SWT.WRAP | SWT.MULTI);
|
80
|
addControl(text_cache);
|
81
|
text_cache.addModifyListener(this);
|
82
|
text_cache.setLayoutData(LayoutConstants.FILL());
|
83
|
|
84
|
// Don't accept carriage returns as input
|
85
|
text_cache.addKeyListener( new KeyAdapter(){
|
86
|
@Override
|
87
|
public void keyPressed(KeyEvent e) {
|
88
|
if(e.character == SWT.CR) {
|
89
|
e.doit = false;
|
90
|
}
|
91
|
}
|
92
|
});
|
93
|
|
94
|
button_toggle = formFactory.createButton(container, "Edit", SWT.TOGGLE);
|
95
|
button_toggle.setText("");
|
96
|
button_toggle.setImage(ImageResources.getImage(ImageResources.LOCK_ICON));
|
97
|
addControl(button_toggle);
|
98
|
button_toggle.addSelectionListener(this);
|
99
|
|
100
|
setState(initialState);
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* <p>setText</p>
|
105
|
*
|
106
|
* @param text a {@link java.lang.String} object.
|
107
|
*/
|
108
|
public void setText(String text){
|
109
|
if(text != null){
|
110
|
// store current caret position
|
111
|
int caretPosition = text_cache.getCaretPosition();
|
112
|
|
113
|
text_cache.removeModifyListener(this);
|
114
|
text_cache.setText(text);
|
115
|
text_cache.addModifyListener(this);
|
116
|
|
117
|
// restore caret position
|
118
|
text_cache.setSelection(caretPosition);
|
119
|
}
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* <p>getText</p>
|
124
|
*
|
125
|
* @return a {@link java.lang.String} object.
|
126
|
*/
|
127
|
public String getText(){
|
128
|
return text_cache.getText();
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* <p>Setter for the field <code>state</code>.</p>
|
133
|
*
|
134
|
* @param state a boolean.
|
135
|
*/
|
136
|
private void setState(boolean state) {
|
137
|
if(state){
|
138
|
button_toggle.setImage(ImageResources.getImage(ImageResources.LOCK_OPEN_ICON));
|
139
|
}
|
140
|
else{
|
141
|
button_toggle.setImage(ImageResources.getImage(ImageResources.LOCK_ICON));
|
142
|
}
|
143
|
text_cache.setEnabled(state);
|
144
|
this.state = state;
|
145
|
}
|
146
|
|
147
|
/** {@inheritDoc} */
|
148
|
@Override
|
149
|
public void setEnabled(boolean enabled) {
|
150
|
text_cache.setEnabled(enabled);
|
151
|
button_toggle.setGrayed(enabled);
|
152
|
button_toggle.setSelection(enabled);
|
153
|
String symbolicName = enabled ? Resources.COLOR_FONT_DEFAULT : Resources.COLOR_TEXT_DISABLED;
|
154
|
text_cache.setForeground(StoreUtil.getColor(symbolicName));
|
155
|
}
|
156
|
|
157
|
/* (non-Javadoc)
|
158
|
* @see eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement#isEnabled()
|
159
|
*/
|
160
|
@Override
|
161
|
public boolean isEnabled() {
|
162
|
return text_cache.isEnabled();
|
163
|
}
|
164
|
|
165
|
/**
|
166
|
* <p>Getter for the field <code>state</code>.</p>
|
167
|
*
|
168
|
* @return a boolean.
|
169
|
*/
|
170
|
public boolean getState(){
|
171
|
return state;
|
172
|
}
|
173
|
|
174
|
/* (non-Javadoc)
|
175
|
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
|
176
|
*/
|
177
|
/** {@inheritDoc} */
|
178
|
@Override
|
179
|
public void widgetSelected(SelectionEvent e) {
|
180
|
setState(button_toggle.getSelection());
|
181
|
for(SelectionListener listener : selectionListener){
|
182
|
listener.widgetSelected(e);
|
183
|
}
|
184
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* <p>addSelectionListener</p>
|
189
|
*
|
190
|
* @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
|
191
|
*/
|
192
|
public void addSelectionListener(SelectionListener listener){
|
193
|
selectionListener.add(listener);
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* <p>removeSelectionListener</p>
|
198
|
*
|
199
|
* @param listener a {@link org.eclipse.swt.events.SelectionListener} object.
|
200
|
*/
|
201
|
public void removeSelectionListener(SelectionListener listener){
|
202
|
selectionListener.remove(listener);
|
203
|
}
|
204
|
|
205
|
/** {@inheritDoc} */
|
206
|
@Override
|
207
|
public void widgetDefaultSelected(SelectionEvent e) {}
|
208
|
|
209
|
/** {@inheritDoc} */
|
210
|
@Override
|
211
|
public void modifyText(ModifyEvent e) {
|
212
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
213
|
}
|
214
|
|
215
|
/** {@inheritDoc} */
|
216
|
@Override
|
217
|
public void setIrrelevant(boolean irrelevant) {
|
218
|
String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
|
219
|
|
220
|
Color color = getColor(colorId);
|
221
|
text_cache.setBackground(color);
|
222
|
}
|
223
|
|
224
|
/** {@inheritDoc} */
|
225
|
@Override
|
226
|
public void setBackground(Color color) {
|
227
|
label.setBackground(color);
|
228
|
container.setBackground(color);
|
229
|
}
|
230
|
|
231
|
@Override
|
232
|
public void setSelected(boolean selected) {
|
233
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
234
|
}
|
235
|
}
|