📄 basicmenuitemui.java
字号:
/* BasicMenuItemUI.java -- Copyright (C) 2002, 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.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Insets;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.util.ArrayList;import javax.swing.AbstractAction;import javax.swing.ActionMap;import javax.swing.ButtonModel;import javax.swing.Icon;import javax.swing.InputMap;import javax.swing.JCheckBoxMenuItem;import javax.swing.JComponent;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;import javax.swing.KeyStroke;import javax.swing.LookAndFeel;import javax.swing.MenuElement;import javax.swing.MenuSelectionManager;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.UIDefaults;import javax.swing.UIManager;import javax.swing.event.MenuDragMouseEvent;import javax.swing.event.MenuDragMouseListener;import javax.swing.event.MenuKeyEvent;import javax.swing.event.MenuKeyListener;import javax.swing.event.MouseInputListener;import javax.swing.plaf.ActionMapUIResource;import javax.swing.plaf.ComponentInputMapUIResource;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.MenuItemUI;/** * UI Delegate for JMenuItem. */public class BasicMenuItemUI extends MenuItemUI{ /** * Font to be used when displaying menu item's accelerator. */ protected Font acceleratorFont; /** * Color to be used when displaying menu item's accelerator. */ protected Color acceleratorForeground; /** * Color to be used when displaying menu item's accelerator when menu item is * selected. */ protected Color acceleratorSelectionForeground; /** * Icon that is displayed after the text to indicated that this menu contains * submenu. */ protected Icon arrowIcon; /** * Icon that is displayed before the text. This icon is only used in * JCheckBoxMenuItem or JRadioBoxMenuItem. */ protected Icon checkIcon; /** * Number of spaces between icon and text. */ protected int defaultTextIconGap = 4; /** * Color of the text when menu item is disabled */ protected Color disabledForeground; /** * The menu Drag mouse listener listening to the menu item. */ protected MenuDragMouseListener menuDragMouseListener; /** * The menu item itself */ protected JMenuItem menuItem; /** * Menu Key listener listening to the menu item. */ protected MenuKeyListener menuKeyListener; /** * mouse input listener listening to menu item. */ protected MouseInputListener mouseInputListener; /** * Indicates if border should be painted */ protected boolean oldBorderPainted; /** * Color of text that is used when menu item is selected */ protected Color selectionBackground; /** * Color of the text that is used when menu item is selected. */ protected Color selectionForeground; /** * String that separates description of the modifiers and the key */ private String acceleratorDelimiter; /** * ItemListener to listen for item changes in the menu item */ private ItemListener itemListener; /** * Number of spaces between accelerator and menu item's label. */ private int defaultAcceleratorLabelGap = 10; /** * The gap between different menus on the MenuBar. */ private int MenuGap = 10; /** A PropertyChangeListener to make UI updates after property changes **/ PropertyChangeHandler propertyChangeListener; /** * A class to handle PropertChangeEvents for the JMenuItem * @author Anthony Balkissoon abalkiss at redhat dot com. */ class PropertyChangeHandler implements PropertyChangeListener { /** * This method is called when a property of the menuItem is changed. * Currently it is only used to update the accelerator key bindings. * * @param e * the PropertyChangeEvent */ public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName() == "accelerator") { InputMap map = SwingUtilities.getUIInputMap(menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW); if (map != null) map.remove((KeyStroke)e.getOldValue()); else map = new ComponentInputMapUIResource(menuItem); map.put((KeyStroke)e.getNewValue(), "doClick"); } } } /** * A class to handle accelerator keys. This is the Action we will * perform when the accelerator key for this JMenuItem is pressed. * @author Anthony Balkissoon abalkiss at redhat dot com * */ class ClickAction extends AbstractAction { /** * This is what is done when the accelerator key for the JMenuItem is * pressed. */ public void actionPerformed(ActionEvent event) { doClick(MenuSelectionManager.defaultManager()); } } /** * Creates a new BasicMenuItemUI object. */ public BasicMenuItemUI() { mouseInputListener = createMouseInputListener(menuItem); menuDragMouseListener = createMenuDragMouseListener(menuItem); menuKeyListener = createMenuKeyListener(menuItem); itemListener = new ItemHandler(); propertyChangeListener = new PropertyChangeHandler(); } /** * Create MenuDragMouseListener to listen for mouse dragged events. * * @param c * menu item to listen to * @return The MenuDragMouseListener */ protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) { return new MenuDragMouseHandler(); } /** * Creates MenuKeyListener to listen to key events occuring when menu item is * visible on the screen. * * @param c * menu item to listen to * @return The MenuKeyListener */ protected MenuKeyListener createMenuKeyListener(JComponent c) { return new MenuKeyHandler(); } /** * Handles mouse input events occuring for this menu item * * @param c * menu item to listen to * @return The MouseInputListener */ protected MouseInputListener createMouseInputListener(JComponent c) { return new MouseInputHandler(); } /** * Factory method to create a BasicMenuItemUI for the given {@link * JComponent}, which should be a {@link JMenuItem}. * * @param c * The {@link JComponent} a UI is being created for. * @return A BasicMenuItemUI for the {@link JComponent}. */ public static ComponentUI createUI(JComponent c) { return new BasicMenuItemUI(); } /** * Programatically clicks menu item. * * @param msm * MenuSelectionManager for the menu hierarchy */ protected void doClick(MenuSelectionManager msm) { menuItem.doClick(); msm.clearSelectedPath(); } /** * Returns maximum size for the specified menu item * * @param c * component for which to get maximum size * @return Maximum size for the specified menu item. */ public Dimension getMaximumSize(JComponent c) { return null; } /** * Returns minimum size for the specified menu item * * @param c * component for which to get minimum size * @return Minimum size for the specified menu item. */ public Dimension getMinimumSize(JComponent c) { return null; } /** * Returns path to this menu item. * * @return $MenuElement[]$ Returns array of menu elements that constitute a * path to this menu item. */ public MenuElement[] getPath() { ArrayList path = new ArrayList(); // Path to menu should also include its popup menu. if (menuItem instanceof JMenu) path.add(((JMenu) menuItem).getPopupMenu()); Component c = menuItem; while (c instanceof MenuElement) { path.add(0, (MenuElement) c); if (c instanceof JPopupMenu) c = ((JPopupMenu) c).getInvoker(); else c = c.getParent(); } MenuElement[] pathArray = new MenuElement[path.size()]; path.toArray(pathArray); return pathArray; } /** * Returns preferred size for the given menu item. * * @param c * menu item for which to get preferred size * @param checkIcon * check icon displayed in the given menu item * @param arrowIcon * arrow icon displayed in the given menu item * @param defaultTextIconGap * space between icon and text in the given menuItem * @return $Dimension$ preferred size for the given menu item */ protected Dimension getPreferredMenuItemSize(JComponent c, Icon checkIcon, Icon arrowIcon, int defaultTextIconGap) { JMenuItem m = (JMenuItem) c; Dimension d = BasicGraphicsUtils.getPreferredButtonSize(m, defaultTextIconGap);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -