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