Path: blob/master/src/java.desktop/share/classes/javax/swing/ButtonModel.java
41153 views
/*1* Copyright (c) 1997, 2006, 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;252627import java.awt.event.*;28import java.awt.*;29import javax.swing.event.*;3031/**32* State model for buttons.33* <p>34* This model is used for regular buttons, as well as check boxes35* and radio buttons, which are special kinds of buttons. In practice,36* a button's UI takes the responsibility of calling methods on its37* model to manage the state, as detailed below:38* <p>39* In simple terms, pressing and releasing the mouse over a regular40* button triggers the button and causes and <code>ActionEvent</code>41* to be fired. The same behavior can be produced via a keyboard key42* defined by the look and feel of the button (typically the SPACE BAR).43* Pressing and releasing this key while the button has44* focus will give the same results. For check boxes and radio buttons, the45* mouse or keyboard equivalent sequence just described causes the button46* to become selected.47* <p>48* In details, the state model for buttons works as follows49* when used with the mouse:50* <br>51* Pressing the mouse on top of a button makes the model both52* armed and pressed. As long as the mouse remains down,53* the model remains pressed, even if the mouse moves54* outside the button. On the contrary, the model is only55* armed while the mouse remains pressed within the bounds of56* the button (it can move in or out of the button, but the model57* is only armed during the portion of time spent within the button).58* A button is triggered, and an <code>ActionEvent</code> is fired,59* when the mouse is released while the model is armed60* - meaning when it is released over top of the button after the mouse61* has previously been pressed on that button (and not already released).62* Upon mouse release, the model becomes unarmed and unpressed.63* <p>64* In details, the state model for buttons works as follows65* when used with the keyboard:66* <br>67* Pressing the look and feel defined keyboard key while the button68* has focus makes the model both armed and pressed. As long as this key69* remains down, the model remains in this state. Releasing the key sets70* the model to unarmed and unpressed, triggers the button, and causes an71* <code>ActionEvent</code> to be fired.72*73* @author Jeff Dinkins74* @since 1.275*/76public interface ButtonModel extends ItemSelectable {7778/**79* Indicates partial commitment towards triggering the80* button.81*82* @return <code>true</code> if the button is armed,83* and ready to be triggered84* @see #setArmed85*/86boolean isArmed();8788/**89* Indicates if the button has been selected. Only needed for90* certain types of buttons - such as radio buttons and check boxes.91*92* @return <code>true</code> if the button is selected93*/94boolean isSelected();9596/**97* Indicates if the button can be selected or triggered by98* an input device, such as a mouse pointer.99*100* @return <code>true</code> if the button is enabled101*/102boolean isEnabled();103104/**105* Indicates if the button is pressed.106*107* @return <code>true</code> if the button is pressed108*/109boolean isPressed();110111/**112* Indicates that the mouse is over the button.113*114* @return <code>true</code> if the mouse is over the button115*/116boolean isRollover();117118/**119* Marks the button as armed or unarmed.120*121* @param b whether or not the button should be armed122*/123public void setArmed(boolean b);124125/**126* Selects or deselects the button.127*128* @param b <code>true</code> selects the button,129* <code>false</code> deselects the button130*/131public void setSelected(boolean b);132133/**134* Enables or disables the button.135*136* @param b whether or not the button should be enabled137* @see #isEnabled138*/139public void setEnabled(boolean b);140141/**142* Sets the button to pressed or unpressed.143*144* @param b whether or not the button should be pressed145* @see #isPressed146*/147public void setPressed(boolean b);148149/**150* Sets or clears the button's rollover state151*152* @param b whether or not the button is in the rollover state153* @see #isRollover154*/155public void setRollover(boolean b);156157/**158* Sets the keyboard mnemonic (shortcut key or159* accelerator key) for the button.160*161* @param key an int specifying the accelerator key162*/163public void setMnemonic(int key);164165/**166* Gets the keyboard mnemonic for the button.167*168* @return an int specifying the accelerator key169* @see #setMnemonic170*/171public int getMnemonic();172173/**174* Sets the action command string that gets sent as part of the175* <code>ActionEvent</code> when the button is triggered.176*177* @param s the <code>String</code> that identifies the generated event178* @see #getActionCommand179* @see java.awt.event.ActionEvent#getActionCommand180*/181public void setActionCommand(String s);182183/**184* Returns the action command string for the button.185*186* @return the <code>String</code> that identifies the generated event187* @see #setActionCommand188*/189public String getActionCommand();190191/**192* Identifies the group the button belongs to --193* needed for radio buttons, which are mutually194* exclusive within their group.195*196* @param group the <code>ButtonGroup</code> the button belongs to197*/198public void setGroup(ButtonGroup group);199200/**201* Returns the group that the button belongs to.202* Normally used with radio buttons, which are mutually203* exclusive within their group.204*205* @implSpec The default implementation of this method returns {@code null}.206* Subclasses should return the group set by setGroup().207*208* @return the <code>ButtonGroup</code> that the button belongs to209* @since 10210*/211default ButtonGroup getGroup() {212return null;213}214215/**216* Adds an <code>ActionListener</code> to the model.217*218* @param l the listener to add219*/220void addActionListener(ActionListener l);221222/**223* Removes an <code>ActionListener</code> from the model.224*225* @param l the listener to remove226*/227void removeActionListener(ActionListener l);228229/**230* Adds an <code>ItemListener</code> to the model.231*232* @param l the listener to add233*/234void addItemListener(ItemListener l);235236/**237* Removes an <code>ItemListener</code> from the model.238*239* @param l the listener to remove240*/241void removeItemListener(ItemListener l);242243/**244* Adds a <code>ChangeListener</code> to the model.245*246* @param l the listener to add247*/248void addChangeListener(ChangeListener l);249250/**251* Removes a <code>ChangeListener</code> from the model.252*253* @param l the listener to remove254*/255void removeChangeListener(ChangeListener l);256257}258259260