📄 synthmenuitemui.java
字号:
/* * @(#)SynthMenuItemUI.java 1.25 05/01/25 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.plaf.synth;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.plaf.basic.*;import javax.swing.text.View;import sun.swing.plaf.synth.*;import sun.swing.SwingUtilities2;/** * Synth's MenuItemUI. * * @version 1.25, 01/25/05 * @author Georges Saab * @author David Karlton * @author Arnaud Weber * @author Fredrik Lagerblad */class SynthMenuItemUI extends BasicMenuItemUI implements PropertyChangeListener, SynthUI { private SynthStyle style; private SynthStyle accStyle; private String acceleratorDelimiter; public static ComponentUI createUI(JComponent c) { return new SynthMenuItemUI(); } // // The next handful of static methods are used by both SynthMenuUI // and SynthMenuItemUI. This is necessitated by SynthMenuUI not // extending SynthMenuItemUI. // static Dimension getPreferredMenuItemSize(SynthContext context, SynthContext accContext, boolean useCheckAndArrow, JComponent c, Icon checkIcon, Icon arrowIcon, int defaultTextIconGap, String acceleratorDelimiter) { JMenuItem b = (JMenuItem) c; Icon icon = (Icon) b.getIcon(); String text = b.getText(); KeyStroke accelerator = b.getAccelerator(); String acceleratorText = ""; if (accelerator != null) { int modifiers = accelerator.getModifiers(); if (modifiers > 0) { acceleratorText = KeyEvent.getKeyModifiersText(modifiers); acceleratorText += acceleratorDelimiter; } int keyCode = accelerator.getKeyCode(); if (keyCode != 0) { acceleratorText += KeyEvent.getKeyText(keyCode); } else { acceleratorText += accelerator.getKeyChar(); } } Font font = context.getStyle().getFont(context); FontMetrics fm = b.getFontMetrics(font); FontMetrics fmAccel = b.getFontMetrics(accContext.getStyle(). getFont(accContext)); resetRects(); layoutMenuItem( context, fm, accContext, text, fmAccel, acceleratorText, icon, checkIcon, arrowIcon, b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, acceleratorRect, checkIconRect, arrowIconRect, text == null ? 0 : defaultTextIconGap, defaultTextIconGap, useCheckAndArrow); // find the union of the icon and text rects r.setBounds(textRect); r = SwingUtilities.computeUnion(iconRect.x, iconRect.y, iconRect.width, iconRect.height, r); // To make the accelerator texts appear in a column, // find the widest MenuItem text and the widest accelerator text. // Get the parent, which stores the information. Container parent = b.getParent(); if (parent instanceof JPopupMenu) { SynthPopupMenuUI popupUI = (SynthPopupMenuUI)SynthLookAndFeel. getUIOfType(((JPopupMenu)parent).getUI(), SynthPopupMenuUI.class); if (popupUI != null) { r.width = popupUI.adjustTextWidth(r.width); popupUI.adjustAcceleratorWidth(acceleratorRect.width); r.width += popupUI.getMaxAcceleratorWidth(); } } else if (parent != null && !(b instanceof JMenu && ((JMenu)b).isTopLevelMenu())) { r.width += acceleratorRect.width; } if( useCheckAndArrow ) { // Add in the checkIcon r.width += checkIconRect.width; r.width += defaultTextIconGap; // Add in the arrowIcon r.width += defaultTextIconGap; r.width += arrowIconRect.width; } r.width += 2*defaultTextIconGap; Insets insets = b.getInsets(); if(insets != null) { r.width += insets.left + insets.right; r.height += insets.top + insets.bottom; } // if the width is even, bump it up one. This is critical // for the focus dash line to draw properly if(r.width%2 == 0) { r.width++; } // if the height is even, bump it up one. This is critical // for the text to center properly if(r.height%2 == 0) { r.height++; } return r.getSize(); } static void paint(SynthContext context, SynthContext accContext, Graphics g, Icon checkIcon, Icon arrowIcon, boolean useCheckAndArrow, String acceleratorDelimiter, int defaultTextIconGap) { JComponent c = context.getComponent(); JMenuItem b = (JMenuItem)c; ButtonModel model = b.getModel(); Insets i = b.getInsets(); resetRects(); viewRect.setBounds(0, 0, b.getWidth(), b.getHeight()); viewRect.x += i.left; viewRect.y += i.top; viewRect.width -= (i.right + viewRect.x); viewRect.height -= (i.bottom + viewRect.y); SynthStyle style = context.getStyle(); Font f = style.getFont(context); g.setFont(f); FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f); FontMetrics accFM = SwingUtilities2.getFontMetrics(c, g, accContext.getStyle(). getFont(accContext)); // get Accelerator text KeyStroke accelerator = b.getAccelerator(); String acceleratorText = ""; if (accelerator != null) { int modifiers = accelerator.getModifiers(); if (modifiers > 0) { acceleratorText = KeyEvent.getKeyModifiersText(modifiers); acceleratorText += acceleratorDelimiter; } int keyCode = accelerator.getKeyCode(); if (keyCode != 0) { acceleratorText += KeyEvent.getKeyText(keyCode); } else { acceleratorText += accelerator.getKeyChar(); } } // layoutl the text and icon String text = layoutMenuItem(context, fm, accContext, b.getText(), accFM, acceleratorText, b.getIcon(), checkIcon, arrowIcon, b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, acceleratorRect, checkIconRect, arrowIconRect, b.getText() == null ? 0 : defaultTextIconGap, defaultTextIconGap, useCheckAndArrow ); // Paint the Check if (checkIcon != null && useCheckAndArrow ) { SynthIcon.paintIcon(checkIcon, context, g, checkIconRect.x, checkIconRect.y, checkIconRect.width, checkIconRect.height); } // Paint the Icon if(b.getIcon() != null) { Icon icon; if(!model.isEnabled()) { icon = (Icon) b.getDisabledIcon(); } else if(model.isPressed() && model.isArmed()) { icon = (Icon) b.getPressedIcon(); if(icon == null) { // Use default icon icon = (Icon) b.getIcon(); } } else { icon = (Icon) b.getIcon(); } if (icon!=null) { SynthIcon.paintIcon(icon, context, g, iconRect.x, iconRect.y, iconRect.width, iconRect.height); } } // Draw the Text if(text != null) { View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { v.paint(g, textRect); } else { g.setColor(style.getColor(context, ColorType.TEXT_FOREGROUND)); g.setFont(style.getFont(context)); style.getGraphicsUtils(context).paintText(context, g, text, textRect.x, textRect.y, b.getDisplayedMnemonicIndex()); } } // Draw the Accelerator Text if(acceleratorText != null && !acceleratorText.equals("")) { // Get the maxAccWidth from the parent to calculate the offset. int accOffset = 0; Container parent = b.getParent(); if (parent != null && parent instanceof JPopupMenu) { SynthPopupMenuUI popupUI = (SynthPopupMenuUI) ((JPopupMenu)parent).getUI(); if (popupUI != null) { // Calculate the offset, with which the accelerator texts // will be drawn with. int max = popupUI.getMaxAcceleratorWidth(); if (max > 0) { accOffset = max - acceleratorRect.width; } } } SynthStyle accStyle = accContext.getStyle(); g.setColor(accStyle.getColor(accContext, ColorType.TEXT_FOREGROUND)); g.setFont(accStyle.getFont(accContext)); accStyle.getGraphicsUtils(accContext).paintText( accContext, g, acceleratorText, acceleratorRect.x - accOffset, acceleratorRect.y, -1); } // Paint the Arrow if (arrowIcon != null && useCheckAndArrow) { SynthIcon.paintIcon(arrowIcon, context, g, arrowIconRect.x, arrowIconRect.y, arrowIconRect.width, arrowIconRect.height); } } /** * Compute and return the location of the icons origin, the * location of origin of the text baseline, and a possibly clipped * version of the compound labels string. Locations are computed * relative to the viewRect rectangle. */ private static String layoutMenuItem( SynthContext context, FontMetrics fm, SynthContext accContext, String text, FontMetrics fmAccel, String acceleratorText, Icon icon, Icon checkIcon, Icon arrowIcon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewRect, Rectangle iconRect, Rectangle textRect, Rectangle acceleratorRect, Rectangle checkIconRect, Rectangle arrowIconRect, int textIconGap, int menuItemGap, boolean useCheckAndArrow ) { // If parent is JPopupMenu, get and store it's UI SynthPopupMenuUI popupUI = null; JComponent b = context.getComponent(); Container parent = b.getParent(); if(parent instanceof JPopupMenu) { popupUI = (SynthPopupMenuUI)SynthLookAndFeel. getUIOfType(((JPopupMenu)parent).getUI(), SynthPopupMenuUI.class); } context.getStyle().getGraphicsUtils(context).layoutText( context, fm, text, icon,horizontalAlignment, verticalAlignment, horizontalTextPosition, verticalTextPosition, viewRect, iconRect, textRect, textIconGap); /* Initialize the acceelratorText bounds rectangle textRect. If a null * or and empty String was specified we substitute "" here * and use 0,0,0,0 for acceleratorTextRect. */ if( (acceleratorText == null) || acceleratorText.equals("") ) { acceleratorRect.width = acceleratorRect.height = 0; acceleratorText = ""; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -