1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/* *************************************************************************
IT Mill Toolkit
Development of Browser User Intarfaces Made Easy
Copyright (C) 2000-2006 IT Mill Ltd
*************************************************************************
This product is distributed under commercial license that can be found
from the product package on license/license.txt. Use of this product might
require purchasing a commercial license from IT Mill Ltd. For guidelines
on usage, see license/licensing-guidelines.html
*************************************************************************
For more information, contact:
IT Mill Ltd phone: +358 2 4802 7180
Ruukinkatu 2-4 fax: +358 2 4802 7181
20540, Turku email: info@itmill.com
Finland company www: www.itmill.com
Primary source for information and releases: www.itmill.com
********************************************************************** */
package com.itmill.toolkit.ui;
import java.util.Map;
import java.lang.reflect.Method;
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;
/** A generic button component.
*
* @author IT Mill Ltd.
* @version @VERSION@
* @since 3.0
*/
public class Button extends AbstractField {
/* Final members *************************************************** */
/** strings to be catched at adatapter (transformer) */
private static final String BUTTON_VAR_NAME = "clicked";
/* Private members ************************************************* */
boolean switchMode = false;
/** Creates a new push button.
*
* The value of the push button is allways false and they are
* immediate by default.
*
*/
public Button() {
setSwitchMode(false);
}
/** Creates a new push button.
*
* The value of the push button is allways false and they are
* immediate by default.
*
* @param caption Button caption
*/
public Button(String caption) {
setCaption(caption);
setSwitchMode(false);
}
/** Creates a new push button with click listener.
* @param caption Button caption
* @param listener Button click listener
*/
public Button(String caption, ClickListener listener) {
this(caption);
addListener(listener);
}
/** Creates a new push button with a method listening button clicks.
* The method must have either no parameters, or only one parameter of
* Button.ClickEvent type.
* @param caption Button caption
* @param target Object having the method for listening button clicks
* @param methodName The name of the method in target object, that
* receives button click events.
*/
public Button(String caption, Object target, String methodName) {
this(caption);
addListener(ClickEvent.class, target, methodName);
}
/** Creates new switch button with initial value.
* @param state Initial state of the switch-button.
*/
public Button(String caption, boolean initialState) {
setCaption(caption);
setValue(new Boolean(initialState));
setSwitchMode(true);
}
/** Creates new switch button that is connected to a boolean property.
* @param state Initial state of the switch-button.
*/
public Button(String caption, Property dataSource) {
setCaption(caption);
setSwitchMode(true);
setPropertyDataSource(dataSource);
}
/** Get component UIDL tag.
* @return Component UIDL tag as string.
*/
public String getTag() {
return "button";
}
/** Paint the content of this component.
* @param event PaintEvent.
* @throws IOException Passed from the UIDLStream.
* @throws PaintException The paint operation failed.
*/
public void paintContent(PaintTarget target) throws PaintException {
super.paintContent(target);
if (isSwitchMode())
target.addAttribute("type", "switch");
boolean state;
try {
state = ((Boolean) getValue()).booleanValue();
} catch (NullPointerException e) {
state = false;
}
target.addVariable(this, "state", state);
}
/** Invoked when the value of a variable has changed. Button
* listeners are notified if the button is clicked.
* @param event Variable change event.
*/
public void changeVariables(Object source, Map variables) {
if (variables.containsKey("state")) {
// Get the new and old button states
Boolean newValue = (Boolean) variables.get("state");
Boolean oldValue = (Boolean) getValue();
if (isSwitchMode()) {
// For switch button, the event is only sent if the
// switch state is changed
if (newValue != null
&& !newValue.equals(oldValue)
&& !isReadOnly()) {
setValue(newValue);
fireClick();
}
} else {
// Only send click event if the button is pushed
if (newValue.booleanValue())
fireClick();
// If the button is true for some reason, release it
if (oldValue.booleanValue())
setValue(new Boolean(false));
}
}
}
/**
* Returns the switchMode.
* @return boolean
*/
public boolean isSwitchMode() {
return switchMode;
}
/**
* Sets the switchMode.
* @param switchMode The switchMode to set
*/
public void setSwitchMode(boolean switchMode) {
this.switchMode = switchMode;
if (!switchMode) {
setImmediate(true);
setValue(new Boolean(false));
}
}
/** Set immediate mode.
* @see com.itmill.toolkit.ui.AbstractComponent#setImmediate(boolean)
*
* Push buttons can not be set in non-immediate mode.
*/
public void setImmediate(boolean immediate) {
// Push buttons are allways immediate
super.setImmediate(!isSwitchMode() || immediate);
}
/** The type of the button as a property.
* @see com.itmill.toolkit.data.Property#getType()
*/
public Class getType() {
return Boolean.class;
}
/* Click event ************************************************ */
private static final Method BUTTON_CLICK_METHOD;
static {
try {
BUTTON_CLICK_METHOD =
ClickListener.class.getDeclaredMethod(
"buttonClick",
new Class[] { ClickEvent.class });
} catch (java.lang.NoSuchMethodException e) {
// This should never happen
throw new java.lang.RuntimeException();
}
}
/** Click event. This event is thrown, when the button is clicked.
* @author IT Mill Ltd.
* @version @VERSION@
* @since 3.0
*/
public class ClickEvent extends Component.Event {
/**
* Serial generated by eclipse.
*/
private static final long serialVersionUID = 3546647602931118393L;
/** New instance of text change event
* @param source Source of the event.
*/
public ClickEvent(Component source) {
super(source);
}
/** Button where the event occurred
* @return Source of the event.
*/
public Button getButton() {
return (Button) getSource();
}
}
/** Button click listener
* @author IT Mill Ltd.
* @version @VERSION@
* @since 3.0
*/
public interface ClickListener {
/** Button has been pressed.
* @param event Button click event.
*/
public void buttonClick(ClickEvent event);
}
/** Add button click listener
* @param listener Listener to be added.
*/
public void addListener(ClickListener listener) {
addListener(ClickEvent.class, listener, BUTTON_CLICK_METHOD);
}
/** Remove button click listener
* @param listener Listener to be removed.
*/
public void removeListener(ClickListener listener) {
removeListener(ClickEvent.class, listener, BUTTON_CLICK_METHOD);
}
/** Emit options change event. */
protected void fireClick() {
fireEvent(new Button.ClickEvent(this));
}
}
|