< prev index next >

src/java.desktop/share/classes/javax/swing/DefaultButtonModel.java

Print this page




  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.awt.image.*;
  30 import java.io.Serializable;
  31 import java.util.EventListener;
  32 import javax.swing.event.*;
  33 
  34 /**
  35  * The default implementation of a <code>Button</code> component's data model.
  36  * <p>
  37  * <strong>Warning:</strong>
  38  * Serialized objects of this class will not be compatible with
  39  * future Swing releases. The current serialization support is
  40  * appropriate for short term storage or RMI between applications running
  41  * the same version of Swing. As of 1.4, support for long term storage
  42  * of all JavaBeans&trade;
  43  * has been added to the <code>java.beans</code> package.
  44  * Please see {@link java.beans.XMLEncoder}.
  45  *
  46  * @author Jeff Dinkins
  47  * @since 1.2
  48  */
  49 @SuppressWarnings("serial") // Same-version serialization only
  50 public class DefaultButtonModel implements ButtonModel, Serializable {
  51 
  52     /** The bitmask used to store the state of the button. */
  53     protected int stateMask = 0;
  54 
  55     /** The action command string fired by the button. */
  56     protected String actionCommand = null;
  57 
  58     /** The button group that the button belongs to. */
  59     protected ButtonGroup group = null;
  60 
  61     /** The button's mnemonic. */
  62     protected int mnemonic = 0;
  63 
  64     /**
  65      * Only one <code>ChangeEvent</code> is needed per button model
  66      * instance since the event's only state is the source property.
  67      * The source of events generated is always "this".
  68      */
  69     protected transient ChangeEvent changeEvent = null;
  70 
  71     /** Stores the listeners on this model. */
  72     protected EventListenerList listenerList = new EventListenerList();
  73 
  74     // controls the usage of the MenuItem.disabledAreNavigable UIDefaults
  75     // property in the setArmed() method
  76     private boolean menuItem = false;
  77 
  78     /**
  79      * Constructs a <code>DefaultButtonModel</code>.
  80      *
  81      */
  82     public DefaultButtonModel() {
  83         stateMask = 0;
  84         setEnabled(true);
  85     }
  86 
  87     /**
  88      * Identifies the "armed" bit in the bitmask, which
  89      * indicates partial commitment towards choosing/triggering
  90      * the button.
  91      */
  92     public static final int ARMED = 1 << 0;
  93 
  94     /**
  95      * Identifies the "selected" bit in the bitmask, which
  96      * indicates that the button has been selected. Only needed for
  97      * certain types of buttons - such as radio button or check box.
  98      */
  99     public static final int SELECTED = 1 << 1;


 299     public int getMnemonic() {
 300         return mnemonic;
 301     }
 302 
 303     /**
 304      * {@inheritDoc}
 305      */
 306     public void addChangeListener(ChangeListener l) {
 307         listenerList.add(ChangeListener.class, l);
 308     }
 309 
 310     /**
 311      * {@inheritDoc}
 312      */
 313     public void removeChangeListener(ChangeListener l) {
 314         listenerList.remove(ChangeListener.class, l);
 315     }
 316 
 317     /**
 318      * Returns an array of all the change listeners
 319      * registered on this <code>DefaultButtonModel</code>.
 320      *
 321      * @return all of this model's <code>ChangeListener</code>s
 322      *         or an empty
 323      *         array if no change listeners are currently registered
 324      *
 325      * @see #addChangeListener
 326      * @see #removeChangeListener
 327      *
 328      * @since 1.4
 329      */
 330     public ChangeListener[] getChangeListeners() {
 331         return listenerList.getListeners(ChangeListener.class);
 332     }
 333 
 334     /**
 335      * Notifies all listeners that have registered interest for
 336      * notification on this event type.  The event instance
 337      * is created lazily.
 338      *
 339      * @see EventListenerList
 340      */
 341     protected void fireStateChanged() {


 352             }
 353         }
 354     }
 355 
 356     /**
 357      * {@inheritDoc}
 358      */
 359     public void addActionListener(ActionListener l) {
 360         listenerList.add(ActionListener.class, l);
 361     }
 362 
 363     /**
 364      * {@inheritDoc}
 365      */
 366     public void removeActionListener(ActionListener l) {
 367         listenerList.remove(ActionListener.class, l);
 368     }
 369 
 370     /**
 371      * Returns an array of all the action listeners
 372      * registered on this <code>DefaultButtonModel</code>.
 373      *
 374      * @return all of this model's <code>ActionListener</code>s
 375      *         or an empty
 376      *         array if no action listeners are currently registered
 377      *
 378      * @see #addActionListener
 379      * @see #removeActionListener
 380      *
 381      * @since 1.4
 382      */
 383     public ActionListener[] getActionListeners() {
 384         return listenerList.getListeners(ActionListener.class);
 385     }
 386 
 387     /**
 388      * Notifies all listeners that have registered interest for
 389      * notification on this event type.
 390      *
 391      * @param e the <code>ActionEvent</code> to deliver to listeners
 392      * @see EventListenerList
 393      */
 394     protected void fireActionPerformed(ActionEvent e) {
 395         // Guaranteed to return a non-null array
 396         Object[] listeners = listenerList.getListenerList();
 397         // Process the listeners last to first, notifying
 398         // those that are interested in this event
 399         for (int i = listeners.length-2; i>=0; i-=2) {
 400             if (listeners[i]==ActionListener.class) {
 401                 // Lazily create the event:
 402                 // if (changeEvent == null)
 403                 // changeEvent = new ChangeEvent(this);
 404                 ((ActionListener)listeners[i+1]).actionPerformed(e);
 405             }
 406         }
 407     }
 408 
 409     /**
 410      * {@inheritDoc}
 411      */
 412     public void addItemListener(ItemListener l) {
 413         listenerList.add(ItemListener.class, l);
 414     }
 415 
 416     /**
 417      * {@inheritDoc}
 418      */
 419     public void removeItemListener(ItemListener l) {
 420         listenerList.remove(ItemListener.class, l);
 421     }
 422 
 423     /**
 424      * Returns an array of all the item listeners
 425      * registered on this <code>DefaultButtonModel</code>.
 426      *
 427      * @return all of this model's <code>ItemListener</code>s
 428      *         or an empty
 429      *         array if no item listeners are currently registered
 430      *
 431      * @see #addItemListener
 432      * @see #removeItemListener
 433      *
 434      * @since 1.4
 435      */
 436     public ItemListener[] getItemListeners() {
 437         return listenerList.getListeners(ItemListener.class);
 438     }
 439 
 440     /**
 441      * Notifies all listeners that have registered interest for
 442      * notification on this event type.
 443      *
 444      * @param e the <code>ItemEvent</code> to deliver to listeners
 445      * @see EventListenerList
 446      */
 447     protected void fireItemStateChanged(ItemEvent e) {
 448         // Guaranteed to return a non-null array
 449         Object[] listeners = listenerList.getListenerList();
 450         // Process the listeners last to first, notifying
 451         // those that are interested in this event
 452         for (int i = listeners.length-2; i>=0; i-=2) {
 453             if (listeners[i]==ItemListener.class) {
 454                 // Lazily create the event:
 455                 // if (changeEvent == null)
 456                 // changeEvent = new ChangeEvent(this);
 457                 ((ItemListener)listeners[i+1]).itemStateChanged(e);
 458             }
 459         }
 460     }
 461 
 462     /**
 463      * Returns an array of all the objects currently registered as
 464      * <code><em>Foo</em>Listener</code>s
 465      * upon this model.
 466      * <code><em>Foo</em>Listener</code>s
 467      * are registered using the <code>add<em>Foo</em>Listener</code> method.
 468      * <p>
 469      * You can specify the <code>listenerType</code> argument
 470      * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
 471      * For example, you can query a <code>DefaultButtonModel</code>
 472      * instance <code>m</code>
 473      * for its action listeners
 474      * with the following code:
 475      *
 476      * <pre>ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));</pre>
 477      *
 478      * If no such listeners exist,
 479      * this method returns an empty array.
 480      *
 481      * @param <T> the type of requested listeners
 482      * @param listenerType  the type of listeners requested;
 483      *          this parameter should specify an interface
 484      *          that descends from <code>java.util.EventListener</code>
 485      * @return an array of all objects registered as
 486      *          <code><em>Foo</em>Listener</code>s
 487      *          on this model,
 488      *          or an empty array if no such
 489      *          listeners have been added
 490      * @exception ClassCastException if <code>listenerType</code> doesn't
 491      *          specify a class or interface that implements
 492      *          <code>java.util.EventListener</code>
 493      *
 494      * @see #getActionListeners
 495      * @see #getChangeListeners
 496      * @see #getItemListeners
 497      *
 498      * @since 1.3
 499      */
 500     public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
 501         return listenerList.getListeners(listenerType);
 502     }
 503 
 504     /** Overridden to return <code>null</code>. */
 505     public Object[] getSelectedObjects() {
 506         return null;
 507     }
 508 
 509     /**
 510      * {@inheritDoc}
 511      */
 512     public void setGroup(ButtonGroup group) {
 513         this.group = group;
 514     }
 515 
 516     /**
 517      * Returns the group that the button belongs to.
 518      * Normally used with radio buttons, which are mutually
 519      * exclusive within their group.
 520      *
 521      * @return the <code>ButtonGroup</code> that the button belongs to
 522      *
 523      * @since 1.3
 524      */
 525     public ButtonGroup getGroup() {
 526         return group;
 527     }
 528 
 529     boolean isMenuItem() {
 530         return menuItem;
 531     }
 532 
 533     void setMenuItem(boolean menuItem) {
 534         this.menuItem = menuItem;
 535     }
 536 }


  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.awt.image.*;
  30 import java.io.Serializable;
  31 import java.util.EventListener;
  32 import javax.swing.event.*;
  33 
  34 /**
  35  * The default implementation of a {@code Button} component's data model.
  36  * <p>
  37  * <strong>Warning:</strong>
  38  * Serialized objects of this class will not be compatible with
  39  * future Swing releases. The current serialization support is
  40  * appropriate for short term storage or RMI between applications running
  41  * the same version of Swing. As of 1.4, support for long term storage
  42  * of all JavaBeans&trade;
  43  * has been added to the {@code java.beans} package.
  44  * Please see {@link java.beans.XMLEncoder}.
  45  *
  46  * @author Jeff Dinkins
  47  * @since 1.2
  48  */
  49 @SuppressWarnings("serial") // Same-version serialization only
  50 public class DefaultButtonModel implements ButtonModel, Serializable {
  51 
  52     /** The bitmask used to store the state of the button. */
  53     protected int stateMask = 0;
  54 
  55     /** The action command string fired by the button. */
  56     protected String actionCommand = null;
  57 
  58     /** The button group that the button belongs to. */
  59     protected ButtonGroup group = null;
  60 
  61     /** The button's mnemonic. */
  62     protected int mnemonic = 0;
  63 
  64     /**
  65      * Only one {@code ChangeEvent} is needed per button model
  66      * instance since the event's only state is the source property.
  67      * The source of events generated is always "this".
  68      */
  69     protected transient ChangeEvent changeEvent = null;
  70 
  71     /** Stores the listeners on this model. */
  72     protected EventListenerList listenerList = new EventListenerList();
  73 
  74     // controls the usage of the MenuItem.disabledAreNavigable UIDefaults
  75     // property in the setArmed() method
  76     private boolean menuItem = false;
  77 
  78     /**
  79      * Constructs a {@code DefaultButtonModel}.
  80      *
  81      */
  82     public DefaultButtonModel() {
  83         stateMask = 0;
  84         setEnabled(true);
  85     }
  86 
  87     /**
  88      * Identifies the "armed" bit in the bitmask, which
  89      * indicates partial commitment towards choosing/triggering
  90      * the button.
  91      */
  92     public static final int ARMED = 1 << 0;
  93 
  94     /**
  95      * Identifies the "selected" bit in the bitmask, which
  96      * indicates that the button has been selected. Only needed for
  97      * certain types of buttons - such as radio button or check box.
  98      */
  99     public static final int SELECTED = 1 << 1;


 299     public int getMnemonic() {
 300         return mnemonic;
 301     }
 302 
 303     /**
 304      * {@inheritDoc}
 305      */
 306     public void addChangeListener(ChangeListener l) {
 307         listenerList.add(ChangeListener.class, l);
 308     }
 309 
 310     /**
 311      * {@inheritDoc}
 312      */
 313     public void removeChangeListener(ChangeListener l) {
 314         listenerList.remove(ChangeListener.class, l);
 315     }
 316 
 317     /**
 318      * Returns an array of all the change listeners
 319      * registered on this {@code DefaultButtonModel}.
 320      *
 321      * @return all of this model's {@code ChangeListener}s
 322      *         or an empty
 323      *         array if no change listeners are currently registered
 324      *
 325      * @see #addChangeListener
 326      * @see #removeChangeListener
 327      *
 328      * @since 1.4
 329      */
 330     public ChangeListener[] getChangeListeners() {
 331         return listenerList.getListeners(ChangeListener.class);
 332     }
 333 
 334     /**
 335      * Notifies all listeners that have registered interest for
 336      * notification on this event type.  The event instance
 337      * is created lazily.
 338      *
 339      * @see EventListenerList
 340      */
 341     protected void fireStateChanged() {


 352             }
 353         }
 354     }
 355 
 356     /**
 357      * {@inheritDoc}
 358      */
 359     public void addActionListener(ActionListener l) {
 360         listenerList.add(ActionListener.class, l);
 361     }
 362 
 363     /**
 364      * {@inheritDoc}
 365      */
 366     public void removeActionListener(ActionListener l) {
 367         listenerList.remove(ActionListener.class, l);
 368     }
 369 
 370     /**
 371      * Returns an array of all the action listeners
 372      * registered on this {@code DefaultButtonModel}.
 373      *
 374      * @return all of this model's {@code ActionListener}s
 375      *         or an empty
 376      *         array if no action listeners are currently registered
 377      *
 378      * @see #addActionListener
 379      * @see #removeActionListener
 380      *
 381      * @since 1.4
 382      */
 383     public ActionListener[] getActionListeners() {
 384         return listenerList.getListeners(ActionListener.class);
 385     }
 386 
 387     /**
 388      * Notifies all listeners that have registered interest for
 389      * notification on this event type.
 390      *
 391      * @param e the {@code ActionEvent} to deliver to listeners
 392      * @see EventListenerList
 393      */
 394     protected void fireActionPerformed(ActionEvent e) {
 395         // Guaranteed to return a non-null array
 396         Object[] listeners = listenerList.getListenerList();
 397         // Process the listeners last to first, notifying
 398         // those that are interested in this event
 399         for (int i = listeners.length-2; i>=0; i-=2) {
 400             if (listeners[i]==ActionListener.class) {
 401                 // Lazily create the event:
 402                 // if (changeEvent == null)
 403                 // changeEvent = new ChangeEvent(this);
 404                 ((ActionListener)listeners[i+1]).actionPerformed(e);
 405             }
 406         }
 407     }
 408 
 409     /**
 410      * {@inheritDoc}
 411      */
 412     public void addItemListener(ItemListener l) {
 413         listenerList.add(ItemListener.class, l);
 414     }
 415 
 416     /**
 417      * {@inheritDoc}
 418      */
 419     public void removeItemListener(ItemListener l) {
 420         listenerList.remove(ItemListener.class, l);
 421     }
 422 
 423     /**
 424      * Returns an array of all the item listeners
 425      * registered on this {@code DefaultButtonModel}.
 426      *
 427      * @return all of this model's {@code ItemListener}s
 428      *         or an empty
 429      *         array if no item listeners are currently registered
 430      *
 431      * @see #addItemListener
 432      * @see #removeItemListener
 433      *
 434      * @since 1.4
 435      */
 436     public ItemListener[] getItemListeners() {
 437         return listenerList.getListeners(ItemListener.class);
 438     }
 439 
 440     /**
 441      * Notifies all listeners that have registered interest for
 442      * notification on this event type.
 443      *
 444      * @param e the {@code ItemEvent} to deliver to listeners
 445      * @see EventListenerList
 446      */
 447     protected void fireItemStateChanged(ItemEvent e) {
 448         // Guaranteed to return a non-null array
 449         Object[] listeners = listenerList.getListenerList();
 450         // Process the listeners last to first, notifying
 451         // those that are interested in this event
 452         for (int i = listeners.length-2; i>=0; i-=2) {
 453             if (listeners[i]==ItemListener.class) {
 454                 // Lazily create the event:
 455                 // if (changeEvent == null)
 456                 // changeEvent = new ChangeEvent(this);
 457                 ((ItemListener)listeners[i+1]).itemStateChanged(e);
 458             }
 459         }
 460     }
 461 
 462     /**
 463      * Returns an array of all the objects currently registered as
 464      * <code><em>Foo</em>Listener</code>s
 465      * upon this model.
 466      * <code><em>Foo</em>Listener</code>s
 467      * are registered using the <code>add<em>Foo</em>Listener</code> method.
 468      * <p>
 469      * You can specify the {@code listenerType} argument
 470      * with a class literal, such as <code><em>Foo</em>Listener.class</code>.
 471      * For example, you can query a {@code DefaultButtonModel}
 472      * instance {@code m}
 473      * for its action listeners
 474      * with the following code:
 475      *
 476      * <pre>ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));</pre>
 477      *
 478      * If no such listeners exist,
 479      * this method returns an empty array.
 480      *
 481      * @param <T> the type of requested listeners
 482      * @param listenerType  the type of listeners requested;
 483      *          this parameter should specify an interface
 484      *          that descends from {@code java.util.EventListener}
 485      * @return an array of all objects registered as
 486      *          <code><em>Foo</em>Listener</code>s
 487      *          on this model,
 488      *          or an empty array if no such
 489      *          listeners have been added
 490      * @exception ClassCastException if {@code listenerType} doesn't
 491      *          specify a class or interface that implements
 492      *          {@code java.util.EventListener}
 493      *
 494      * @see #getActionListeners
 495      * @see #getChangeListeners
 496      * @see #getItemListeners
 497      *
 498      * @since 1.3
 499      */
 500     public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
 501         return listenerList.getListeners(listenerType);
 502     }
 503 
 504     /** Overridden to return {@code null}. */
 505     public Object[] getSelectedObjects() {
 506         return null;
 507     }
 508 
 509     /**
 510      * {@inheritDoc}
 511      */
 512     public void setGroup(ButtonGroup group) {
 513         this.group = group;
 514     }
 515 
 516     /**
 517      * Returns the group that the button belongs to.
 518      * Normally used with radio buttons, which are mutually
 519      * exclusive within their group.
 520      *
 521      * @return the {@code ButtonGroup} that the button belongs to
 522      *
 523      * @since 1.3
 524      */
 525     public ButtonGroup getGroup() {
 526         return group;
 527     }
 528 
 529     boolean isMenuItem() {
 530         return menuItem;
 531     }
 532 
 533     void setMenuItem(boolean menuItem) {
 534         this.menuItem = menuItem;
 535     }
 536 }
< prev index next >