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

📄 basicpopupmenuui.java

📁 java jdk 1.4的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                    path[len-1] = nextChild;                    msm.setSelectedPath(path);		}	    }	}    }    private static class SelectParentChildAction extends AbstractAction {        static boolean PARENT = false;        static boolean CHILD = true;        boolean direction;        SelectParentChildAction(boolean dir) {            direction = dir;        }	public void actionPerformed(ActionEvent e) {            MenuSelectionManager msm = MenuSelectionManager.defaultManager();            MenuElement path[] = msm.getSelectedPath();            int len = path.length;            if (direction == PARENT) {                // selecting parent                int popupIndex = len-1;                if (len > 2 &&                    // check if we have an open submenu. A submenu item may or                    // may not be selected, so submenu popup can be either the                    // last or next to the last item.                    (path[popupIndex] instanceof JPopupMenu ||                     path[--popupIndex] instanceof JPopupMenu) &&                    !((JMenu)path[popupIndex-1]).isTopLevelMenu()) {                    // we have a submenu, just close it                    MenuElement newPath[] = new MenuElement[popupIndex];                    System.arraycopy(path, 0, newPath, 0, popupIndex);                    msm.setSelectedPath(newPath);                    return;                }            } else {                // selecting child                if (len > 0 && path[len-1] instanceof JMenu &&                    !((JMenu)path[len-1]).isTopLevelMenu()) {                    // we have a submenu, open it                    JMenu menu = (JMenu)path[len-1];                    JPopupMenu popup = menu.getPopupMenu();                    MenuElement[] subs = popup.getSubElements();                    MenuElement item = findEnabledChild(subs, -1, true);                    MenuElement[] newPath;                    if (item == null) {                        newPath = new MenuElement[len+1];                    } else {                        newPath = new MenuElement[len+2];                        newPath[len+1] = item;                    }                    System.arraycopy(path, 0, newPath, 0, len);                    newPath[len] = popup;                    msm.setSelectedPath(newPath);                    return;                }            }            // check if we have a toplevel menu selected.            // If this is the case, we select another toplevel menu	    if (len > 1 && path[0] instanceof JMenuBar) {                MenuElement currentMenu = path[1];		MenuElement nextMenu = findEnabledChild(                    path[0].getSubElements(), currentMenu, direction);		if (nextMenu != null && nextMenu != currentMenu) {		    MenuElement newSelection[];		    if (len == 2) {                        // menu is selected but its popup not shown			newSelection = new MenuElement[2];			newSelection[0] = path[0];			newSelection[1] = nextMenu;		    } else {                        // menu is selected and its popup is shown			newSelection = new MenuElement[3];			newSelection[0] = path[0];			newSelection[1] = nextMenu;			newSelection[2] = ((JMenu)nextMenu).getPopupMenu();                    }		    msm.setSelectedPath(newSelection);		}	    }	}    }    /**     * This helper is added to MenuSelectionManager as a ChangeListener to      * listen to menu selection changes. When a menu is activated, it passes     * focus to its parent JRootPane, and installs an ActionMap/InputMap pair     * on that JRootPane. Those maps are necessary in order for menu     * navigation to work. When menu is being deactivated, it restores focus     * to the component that has had it before menu activation, and uninstalls     * the maps.     * This helper is also installed as a KeyListener on root pane when menu     * is active. It forwards key events to MenuSelectionManager for mnemonic     * keys handling.     */    private static class MenuKeyboardHelper        implements ChangeListener, KeyListener {        private Component lastFocused = null;  	private MenuElement[] lastPathSelected = new MenuElement[0];        private JPopupMenu lastPopup;        private JRootPane invokerRootPane;        private ActionMap menuActionMap = getActionMap();        private InputMap menuInputMap;        private boolean focusTraversalKeysEnabled;        /*         * Fix for 4213634         * If this is false, KEY_TYPED and KEY_RELEASED events are NOT         * processed. This is needed to avoid activating a menuitem when         * the menu and menuitem share the same mnemonic.         */        private boolean receivedKeyPressed = false;        void removeItems() {            if (lastFocused != null) {                if(!lastFocused.requestFocusInWindow()) {                    // Workarounr for 4810575.                    // If lastFocused is not in currently focused window                    // requestFocusInWindow will fail. In this case we must                    // request focus by requestFocus() if it was not                    // transferred from our popup.                    Window cfw = KeyboardFocusManager                                 .getCurrentKeyboardFocusManager()                                  .getFocusedWindow();                    if(cfw != null &&                       "###focusableSwingPopup###".equals(cfw.getName())) {                        lastFocused.requestFocus();                    }                }                lastFocused = null;            }            if (invokerRootPane != null) {                invokerRootPane.removeKeyListener(menuKeyboardHelper);                invokerRootPane.setFocusTraversalKeysEnabled(focusTraversalKeysEnabled);                removeUIInputMap(invokerRootPane, menuInputMap);                removeUIActionMap(invokerRootPane, menuActionMap);                invokerRootPane = null;            }             receivedKeyPressed = false;        }        private FocusListener rootPaneFocusListener = new FocusAdapter() {                public void focusGained(FocusEvent ev) {                    Component opposite = ev.getOppositeComponent();                    if (opposite != null) {                        lastFocused = opposite;                    }                    ev.getComponent().removeFocusListener(this);                }            };        /**         * Return the last JPopupMenu in <code>path</code>,         * or <code>null</code> if none found         */        JPopupMenu getActivePopup(MenuElement[] path) {            for (int i=path.length-1; i>=0; i--) {                MenuElement elem = path[i];                if (elem instanceof JPopupMenu) {                    return (JPopupMenu)elem;                }            }            return null;        }        void addUIInputMap(JComponent c, InputMap map) {            InputMap lastNonUI = null;            InputMap parent = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);            while (parent != null && !(parent instanceof UIResource)) {                lastNonUI = parent;                parent = parent.getParent();            }            if (lastNonUI == null) {                c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map);            } else {                lastNonUI.setParent(map);            }            map.setParent(parent);        }        void addUIActionMap(JComponent c, ActionMap map) {            ActionMap lastNonUI = null;            ActionMap parent = c.getActionMap();            while (parent != null && !(parent instanceof UIResource)) {                lastNonUI = parent;                parent = parent.getParent();            }            if (lastNonUI == null) {                c.setActionMap(map);            } else {                lastNonUI.setParent(map);            }            map.setParent(parent);        }        void removeUIInputMap(JComponent c, InputMap map) {            InputMap im = null;            InputMap parent = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);            while (parent != null) {                if (parent == map) {                    if (im == null) {                        c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW,                                      map.getParent());                    } else {                        im.setParent(map.getParent());                    }                    break;                }                im = parent;                parent = parent.getParent();            }        }        void removeUIActionMap(JComponent c, ActionMap map) {            ActionMap im = null;            ActionMap parent = c.getActionMap();            while (parent != null) {                if (parent == map) {                    if (im == null) {                        c.setActionMap(map.getParent());                    } else {                        im.setParent(map.getParent());                    }                    break;                }                im = parent;                parent = parent.getParent();            }        }        public void stateChanged(ChangeEvent ev) {            if (!(UIManager.getLookAndFeel() instanceof BasicLookAndFeel)) {                MenuSelectionManager msm = MenuSelectionManager.                                           defaultManager();                msm.removeChangeListener(this);                menuKeyboardHelperInstalled = false;                return;            }	    MenuSelectionManager msm = (MenuSelectionManager)ev.getSource();	    MenuElement[] p = msm.getSelectedPath();            JPopupMenu popup = getActivePopup(p);            if (popup != null && !popup.isFocusable()) {                // Do nothing for non-focusable popups                return;            }            if   (lastPathSelected.length != 0 && p.length != 0 ) {                if (!checkInvokerEqual(p[0],lastPathSelected[0])) {                        removeItems();                        lastPathSelected = new MenuElement[0];                        }        	}	    if (lastPathSelected.length == 0 && p.length > 0) {                // menu posted                JComponent invoker;                if (popup == null) {                    if (p.length == 2 && p[0] instanceof JMenuBar &&                        p[1] instanceof JMenu) {                        // a menu has been selected but not open                        invoker = (JComponent)p[1];                        popup = ((JMenu)invoker).getPopupMenu();                    } else {                        return;                    }                } else {                    Component c = popup.getInvoker();                    if(c instanceof JFrame) {                        invoker = ((JFrame)c).getRootPane();                    } else if(c instanceof JApplet) {                        invoker = ((JApplet)c).getRootPane();                    } else {                        while (!(c instanceof JComponent)) {                            if (c == null) {                                return;                            }                            c = c.getParent();                        }                        invoker = (JComponent)c;                    }                }                // remember current focus owner                lastFocused = KeyboardFocusManager.                    getCurrentKeyboardFocusManager().getFocusOwner();                // request focus on root pane and install keybindings                // used for menu navigation                invokerRootPane = SwingUtilities.getRootPane(invoker);                if (invokerRootPane != null) {                    invokerRootPane.addFocusListener(rootPaneFocusListener);                    invokerRootPane.requestFocus(true);                    invokerRootPane.addKeyListener(menuKeyboardHelper);                    focusTraversalKeysEnabled = invokerRootPane.                                      getFocusTraversalKeysEnabled();                    invokerRootPane.setFocusTraversalKeysEnabled(false);                    menuInputMap = getInputMap(popup, invokerRootPane);                    addUIInputMap(invokerRootPane, menuInputMap);                    addUIActionMap(invokerRootPane, menuActionMap);                }            } else if (lastPathSelected.length != 0 && p.length == 0) {		// menu hidden -- return focus to where it had been before                // and uninstall menu keybindings                   removeItems();	    } else {                if (popup != lastPopup) {                    receivedKeyPressed = false;                }            }            // Remember the last path selected            lastPathSelected = p;            lastPopup = popup;        }        public void keyPressed(KeyEvent ev) {            receivedKeyPressed = true;            MenuSelectionManager.defaultManager().processKeyEvent(ev);        }        public void keyReleased(KeyEvent ev) {	    if (receivedKeyPressed) {		receivedKeyPressed = false;                MenuSelectionManager.defaultManager().processKeyEvent(ev);            }        }        public void keyTyped(KeyEvent ev) {	    if (receivedKeyPressed) {                MenuSelectionManager.defaultManager().processKeyEvent(ev);            }        }    }}

⌨️ 快捷键说明

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