📄 basiccombopopup.java
字号:
/* BasicComboPopup.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., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 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.Dimension;import java.awt.Point;import java.awt.Rectangle;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.MouseMotionAdapter;import java.awt.event.MouseMotionListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.swing.BorderFactory;import javax.swing.ComboBoxModel;import javax.swing.JComboBox;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JPopupMenu;import javax.swing.JScrollBar;import javax.swing.JScrollPane;import javax.swing.ListCellRenderer;import javax.swing.ListSelectionModel;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.Timer;import javax.swing.event.ListDataEvent;import javax.swing.event.ListDataListener;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.event.PopupMenuEvent;import javax.swing.event.PopupMenuListener;/** * UI Delegate for ComboPopup * * @author Olga Rodimina */public class BasicComboPopup extends JPopupMenu implements ComboPopup{ /* Timer for autoscrolling */ protected Timer autoscrollTimer; /** ComboBox associated with this popup */ protected JComboBox comboBox; /** FIXME: Need to document */ protected boolean hasEntered; /** * Indicates whether the scroll bar located in popup menu with comboBox's * list of items is currently autoscrolling. This happens when mouse event * originated in the combo box and is dragged outside of its bounds */ protected boolean isAutoScrolling; /** ItemListener listening to the selection changes in the combo box */ protected ItemListener itemListener; /** This listener is not used */ protected KeyListener keyListener; /** JList which is used to display item is the combo box */ protected JList list; /** This listener is not used */ protected ListDataListener listDataListener; /** * MouseListener listening to mouse events occuring in the combo box's * list. */ protected MouseListener listMouseListener; /** * MouseMotionListener listening to mouse motion events occuring in the * combo box's list */ protected MouseMotionListener listMouseMotionListener; /** This listener is not used */ protected ListSelectionListener listSelectionListener; /** MouseListener listening to mouse events occuring in the combo box */ protected MouseListener mouseListener; /** * MouseMotionListener listening to mouse motion events occuring in the * combo box */ protected MouseMotionListener mouseMotionListener; /** * PropertyChangeListener listening to changes occuring in the bound * properties of the combo box */ protected PropertyChangeListener propertyChangeListener; /** direction for scrolling down list of combo box's items */ protected static final int SCROLL_DOWN = 1; /** direction for scrolling up list of combo box's items */ protected static final int SCROLL_UP = 0; /** Indicates auto scrolling direction */ protected int scrollDirection; /** JScrollPane that contains list portion of the combo box */ protected JScrollPane scroller; /** This field is not used */ protected boolean valueIsAdjusting; /** * Creates a new BasicComboPopup object. * * @param comboBox the combo box with which this popup should be associated */ public BasicComboPopup(JComboBox comboBox) { this.comboBox = comboBox; installComboBoxListeners(); configurePopup(); setLightWeightPopupEnabled(comboBox.isLightWeightPopupEnabled()); } /** * This method displays drow down list of combo box items on the screen. */ public void show() { Rectangle cbBounds = comboBox.getBounds(); // popup should have same width as the comboBox and should be hight anough // to display number of rows equal to 'maximumRowCount' property int popupHeight = getPopupHeightForRowCount(comboBox.getMaximumRowCount()); scroller.setPreferredSize(new Dimension(cbBounds.width, popupHeight)); pack(); // Highlight selected item in the combo box's drop down list if (comboBox.getSelectedIndex() != -1) list.setSelectedIndex(comboBox.getSelectedIndex()); //scroll scrollbar s.t. selected item is visible JScrollBar scrollbar = scroller.getVerticalScrollBar(); int selectedIndex = comboBox.getSelectedIndex(); if (selectedIndex > comboBox.getMaximumRowCount()) scrollbar.setValue(getPopupHeightForRowCount(selectedIndex)); // location specified is relative to comboBox super.show(comboBox, 0, cbBounds.height); } /** * This method hides drop down list of items */ public void hide() { super.setVisible(false); } /** * Return list cointaining JComboBox's items * * @return list cointaining JComboBox's items */ public JList getList() { return list; } /** * Returns MouseListener that is listening to mouse events occuring in the * combo box. * * @return MouseListener */ public MouseListener getMouseListener() { return mouseListener; } /** * Returns MouseMotionListener that is listening to mouse motion events * occuring in the combo box. * * @return MouseMotionListener */ public MouseMotionListener getMouseMotionListener() { return mouseMotionListener; } /** * Returns KeyListener listening to key events occuring in the combo box. * This method returns null because KeyHandler is not longer used. * * @return KeyListener */ public KeyListener getKeyListener() { return keyListener; } /** * This method uninstalls the UI for the given JComponent. */ public void uninstallingUI() { uninstallComboBoxModelListeners(comboBox.getModel()); uninstallListeners(); uninstallKeyboardActions(); } /** * This method uninstalls listeners that were listening to changes occuring * in the comb box's data model * * @param model data model for the combo box from which to uninstall * listeners */ protected void uninstallComboBoxModelListeners(ComboBoxModel model) { model.removeListDataListener(listDataListener); } /** * This method uninstalls keyboard actions installed by the UI. */ protected void uninstallKeyboardActions() { // FIXME: Need to implement } /** * This method fires PopupMenuEvent indicating that combo box's popup list * of items will become visible */ protected void firePopupMenuWillBecomeVisible() { PopupMenuListener[] ll = comboBox.getPopupMenuListeners(); for (int i = 0; i < ll.length; i++) ll[i].popupMenuWillBecomeVisible(new PopupMenuEvent(comboBox)); } /** * This method fires PopupMenuEvent indicating that combo box's popup list * of items will become invisible. */ protected void firePopupMenuWillBecomeInvisible() { PopupMenuListener[] ll = comboBox.getPopupMenuListeners(); for (int i = 0; i < ll.length; i++) ll[i].popupMenuWillBecomeInvisible(new PopupMenuEvent(comboBox)); } /** * This method fires PopupMenuEvent indicating that combo box's popup list * of items was closed without selection. */ protected void firePopupMenuCanceled() { PopupMenuListener[] ll = comboBox.getPopupMenuListeners(); for (int i = 0; i < ll.length; i++) ll[i].popupMenuCanceled(new PopupMenuEvent(comboBox)); } /** * Creates MouseListener to listen to mouse events occuring in the combo * box. Note that this listener doesn't listen to mouse events occuring in * the popup portion of the combo box, it only listens to main combo box * part. * * @return new MouseMotionListener that listens to mouse events occuring in * the combo box */ protected MouseListener createMouseListener() { return new InvocationMouseHandler(); } /** * Create Mouse listener that listens to mouse dragging events occuring in * the combo box. This listener is responsible for changing the selection * in the combo box list to the component over which mouse is being * currently dragged * * @return new MouseMotionListener that listens to mouse dragging events * occuring in the combo box */ protected MouseMotionListener createMouseMotionListener() { return new InvocationMouseMotionHandler(); } /** * KeyListener created in this method is not used anymore. * * @return KeyListener that does nothing */ protected KeyListener createKeyListener() { return new InvocationKeyHandler(); } /** * ListSelectionListener created in this method is not used anymore * * @return ListSelectionListener that does nothing */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -