Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/DefaultButtonModel.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package javax.swing;2526import java.awt.*;27import java.awt.event.*;28import java.awt.image.*;29import java.io.Serializable;30import java.util.EventListener;31import javax.swing.event.*;3233/**34* The default implementation of a <code>Button</code> component's data model.35* <p>36* <strong>Warning:</strong>37* Serialized objects of this class will not be compatible with38* future Swing releases. The current serialization support is39* appropriate for short term storage or RMI between applications running40* the same version of Swing. As of 1.4, support for long term storage41* of all JavaBeans™42* has been added to the <code>java.beans</code> package.43* Please see {@link java.beans.XMLEncoder}.44*45* @author Jeff Dinkins46*/47public class DefaultButtonModel implements ButtonModel, Serializable {4849/** The bitmask used to store the state of the button. */50protected int stateMask = 0;5152/** The action command string fired by the button. */53protected String actionCommand = null;5455/** The button group that the button belongs to. */56protected ButtonGroup group = null;5758/** The button's mnemonic. */59protected int mnemonic = 0;6061/**62* Only one <code>ChangeEvent</code> is needed per button model63* instance since the event's only state is the source property.64* The source of events generated is always "this".65*/66protected transient ChangeEvent changeEvent = null;6768/** Stores the listeners on this model. */69protected EventListenerList listenerList = new EventListenerList();7071// controls the usage of the MenuItem.disabledAreNavigable UIDefaults72// property in the setArmed() method73private boolean menuItem = false;7475/**76* Constructs a <code>DefaultButtonModel</code>.77*78*/79public DefaultButtonModel() {80stateMask = 0;81setEnabled(true);82}8384/**85* Identifies the "armed" bit in the bitmask, which86* indicates partial commitment towards choosing/triggering87* the button.88*/89public final static int ARMED = 1 << 0;9091/**92* Identifies the "selected" bit in the bitmask, which93* indicates that the button has been selected. Only needed for94* certain types of buttons - such as radio button or check box.95*/96public final static int SELECTED = 1 << 1;9798/**99* Identifies the "pressed" bit in the bitmask, which100* indicates that the button is pressed.101*/102public final static int PRESSED = 1 << 2;103104/**105* Identifies the "enabled" bit in the bitmask, which106* indicates that the button can be selected by107* an input device (such as a mouse pointer).108*/109public final static int ENABLED = 1 << 3;110111/**112* Identifies the "rollover" bit in the bitmask, which113* indicates that the mouse is over the button.114*/115public final static int ROLLOVER = 1 << 4;116117/**118* {@inheritDoc}119*/120public void setActionCommand(String actionCommand) {121this.actionCommand = actionCommand;122}123124/**125* {@inheritDoc}126*/127public String getActionCommand() {128return actionCommand;129}130131/**132* {@inheritDoc}133*/134public boolean isArmed() {135return (stateMask & ARMED) != 0;136}137138/**139* {@inheritDoc}140*/141public boolean isSelected() {142return (stateMask & SELECTED) != 0;143}144145/**146* {@inheritDoc}147*/148public boolean isEnabled() {149return (stateMask & ENABLED) != 0;150}151152/**153* {@inheritDoc}154*/155public boolean isPressed() {156return (stateMask & PRESSED) != 0;157}158159/**160* {@inheritDoc}161*/162public boolean isRollover() {163return (stateMask & ROLLOVER) != 0;164}165166/**167* {@inheritDoc}168*/169public void setArmed(boolean b) {170if(isMenuItem() &&171UIManager.getBoolean("MenuItem.disabledAreNavigable")) {172if ((isArmed() == b)) {173return;174}175} else {176if ((isArmed() == b) || !isEnabled()) {177return;178}179}180181if (b) {182stateMask |= ARMED;183} else {184stateMask &= ~ARMED;185}186187fireStateChanged();188}189190/**191* {@inheritDoc}192*/193public void setEnabled(boolean b) {194if(isEnabled() == b) {195return;196}197198if (b) {199stateMask |= ENABLED;200} else {201stateMask &= ~ENABLED;202// unarm and unpress, just in case203stateMask &= ~ARMED;204stateMask &= ~PRESSED;205}206207208fireStateChanged();209}210211/**212* {@inheritDoc}213*/214public void setSelected(boolean b) {215if (this.isSelected() == b) {216return;217}218219if (b) {220stateMask |= SELECTED;221} else {222stateMask &= ~SELECTED;223}224225fireItemStateChanged(226new ItemEvent(this,227ItemEvent.ITEM_STATE_CHANGED,228this,229b ? ItemEvent.SELECTED : ItemEvent.DESELECTED));230231fireStateChanged();232233}234235236/**237* {@inheritDoc}238*/239public void setPressed(boolean b) {240if((isPressed() == b) || !isEnabled()) {241return;242}243244if (b) {245stateMask |= PRESSED;246} else {247stateMask &= ~PRESSED;248}249250if(!isPressed() && isArmed()) {251int modifiers = 0;252AWTEvent currentEvent = EventQueue.getCurrentEvent();253if (currentEvent instanceof InputEvent) {254modifiers = ((InputEvent)currentEvent).getModifiers();255} else if (currentEvent instanceof ActionEvent) {256modifiers = ((ActionEvent)currentEvent).getModifiers();257}258fireActionPerformed(259new ActionEvent(this, ActionEvent.ACTION_PERFORMED,260getActionCommand(),261EventQueue.getMostRecentEventTime(),262modifiers));263}264265fireStateChanged();266}267268/**269* {@inheritDoc}270*/271public void setRollover(boolean b) {272if((isRollover() == b) || !isEnabled()) {273return;274}275276if (b) {277stateMask |= ROLLOVER;278} else {279stateMask &= ~ROLLOVER;280}281282fireStateChanged();283}284285/**286* {@inheritDoc}287*/288public void setMnemonic(int key) {289mnemonic = key;290fireStateChanged();291}292293/**294* {@inheritDoc}295*/296public int getMnemonic() {297return mnemonic;298}299300/**301* {@inheritDoc}302*/303public void addChangeListener(ChangeListener l) {304listenerList.add(ChangeListener.class, l);305}306307/**308* {@inheritDoc}309*/310public void removeChangeListener(ChangeListener l) {311listenerList.remove(ChangeListener.class, l);312}313314/**315* Returns an array of all the change listeners316* registered on this <code>DefaultButtonModel</code>.317*318* @return all of this model's <code>ChangeListener</code>s319* or an empty320* array if no change listeners are currently registered321*322* @see #addChangeListener323* @see #removeChangeListener324*325* @since 1.4326*/327public ChangeListener[] getChangeListeners() {328return listenerList.getListeners(ChangeListener.class);329}330331/**332* Notifies all listeners that have registered interest for333* notification on this event type. The event instance334* is created lazily.335*336* @see EventListenerList337*/338protected void fireStateChanged() {339// Guaranteed to return a non-null array340Object[] listeners = listenerList.getListenerList();341// Process the listeners last to first, notifying342// those that are interested in this event343for (int i = listeners.length-2; i>=0; i-=2) {344if (listeners[i]==ChangeListener.class) {345// Lazily create the event:346if (changeEvent == null)347changeEvent = new ChangeEvent(this);348((ChangeListener)listeners[i+1]).stateChanged(changeEvent);349}350}351}352353/**354* {@inheritDoc}355*/356public void addActionListener(ActionListener l) {357listenerList.add(ActionListener.class, l);358}359360/**361* {@inheritDoc}362*/363public void removeActionListener(ActionListener l) {364listenerList.remove(ActionListener.class, l);365}366367/**368* Returns an array of all the action listeners369* registered on this <code>DefaultButtonModel</code>.370*371* @return all of this model's <code>ActionListener</code>s372* or an empty373* array if no action listeners are currently registered374*375* @see #addActionListener376* @see #removeActionListener377*378* @since 1.4379*/380public ActionListener[] getActionListeners() {381return listenerList.getListeners(ActionListener.class);382}383384/**385* Notifies all listeners that have registered interest for386* notification on this event type.387*388* @param e the <code>ActionEvent</code> to deliver to listeners389* @see EventListenerList390*/391protected void fireActionPerformed(ActionEvent e) {392// Guaranteed to return a non-null array393Object[] listeners = listenerList.getListenerList();394// Process the listeners last to first, notifying395// those that are interested in this event396for (int i = listeners.length-2; i>=0; i-=2) {397if (listeners[i]==ActionListener.class) {398// Lazily create the event:399// if (changeEvent == null)400// changeEvent = new ChangeEvent(this);401((ActionListener)listeners[i+1]).actionPerformed(e);402}403}404}405406/**407* {@inheritDoc}408*/409public void addItemListener(ItemListener l) {410listenerList.add(ItemListener.class, l);411}412413/**414* {@inheritDoc}415*/416public void removeItemListener(ItemListener l) {417listenerList.remove(ItemListener.class, l);418}419420/**421* Returns an array of all the item listeners422* registered on this <code>DefaultButtonModel</code>.423*424* @return all of this model's <code>ItemListener</code>s425* or an empty426* array if no item listeners are currently registered427*428* @see #addItemListener429* @see #removeItemListener430*431* @since 1.4432*/433public ItemListener[] getItemListeners() {434return listenerList.getListeners(ItemListener.class);435}436437/**438* Notifies all listeners that have registered interest for439* notification on this event type.440*441* @param e the <code>ItemEvent</code> to deliver to listeners442* @see EventListenerList443*/444protected void fireItemStateChanged(ItemEvent e) {445// Guaranteed to return a non-null array446Object[] listeners = listenerList.getListenerList();447// Process the listeners last to first, notifying448// those that are interested in this event449for (int i = listeners.length-2; i>=0; i-=2) {450if (listeners[i]==ItemListener.class) {451// Lazily create the event:452// if (changeEvent == null)453// changeEvent = new ChangeEvent(this);454((ItemListener)listeners[i+1]).itemStateChanged(e);455}456}457}458459/**460* Returns an array of all the objects currently registered as461* <code><em>Foo</em>Listener</code>s462* upon this model.463* <code><em>Foo</em>Listener</code>s464* are registered using the <code>add<em>Foo</em>Listener</code> method.465* <p>466* You can specify the <code>listenerType</code> argument467* with a class literal, such as <code><em>Foo</em>Listener.class</code>.468* For example, you can query a <code>DefaultButtonModel</code>469* instance <code>m</code>470* for its action listeners471* with the following code:472*473* <pre>ActionListener[] als = (ActionListener[])(m.getListeners(ActionListener.class));</pre>474*475* If no such listeners exist,476* this method returns an empty array.477*478* @param listenerType the type of listeners requested;479* this parameter should specify an interface480* that descends from <code>java.util.EventListener</code>481* @return an array of all objects registered as482* <code><em>Foo</em>Listener</code>s483* on this model,484* or an empty array if no such485* listeners have been added486* @exception ClassCastException if <code>listenerType</code> doesn't487* specify a class or interface that implements488* <code>java.util.EventListener</code>489*490* @see #getActionListeners491* @see #getChangeListeners492* @see #getItemListeners493*494* @since 1.3495*/496public <T extends EventListener> T[] getListeners(Class<T> listenerType) {497return listenerList.getListeners(listenerType);498}499500/** Overridden to return <code>null</code>. */501public Object[] getSelectedObjects() {502return null;503}504505/**506* {@inheritDoc}507*/508public void setGroup(ButtonGroup group) {509this.group = group;510}511512/**513* Returns the group that the button belongs to.514* Normally used with radio buttons, which are mutually515* exclusive within their group.516*517* @return the <code>ButtonGroup</code> that the button belongs to518*519* @since 1.3520*/521public ButtonGroup getGroup() {522return group;523}524525boolean isMenuItem() {526return menuItem;527}528529void setMenuItem(boolean menuItem) {530this.menuItem = menuItem;531}532}533534535