abstractbutton.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,946 行 · 第 1/4 页

JAVA
1,946
字号
/* AbstractButton.java -- Provides basic button functionality.   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 modifyit under the terms of the GNU General Public License as published bythe 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, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package javax.swing;import java.awt.Graphics;import java.awt.Image;import java.awt.Insets;import java.awt.ItemSelectable;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.accessibility.AccessibleAction;import javax.accessibility.AccessibleIcon;import javax.accessibility.AccessibleRelationSet;import javax.accessibility.AccessibleStateSet;import javax.accessibility.AccessibleText;import javax.accessibility.AccessibleValue;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.plaf.ButtonUI;import javax.swing.text.AttributeSet;/** * <p>The purpose of this class is to serve as a facade over a number of * classes which collectively represent the semantics of a button: the * button's model, its listeners, its action, and its look and feel. Some * parts of a button's state are stored explicitly in this class, other * parts are delegates to the model. Some methods related to buttons are * implemented in this class, other methods pass through to the current  * model or look and feel.</p> * * <p>Furthermore this class is supposed to serve as a base class for * several kinds of buttons with similar but non-identical semantics: * toggle buttons (radio buttons and checkboxes), simple "push" buttons, * menu items.</p> * * <p>Buttons have many properties, some of which are stored in this class * while others are delegated to the button's model. The following properties * are available:</p> * * <table> * <tr><th>Property               </th><th>Stored in</th><th>Bound?</th></tr> * * <tr><td>action                 </td><td>button</td> <td>no</td></tr> * <tr><td>actionCommand          </td><td>model</td>  <td>no</td></tr> * <tr><td>borderPainted          </td><td>button</td> <td>yes</td></tr> * <tr><td>contentAreaFilled      </td><td>button</td> <td>yes</td></tr> * <tr><td>disabledIcon           </td><td>button</td> <td>yes</td></tr> * <tr><td>disabledSelectedIcon   </td><td>button</td> <td>yes</td></tr> * <tr><td>displayedMnemonicIndex </td><td>button</td> <td>no</td></tr> * <tr><td>enabled                </td><td>model</td>  <td>no</td></tr> * <tr><td>focusPainted           </td><td>button</td> <td>yes</td></tr> * <tr><td>horizontalAlignment    </td><td>button</td> <td>yes</td></tr> * <tr><td>horizontalTextPosition </td><td>button</td> <td>yes</td></tr> * <tr><td>icon                   </td><td>button</td> <td>yes</td></tr> * <tr><td>iconTextGap            </td><td>button</td> <td>no</td></tr> * <tr><td>label (same as text)   </td><td>model</td>  <td>yes</td></tr> * <tr><td>margin                 </td><td>button</td> <td>yes</td></tr> * <tr><td>multiClickThreshold    </td><td>button</td> <td>no</td></tr> * <tr><td>pressedIcon            </td><td>button</td> <td>yes</td></tr> * <tr><td>rolloverEnabled        </td><td>button</td> <td>yes</td></tr> * <tr><td>rolloverIcon           </td><td>button</td> <td>yes</td></tr> * <tr><td>rolloverSelectedIcon   </td><td>button</td> <td>yes</td></tr> * <tr><td>selected               </td><td>model</td>  <td>no</td></tr> * <tr><td>selectedIcon           </td><td>button</td> <td>yes</td></tr> * <tr><td>selectedObjects        </td><td>button</td> <td>no</td></tr> * <tr><td>text                   </td><td>model</td>  <td>yes</td></tr> * <tr><td>UI                     </td><td>button</td> <td>yes</td></tr> * <tr><td>verticalAlignment      </td><td>button</td> <td>yes</td></tr> * <tr><td>verticalTextPosition   </td><td>button</td> <td>yes</td></tr> * * </table> * * <p>The various behavioral aspects of these properties follows:</p> * * <ul>  * * <li>When non-bound properties stored in the button change, the button * fires ChangeEvents to its ChangeListeners.</li> *  * <li>When bound properties stored in the button change, the button fires * PropertyChangeEvents to its PropertyChangeListeners</li> * * <li>If any of the model's properties change, it fires a ChangeEvent to * its ChangeListeners, which include the button.</li> * * <li>If the button receives a ChangeEvent from its model, it will * propagate the ChangeEvent to its ChangeListeners, with the ChangeEvent's * "source" property set to refer to the button, rather than the model. The * the button will request a repaint, to paint its updated state.</li> * * <li>If the model's "selected" property changes, the model will fire an * ItemEvent to its ItemListeners, which include the button, in addition to * the ChangeEvent which models the property change. The button propagates * ItemEvents directly to its ItemListeners.</li> * * <li>If the model's armed and pressed properties are simultaneously * <code>true</code>, the model will fire an ActionEvent to its * ActionListeners, which include the button. The button will propagate * this ActionEvent to its ActionListeners, with the ActionEvent's "source" * property set to refer to the button, rather than the model.</li> * * </ul> * * @author Ronald Veldema (rveldema@cs.vu.nl) * @author Graydon Hoare (graydon@redhat.com) */public abstract class AbstractButton extends JComponent  implements ItemSelectable, SwingConstants{  private static final long serialVersionUID = -937921345538462020L;    /** The icon displayed by default. */  Icon default_icon;  /** The icon displayed when the button is pressed. */  Icon pressed_icon;  /** The icon displayed when the button is disabled. */  Icon disabeldIcon;  /** The icon displayed when the button is selected. */  Icon selectedIcon;  /** The icon displayed when the button is selected but disabled. */  Icon disabledSelectedIcon;  /** The icon displayed when the button is rolled over. */  Icon rolloverIcon;  /** The icon displayed when the button is selected and rolled over. */  Icon rolloverSelectedIcon;  /** The icon currently displayed. */  Icon current_icon;  /** The text displayed in the button. */  String text;  /** The gap between icon and text, if both icon and text are non-<code>null</code>. */  int iconTextGap;  /** The vertical alignment of the button's text and icon. */  int verticalAlignment;  /** The horizontal alignment of the button's text and icon. */  int horizontalAlignment;  /** The horizontal position of the button's text relative to its icon. */  int horizontalTextPosition;  /** The vertical position of the button's text relative to its icon. */  int verticalTextPosition;  /** Whether or not the button paints its border. */  boolean borderPainted;  /** Whether or not the button paints its focus state. */  boolean focusPainted;  /** Whether or not the button fills its content area. */  boolean contentAreaFilled;    /** Whether rollover is enabled. */  boolean rollOverEnabled;  /** The action taken when the button is clicked. */  Action action;  /** The button's current state. */  protected ButtonModel model;  /** The margin between the button's border and its label. */  Insets margin;  /** A hint to the look and feel class, suggesting which character in the   * button's label should be underlined when drawing the label. */  int mnemonicIndex;  /** Listener the button uses to receive ActionEvents from its model.  */  protected ActionListener actionListener;  /** Listener the button uses to receive ItemEvents from its model.  */  protected ItemListener itemListener;  /** Listener the button uses to receive ChangeEvents from its model.  */    protected ChangeListener changeListener;  /** The time in miliseconds in which clicks get coalesced into a single   * <code>ActionEvent</code>. */  long multiClickThreshhold;    /** Listener the button uses to receive PropertyChangeEvents from its      Action. */  PropertyChangeListener actionPropertyChangeListener;    /** ChangeEvent that is fired to button's ChangeEventListeners  */    protected ChangeEvent changeEvent = new ChangeEvent(this);    /** Fired in a PropertyChangeEvent when the "borderPainted" property changes. */  public static final String BORDER_PAINTED_CHANGED_PROPERTY = "borderPainted";    /** Fired in a PropertyChangeEvent when the "contentAreaFilled" property changes. */  public static final String CONTENT_AREA_FILLED_CHANGED_PROPERTY = "contentAreaFilled";    /** Fired in a PropertyChangeEvent when the "disabledIcon" property changes. */  public static final String DISABLED_ICON_CHANGED_PROPERTY = "disabledIcon";    /** Fired in a PropertyChangeEvent when the "disabledSelectedIcon" property changes. */  public static final String DISABLED_SELECTED_ICON_CHANGED_PROPERTY = "disabledSelectedIcon";    /** Fired in a PropertyChangeEvent when the "focusPainted" property changes. */  public static final String FOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted";  /** Fired in a PropertyChangeEvent when the "horizontalAlignment" property changes. */  public static final String HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY = "horizontalAlignment";  /** Fired in a PropertyChangeEvent when the "horizontalTextPosition" property changes. */  public static final String HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY = "horizontalTextPosition";  /** Fired in a PropertyChangeEvent when the "icon" property changes. */  public static final String ICON_CHANGED_PROPERTY = "icon";  /** Fired in a PropertyChangeEvent when the "margin" property changes. */  public static final String MARGIN_CHANGED_PROPERTY = "margin";  /** Fired in a PropertyChangeEvent when the "mnemonic" property changes. */  public static final String MNEMONIC_CHANGED_PROPERTY = "mnemonic";  /** Fired in a PropertyChangeEvent when the "model" property changes. */  public static final String MODEL_CHANGED_PROPERTY = "model";  /** Fired in a PropertyChangeEvent when the "pressedIcon" property changes. */  public static final String PRESSED_ICON_CHANGED_PROPERTY = "pressedIcon";  /** Fired in a PropertyChangeEvent when the "rolloverEnabled" property changes. */  public static final String ROLLOVER_ENABLED_CHANGED_PROPERTY = "rolloverEnabled";  /** Fired in a PropertyChangeEvent when the "rolloverIcon" property changes. */  public static final String ROLLOVER_ICON_CHANGED_PROPERTY = "rolloverIcon";    /** Fired in a PropertyChangeEvent when the "rolloverSelectedIcon" property changes. */  public static final String ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY = "rolloverSelectedIcon";    /** Fired in a PropertyChangeEvent when the "selectedIcon" property changes. */  public static final String SELECTED_ICON_CHANGED_PROPERTY = "selectedIcon";  /** Fired in a PropertyChangeEvent when the "text" property changes. */  public static final String TEXT_CHANGED_PROPERTY = "text";  /** Fired in a PropertyChangeEvent when the "verticalAlignment" property changes. */  public static final String VERTICAL_ALIGNMENT_CHANGED_PROPERTY = "verticalAlignment";  /** Fired in a PropertyChangeEvent when the "verticalTextPosition" property changes. */  public static final String VERTICAL_TEXT_POSITION_CHANGED_PROPERTY = "verticalTextPosition";  /**   * A Java Accessibility extension of the AbstractButton.   */  protected abstract class AccessibleAbstractButton    extends AccessibleJComponent implements AccessibleAction, AccessibleValue,                                            AccessibleText  {    private static final long serialVersionUID = -5673062525319836790L;        protected AccessibleAbstractButton()    {    }    public AccessibleStateSet getAccessibleStateSet()    {      return null; // TODO    }    public String getAccessibleName()    {      return null; // TODO    }    public AccessibleIcon[] getAccessibleIcon()    {      return null; // TODO    }    public AccessibleRelationSet getAccessibleRelationSet()    {      return null; // TODO    }    public AccessibleAction getAccessibleAction()    {      return null; // TODO    }    public AccessibleValue getAccessibleValue()    {      return null; // TODO    }    public int getAccessibleActionCount()    {      return 0; // TODO    }    public String getAccessibleActionDescription(int value0)    {      return null; // TODO    }    public boolean doAccessibleAction(int value0)    {      return false; // TODO    }    public Number getCurrentAccessibleValue()    {      return null; // TODO    }    public boolean setCurrentAccessibleValue(Number value0)    {      return false; // TODO    }    public Number getMinimumAccessibleValue()    {      return null; // TODO    }    public Number getMaximumAccessibleValue()    {      return null; // TODO    }    public AccessibleText getAccessibleText()    {      return null; // TODO    }    public int getIndexAtPoint(Point value0)    {      return 0; // TODO    }    public Rectangle getCharacterBounds(int value0)    {      return null; // TODO    }    public int getCharCount()    {      return 0; // TODO    }    public int getCaretPosition()    {      return 0; // TODO    }    public String getAtIndex(int value0, int value1)    {      return null; // TODO    }    public String getAfterIndex(int value0, int value1)    {      return null; // TODO    }    public String getBeforeIndex(int value0, int value1)    {      return null; // TODO    }    public AttributeSet getCharacterAttribute(int value0)    {      return null; // TODO    }    public int getSelectionStart()    {      return 0; // TODO    }    public int getSelectionEnd()    {      return 0; // TODO    }    public String getSelectedText()    {      return null; // TODO    }    private Rectangle getTextRectangle()    {      return null; // TODO    }  }  /**   * Creates a new AbstractButton object.   */  public AbstractButton()  {    this("",null);  }  /**   * Creates a new AbstractButton object.   *   * @param txt Value to use for the button's "text" property   * @param icon Value to use for the button's "defaultIcon" property   */  AbstractButton(String txt, Icon icon)  {    init (txt, icon);    updateUI();  }  /**   * Get the model the button is currently using.   *   * @return The current model   */  public ButtonModel getModel()  {    return model;  }  /**   * Set the model the button is currently using. This un-registers all    * listeners associated with the current model, and re-registers them   * with the new model.   *   * @param newModel The new model   */  public void setModel(ButtonModel newModel)  {    if (newModel == model)      return;    if (model != null)      {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?