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.AbstractUtility;
|
29
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
30
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
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 btnToggleProtectTitleCache;
|
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
|
btnToggleProtectTitleCache.setVisible(b);
|
52
|
label.setVisible(b);
|
53
|
container.setVisible(b);
|
54
|
}
|
55
|
|
56
|
protected ToggleableTextElement(CdmFormFactory formFactory, ICdmFormElement parentElement,
|
57
|
String labelString, String initialText, boolean initialState, int style) {
|
58
|
super(formFactory, parentElement);
|
59
|
|
60
|
label = formFactory.createLabel(getLayoutComposite(), labelString, style);
|
61
|
addControl(label);
|
62
|
|
63
|
container = formFactory.createComposite(getLayoutComposite(), SWT.WRAP);
|
64
|
container.setLayout(LayoutConstants.LAYOUT(2, false));
|
65
|
container.setLayoutData(LayoutConstants.FILL_HORIZONTALLY());
|
66
|
addControl(container);
|
67
|
|
68
|
text_cache = formFactory.createText(container, initialText, SWT.WRAP | SWT.MULTI);
|
69
|
addControl(text_cache);
|
70
|
text_cache.addModifyListener(this);
|
71
|
text_cache.setLayoutData(LayoutConstants.FILL());
|
72
|
|
73
|
// Don't accept carriage returns as input
|
74
|
text_cache.addKeyListener( new KeyAdapter(){
|
75
|
@Override
|
76
|
public void keyPressed(KeyEvent e) {
|
77
|
if(e.character == SWT.CR) {
|
78
|
e.doit = false;
|
79
|
}
|
80
|
}
|
81
|
});
|
82
|
|
83
|
btnToggleProtectTitleCache = formFactory.createButton(container, "Edit", SWT.TOGGLE);
|
84
|
btnToggleProtectTitleCache.setText("");
|
85
|
btnToggleProtectTitleCache.setImage(ImageResources.getImage(ImageResources.LOCK_ICON));
|
86
|
addControl(btnToggleProtectTitleCache);
|
87
|
btnToggleProtectTitleCache.addSelectionListener(this);
|
88
|
|
89
|
setState(initialState);
|
90
|
}
|
91
|
|
92
|
public void setText(String text){
|
93
|
if(text != null){
|
94
|
// store current caret position
|
95
|
int caretPosition = text_cache.getCaretPosition();
|
96
|
|
97
|
text_cache.removeModifyListener(this);
|
98
|
text_cache.setText(text);
|
99
|
text_cache.addModifyListener(this);
|
100
|
|
101
|
// restore caret position
|
102
|
text_cache.setSelection(caretPosition);
|
103
|
}
|
104
|
}
|
105
|
|
106
|
public String getText(){
|
107
|
return text_cache.getText();
|
108
|
}
|
109
|
|
110
|
private void setState(boolean state) {
|
111
|
if(state){
|
112
|
btnToggleProtectTitleCache.setImage(ImageResources.getImage(ImageResources.LOCK_OPEN_ICON));
|
113
|
}
|
114
|
else{
|
115
|
btnToggleProtectTitleCache.setImage(ImageResources.getImage(ImageResources.LOCK_ICON));
|
116
|
}
|
117
|
setEnabled(state);
|
118
|
this.state = state;
|
119
|
}
|
120
|
|
121
|
@Override
|
122
|
public void setEnabled(boolean enabled) {
|
123
|
text_cache.setEnabled(enabled);
|
124
|
btnToggleProtectTitleCache.setGrayed(enabled);
|
125
|
btnToggleProtectTitleCache.setSelection(enabled);
|
126
|
Color color = enabled ? null : AbstractUtility.getColor(Resources.COLOR_TEXT_DISABLED);
|
127
|
|
128
|
text_cache.setForeground(color);
|
129
|
}
|
130
|
|
131
|
@Override
|
132
|
public boolean isEnabled() {
|
133
|
return text_cache.isEnabled();
|
134
|
}
|
135
|
|
136
|
public boolean getState(){
|
137
|
return state;
|
138
|
}
|
139
|
|
140
|
public Button getBtnToggleProtectTitleCache() {
|
141
|
return btnToggleProtectTitleCache;
|
142
|
}
|
143
|
|
144
|
@Override
|
145
|
public void widgetSelected(SelectionEvent e) {
|
146
|
setState(btnToggleProtectTitleCache.getSelection());
|
147
|
for(SelectionListener listener : selectionListener){
|
148
|
listener.widgetSelected(e);
|
149
|
}
|
150
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
151
|
}
|
152
|
|
153
|
public void addSelectionListener(SelectionListener listener){
|
154
|
selectionListener.add(listener);
|
155
|
}
|
156
|
|
157
|
public void removeSelectionListener(SelectionListener listener){
|
158
|
selectionListener.remove(listener);
|
159
|
}
|
160
|
|
161
|
@Override
|
162
|
public void widgetDefaultSelected(SelectionEvent e) {}
|
163
|
|
164
|
@Override
|
165
|
public void modifyText(ModifyEvent e) {
|
166
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(this, e));
|
167
|
}
|
168
|
|
169
|
@Override
|
170
|
public void setIrrelevant(boolean irrelevant) {
|
171
|
String colorId = irrelevant ? Resources.COLOR_COMPOSITE_IRRELEVANT : Resources.COLOR_COMPOSITE_BACKGROUND;
|
172
|
|
173
|
Color color = getColor(colorId);
|
174
|
text_cache.setBackground(color);
|
175
|
}
|
176
|
|
177
|
@Override
|
178
|
public void setBackground(Color color) {
|
179
|
label.setBackground(color);
|
180
|
container.setBackground(color);
|
181
|
}
|
182
|
|
183
|
@Override
|
184
|
public void setSelected(boolean selected) {
|
185
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
186
|
}
|
187
|
}
|