basiccomboboxui.java

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

JAVA
1,239
字号
/* BasicComboBoxUI.java --   Copyright (C) 2004, 2005  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.plaf.basic;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Insets;import java.awt.LayoutManager;import java.awt.Rectangle;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.accessibility.Accessible;import javax.swing.CellRendererPane;import javax.swing.ComboBoxEditor;import javax.swing.ComboBoxModel;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JComponent;import javax.swing.JList;import javax.swing.ListCellRenderer;import javax.swing.UIDefaults;import javax.swing.UIManager;import javax.swing.event.ListDataEvent;import javax.swing.event.ListDataListener;import javax.swing.plaf.ComboBoxUI;import javax.swing.plaf.ComponentUI;/** * UI Delegate for JComboBox * * @author Olga Rodimina * @author Robert Schuster */public class BasicComboBoxUI extends ComboBoxUI{  /**   * This arrow button that is displayed in the rigth side of JComboBox. This   * button is used to hide and show combo box's list of items   */  protected JButton arrowButton;  /**   * The combo box for which this UI delegate is for   */  protected JComboBox comboBox;  /**   * Component that is responsible for displaying/editting  selected item of   * the combo box. By default JTextField is used as an editor for the   * JComboBox   */  protected Component editor;  /**   * Listener listening to focus events occuring in the JComboBox   */  protected FocusListener focusListener;  /**   * tells whether JComboBox currently has focus   */  protected boolean hasFocus;  /**   * Listener listening to item events fired by the JComboBox   */  protected ItemListener itemListener;  /**   * KeyListener listening to key events that occur while JComboBox has focus   */  protected KeyListener keyListener;  /**   * MouseListener listening to mouse events occuring in the combo box   */  private MouseListener mouseListener;  /**   * List used when rendering selected item of the combo box. The selection   * and foreground colors for combo box renderer  are configured from this   * list   */  protected JList listBox;  /**   * ListDataListener listening to JComboBox model   */  protected ListDataListener listDataListener;  /**   * Popup list containing combo box's menu items   */  protected ComboPopup popup;  protected KeyListener popupKeyListener;  protected MouseListener popupMouseListener;  protected MouseMotionListener popupMouseMotionListener;  /**   * Listener listening to changes in the bound properties of JComboBox   */  protected PropertyChangeListener propertyChangeListener;  /**   * Colors that are used to render selected item in the combo box.   */  private Color shadow;  private Color darkShadow;  private Color highlight;  private Color lightHighlight;  /* Size of the largest item in the comboBox */  private Dimension largestItemSize;  // It seems that JComboBox doesn't have a border set explicitely. So we just  // paint the border everytime combo box is displayed.   /* border insets for this JComboBox*/  private static final Insets borderInsets = new Insets(2, 2, 2, 2);  // Width of the arrow button    private static int arrowButtonWidth = 15;  // FIXME: This fields aren't used anywhere at this moment.  protected Dimension cachedMinimumSize;  protected CellRendererPane currentValuePane;  protected boolean isMinimumSizeDirty;  /**   * Creates a new BasicComboBoxUI object.   */  public BasicComboBoxUI()  {  }  /**   * Factory method to create a BasicComboBoxUI for the given {@link   * JComponent}, which should be a {@link JComboBox}.   *   * @param c The {@link JComponent} a UI is being created for.   *   * @return A BasicComboBoxUI for the {@link JComponent}.   */  public static ComponentUI createUI(JComponent c)  {    return new BasicComboBoxUI();  }  /**   * This method installs the UI for the given JComponent.   *   * @param c The JComponent to install a UI for.   */  public void installUI(JComponent c)  {    super.installUI(c);    if (c instanceof JComboBox)      {	comboBox = (JComboBox) c;	comboBox.setOpaque(true);	comboBox.setLayout(createLayoutManager());	installDefaults();	installComponents();	installListeners();	installKeyboardActions();      }  }  /**   * This method uninstalls the UI.   *   * @param c The JComponent that is having this UI removed.   */  public void uninstallUI(JComponent c)  {    uninstallKeyboardActions();    uninstallListeners();    uninstallComponents();    uninstallDefaults();    comboBox = null;  }  /**   * This method installs the defaults that are defined in  the Basic look and   * feel for this {@link JComboBox}.   */  protected void installDefaults()  {    UIDefaults defaults = UIManager.getLookAndFeelDefaults();    comboBox.setBackground(defaults.getColor("ComboBox.background"));    comboBox.setFont(defaults.getFont("ComboBox.font"));    comboBox.setForeground(defaults.getColor("ComboBox.foreground"));    // Set default color that should be used to to render selected item    // of the combo box.    shadow = defaults.getColor("Button.shadow");    darkShadow = defaults.getColor("Button.darkShadow");    lightHighlight = defaults.getColor("Button.light");    highlight = defaults.getColor("Button.highlight");  }  /**   * This method creates and installs the listeners for this UI.   */  protected void installListeners()  {    // install combo box's listeners    propertyChangeListener = createPropertyChangeListener();    comboBox.addPropertyChangeListener(propertyChangeListener);    focusListener = createFocusListener();    comboBox.addFocusListener(focusListener);    itemListener = createItemListener();    comboBox.addItemListener(itemListener);    keyListener = createKeyListener();    comboBox.addKeyListener(keyListener);    mouseListener = createMouseListener();    comboBox.addMouseListener(mouseListener);    // install listeners that listen to combo box model    listDataListener = createListDataListener();    comboBox.getModel().addListDataListener(listDataListener);    configureArrowButton();  }  /**   * This method uninstalls the defaults and sets any objects created during   * install to null   */  protected void uninstallDefaults()  {    UIDefaults defaults = UIManager.getLookAndFeelDefaults();    comboBox.setBackground(null);    comboBox.setFont(null);    comboBox.setForeground(null);    shadow = null;    darkShadow = null;    lightHighlight = null;    highlight = null;  }  /**   * Detaches all the listeners we attached in {@link #installListeners}.   */  protected void uninstallListeners()  {    comboBox.removePropertyChangeListener(propertyChangeListener);    propertyChangeListener = null;    comboBox.removeFocusListener(focusListener);    focusListener = null;    comboBox.removeItemListener(itemListener);    itemListener = null;    comboBox.removeKeyListener(keyListener);    keyListener = null;    comboBox.removeMouseListener(mouseListener);    mouseListener = null;    comboBox.getModel().removeListDataListener(listDataListener);    listDataListener = null;    unconfigureArrowButton();  }  /**   * This method creates popup that will contain list of combo box's items   *   * @return popup containing list of combo box's items   */  protected ComboPopup createPopup()  {    return new BasicComboPopup(comboBox);  }  /**   * Creates KeyListener to listen to key events.   *   * @return KeyListener that listens to key events.   */  protected KeyListener createKeyListener()  {    return new KeyHandler();  }  /**   * This method create MouseListener that will listen to mouse event occuring   * in combo box.   *   * @return the MouseListener   */  private MouseListener createMouseListener()  {    return new MouseHandler();  }  /**   * This method create FocusListener that will listen to changes in this   * JComboBox's focus.   *   * @return theFocusListener   */  protected FocusListener createFocusListener()  {    return new FocusHandler();  }  /**   * This method create ListDataListener to listen to ComboBox's  data model   *   * @return ListDataListener   */  protected ListDataListener createListDataListener()  {    return new ListDataHandler();  }  /**   * This method creates ItemListener that will listen to to the changes in   * the JComboBox's selection.   *   * @return the ItemListener   */  protected ItemListener createItemListener()  {    return new ItemHandler();  }  /**   * This method creates PropertyChangeListener to listen to  the changes in   * the JComboBox's bound properties.   *   * @return the PropertyChangeListener   */  protected PropertyChangeListener createPropertyChangeListener()  {    return new PropertyChangeHandler();  }  /**   * This method returns layout manager for the combo box.   *   * @return layout manager for the combo box   */  protected LayoutManager createLayoutManager()  {    return new ComboBoxLayoutManager();  }  /**   * This method creates component that will be responsible for rendering the   * selected component in the combo box.

⌨️ 快捷键说明

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