synthpopupmenuui.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,206 行 · 第 1/3 页

JAVA
1,206
字号
/* * @(#)SynthPopupMenuUI.java	1.14 03/04/10 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.gtk;import javax.swing.*;import javax.swing.event.*;import javax.swing.plaf.*;import javax.swing.border.*;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Graphics;import java.awt.KeyboardFocusManager;import java.awt.Window;import java.awt.event.*;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeEvent;import java.util.*;/** * @version 1.14, 04/10/03 (based on BasicPopupMenuUI v 1.96) * @author Georges Saab * @author David Karlton * @author Arnaud Weber */class SynthPopupMenuUI extends PopupMenuUI implements PropertyChangeListener,                                    SynthUI {    /**     * Maximum size of the text portion of the children menu items.     */    private int maxTextWidth;    /**     * Maximum size of the text for the acclerator portion of the children     * menu items.     */    private int maxAcceleratorWidth;    static boolean menuKeyboardHelperInstalled = false;    static MenuKeyboardHelper menuKeyboardHelper = null;    protected JPopupMenu popupMenu = null;    private SynthStyle style;    private static boolean checkInvokerEqual(MenuElement present, MenuElement last) {        Component invokerPresent = present.getComponent();        Component invokerLast = last.getComponent();        if (invokerPresent instanceof JPopupMenu) {            invokerPresent = ((JPopupMenu)invokerPresent).getInvoker();        }        if (invokerLast instanceof JPopupMenu) {            invokerLast = ((JPopupMenu)invokerLast).getInvoker();        }        return (invokerPresent == invokerLast);    }    public static ComponentUI createUI(JComponent x) {	return new SynthPopupMenuUI();    }    public void installUI(JComponent c) {	popupMenu = (JPopupMenu) c;	installDefaults();        installListeners();        installKeyboardActions();    }    public void installDefaults() {	if (popupMenu.getLayout() == null ||	    popupMenu.getLayout() instanceof UIResource) {	    popupMenu.setLayout(new DefaultMenuLayout(                                    popupMenu, BoxLayout.Y_AXIS));        }        fetchStyle(popupMenu);    }    private void fetchStyle(JComponent c) {        SynthContext context = getContext(c, ENABLED);        style = SynthLookAndFeel.updateStyle(context, this);        context.dispose();    }    protected void installListeners() {	if (mouseGrabber == null) {	    mouseGrabber = new MouseGrabber();	}	if (basicPopupMenuListener == null) {	    basicPopupMenuListener = createPopupMenuListener();	} 	popupMenu.addPopupMenuListener(basicPopupMenuListener);        if (!menuKeyboardHelperInstalled) {            if (menuKeyboardHelper == null) {                menuKeyboardHelper = new MenuKeyboardHelper();            }            MenuSelectionManager msm = MenuSelectionManager.defaultManager();            msm.addChangeListener(menuKeyboardHelper);        }        popupMenu.addPropertyChangeListener(this);    }    protected void installKeyboardActions() {    }    static InputMap getInputMap(JPopupMenu popup, JComponent c) {        InputMap windowInputMap = null;        PopupMenuUI ui = popup.getUI();        SynthContext context = null;        Object[] bindings = null;        if (ui instanceof SynthPopupMenuUI) {            context = ((SynthPopupMenuUI)ui).getContext(popup, ENABLED);            bindings = (Object[])context.getStyle().get(context,                                   "PopupMenu.selectedWindowInputMapBindings");        }        if (bindings == null) {            bindings = new Object[] {		  "ESCAPE", "cancel",                    "DOWN", "selectNext",		 "KP_DOWN", "selectNext",		      "UP", "selectPrevious",		   "KP_UP", "selectPrevious",		    "LEFT", "selectParent",		 "KP_LEFT", "selectParent",		   "RIGHT", "selectChild",		"KP_RIGHT", "selectChild",		   "ENTER", "return",		   "SPACE", "return"	    };        }        windowInputMap = LookAndFeel.makeComponentInputMap(c, bindings);        if (!popup.getComponentOrientation().isLeftToRight()) {            Object[] km = null;            if (context != null) {                km = (Object[])context.getStyle().get(context,                       "PopupMenu.selectedWindowInputMapBindings.RightToLeft");            }            if (km == null) {                km = new Object[] {                    "LEFT", "selectChild",                    "KP_LEFT", "selectChild",                    "RIGHT", "selectParent",                    "KP_RIGHT", "selectParent",                };            }            InputMap rightToLeftInputMap = LookAndFeel.                                           makeComponentInputMap(c, km);            rightToLeftInputMap.setParent(windowInputMap);            windowInputMap = rightToLeftInputMap;        }        if (context != null) {            context.dispose();        }        return windowInputMap;    }    static ActionMap getActionMap() {        return LazyActionMap.getActionMap(SynthPopupMenuUI.class,                                          "PopupMenu.actionMap");    }      public static void loadActionMap(ActionMap map) {        // NOTE: this needs to remain static. If you have a need to        // have Actions that reference the UI in the ActionMap,        // then you'll also need to change the registeration of the        // ActionMap.        // PENDING: need support for actions./*  	// Set the ActionMap's parent to the Auditory Feedback Action Map  	BasicLookAndFeel lf = (SynthLookAndFeel)UIManager.getLookAndFeel();	ActionMap audioMap = lf.getAudioActionMap();	map.setParent(audioMap);*/	map.put("cancel", new CancelAction());        map.put("selectNext",                new SelectNextItemAction(SelectNextItemAction.FORWARD));        map.put("selectPrevious",                new SelectNextItemAction(SelectNextItemAction.BACKWARD));	map.put("selectParent",                new SelectParentChildAction(SelectParentChildAction.PARENT));	map.put("selectChild",                new SelectParentChildAction(SelectParentChildAction.CHILD));	map.put("return", new ReturnAction());    }    public void uninstallUI(JComponent c) {        uninstallDefaults();        uninstallListeners();        uninstallKeyboardActions();		popupMenu = null;    }        protected void uninstallDefaults() {        SynthContext context = getContext(popupMenu, ENABLED);        style.uninstallDefaults(context);        context.dispose();        style = null;        if (popupMenu.getLayout() instanceof UIResource) {            popupMenu.setLayout(null);        }    }    protected void uninstallListeners() {	if (basicPopupMenuListener != null) {	    popupMenu.removePopupMenuListener(basicPopupMenuListener);	}        popupMenu.removePropertyChangeListener(this);    }    protected void uninstallKeyboardActions() {	SwingUtilities.replaceUIActionMap(popupMenu, null);	SwingUtilities.replaceUIInputMap(popupMenu, 				  JComponent.WHEN_IN_FOCUSED_WINDOW, null);    }    public SynthContext getContext(JComponent c) {        return getContext(c, getComponentState(c));    }    private SynthContext getContext(JComponent c, int state) {        return SynthContext.getContext(SynthContext.class, c,                    SynthLookAndFeel.getRegion(c), style, state);    }    private Region getRegion(JComponent c) {        return SynthLookAndFeel.getRegion(c);    }    private int getComponentState(JComponent c) {        return SynthLookAndFeel.getComponentState(c);    }    /**     * Resets the max text and accerator widths.     */    void resetAcceleratorWidths() {        maxTextWidth = maxAcceleratorWidth = 0;    }    /**     * Adjusts the width needed to display the maximum menu item string.     *     * @param width Text width.     * @return max width     */    int adjustTextWidth(int width) {        maxTextWidth = Math.max(maxTextWidth, width);        return maxTextWidth;    }    /**     * Adjusts the width needed to display the maximum accelerator.     *     * @param width Text width.     * @return max width     */    int adjustAcceleratorWidth(int width) {        maxAcceleratorWidth = Math.max(maxAcceleratorWidth, width);        return maxAcceleratorWidth;    }    /**     * Maximum size to display text of children menu items.     */    int getMaxTextWidth() {        return maxTextWidth;    }    /**     * Maximum size needed to display accelerators of children menu items.     */    int getMaxAcceleratorWidth() {        return maxAcceleratorWidth;    }    public Dimension getMinimumSize(JComponent c) {        return getPreferredSize(c);    }    public Dimension getPreferredSize(JComponent c) {	return null;    }    public Dimension getMaximumSize(JComponent c) {        return getPreferredSize(c);    }    public void update(Graphics g, JComponent c) {        SynthContext context = getContext(c);        SynthLookAndFeel.update(context, g);        paint(context, g);        context.dispose();    }    public void paint(Graphics g, JComponent c) {        SynthContext context = getContext(c);        paint(context, g);        context.dispose();    }    protected void paint(SynthContext context, Graphics g) {    }    public void propertyChange(PropertyChangeEvent e) {        if (SynthLookAndFeel.shouldUpdateStyle(e)) {            fetchStyle(popupMenu);        }    }/////////////////////////////////////////////////////////////////////////////////// Grab Code///////////////////////////////////////////////////////////////////////////////    private static Window getWindow(Component c) {        Component w = c;        while(!(w instanceof Window) && (w!=null)) {            w = w.getParent();        }        return (Window)w;    }        private static MenuElement getFirstPopup() {	MenuSelectionManager msm = MenuSelectionManager.defaultManager();	MenuElement[] p = msm.getSelectedPath();	MenuElement me = null;	    		for(int i = 0 ; me == null && i < p.length ; i++) {	    if (p[i] instanceof JPopupMenu)		me = p[i];	}		return me;    }    private static JPopupMenu getLastPopup() {	MenuSelectionManager msm = MenuSelectionManager.defaultManager();	MenuElement[] p = msm.getSelectedPath();	JPopupMenu popup = null;	    		for(int i = p.length - 1; popup == null && i >= 0; i--) {	    if (p[i] instanceof JPopupMenu)		popup = (JPopupMenu)p[i];	}	return popup;    }    private static List getPopups() {	MenuSelectionManager msm = MenuSelectionManager.defaultManager();	MenuElement[] p = msm.getSelectedPath();		List list = new ArrayList(p.length); 	for(int i = 0; i < p.length; i++) {	    if (p[i] instanceof JPopupMenu) {		list.add((JPopupMenu)p[i]);	    }	}	return list;    }    private transient static MouseGrabber mouseGrabber = null;    private static class MouseGrabber        implements MouseListener, MouseMotionListener, MouseWheelListener,                   WindowListener, WindowFocusListener, ComponentListener, ChangeListener {	Vector grabbed = new Vector();	MenuElement lastGrabbed = null;	boolean lastGrabbedMenuBarChild = false;        public MouseGrabber() {	    MenuSelectionManager msm = MenuSelectionManager.defaultManager();	    msm.addChangeListener(this);        }		private void requestAddGrab(Component invoker) {	    Window ancestor;	    ancestor = getWindow(invoker);	    MenuSelectionManager msm = MenuSelectionManager.defaultManager();	    MenuElement[] p = msm.getSelectedPath();	    Component excluded = null;	    

⌨️ 快捷键说明

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