synthmenuitemui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,066 行 · 第 1/3 页
JAVA
1,066 行
/* * @(#)SynthMenuItemUI.java 1.18 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.plaf.basic.BasicHTML;import java.awt.*;import java.awt.event.*;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.swing.*;import javax.swing.event.*;import javax.swing.border.*;import javax.swing.plaf.*;import javax.swing.text.View;/** * BasicMenuItem implementation * * @version 1.18, 04/10/03 (based on BasicMenuItemUI v 1.116) * @author Georges Saab * @author David Karlton * @author Arnaud Weber * @author Fredrik Lagerblad */class SynthMenuItemUI extends MenuItemUI implements SynthUI { static final String MAX_TEXT_WIDTH = "maxTextWidth"; static final String MAX_ACC_WIDTH = "maxAccWidth"; private SynthStyle style; private SynthStyle accStyle; protected JMenuItem menuItem = null; private String acceleratorDelimiter; protected int defaultTextIconGap; protected MouseInputListener mouseInputListener; protected MenuDragMouseListener menuDragMouseListener; protected MenuKeyListener menuKeyListener; private PropertyChangeListener propertyChangeListener; protected Icon arrowIcon = null; protected Icon checkIcon = null; /** Used for accelerator binding, lazily created. */ InputMap windowInputMap; /* 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. public static ComponentUI createUI(JComponent c) { return new SynthMenuItemUI(); } 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. map.put("doClick", new ClickAction()); } public void installUI(JComponent c) { menuItem = (JMenuItem) c; installDefaults(); installComponents(menuItem); installListeners(); installKeyboardActions(); } protected void installDefaults() { fetchStyle(menuItem); } private void fetchStyle(JMenuItem mi) { SynthContext context = getContext(mi, ENABLED); SynthStyle oldStyle = style; style = SynthLookAndFeel.updateStyle(context, this); if (oldStyle != style) { String prefix = getPropertyPrefix(); defaultTextIconGap = style.getInt( context, prefix + ".textIconGap", 4); if (menuItem.getMargin() == null || (menuItem.getMargin() instanceof UIResource)) { Insets insets = (Insets)style.get(context, prefix + ".margin"); if (insets == null) { // Some places assume margins are non-null. insets = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS; } menuItem.setMargin(insets); } acceleratorDelimiter = style.getString(context, prefix + ".acceleratorDelimiter", "+"); arrowIcon = style.getIcon(context, prefix + ".arrowIcon"); checkIcon = style.getIcon(context, prefix + ".checkIcon"); } context.dispose(); SynthContext accContext = getContext(mi, Region.MENU_ITEM_ACCELERATOR, ENABLED); accStyle = SynthLookAndFeel.updateStyle(accContext, this); accContext.dispose(); } /** * @since 1.3 */ protected void installComponents(JMenuItem menuItem){ BasicHTML.updateRenderer(menuItem, menuItem.getText()); } protected String getPropertyPrefix() { return "MenuItem"; } protected void installListeners() { if ((mouseInputListener = createMouseInputListener(menuItem)) != null) { menuItem.addMouseListener(mouseInputListener); menuItem.addMouseMotionListener(mouseInputListener); } if ((menuDragMouseListener = createMenuDragMouseListener(menuItem)) != null) { menuItem.addMenuDragMouseListener(menuDragMouseListener); } if ((menuKeyListener = createMenuKeyListener(menuItem)) != null) { menuItem.addMenuKeyListener(menuKeyListener); } if ((propertyChangeListener = createPropertyChangeListener(menuItem)) != null) { menuItem.addPropertyChangeListener(propertyChangeListener); } } protected void installKeyboardActions() { registerActionMap(); updateAcceleratorBinding(); } public void uninstallUI(JComponent c) { menuItem = (JMenuItem)c; uninstallDefaults(); uninstallComponents(menuItem); uninstallListeners(); uninstallKeyboardActions(); menuItem = null; } protected void uninstallDefaults() { if (menuItem.getMargin() instanceof UIResource) menuItem.setMargin(null); if (arrowIcon instanceof UIResource) arrowIcon = null; if (checkIcon instanceof UIResource) checkIcon = null; SynthContext context = getContext(menuItem, ENABLED); style.uninstallDefaults(context); context.dispose(); style = null; SynthContext accContext = getContext(menuItem, Region.MENU_ITEM_ACCELERATOR, ENABLED); accStyle.uninstallDefaults(accContext); accContext.dispose(); accStyle = null; } /** * @since 1.3 */ protected void uninstallComponents(JMenuItem menuItem){ BasicHTML.updateRenderer(menuItem, ""); } protected void uninstallListeners() { if (mouseInputListener != null) { menuItem.removeMouseListener(mouseInputListener); menuItem.removeMouseMotionListener(mouseInputListener); } if (menuDragMouseListener != null) { menuItem.removeMenuDragMouseListener(menuDragMouseListener); } if (menuKeyListener != null) { menuItem.removeMenuKeyListener(menuKeyListener); } if (propertyChangeListener != null) { menuItem.removePropertyChangeListener(propertyChangeListener); } mouseInputListener = null; menuDragMouseListener = null; menuKeyListener = null; propertyChangeListener = null; } protected void uninstallKeyboardActions() { SwingUtilities.replaceUIActionMap(menuItem, null); if (windowInputMap != null) { SwingUtilities.replaceUIInputMap(menuItem, JComponent. WHEN_IN_FOCUSED_WINDOW, null); windowInputMap = null; } } protected MouseInputListener createMouseInputListener(JComponent c) { return new MouseInputHandler(); } protected MenuDragMouseListener createMenuDragMouseListener(JComponent c) { return new MenuDragMouseHandler(); } protected MenuKeyListener createMenuKeyListener(JComponent c) { return new MenuKeyHandler(); } private PropertyChangeListener createPropertyChangeListener(JComponent c) { return new PropertyChangeHandler(); } public SynthContext getContext(JComponent c) { return getContext(c, getComponentState(c)); } SynthContext getContext(JComponent c, int state) { return SynthContext.getContext(SynthContext.class, c, SynthLookAndFeel.getRegion(c), style, state); } public SynthContext getContext(JComponent c, Region region) { return getContext(c, region, getComponentState(c, region)); } private SynthContext getContext(JComponent c, Region region, int state) { return SynthContext.getContext(SynthContext.class, c, region, accStyle, state); } private Region getRegion(JComponent c) { return SynthLookAndFeel.getRegion(c); } private int getComponentState(JComponent c) { int state; if (!c.isEnabled()) { return DISABLED; } if (menuItem.isArmed()) { state = MOUSE_OVER; } else { state = SynthLookAndFeel.getComponentState(c); } if (menuItem.isSelected()) { state |= SELECTED; } return state; } private int getComponentState(JComponent c, Region region) { return getComponentState(c); } void registerActionMap() { LazyActionMap.installLazyActionMap(menuItem, SynthMenuItemUI.class, "MenuItem.actionMap"); } InputMap createInputMap(int condition) { if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) { return new ComponentInputMapUIResource(menuItem); } return null; } void updateAcceleratorBinding() { KeyStroke accelerator = menuItem.getAccelerator(); if (windowInputMap != null) { windowInputMap.clear(); } if (accelerator != null) { if (windowInputMap == null) { windowInputMap = createInputMap(JComponent. WHEN_IN_FOCUSED_WINDOW); SwingUtilities.replaceUIInputMap(menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW, windowInputMap); } windowInputMap.put(accelerator, "doClick"); } } public Dimension getMinimumSize(JComponent c) { Dimension d = null; View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { d = getPreferredSize(c); d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS); } return d; } public Dimension getPreferredSize(JComponent c) { return getPreferredMenuItemSize(c, checkIcon, arrowIcon, defaultTextIconGap); } public Dimension getMaximumSize(JComponent c) { Dimension d = null; View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { d = getPreferredSize(c); d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS); } return d; } // these rects are used for painting and preferredsize calculations. // they used to be regenerated constantly. Now they are reused. static Rectangle iconRect = new Rectangle(); static Rectangle textRect = new Rectangle(); static Rectangle acceleratorRect = new Rectangle(); static Rectangle checkIconRect = new Rectangle(); static Rectangle arrowIconRect = new Rectangle(); static Rectangle viewRect = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE); static Rectangle r = new Rectangle(); private void resetRects() { iconRect.setBounds(0, 0, 0, 0); textRect.setBounds(0, 0, 0, 0); acceleratorRect.setBounds(0, 0, 0, 0); checkIconRect.setBounds(0, 0, 0, 0); arrowIconRect.setBounds(0, 0, 0, 0); viewRect.setBounds(0,0,Short.MAX_VALUE, Short.MAX_VALUE); r.setBounds(0, 0, 0, 0); } protected Dimension getPreferredMenuItemSize(JComponent c,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?