📄 xpmenuitemui.java
字号:
// Beta// It works, but there should be a rework to remove most of the code here!/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** XP Look and Feel ** ** (C) Copyright 2002, by Stefan Krause ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//* * * This is an almost unchanged version of MetalMenuItemUI. * * * @(#)BasicFileChooserUI.java 1.45 02/04/11 * * Copyright 2002 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package com.stefankrause.xplookandfeel;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;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.JRadioButtonMenuItem;import javax.swing.KeyStroke;import javax.swing.LookAndFeel;import javax.swing.MenuElement;import javax.swing.MenuSelectionManager;import javax.swing.SwingUtilities;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;import javax.swing.plaf.UIResource;import javax.swing.plaf.basic.BasicGraphicsUtils;import javax.swing.plaf.basic.BasicHTML;import javax.swing.text.View;import com.stefankrause.xplookandfeel.skin.Skin;import com.stefankrause.xplookandfeel.skin.SkinMenuItem;/** * XPMenuItemUI implementation * * @version 1.115 12/03/01 * @author Georges Saab * @author David Karlton * @author Arnaud Weber * @author Fredrik Lagerblad */public class XPMenuItemUI 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 static int defaultTextIconGap, defaultIconGap; protected Font acceleratorFont; protected MouseInputListener mouseInputListener; protected MenuDragMouseListener menuDragMouseListener; protected MenuKeyListener menuKeyListener; private PropertyChangeListener propertyChangeListener; protected Icon arrowIcon = null; protected Icon checkIcon = null; protected boolean oldBorderPainted; /** Used for accelerator binding, lazily created. */ InputMap windowInputMap; /* 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 text and accelerator text widths */ static final String MAX_TEXT_WIDTH = "maxTextWidth"; static final String MAX_ACC_WIDTH = "maxAccWidth"; static final String MAX_ICON_WIDTH = "maxIconWidth"; static SkinMenuItem skin=new SkinMenuItem("Menu.res",0,1,2,3, 2); static Skin topSkin=new Skin("MenuTop.res",3, 1,1, 6, 2); public void installUI(JComponent c) { menuItem = (JMenuItem) c; installDefaults(); installComponents(menuItem); installListeners(); installKeyboardActions(); } /** * @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() { ActionMap actionMap = getActionMap(); SwingUtilities.replaceUIActionMap(menuItem, actionMap); updateAcceleratorBinding(); } 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. Container parent = menuItem.getParent(); if ((parent != null && parent instanceof JComponent) && !(menuItem instanceof JMenu && ((JMenu) menuItem).isTopLevelMenu())) { JComponent p = (JComponent) parent; p.putClientProperty(MAX_ACC_WIDTH, null); p.putClientProperty(MAX_TEXT_WIDTH, null); } menuItem = null; } protected void uninstallDefaults() { LookAndFeel.uninstallBorder(menuItem); menuItem.setBorderPainted(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; } protected void uninstallKeyboardActions() { SwingUtilities.replaceUIActionMap(menuItem, null); if (windowInputMap != null) { SwingUtilities.replaceUIInputMap(menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW, null); windowInputMap = null; } } protected MouseInputListener createMouseInputListener(JComponent c) { return new MouseInputHandler(); } protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) { return new MenuDragMouseHandler(); } protected MenuKeyListener createMenuKeyListener(JComponent c) { return new MenuKeyHandler(); } private PropertyChangeListener createPropertyChangeListener(JComponent c) { return new PropertyChangeHandler(); } ActionMap getActionMap() { String propertyPrefix = getPropertyPrefix(); String uiKey = propertyPrefix + ".actionMap"; ActionMap am = (ActionMap) UIManager.get(uiKey); if (am == null) { am = createActionMap(); UIManager.getLookAndFeelDefaults().put(uiKey, am); } return am; } ActionMap createActionMap() { ActionMap map = new ActionMapUIResource(); map.put("doClick", new ClickAction()); // Set the ActionMap's parent to the Auditory Feedback Action Map // BasicLookAndFeel lf = (BasicLookAndFeel) UIManager.getLookAndFeel(); // ActionMap audioMap = lf.getAudioActionMap(); // map.setParent(audioMap); return map; } InputMap createInputMap(int condition) { if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) { return new ComponentInputMapUIResource(menuItem); } return null; } void updateAcceleratorBinding() { KeyStroke accelerator = menuItem.getAccelerator(); if (windowInputMap != null) { windowInputMap.clear(); } if (accelerator != null) { if (windowInputMap == null) { windowInputMap = createInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); SwingUtilities.replaceUIInputMap(menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW, windowInputMap); } windowInputMap.put(accelerator, "doClick"); } } public Dimension getMinimumSize(JComponent c) { Dimension d = null; View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { d = getPreferredSize(c); d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS); } return d; } public Dimension getPreferredSize(JComponent c) { return getPreferredMenuItemSize(c, checkIcon, arrowIcon, defaultTextIconGap); } /** * Renders the text of the current menu item. * <p> * @param g graphics context * @param menuItem menu item to render * @param textRect bounding rectangle for rendering the text * @param text string to render * @since 1.4 */ protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) { ButtonModel model = menuItem.getModel(); FontMetrics fm = g.getFontMetrics(); int mnemIndex = menuItem.getDisplayedMnemonicIndex(); if (!model.isEnabled()) { // *** paint the text disabled if (UIManager.get("MenuItem.disabledForeground") instanceof Color) { g.setColor(UIManager.getColor("MenuItem.disabledForeground")); BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent()); } else { g.setColor(menuItem.getBackground().brighter()); BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent()); g.setColor(menuItem.getBackground().darker()); BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemIndex, textRect.x - 1, textRect.y + fm.getAscent() - 1); } } else { // *** paint the text normally if (model.isArmed() || (menuItem instanceof JMenu && model.isSelected())) { g.setColor(selectionForeground); // Uses protected field. } BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent()); } } public Dimension getMaximumSize(JComponent c) { Dimension d = null; View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { d = getPreferredSize(c); d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS); } return d; } // these rects are used for painting and preferredsize calculations. // they used to be regenerated constantly. Now they are reused. static Rectangle zeroRect = new Rectangle(0, 0, 0, 0); static Rectangle iconRect = new Rectangle(); static Rectangle textRect = new Rectangle(); static Rectangle acceleratorRect = new Rectangle(); static Rectangle checkIconRect = new Rectangle(); static Rectangle arrowIconRect = new Rectangle(); static Rectangle viewRect = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE); static Rectangle r = new Rectangle(); private void resetRects() { iconRect.setBounds(zeroRect); textRect.setBounds(zeroRect); acceleratorRect.setBounds(zeroRect);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -