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