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