KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > DefaultButtonModel


1 /*
2  * @(#)DefaultButtonModel.java 1.45 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.awt.image.*;
12 import java.io.Serializable JavaDoc;
13 import java.util.EventListener JavaDoc;
14 import javax.swing.event.*;
15
16 /**
17  * The default implementation of a <code>Button</code> component's data model.
18  * <p>
19  * <strong>Warning:</strong>
20  * Serialized objects of this class will not be compatible with
21  * future Swing releases. The current serialization support is
22  * appropriate for short term storage or RMI between applications running
23  * the same version of Swing. As of 1.4, support for long term storage
24  * of all JavaBeans<sup><font size="-2">TM</font></sup>
25  * has been added to the <code>java.beans</code> package.
26  * Please see {@link java.beans.XMLEncoder}.
27  *
28  * @version 1.45 05/05/04
29  * @author Jeff Dinkins
30  */

31 public class DefaultButtonModel implements ButtonModel JavaDoc, Serializable JavaDoc {
32
33     protected int stateMask = 0;
34     protected String JavaDoc actionCommand = null;
35     protected ButtonGroup JavaDoc group = null;
36         
37     protected int mnemonic = 0;
38
39     /**
40      * Only one <code>ChangeEvent</code> is needed per button model
41      * instance since the
42      * event's only state is the source property. The source of events
43      * generated is always "this".
44      */

45     protected transient ChangeEvent changeEvent = null;
46     protected EventListenerList listenerList = new EventListenerList();
47     
48         
49     /**
50      * Constructs a default <code>JButtonModel</code>.
51      *
52      */

53     public DefaultButtonModel() {
54         stateMask = 0;
55         setEnabled(true);
56     }
57         
58     /**
59      * Indicates partial commitment towards choosing the
60      * button.
61      */

62     public final static int ARMED = 1 << 0;
63         
64     /**
65      * Indicates that the button has been selected. Only needed for
66      * certain types of buttons - such as RadioButton or Checkbox.
67      */

68     public final static int SELECTED = 1 << 1;
69         
70     /**
71      * Indicates that the button has been "pressed"
72      * (typically, when the mouse is released).
73      */

74     public final static int PRESSED = 1 << 2;
75         
76     /**
77      * Indicates that the button can be selected by
78      * an input device (such as a mouse pointer).
79      */

80     public final static int ENABLED = 1 << 3;
81
82     /**
83      * Indicates that the mouse is over the button.
84      */

85     public final static int ROLLOVER = 1 << 4;
86         
87     /**
88      * Sets the <code>actionCommand</code> string that gets sent as
89      * part of the event when the button is pressed.
90      *
91      * @param actionCommand the <code>String</code> that identifies
92      * the generated event
93      */

94     public void setActionCommand(String JavaDoc actionCommand) {
95         this.actionCommand = actionCommand;
96     }
97         
98     /**
99      * Returns the action command for this button.
100      *
101      * @return the <code>String</code> that identifies the generated event
102      * @see #setActionCommand
103      */

104     public String JavaDoc getActionCommand() {
105         return actionCommand;
106     }
107
108     /**
109      * Indicates partial commitment towards pressing the
110      * button.
111      *
112      * @return true if the button is armed, and ready to be pressed
113      * @see #setArmed
114      */

115     public boolean isArmed() {
116         return (stateMask & ARMED) != 0;
117     }
118         
119     /**
120      * Indicates if the button has been selected. Only needed for
121      * certain types of buttons - such as RadioButton or Checkbox.
122      *
123      * @return true if the button is selected
124      */

125     public boolean isSelected() {
126         return (stateMask & SELECTED) != 0;
127     }
128         
129     /**
130      * Indicates whether the button can be selected or pressed by
131      * an input device (such as a mouse pointer). (Checkbox-buttons
132      * are selected, regular buttons are "pressed".)
133      *
134      * @return true if the button is enabled, and therefore
135      * selectable (or pressable)
136      */

137     public boolean isEnabled() {
138         return (stateMask & ENABLED) != 0;
139     }
140         
141     /**
142      * Indicates whether button has been pressed.
143      *
144      * @return true if the button has been pressed
145      */

146     public boolean isPressed() {
147         return (stateMask & PRESSED) != 0;
148     }
149         
150     /**
151      * Indicates that the mouse is over the button.
152      *
153      * @return true if the mouse is over the button
154      */

155     public boolean isRollover() {
156         return (stateMask & ROLLOVER) != 0;
157     }
158         
159     /**
160      * Marks the button as "armed". If the mouse button is
161      * released while it is over this item, the button's action event
162      * fires. If the mouse button is released elsewhere, the
163      * event does not fire and the button is disarmed.
164      *
165      * @param b true to arm the button so it can be selected
166      */

167     public void setArmed(boolean b) {
168         if((isArmed() == b) || !isEnabled()) {
169             return;
170         }
171             
172         if (b) {
173             stateMask |= ARMED;
174         } else {
175             stateMask &= ~ARMED;
176         }
177             
178         fireStateChanged();
179     }
180
181     /**
182      * Enables or disables the button.
183      *
184      * @param b true to enable the button
185      * @see #isEnabled
186      */

187     public void setEnabled(boolean b) {
188         if(isEnabled() == b) {
189             return;
190         }
191             
192         if (b) {
193             stateMask |= ENABLED;
194         } else {
195             stateMask &= ~ENABLED;
196         // unarm and unpress, just in case
197
stateMask &= ~ARMED;
198             stateMask &= ~PRESSED;
199         }
200
201             
202         fireStateChanged();
203     }
204         
205     /**
206      * Selects or deselects the button.
207      *
208      * @param b true selects the button,
209      * false deselects the button
210      */

211     public void setSelected(boolean b) {
212         if (this.isSelected() == b) {
213             return;
214         }
215
216         if (b) {
217             stateMask |= SELECTED;
218         } else {
219             stateMask &= ~SELECTED;
220         }
221
222         fireItemStateChanged(
223                 new ItemEvent(this,
224                               ItemEvent.ITEM_STATE_CHANGED,
225                               this,
226                               b ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
227         
228         fireStateChanged();
229         
230     }
231         
232         
233     /**
234      * Sets the button to pressed or unpressed.
235      *
236      * @param b true to set the button to "pressed"
237      * @see #isPressed
238      */

239     public void setPressed(boolean b) {
240         if((isPressed() == b) || !isEnabled()) {
241             return;
242         }
243         
244         if (b) {
245             stateMask |= PRESSED;
246         } else {
247             stateMask &= ~PRESSED;
248         }
249
250         if(!isPressed() && isArmed()) {
251             int modifiers = 0;
252             AWTEvent currentEvent = EventQueue.getCurrentEvent();
253             if (currentEvent instanceof InputEvent) {
254                 modifiers = ((InputEvent)currentEvent).getModifiers();
255             } else if (currentEvent instanceof ActionEvent) {
256                 modifiers = ((ActionEvent)currentEvent).getModifiers();
257             }
258             fireActionPerformed(
259                 new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
260                                 getActionCommand(),
261                                 EventQueue.getMostRecentEventTime(),
262                                 modifiers));
263         }
264             
265         fireStateChanged();
266     }
267
268     /**
269      * Sets or clears the button's rollover state
270      *
271      * @param b true to turn on rollover
272      * @see #isRollover
273      */

274     public void setRollover(boolean b) {
275         if((isRollover() == b) || !isEnabled()) {
276             return;
277         }
278         
279         if (b) {
280             stateMask |= ROLLOVER;
281         } else {
282             stateMask &= ~ROLLOVER;
283         }
284
285         fireStateChanged();
286     }
287
288     /**
289      * Sets the keyboard mnemonic (shortcut key or
290      * accelerator key) for this button.
291      *
292      * @param key an int specifying the accelerator key
293      */

294     public void setMnemonic(int key) {
295     mnemonic = key;
296     fireStateChanged();
297     }
298
299     /**
300      * Gets the keyboard mnemonic for this model
301      *
302      * @return an int specifying the accelerator key
303      * @see #setMnemonic
304      */

305     public int getMnemonic() {
306     return mnemonic;
307     }
308
309     /**
310      * Adds a <code>ChangeListener</code> to the button.
311      *
312      * @param l the listener to add
313      */

314     public void addChangeListener(ChangeListener l) {
315         listenerList.add(ChangeListener.class, l);
316     }
317     
318     /**
319      * Removes a <code>ChangeListener</code> from the button.
320      *
321      * @param l the listener to remove
322      */

323     public void removeChangeListener(ChangeListener l) {
324         listenerList.remove(ChangeListener.class, l);
325     }
326
327     /**
328      * Returns an array of all the change listeners
329      * registered on this <code>DefaultButtonModel</code>.
330      *
331      * @return all of this model's <code>ChangeListener</code>s
332      * or an empty
333      * array if no change listeners are currently registered
334      *
335      * @see #addChangeListener
336      * @see #removeChangeListener
337      *
338      * @since 1.4
339      */

340     public ChangeListener[] getChangeListeners() {
341         return (ChangeListener[])listenerList.getListeners(
342                 ChangeListener.class);
343     }
344
345     /**
346      * Notifies all listeners that have registered interest for
347      * notification on this event type. The event instance
348      * is created lazily.
349      *
350      * @see EventListenerList
351      */

352     protected void fireStateChanged() {
353         // Guaranteed to return a non-null array
354
Object JavaDoc[] listeners = listenerList.getListenerList();
355         // Process the listeners last to first, notifying
356
// those that are interested in this event
357
for (int i = listeners.length-2; i>=0; i-=2) {
358             if (listeners[i]==ChangeListener.class) {
359                 // Lazily create the event:
360
if (changeEvent == null)
361                     changeEvent = new ChangeEvent(this);
362                 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
363             }
364         }
365     }
366     
367     /**
368      * Adds an <code>ActionListener</code> to the button.
369      *
370      * @param l the listener to add
371      */

372     public void addActionListener(ActionListener l) {
373         listenerList.add(ActionListener.class, l);
374     }
375         
376     /**
377      * Removes an <code>ActionListener</code> from the button.
378      *
379      * @param l the listener to remove
380      */

381     public void removeActionListener(ActionListener l) {
382         listenerList.remove(ActionListener.class, l);
383     }
384
385     /**
386      * Returns an array of all the action listeners
387      * registered on this <code>DefaultButtonModel</code>.
388      *
389      * @return all of this model's <code>ActionListener</code>s
390      * or an empty
391      * array if no action listeners are currently registered
392      *
393      * @see #addActionListener
394      * @see #removeActionListener
395      *
396      * @since 1.4
397      */

398     public ActionListener[] getActionListeners() {
399         return (ActionListener[])listenerList.getListeners(
400                 ActionListener.class);
401     }
402
403     /**
404      * Notifies all listeners that have registered interest for
405      * notification on this event type.
406      *
407      * @param e the <code>ActionEvent</code> to deliver to listeners
408      * @see EventListenerList
409      */

410     protected void fireActionPerformed(ActionEvent e) {
411         // Guaranteed to return a non-null array
412
Object JavaDoc[] listeners = listenerList.getListenerList();
413         // Process the listeners last to first, notifying
414
// those that are interested in this event
415
for (int i = listeners.length-2; i>=0; i-=2) {
416             if (listeners[i]==ActionListener.class) {
417                 // Lazily create the event:
418
// if (changeEvent == null)
419
// changeEvent = new ChangeEvent(this);
420
((ActionListener)listeners[i+1]).actionPerformed(e);
421             }
422         }
423     }
424
425     /**
426      * Adds an <code>ItemListener</code> to the button.
427      *
428      * @param l the listener to add
429      */

430     public void addItemListener(ItemListener l) {
431         listenerList.add(ItemListener.class, l);
432     }
433         
434     /**
435      * Removes an <code>ItemListener</code> from the button.
436      *
437      * @param l the listener to remove
438      */

439     public void removeItemListener(ItemListener l) {
440         listenerList.remove(ItemListener.class, l);
441     }
442
443     /**
444      * Returns an array of all the item listeners
445      * registered on this <code>DefaultButtonModel</code>.
446      *
447      * @return all of this model's <code>ItemListener</code>s
448      * or an empty
449      * array if no item listeners are currently registered
450      *
451      * @see #addItemListener
452      * @see #removeItemListener
453      *
454      * @since 1.4
455      */

456     public ItemListener[] getItemListeners() {
457         return (ItemListener[])listenerList.getListeners(ItemListener.class);
458     }
459
460     /**
461      * Notifies all listeners that have registered interest for
462      * notification on this event type.
463      *
464      * @param e the <code>ItemEvent</code> to deliver to listeners
465      * @see EventListenerList
466      */

467     protected void fireItemStateChanged(ItemEvent e) {
468         // Guaranteed to return a non-null array
469
Object JavaDoc[] listeners = listenerList.getListenerList();
470         // Process the listeners last to first, notifying
471
// those that are interested in this event
472
for (int i = listeners.length-2; i>=0; i-=2) {
473             if (listeners[i]==ItemListener.class) {
474                 // Lazily create the event:
475
// if (changeEvent == null)
476
// changeEvent = new ChangeEvent(this);
477
((ItemListener)listeners[i+1]).itemStateChanged(e);
478             }
479         }
480     }
481
482     /**
483      * Returns an array of all the objects currently registered as
484      * <code><em>Foo</em>Listener</code>s
485      * upon this model.
486      * <code><em>Foo</em>Listener</code>s
487      * are registered using the <code>add<em>Foo</em>Listener</code> method.
488      * <p>
489      * You can specify the <code>listenerType</code> argument
490      * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
491      * For example, you can query a <code>DefaultButtonModel</code>
492      * instance <code>m</code>
493      * for its action listeners
494      * with the following code:
495      *
496      * <pre>ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));</pre>
497      *
498      * If no such listeners exist,
499      * this method returns an empty array.
500      *
501      * @param listenerType the type of listeners requested;
502      * this parameter should specify an interface
503      * that descends from <code>java.util.EventListener</code>
504      * @return an array of all objects registered as
505      * <code><em>Foo</em>Listener</code>s
506      * on this model,
507      * or an empty array if no such
508      * listeners have been added
509      * @exception ClassCastException if <code>listenerType</code> doesn't
510      * specify a class or interface that implements
511      * <code>java.util.EventListener</code>
512      *
513      * @see #getActionListeners
514      * @see #getChangeListeners
515      * @see #getItemListeners
516      *
517      * @since 1.3
518      */

519     public <T extends EventListener JavaDoc> T[] getListeners(Class JavaDoc<T> listenerType) {
520     return listenerList.getListeners(listenerType);
521     }
522
523     /** Overridden to return <code>null</code>. */
524     public Object JavaDoc[] getSelectedObjects() {
525         return null;
526     }
527
528     /**
529      * Identifies the group this button belongs to --
530      * needed for radio buttons, which are mutually
531      * exclusive within their group.
532      *
533      * @param group the <code>ButtonGroup</code> this button belongs to
534      */

535     public void setGroup(ButtonGroup JavaDoc group) {
536         this.group = group;
537     }
538
539     /**
540      * Returns the group that this button belongs to.
541      * Normally used with radio buttons, which are mutually
542      * exclusive within their group.
543      *
544      * @return a <code>ButtonGroup</code> that this button belongs to
545      *
546      * @since 1.3
547      */

548     public ButtonGroup JavaDoc getGroup() {
549         return group;
550     }
551
552 }
553
Popular Tags