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
|
package eu.etaxonomy.taxeditor.ui.element;
|
10
|
|
11
|
import java.util.HashSet;
|
12
|
import java.util.Set;
|
13
|
|
14
|
import org.eclipse.swt.SWT;
|
15
|
import org.eclipse.swt.events.KeyAdapter;
|
16
|
import org.eclipse.swt.events.KeyEvent;
|
17
|
import org.eclipse.swt.events.ModifyEvent;
|
18
|
import org.eclipse.swt.events.ModifyListener;
|
19
|
import org.eclipse.swt.events.SelectionEvent;
|
20
|
import org.eclipse.swt.events.SelectionListener;
|
21
|
import org.eclipse.swt.graphics.Color;
|
22
|
import org.eclipse.swt.widgets.Button;
|
23
|
import org.eclipse.swt.widgets.Composite;
|
24
|
import org.eclipse.swt.widgets.Label;
|
25
|
import org.eclipse.swt.widgets.Text;
|
26
|
|
27
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
28
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
29
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
30
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
31
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
32
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
33
|
|
34
|
/**
|
35
|
* When the button is pressed, this textfield may be edited.
|
36
|
*
|
37
|
* @author n.hoffmann
|
38
|
* @created Nov 18, 2009
|
39
|
*/
|
40
|
public class ToggleableTextElement
|
41
|
extends AbstractRelevanceFormElement
|
42
|
implements SelectionListener, ModifyListener,
|
43
|
IEnableableFormElement, ISelectable {
|
44
|
|
45
|
private final Text text_cache;
|
46
|
private final Button btnToggleProtectTitleCache;
|
47
|
private boolean state;
|
48
|
private final Set<SelectionListener> selectionListener = new HashSet<>();
|
49
|
private final Label label;
|
50
|
private final Composite container;
|
51
|
private final CacheRelevance relevance;
|
52
|
|
53
|
private boolean isEnabled = true;
|
54
|
//TODO for RL
|
55
|
public void setVisible(boolean b) {
|
56
|
text_cache.setVisible(b);
|
57
|
btnToggleProtectTitleCache.setVisible(b);
|
58
|
label.setVisible(b);
|
59
|
container.setVisible(b);
|
60
|
}
|
61
|
|
62
|
protected ToggleableTextElement(CdmFormFactory formFactory, ICdmFormElement parentElement,
|
63
|
String labelString, String initialText, boolean initialState,
|
64
|
CacheRelevance relevance, 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(LayoutConstants.LAYOUT(2, false));
|
72
|
container.setLayoutData(LayoutConstants.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(LayoutConstants.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 (!state){
|
85
|
return;
|
86
|
}
|
87
|
if(e.character == SWT.CR) {
|
88
|
e.doit = false;
|
89
|
}
|
90
|
}
|
91
|
});
|
92
|
|
93
|
btnToggleProtectTitleCache = formFactory.createButton(container, "Edit", SWT.TOGGLE);
|
94
|
btnToggleProtectTitleCache.setText("");
|
95
|
btnToggleProtectTitleCache.setImage(ImageResources.getImage(ImageResources.LOCK_ICON));
|
96
|
addControl(btnToggleProtectTitleCache);
|
97
|
btnToggleProtectTitleCache.addSelectionListener(this);
|
98
|
|
99
|
this.relevance = relevance;
|
100
|
setState(initialState);
|
101
|
}
|
102
|
|
103
|
public void setText(String text){
|
104
|
if(text != null){
|
105
|
// store current caret position
|
106
|
int caretPosition = text_cache.getCaretPosition();
|
107
|
|
108
|
StoreUtil.setTextWithoutModifyListeners(text_cache, text != null? text.trim(): text);
|
109
|
// text_cache.removeModifyListener(this);
|
110
|
// text_cache.setText(text);
|
111
|
// text_cache.addModifyListener(this);
|
112
|
|
113
|
// restore caret position
|
114
|
text_cache.setSelection(caretPosition);
|
115
|
}
|
116
|
}
|
117
|
|
118
|
public String getText(){
|
119
|
return text_cache.getText();
|
120
|
}
|
121
|
|
122
|
private void setState(boolean state) {
|
123
|
if(state){
|
124
|
btnToggleProtectTitleCache.setImage(ImageResources.getImage(ImageResources.LOCK_OPEN_ICON));
|
125
|
btnToggleProtectTitleCache.setToolTipText(Messages.ToggleableText_ToolTip_open);
|
126
|
}
|
127
|
else{
|
128
|
btnToggleProtectTitleCache.setImage(ImageResources.getImage(ImageResources.LOCK_ICON));
|
129
|
btnToggleProtectTitleCache.setToolTipText(Messages.ToggleableText_ToolTip_closed);
|
130
|
}
|
131
|
setCacheEnabled(state);
|
132
|
this.state = state;
|
133
|
}
|
134
|
|
135
|
|
136
|
public void setCacheEnabled(boolean enabled) {
|
137
|
text_cache.setEditable(enabled);
|
138
|
btnToggleProtectTitleCache.setGrayed(enabled);
|
139
|
btnToggleProtectTitleCache.setSelection(enabled);
|
140
|
Color color = enabled ? null : AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED);
|
141
|
|
142
|
text_cache.setForeground(color);
|
143
|
}
|
144
|
|
145
|
@Override
|
146
|
public void setEnabled(boolean isEnabled){
|
147
|
this.isEnabled = isEnabled;
|
148
|
text_cache.setEditable(isEnabled);
|
149
|
btnToggleProtectTitleCache.setEnabled(isEnabled);
|
150
|
}
|
151
|
|
152
|
@Override
|
153
|
public boolean isEnabled() {
|
154
|
return isEnabled;
|
155
|
}
|
156
|
|
157
|
public boolean isCacheEnabled() {
|
158
|
return btnToggleProtectTitleCache.getSelection();
|
159
|
}
|
160
|
|
161
|
public CacheRelevance getRelevance() {
|
162
|
return this.relevance;
|
163
|
}
|
164
|
|
165
|
public boolean getState(){
|
166
|
return state;
|
167
|
}
|
168
|
|
169
|
public Button getBtnToggleProtectTitleCache() {
|
170
|
return btnToggleProtectTitleCache;
|
171
|
}
|
172
|
|
173
|
@Override
|
174
|
public void widgetSelected(SelectionEvent e) {
|
175
|
setState(btnToggleProtectTitleCache.getSelection());
|
176
|
for(SelectionListener listener : selectionListener){
|
177
|
listener.widgetSelected(e);
|
178
|
}
|
179
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
180
|
}
|
181
|
|
182
|
public void addSelectionListener(SelectionListener listener){
|
183
|
selectionListener.add(listener);
|
184
|
}
|
185
|
|
186
|
public void removeSelectionListener(SelectionListener listener){
|
187
|
selectionListener.remove(listener);
|
188
|
}
|
189
|
|
190
|
@Override
|
191
|
public void widgetDefaultSelected(SelectionEvent e) {}
|
192
|
|
193
|
@Override
|
194
|
public void modifyText(ModifyEvent e) {
|
195
|
if (state){
|
196
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
197
|
}
|
198
|
}
|
199
|
|
200
|
@Override
|
201
|
public void updateCacheRelevance() {
|
202
|
Color color = cacheRelevance().getColor();
|
203
|
text_cache.setBackground(color);
|
204
|
}
|
205
|
|
206
|
@Override
|
207
|
public void setBackground(Color color) {
|
208
|
label.setBackground(color);
|
209
|
container.setBackground(color);
|
210
|
}
|
211
|
|
212
|
@Override
|
213
|
public void setSelected(boolean selected) {
|
214
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
215
|
}
|
216
|
|
217
|
@Override
|
218
|
public String toString() {
|
219
|
if (label != null){
|
220
|
return CdmUtils.concat("", "ToggleableTextElement[", label.getText(),"]");
|
221
|
}else{
|
222
|
return super.toString();
|
223
|
}
|
224
|
}
|
225
|
|
226
|
}
|