📄 menuui.java
字号:
package com.digitprop.tonic;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
/** UI delegate for JMenus.
*
* @author Markus Fischer
*
* <p>This software is under the <a href="http://www.gnu.org/copyleft/lesser.html" target="_blank">GNU Lesser General Public License</a>
*/
/*
* ------------------------------------------------------------------------
* Copyright (C) 2004 Markus Fischer
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* 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 Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* You can contact the author at:
* Markus Fischer
* www.digitprop.com
* info@digitprop.com
* ------------------------------------------------------------------------
*/
public class MenuUI extends MenuItemUI
{
/** Associated change listener */
protected ChangeListener changeListener;
/** Associated property change listener */
protected PropertyChangeListener propertyChangeListener;
/** Associated menu listener */
protected MenuListener menuListener;
/** Shared instance of the menuListener */
private static MenuListener sharedMenuListener;
/** Last used mnemonic */
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
/** Diagnostic aids -- should be false for production builds. */
private static final boolean VERBOSE = false; // show reuse hits/misses
/** Diagnostic aids -- should be false for production builds. */
private static final boolean DEBUG = false; // show bad params, misc.
/** Creates and returns a UI delegate for the specified component */
public static ComponentUI createUI(JComponent x)
{
return new MenuUI();
}
protected void installDefaults() {
super.installDefaults();
((JMenu)menuItem).setDelay(200);
}
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 = createMyInputMap(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 getMyActionMap() {
return createMyActionMap();
}
/**
* Invoked to create the ActionMap.
*/
ActionMap createMyActionMap() {
ActionMap am = super.createMyActionMap();
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();
}
/** Returns the maximum size for the specified component */
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) {
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 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -