basicmenuitemui.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 999 行 · 第 1/2 页
JAVA
999 行
/* BasicMenuItemUI.java -- 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.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.KeyEvent;import java.awt.event.MouseEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.util.ArrayList;import javax.swing.Icon;import javax.swing.JComponent;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JPopupMenu;import javax.swing.KeyStroke;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.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; /** * PropertyChangeListener to listen for property changes in the menu item */ private PropertyChangeListener propertyChangeListener; /** * Number of spaces between accelerator and menu item's label. */ private int defaultAcceleratorLabelGap = 4; /** * Creates a new BasicMenuItemUI object. */ public BasicMenuItemUI() { mouseInputListener = createMouseInputListener(menuItem); menuDragMouseListener = createMenuDragMouseListener(menuItem); menuKeyListener = createMenuKeyListener(menuItem); 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 chech 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); // if menu item has accelerator then take accelerator's size into account // when calculating preferred size. KeyStroke accelerator = m.getAccelerator(); Rectangle rect; if (accelerator != null) { rect = getAcceleratorRect(accelerator, m.getToolkit().getFontMetrics(acceleratorFont)); // add width of accelerator's text d.width = d.width + rect.width + defaultAcceleratorLabelGap; // adjust the heigth of the preferred size if necessary if (d.height < rect.height) d.height = rect.height; } if (checkIcon != null) { d.width = d.width + checkIcon.getIconWidth() + defaultTextIconGap; if (checkIcon.getIconHeight() > d.height) d.height = checkIcon.getIconHeight(); } if (arrowIcon != null && (c instanceof JMenu)) { d.width = d.width + arrowIcon.getIconWidth() + defaultTextIconGap; if (arrowIcon.getIconHeight() > d.height) d.height = arrowIcon.getIconHeight(); } return d; } /** * Returns preferred size of the given component * * @param c component for which to return preferred size * * @return $Dimension$ preferred size for the given component */ public Dimension getPreferredSize(JComponent c) { return getPreferredMenuItemSize(c, checkIcon, arrowIcon, defaultTextIconGap); } protected String getPropertyPrefix() { return null; } /** * This method installs the components for this {@link JMenuItem}. * * @param menuItem The {@link JMenuItem} to install components for. */ protected void installComponents(JMenuItem menuItem) { // FIXME: Need to implement } /** * This method installs the defaults that are defined in the Basic look and * feel for this {@link JMenuItem}. */ protected void installDefaults() { UIDefaults defaults = UIManager.getLookAndFeelDefaults(); menuItem.setBackground(defaults.getColor("MenuItem.background")); menuItem.setBorder(defaults.getBorder("MenuItem.border")); menuItem.setFont(defaults.getFont("MenuItem.font")); menuItem.setForeground(defaults.getColor("MenuItem.foreground")); menuItem.setMargin(defaults.getInsets("MenuItem.margin")); menuItem.setOpaque(true); acceleratorFont = defaults.getFont("MenuItem.acceleratorFont"); acceleratorForeground = defaults.getColor("MenuItem.acceleratorForeground"); acceleratorSelectionForeground = defaults.getColor("MenuItem.acceleratorSelectionForeground"); selectionBackground = defaults.getColor("MenuItem.selectionBackground"); selectionForeground = defaults.getColor("MenuItem.selectionForeground"); acceleratorDelimiter = defaults.getString("MenuItem.acceleratorDelimiter"); menuItem.setHorizontalTextPosition(SwingConstants.TRAILING); menuItem.setHorizontalAlignment(SwingConstants.LEADING); } /** * This method installs the keyboard actions for this {@link JMenuItem}. */ protected void installKeyboardActions() { // FIXME: Need to implement } /** * This method installs the listeners for the {@link JMenuItem}. */ protected void installListeners() { menuItem.addMouseListener(mouseInputListener); menuItem.addMouseMotionListener(mouseInputListener); menuItem.addMenuDragMouseListener(menuDragMouseListener); menuItem.addMenuKeyListener(menuKeyListener); menuItem.addPropertyChangeListener(propertyChangeListener); } /** * Installs and initializes all fields for this UI delegate. Any properties * of the UI that need to be initialized and/or set to defaults will be * done now. It will also install any listeners necessary. * * @param c The {@link JComponent} that is having this UI installed. */ public void installUI(JComponent c) { super.installUI(c); menuItem = (JMenuItem) c; installDefaults(); installComponents(menuItem); installListeners(); } /** * Paints given menu item using specified graphics context * * @param g The graphics context used to paint this menu item * @param c Menu Item to paint */ public void paint(Graphics g, JComponent c) { paintMenuItem(g, c, checkIcon, arrowIcon, c.getBackground(), c.getForeground(), defaultTextIconGap); } /** * Paints background of the menu item * * @param g The graphics context used to paint this menu item * @param menuItem menu item to paint * @param bgColor Background color to use when painting menu item */ protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) { Dimension size = getPreferredSize(menuItem); Color foreground = g.getColor(); g.setColor(bgColor); g.drawRect(0, 0, size.width, size.height); g.setColor(foreground); } /** * Paints specified menu item * * @param g The graphics context used to paint this menu item * @param c menu item to paint * @param checkIcon check icon to use when painting menu item * @param arrowIcon arrow icon to use when painting menu item * @param background Background color of the menu item * @param foreground Foreground color of the menu item * @param defaultTextIconGap space to use between icon and * text when painting menu item */ protected void paintMenuItem(Graphics g, JComponent c, Icon checkIcon, Icon arrowIcon, Color background, Color foreground, int defaultTextIconGap) { JMenuItem m = (JMenuItem) c; Rectangle tr = new Rectangle(); // text rectangle Rectangle ir = new Rectangle(); // icon rectangle Rectangle vr = new Rectangle(); // view rectangle Rectangle br = new Rectangle(); // border rectangle Rectangle ar = new Rectangle(); // accelerator rectangle Rectangle cr = new Rectangle(); // checkIcon rectangle int vertAlign = m.getVerticalAlignment(); int horAlign = m.getHorizontalAlignment(); int vertTextPos = m.getVerticalTextPosition(); int horTextPos = m.getHorizontalTextPosition(); Font f = m.getFont(); g.setFont(f); FontMetrics fm = g.getFontMetrics(f); SwingUtilities.calculateInnerArea(m, br); SwingUtilities.calculateInsetArea(br, m.getInsets(), vr); paintBackground(g, m, m.getBackground());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?