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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
/* DefaultButtonModel.java --
Copyright (C) 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.io.Serializable;
import java.util.EventListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
/**
* The pUrpose of this class is to model the dynamic state of an abstract
* button. The concrete button type holding this state may be a a "toggle"
* button (checkbox, radio button) or a "push" button (menu button, button).
* If the model is disabled, only the "selected" property can be changed. An
* attempt to change the "armed", "rollover" or "pressed" properties while
* the model is disabled will be blocked. Any successful (non-blocked) change
* to the model's properties will trigger the firing of a ChangeEvent. Any
* change to the "selected" property will trigger the firing of an ItemEvent
* in addition to ChangeEvent. This is true whether the model is enabled or
* not. One other state change is special: the transition from "enabled,
* armed and pressd" to "enabled, armed and not-pressed". This is considered
* the "trailing edge" of a successful mouse click, and therefore fires an
* ActionEvent in addition to a ChangeEvent. In all other respects this class
* is just a container of boolean flags.
*
* @author Graydon Hoare (graydon_at_redhat.com)
*/
public class DefaultButtonModel implements ButtonModel, Serializable
{
/** DOCUMENT ME! */
private static final long serialVersionUID = -5342609566534980231L;
/**
* Indicates that the button is <em>partially</em> committed to being
* pressed, but not entirely. This usually happens when a user has pressed
* but not yet released the mouse button.
*/
public static final int ARMED = 1;
/**
* State constant indicating that the button is enabled. Buttons cannot be
* pressed or selected unless they are enabled.
*/
public static final int ENABLED = 8;
/**
* State constant indicating that the user is holding down the button. When
* this transitions from true to false, an ActionEvent may be fired,
* depending on the value of the "armed" property.
*/
public static final int PRESSED = 4;
/**
* State constant indicating that the mouse is currently positioned over the
* button.
*/
public static final int ROLLOVER = 16;
/**
* State constant indicating that the button is selected. This constant is
* only meaningful for toggle-type buttons (radio buttons, checkboxes).
*/
public static final int SELECTED = 2;
/**
* Represents the "state properties" (armed, enabled, pressed, rollover and
* selected) by a bitwise combination of integer constants.
*/
protected int stateMask = ENABLED;
/**
* List of ItemListeners, ChangeListeners, and ActionListeners registered on
* this model.
*/
protected EventListenerList listenerList = new EventListenerList();
/** The single ChangeEvent this model (re)uses to call its ChangeListeners. */
protected ChangeEvent changeEvent = new ChangeEvent(this);
/**
* The group this model belongs to. Only one button in a group may be
* selected at any given time.
*/
protected ButtonGroup group;
/**
* The key code (one of {@link java.awt.event.KeyEvent} VK_) used to press
* this button via a keyboard interface.
*/
protected int mnemonic = KeyEvent.VK_UNDEFINED;
/**
* The string used as the "command" property of any ActionEvent this model
* sends.
*/
protected String actionCommand;
/**
* Creates a new DefaultButtonModel object.
*/
public DefaultButtonModel()
{
}
/**
* Return <code>null</code>. Use {@link AbstractButton} if you wish to
* interface with a button via an {@link ItemSelectable} interface.
*
* @return <code>null</code>
*/
public Object[] getSelectedObjects()
{
return null;
}
/**
* Returns a specified class of listeners.
*
* @param listenerType the type of listener to return
*
* @return array of listeners
*/
public EventListener[] getListeners(Class listenerType)
{
return listenerList.getListeners(listenerType);
}
/**
* Add an ActionListener to the model. Usually only called to subscribe an
* AbstractButton's listener to the model.
*
* @param l The listener to add
*/
public void addActionListener(ActionListener l)
{
listenerList.add(ActionListener.class, l);
}
/**
* Remove an ActionListener to the model. Usually only called to unsubscribe
* an AbstractButton's listener to the model.
*
* @param l The listener to remove
*/
public void removeActionListener(ActionListener l)
{
listenerList.remove(ActionListener.class, l);
}
/**
* Returns all registered <code>ActionListener</code> objects.
*
* @return array of <code>ActionListener</code> objects
*/
public ActionListener[] getActionListeners()
{
return (ActionListener[]) listenerList.getListeners(ActionListener.class);
}
/**
* Add an ItemListener to the model. Usually only called to subscribe an
* AbstractButton's listener to the model.
*
* @param l The listener to add
*/
public void addItemListener(ItemListener l)
{
listenerList.add(ItemListener.class, l);
}
/**
* Remove an ItemListener to the model. Usually only called to unsubscribe
* an AbstractButton's listener to the model.
*
* @param l The listener to remove
*/
public void removeItemListener(ItemListener l)
{
listenerList.remove(ItemListener.class, l);
}
/**
* Returns all registered <code>ItemListener</code> objects.
*
* @return array of <code>ItemListener</code> objects
*/
public ItemListener[] getItemListeners()
{
return (ItemListener[]) listenerList.getListeners(ItemListener.class);
}
/**
* Add a ChangeListener to the model. Usually only called to subscribe an
* AbstractButton's listener to the model.
*
* @param l The listener to add
*/
public void addChangeListener(ChangeListener l)
{
listenerList.add(ChangeListener.class, l);
}
/**
* Remove a ChangeListener to the model. Usually only called to unsubscribe
* an AbstractButton's listener to the model.
*
* @param l The listener to remove
*/
public void removeChangeListener(ChangeListener l)
{
listenerList.remove(ChangeListener.class, l);
}
/**
* Returns all registered <code>ChangeListener</code> objects.
*
* @return array of <code>ChangeListener</code> objects
*/
public ChangeListener[] getChangeListeners()
{
return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
}
/**
* Inform each ItemListener in the {@link listenerList} that an ItemEvent
* has occurred. This happens in response to any change to the {@link
* stateMask} field.
*
* @param e The ItemEvent to fire
*/
protected void fireItemStateChanged(ItemEvent e)
{
ItemListener[] ll = getItemListeners();
for (int i = 0; i < ll.length; i++)
ll[i].itemStateChanged(e);
}
/**
* Inform each ActionListener in the {@link listenerList} that an
* ActionEvent has occurred. This happens in response to the any change to
* the {@link stateMask} field which makes the enabled, armed and pressed
* properties all simultaneously <code>true</code>.
*
* @param e The ActionEvent to fire
*/
protected void fireActionPerformed(ActionEvent e)
{
ActionListener[] ll = getActionListeners();
for (int i = 0; i < ll.length; i++)
ll[i].actionPerformed(e);
}
/**
* Inform each ChangeListener in the {@link listenerList} that a ChangeEvent
* has occurred. This happens in response to the any change to a property
* of the model.
*/
protected void fireStateChanged()
{
ChangeListener[] ll = getChangeListeners();
for (int i = 0; i < ll.length; i++)
ll[i].stateChanged(changeEvent);
}
/**
* Helper method to fire a ChangeEvent with the model as the event's source.
*
* @param stateflag DOCUMENT ME!
* @param b DOCUMENT ME!
*/
private void changeState(int stateflag, boolean b)
{
int oldstate = stateMask;
int newstate;
boolean toggle = (this instanceof JToggleButton.ToggleButtonModel);
if (b)
newstate = oldstate | stateflag;
else
newstate = oldstate & ~ stateflag;
if (oldstate == newstate)
return;
if ((stateflag != SELECTED) && (stateflag != ENABLED)
&& (stateMask & ENABLED) == 0)
return;
stateMask = newstate;
fireStateChanged();
if ((oldstate & SELECTED) == 0 && (newstate & SELECTED) == SELECTED)
{
fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
null, ItemEvent.SELECTED));
// If the button is a toggle button then we fire action performed when
// the button changes state (selected/deselected), not when it changes
// from pressed to unpressed
if (toggle)
fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
actionCommand));
if (group != null)
group.setSelected(this, true);
}
else if ((oldstate & SELECTED) == SELECTED && (newstate & SELECTED) == 0)
{
fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
null, ItemEvent.DESELECTED));
// If the button is a toggle button then we fire action performed when
// the button changes state (selected/deselected), not when it changes
// from pressed to unpressed
if (toggle)
fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
actionCommand));
if (group != null)
group.setSelected(this, false);
}
else if (((oldstate & ARMED) == ARMED && (oldstate & PRESSED) == PRESSED)
&& ((newstate & ARMED) == ARMED && (newstate & PRESSED) == 0) && (!toggle))
fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
actionCommand));
}
/**
* Get the value of the model's "armed" property.
*
* @return The current "armed" property
*/
public boolean isArmed()
{
return (stateMask & ARMED) == ARMED;
}
/**
* Set the value of the model's "armed" property.
*
* @param a The new "armed" property
*/
public void setArmed(boolean a)
{
changeState(ARMED, a);
}
/**
* Get the value of the model's "enabled" property.
*
* @return The current "enabled" property.
*/
public boolean isEnabled()
{
return (stateMask & ENABLED) == ENABLED;
}
/**
* Set the value of the model's "enabled" property.
*
* @param e The new "enabled" property
*/
public void setEnabled(boolean e)
{
changeState(ENABLED, e);
}
/**
* Set the value of the model's "pressed" property.
*
* @param p The new "pressed" property
*/
public void setPressed(boolean p)
{
changeState(PRESSED, p);
}
/**
* Get the value of the model's "pressed" property.
*
* @return The current "pressed" property
*/
public boolean isPressed()
{
return (stateMask & PRESSED) == PRESSED;
}
/**
* Set the value of the model's "rollover" property.
*
* @param r The new "rollover" property
*/
public void setRollover(boolean r)
{
changeState(ROLLOVER, r);
}
/**
* Set the value of the model's "selected" property.
*
* @param s The new "selected" property
*/
public void setSelected(boolean s)
{
changeState(SELECTED, s);
}
/**
* Get the value of the model's "selected" property.
*
* @return The current "selected" property
*/
public boolean isSelected()
{
return (stateMask & SELECTED) == SELECTED;
}
/**
* Get the value of the model's "rollover" property.
*
* @return The current "rollover" property
*/
public boolean isRollover()
{
return (stateMask & ROLLOVER) == ROLLOVER;
}
/**
* Get the value of the model's "mnemonic" property.
*
* @return The current "mnemonic" property
*/
public int getMnemonic()
{
return mnemonic;
}
/**
* Set the value of the model's "mnemonic" property.
*
* @param key The new "mnemonic" property
*/
public void setMnemonic(int key)
{
if (mnemonic != key)
{
mnemonic = key;
fireStateChanged();
}
}
/**
* Set the value of the model's "actionCommand" property. This property is
* used as the "command" property of the {@link ActionEvent} fired from the
* model.
*
* @param s The new "actionCommand" property.
*/
public void setActionCommand(String s)
{
if (actionCommand != s)
{
actionCommand = s;
fireStateChanged();
}
}
/**
* Returns the current value of the model's "actionCommand" property.
*
* @return The current "actionCommand" property
*/
public String getActionCommand()
{
return actionCommand;
}
/**
* Set the value of the model's "group" property. The model is said to be a
* member of the {@link ButtonGroup} held in its "group" property, and only
* one model in a given group can have their "selected" property be
* <code>true</code> at a time.
*
* @param g The new "group" property
*/
public void setGroup(ButtonGroup g)
{
if (group != g)
{
group = g;
fireStateChanged();
}
}
/**
* Returns the current value of the model's "group" property.
*
* @return The value of the "group" property
*/
public ButtonGroup getGroup()
{
return group;
}
}
|