⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicmenuitemui.java

📁 java jdk 1.4的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * @(#)BasicMenuItemUI.java	1.118 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing.plaf.basic;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;/** * BasicMenuItem implementation * * @version 1.118 01/23/03 * @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;    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";    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");        menuItem.setOpaque(true);        if(menuItem.getMargin() == null ||            (menuItem.getMargin() instanceof UIResource)) {            menuItem.setMargin(UIManager.getInsets(prefix + ".margin"));        }        defaultTextIconGap = 4;   // Should be from table        LookAndFeel.installBorder(menuItem, prefix + ".border");        oldBorderPainted = menuItem.isBorderPainted();        menuItem.setBorderPainted( ( (Boolean) (UIManager.get(prefix + ".borderPainted")) ).booleanValue() );        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");        }    }    /**     * @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(BasicMenuItemUI.MAX_ACC_WIDTH, null );	    p.putClientProperty(BasicMenuItemUI.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);    }    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);        checkIconRect.setBounds(zeroRect);        arrowIconRect.setBounds(zeroRect);        viewRect.setBounds(0,0,Short.MAX_VALUE, Short.MAX_VALUE);        r.setBounds(zeroRect);    }    protected Dimension getPreferredMenuItemSize(JComponent c,                                                     Icon checkIcon,                                                     Icon arrowIcon,                                                     int defaultTextIconGap) {        JMenuItem b = (JMenuItem) c;        Icon icon = (Icon) b.getIcon();         String text = b.getText();        KeyStroke accelerator =  b.getAccelerator();        String acceleratorText = "";        if (accelerator != null) {            int modifiers = accelerator.getModifiers();            if (modifiers > 0) {                acceleratorText = KeyEvent.getKeyModifiersText(modifiers);                //acceleratorText += "-";                acceleratorText += acceleratorDelimiter;          }            int keyCode = accelerator.getKeyCode();            if (keyCode != 0) {                acceleratorText += KeyEvent.getKeyText(keyCode);            } else {                acceleratorText += accelerator.getKeyChar();            }        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -