📄 basicmenuitemui.java
字号:
/* * @(#)BasicMenuItemUI.java 1.142 06/12/15 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing.plaf.basic;import sun.swing.MenuItemCheckIconFactory;import sun.swing.SwingUtilities2;import static sun.swing.SwingUtilities2.BASICMENUITEMUI_MAX_TEXT_OFFSET;import java.awt.*;import java.awt.event.*;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.swing.*;import javax.swing.event.*;import javax.swing.border.*;import javax.swing.plaf.*;import javax.swing.text.View;import sun.swing.UIAction;/** * BasicMenuItem implementation * * @version 1.142 12/15/06 * @author Georges Saab * @author David Karlton * @author Arnaud Weber * @author Fredrik Lagerblad */public class BasicMenuItemUI extends MenuItemUI{ protected JMenuItem menuItem = null; protected Color selectionBackground; protected Color selectionForeground; protected Color disabledForeground; protected Color acceleratorForeground; protected Color acceleratorSelectionForeground; private String acceleratorDelimiter; protected int defaultTextIconGap; protected Font acceleratorFont; protected MouseInputListener mouseInputListener; protected MenuDragMouseListener menuDragMouseListener; protected MenuKeyListener menuKeyListener; /** * <code>PropertyChangeListener</code> returned from * <code>createPropertyChangeListener</code>. You should not * need to access this field, rather if you want to customize the * <code>PropertyChangeListener</code> override * <code>createPropertyChangeListener</code>. * * @since 1.6 * @see #createPropertyChangeListener */ protected PropertyChangeListener propertyChangeListener; // BasicMenuUI also uses this. Handler handler; protected Icon arrowIcon = null; protected Icon checkIcon = null; protected boolean oldBorderPainted; /* diagnostic aids -- should be false for production builds. */ private static final boolean TRACE = false; // trace creates and disposes private static final boolean VERBOSE = false; // show reuse hits/misses private static final boolean DEBUG = false; // show bad params, misc. /* Client Property keys for icon, text and accelerator widths */ static final String MAX_ARROW_ICON_WIDTH = "maxArrowIconWidth"; static final String MAX_CHECK_ICON_WIDTH = "maxCheckIconWidth"; static final String MAX_ICON_WIDTH = "maxIconWidth"; static final String MAX_TEXT_WIDTH = "maxTextWidth"; static final String MAX_ACC_WIDTH = "maxAccWidth"; /* Client Property key for the icon offset */ static final StringBuffer MAX_ICON_OFFSET = new StringBuffer("maxIconOffset"); static void loadActionMap(LazyActionMap map) { // NOTE: BasicMenuUI also calls into this method. map.put(new Actions(Actions.CLICK)); BasicLookAndFeel.installAudioActionMap(map); } public static ComponentUI createUI(JComponent c) { return new BasicMenuItemUI(); } public void installUI(JComponent c) { menuItem = (JMenuItem) c; installDefaults(); installComponents(menuItem); installListeners(); installKeyboardActions(); } protected void installDefaults() { String prefix = getPropertyPrefix(); acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont"); Object opaque = UIManager.get(getPropertyPrefix() + ".opaque"); if (opaque != null) { LookAndFeel.installProperty(menuItem, "opaque", opaque); } else { LookAndFeel.installProperty(menuItem, "opaque", Boolean.TRUE); } if(menuItem.getMargin() == null || (menuItem.getMargin() instanceof UIResource)) { menuItem.setMargin(UIManager.getInsets(prefix + ".margin")); } LookAndFeel.installProperty(menuItem, "iconTextGap", new Integer(4)); defaultTextIconGap = menuItem.getIconTextGap(); LookAndFeel.installBorder(menuItem, prefix + ".border"); oldBorderPainted = menuItem.isBorderPainted(); LookAndFeel.installProperty(menuItem, "borderPainted", UIManager.get(prefix + ".borderPainted")); LookAndFeel.installColorsAndFont(menuItem, prefix + ".background", prefix + ".foreground", prefix + ".font"); // MenuItem specific defaults if (selectionBackground == null || selectionBackground instanceof UIResource) { selectionBackground = UIManager.getColor(prefix + ".selectionBackground"); } if (selectionForeground == null || selectionForeground instanceof UIResource) { selectionForeground = UIManager.getColor(prefix + ".selectionForeground"); } if (disabledForeground == null || disabledForeground instanceof UIResource) { disabledForeground = UIManager.getColor(prefix + ".disabledForeground"); } if (acceleratorForeground == null || acceleratorForeground instanceof UIResource) { acceleratorForeground = UIManager.getColor(prefix + ".acceleratorForeground"); } if (acceleratorSelectionForeground == null || acceleratorSelectionForeground instanceof UIResource) { acceleratorSelectionForeground = UIManager.getColor(prefix + ".acceleratorSelectionForeground"); } // Get accelerator delimiter acceleratorDelimiter = UIManager.getString("MenuItem.acceleratorDelimiter"); if (acceleratorDelimiter == null) { acceleratorDelimiter = "+"; } // Icons if (arrowIcon == null || arrowIcon instanceof UIResource) { arrowIcon = UIManager.getIcon(prefix + ".arrowIcon"); } if (checkIcon == null || checkIcon instanceof UIResource) { checkIcon = UIManager.getIcon(prefix + ".checkIcon"); MenuItemCheckIconFactory iconFactory = (MenuItemCheckIconFactory) UIManager.get(prefix + ".checkIconFactory"); if (iconFactory != null && iconFactory.isCompatible(checkIcon, prefix)) { checkIcon = iconFactory.getIcon(menuItem); } } } /** * @since 1.3 */ protected void installComponents(JMenuItem menuItem){ BasicHTML.updateRenderer(menuItem, menuItem.getText()); } protected String getPropertyPrefix() { return "MenuItem"; } protected void installListeners() { if ((mouseInputListener = createMouseInputListener(menuItem)) != null) { menuItem.addMouseListener(mouseInputListener); menuItem.addMouseMotionListener(mouseInputListener); } if ((menuDragMouseListener = createMenuDragMouseListener(menuItem)) != null) { menuItem.addMenuDragMouseListener(menuDragMouseListener); } if ((menuKeyListener = createMenuKeyListener(menuItem)) != null) { menuItem.addMenuKeyListener(menuKeyListener); } if ((propertyChangeListener = createPropertyChangeListener(menuItem)) != null) { menuItem.addPropertyChangeListener(propertyChangeListener); } } protected void installKeyboardActions() { installLazyActionMap(); updateAcceleratorBinding(); } void installLazyActionMap() { LazyActionMap.installLazyActionMap(menuItem, BasicMenuItemUI.class, getPropertyPrefix() + ".actionMap"); } public void uninstallUI(JComponent c) { menuItem = (JMenuItem)c; uninstallDefaults(); uninstallComponents(menuItem); uninstallListeners(); uninstallKeyboardActions(); //Remove the textWidth and accWidth values from the parent's Client Properties. JComponent p = getMenuItemParent(menuItem); if(p != null) { p.putClientProperty(BasicMenuItemUI.MAX_CHECK_ICON_WIDTH, null ); p.putClientProperty(BasicMenuItemUI.MAX_ARROW_ICON_WIDTH, null ); p.putClientProperty(BasicMenuItemUI.MAX_ACC_WIDTH, null ); p.putClientProperty(BasicMenuItemUI.MAX_TEXT_WIDTH, null ); p.putClientProperty(BasicMenuItemUI.MAX_ICON_WIDTH, null ); p.putClientProperty(BasicMenuItemUI.MAX_ICON_OFFSET, null ); p.putClientProperty(BASICMENUITEMUI_MAX_TEXT_OFFSET, null ); } menuItem = null; } protected void uninstallDefaults() { LookAndFeel.uninstallBorder(menuItem); LookAndFeel.installProperty(menuItem, "borderPainted", oldBorderPainted); if (menuItem.getMargin() instanceof UIResource) menuItem.setMargin(null); if (arrowIcon instanceof UIResource) arrowIcon = null; if (checkIcon instanceof UIResource) checkIcon = null; } /** * @since 1.3 */ protected void uninstallComponents(JMenuItem menuItem){ BasicHTML.updateRenderer(menuItem, ""); } protected void uninstallListeners() { if (mouseInputListener != null) { menuItem.removeMouseListener(mouseInputListener); menuItem.removeMouseMotionListener(mouseInputListener); } if (menuDragMouseListener != null) { menuItem.removeMenuDragMouseListener(menuDragMouseListener); } if (menuKeyListener != null) { menuItem.removeMenuKeyListener(menuKeyListener); } if (propertyChangeListener != null) { menuItem.removePropertyChangeListener(propertyChangeListener); } mouseInputListener = null; menuDragMouseListener = null; menuKeyListener = null; propertyChangeListener = null; handler = null; } protected void uninstallKeyboardActions() { SwingUtilities.replaceUIActionMap(menuItem, null); SwingUtilities.replaceUIInputMap(menuItem, JComponent. WHEN_IN_FOCUSED_WINDOW, null); } protected MouseInputListener createMouseInputListener(JComponent c) { return getHandler(); } protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) { return getHandler(); } protected MenuKeyListener createMenuKeyListener(JComponent c) { return null; } /** * Creates a <code>PropertyChangeListener</code> which will be added to * the menu item. * If this method returns null then it will not be added to the menu item. * * @return an instance of a <code>PropertyChangeListener</code> or null * @since 1.6 */ protected PropertyChangeListener createPropertyChangeListener(JComponent c) { return getHandler(); } Handler getHandler() { if (handler == null) { handler = new Handler(); } return handler; } InputMap createInputMap(int condition) { if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) { return new ComponentInputMapUIResource(menuItem); } return null; } void updateAcceleratorBinding() { KeyStroke accelerator = menuItem.getAccelerator(); InputMap windowInputMap = SwingUtilities.getUIInputMap( menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW); if (windowInputMap != null) { windowInputMap.clear(); } if (accelerator != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -