?? xpmenuui.java
字號:
// Beta
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* XP Look and Feel *
* *
* (C) Copyright 2002, by Stefan Krause *
* *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package com.stefankrause.xplookandfeel;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MenuDragMouseEvent;
import javax.swing.event.MenuDragMouseListener;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuKeyEvent;
import javax.swing.event.MenuKeyListener;
import javax.swing.event.MenuListener;
import javax.swing.event.MouseInputListener;
import javax.swing.plaf.ComponentUI;
/**
* A default L&F implementation of MenuUI. This implementation
* is a "combined" view/controller.
*
* @version 1.144 04/24/02
* @author Georges Saab
* @author David Karlton
* @author Arnaud Weber
*/
public class XPMenuUI extends XPMenuItemUI {
protected ChangeListener changeListener;
protected PropertyChangeListener propertyChangeListener;
protected MenuListener menuListener;
// Shared instance of the menuListener
private static MenuListener sharedMenuListener;
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 XPMenuUI();
}
public void installUI(JComponent c) {
super.installUI(c);
JMenu m=(JMenu)c;
}
protected void installDefaults() {
super.installDefaults();
((JMenu) menuItem).setDelay(200);
// On JDK 1.4.0 or later:
//crossMenuMnemonic = UIManager.getBoolean("Menu.crossMenuMnemonic");
// But here we come back to JDK 1.3.0 or earlier
crossMenuMnemonic = Boolean.getBoolean(
(UIManager.getString("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 updateMnemonicBinding() {
int mnemonic = menuItem.getModel().getMnemonic();
int[] shortcutKeys = (int[]) UIManager.get("Menu.shortcutKeys");
if (mnemonic == lastMnemonic || shortcutKeys == null) {
return;
}
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();
}
/**
* The ActionMap for BasicMenUI can not be shared, this is subclassed
* to create a new one for each invocation.
*/
ActionMap getActionMap() {
return createActionMap();
}
/**
* Invoked to create the ActionMap.
*/
ActionMap createActionMap() {
ActionMap am = super.createActionMap();
if (am != null) {
am.put("selectMenu", new PostAction((JMenu) menuItem, true));
}
return am;
}
protected MouseInputListener createMouseInputListener(JComponent c) {
return new MouseInputHandler();
}
protected MenuListener createMenuListener(JComponent c) {
if (sharedMenuListener == null) {
sharedMenuListener = new MenuHandler();
}
return sharedMenuListener;
}
protected ChangeListener createChangeListener(JComponent c) {
return null;
}
protected PropertyChangeListener createPropertyChangeListener(JComponent c) {
return new PropertyChangeHandler();
}
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;
}
protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) {
return new MenuDragMouseHandler();
}
protected MenuKeyListener createMenuKeyListener(JComponent c) {
return new MenuKeyHandler();
}
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 PostAction(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 PostAction extends AbstractAction {
JMenu menu;
boolean force = false;
PostAction(JMenu menu, boolean shouldForce) {
this.menu = menu;
this.force = shouldForce;
}
public void actionPerformed(ActionEvent e) {
if (!crossMenuMnemonic) {
JPopupMenu pm = getActivePopupMenu();
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() {
return menu.getModel().isEnabled();
}
}
private class PropertyChangeHandler implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (prop.equals(AbstractButton.MNEMONIC_CHANGED_PROPERTY)) {
updateMnemonicBinding();
}
}
}
/**
* 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.
* <p>
* This class is protected so that it can be subclassed by other look and
* feels to implement their own mouse handling behavior. All overridden
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -