?? basicmenuui.java
字號:
/* * @(#)BasicMenuUI.java 1.158 04/02/26 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.plaf.basic;import sun.swing.DefaultLookup;import sun.swing.UIAction;import java.awt.*;import java.awt.event.*;import java.beans.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.plaf.*;import javax.swing.border.*;import java.util.Arrays;import java.util.ArrayList;/** * A default L&F implementation of MenuUI. This implementation * is a "combined" view/controller. * * @version 1.158 02/26/04 * @author Georges Saab * @author David Karlton * @author Arnaud Weber */public class BasicMenuUI extends BasicMenuItemUI { protected ChangeListener changeListener; protected PropertyChangeListener propertyChangeListener; protected MenuListener menuListener; private int lastMnemonic = 0; /** Uses as the parent of the windowInputMap when selected. */ private InputMap selectedWindowInputMap; /* 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. private static boolean crossMenuMnemonic = true; public static ComponentUI createUI(JComponent x) { return new BasicMenuUI(); } static void loadActionMap(LazyActionMap map) { BasicMenuItemUI.loadActionMap(map); map.put(new Actions(Actions.SELECT, null, true)); } protected void installDefaults() { super.installDefaults(); updateDefaultBackgroundColor(); ((JMenu)menuItem).setDelay(200); crossMenuMnemonic = UIManager.getBoolean("Menu.crossMenuMnemonic"); } protected String getPropertyPrefix() { return "Menu"; } protected void installListeners() { super.installListeners(); if (changeListener == null) changeListener = createChangeListener(menuItem); if (changeListener != null) menuItem.addChangeListener(changeListener); if (propertyChangeListener == null) propertyChangeListener = createPropertyChangeListener(menuItem); if (propertyChangeListener != null) menuItem.addPropertyChangeListener(propertyChangeListener); if (menuListener == null) menuListener = createMenuListener(menuItem); if (menuListener != null) ((JMenu)menuItem).addMenuListener(menuListener); } protected void installKeyboardActions() { super.installKeyboardActions(); updateMnemonicBinding(); } void installLazyActionMap() { LazyActionMap.installLazyActionMap(menuItem, BasicMenuUI.class, getPropertyPrefix() + ".actionMap"); } void updateMnemonicBinding() { int mnemonic = menuItem.getModel().getMnemonic(); int[] shortcutKeys = (int[])DefaultLookup.get(menuItem, this, "Menu.shortcutKeys"); if (shortcutKeys == null) { shortcutKeys = new int[] {KeyEvent.ALT_MASK}; } if (mnemonic == lastMnemonic) { return; } InputMap windowInputMap = SwingUtilities.getUIInputMap( menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW); if (lastMnemonic != 0 && windowInputMap != null) { for (int i=0; i<shortcutKeys.length; i++) { windowInputMap.remove(KeyStroke.getKeyStroke (lastMnemonic, shortcutKeys[i], false)); } } if (mnemonic != 0) { if (windowInputMap == null) { windowInputMap = createInputMap(JComponent. WHEN_IN_FOCUSED_WINDOW); SwingUtilities.replaceUIInputMap(menuItem, JComponent. WHEN_IN_FOCUSED_WINDOW, windowInputMap); } for (int i=0; i<shortcutKeys.length; i++) { windowInputMap.put(KeyStroke.getKeyStroke(mnemonic, shortcutKeys[i], false), "selectMenu"); } } lastMnemonic = mnemonic; } protected void uninstallKeyboardActions() { super.uninstallKeyboardActions(); lastMnemonic = 0; } protected MouseInputListener createMouseInputListener(JComponent c) { return getHandler(); } protected MenuListener createMenuListener(JComponent c) { return null; } protected ChangeListener createChangeListener(JComponent c) { return null; } protected PropertyChangeListener createPropertyChangeListener(JComponent c) { return getHandler(); } BasicMenuItemUI.Handler getHandler() { if (handler == null) { handler = new Handler(); } return handler; } protected void uninstallDefaults() { menuItem.setArmed(false); menuItem.setSelected(false); menuItem.resetKeyboardActions(); super.uninstallDefaults(); } protected void uninstallListeners() { super.uninstallListeners(); if (changeListener != null) menuItem.removeChangeListener(changeListener); if (propertyChangeListener != null) menuItem.removePropertyChangeListener(propertyChangeListener); if (menuListener != null) ((JMenu)menuItem).removeMenuListener(menuListener); changeListener = null; propertyChangeListener = null; menuListener = null; handler = null; } protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) { return getHandler(); } protected MenuKeyListener createMenuKeyListener(JComponent c) { return (MenuKeyListener)getHandler(); } public Dimension getMaximumSize(JComponent c) { if (((JMenu)menuItem).isTopLevelMenu() == true) { Dimension d = c.getPreferredSize(); return new Dimension(d.width, Short.MAX_VALUE); } return null; } protected void setupPostTimer(JMenu menu) { Timer timer = new Timer(menu.getDelay(), new Actions( Actions.SELECT, menu,false)); timer.setRepeats(false); timer.start(); } private static void appendPath(MenuElement[] path, MenuElement elem) { MenuElement newPath[] = new MenuElement[path.length+1]; System.arraycopy(path, 0, newPath, 0, path.length); newPath[path.length] = elem; MenuSelectionManager.defaultManager().setSelectedPath(newPath); } private static class Actions extends UIAction { private static final String SELECT = "selectMenu"; // NOTE: This will be null if the action is registered in the // ActionMap. For the timer use it will be non-null. private JMenu menu; private boolean force=false; Actions(String key, JMenu menu, boolean shouldForce) { super(key); this.menu = menu; this.force = shouldForce; } private JMenu getMenu(ActionEvent e) { if (e.getSource() instanceof JMenu) { return (JMenu)e.getSource(); } return menu; } public void actionPerformed(ActionEvent e) { JMenu menu = getMenu(e); if (!crossMenuMnemonic) { JPopupMenu pm = BasicPopupMenuUI.getLastPopup(); if (pm != null && pm != menu.getParent()) { return; } } final MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager(); if(force) { Container cnt = menu.getParent(); if(cnt != null && cnt instanceof JMenuBar) { MenuElement me[]; MenuElement subElements[]; subElements = menu.getPopupMenu().getSubElements(); if(subElements.length > 0) { me = new MenuElement[4]; me[0] = (MenuElement) cnt; me[1] = (MenuElement) menu; me[2] = (MenuElement) menu.getPopupMenu(); me[3] = subElements[0]; } else { me = new MenuElement[3]; me[0] = (MenuElement)cnt; me[1] = menu; me[2] = (MenuElement) menu.getPopupMenu(); } defaultManager.setSelectedPath(me); } } else { MenuElement path[] = defaultManager.getSelectedPath(); if(path.length > 0 && path[path.length-1] == menu) { appendPath(path, menu.getPopupMenu()); } } } public boolean isEnabled(Object c) { if (c instanceof JMenu) { return ((JMenu)c).isEnabled(); } return true; } } /* * Set the background color depending on whether this is a toplevel menu * in a menubar or a submenu of another menu. */ private void updateDefaultBackgroundColor() { if (!UIManager.getBoolean("Menu.useMenuBarBackgroundForTopLevel")) { return; } JMenu menu = (JMenu)menuItem; if (menu.getBackground() instanceof UIResource) { if (menu.isTopLevelMenu()) { menu.setBackground(UIManager.getColor("MenuBar.background")); } else { menu.setBackground(UIManager.getColor(getPropertyPrefix() + ".background")); } } } /** * Instantiated and used by a menu item to handle the current menu selection * from mouse events. A MouseInputHandler processes and forwards all mouse events * to a shared instance of the MenuSelectionManager.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -