📄 basicmenuitemui.java
字号:
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 zeroRect = new Rectangle(0,0,0,0); 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(zeroRect); textRect.setBounds(zeroRect); acceleratorRect.setBounds(zeroRect); checkIconRect.setBounds(zeroRect); arrowIconRect.setBounds(zeroRect); viewRect.setBounds(0,0,Short.MAX_VALUE, Short.MAX_VALUE); r.setBounds(zeroRect); } // Returns parent of this component if it is not a top-level menu // Otherwise returns null private JComponent getMenuItemParent(JMenuItem mi) { Container parent = mi.getParent(); if ((parent instanceof JComponent) && (!(mi instanceof JMenu) || !((JMenu)mi).isTopLevelMenu())) { return (JComponent) parent; } else { return null; } } protected Dimension getPreferredMenuItemSize(JComponent c, Icon checkIcon, Icon arrowIcon, int defaultTextIconGap) { JMenuItem b = (JMenuItem) c; Icon icon = null; /* * in case .checkIconFactory is defined for this UI and the icon is * compatible with it then the icon is handled by the checkIcon. */ MenuItemCheckIconFactory iconFactory = (MenuItemCheckIconFactory) UIManager.get(getPropertyPrefix() + ".checkIconFactory"); if (iconFactory == null || ! iconFactory.isCompatible(checkIcon, getPropertyPrefix())) { 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 += "-"; acceleratorText += acceleratorDelimiter; } int keyCode = accelerator.getKeyCode(); if (keyCode != 0) { acceleratorText += KeyEvent.getKeyText(keyCode); } else { acceleratorText += accelerator.getKeyChar(); } } Font font = b.getFont(); FontMetrics fm = b.getFontMetrics(font); FontMetrics fmAccel = b.getFontMetrics( acceleratorFont ); resetRects(); layoutMenuItem( fm, 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 ); // labelRect contains size of text and label Rectangle labelRect = iconRect.union(textRect); // Find the result height r.height = max(labelRect.height, checkIconRect.height, arrowIconRect.height, acceleratorRect.height); // Find the result width // Add the leading gap, // the closing one will be added by the latest addMaxWidth() call r.width = defaultTextIconGap; // To determine the width of the parent popup menu (through DefaultMenuLayout) // and to make accelerator texts appear in a column, // find the widest icon, text, check icon, arrow icon and accelerator text. // For the latest menu item we will know them exactly. // It will be the widest menu item and it will determine // the width of the parent popup menu. JComponent p = getMenuItemParent(menuItem); addMaxWidth(p, BasicMenuItemUI.MAX_ICON_WIDTH, iconRect.width, defaultTextIconGap, false); addMaxWidth(p, BasicMenuItemUI.MAX_TEXT_WIDTH, textRect.width, defaultTextIconGap, false); addMaxWidth(p, BasicMenuItemUI.MAX_ACC_WIDTH, acceleratorRect.width, defaultTextIconGap, false); if( useCheckAndArrow() ) { // Force gap for the check icon to avoid absence of the gap between // the text and arrow icon because of the layoutMenuItem() features addMaxWidth(p, BasicMenuItemUI.MAX_CHECK_ICON_WIDTH, checkIconRect.width, defaultTextIconGap, true); addMaxWidth(p, BasicMenuItemUI.MAX_ARROW_ICON_WIDTH, arrowIconRect.width, defaultTextIconGap, false); } 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 && Boolean.TRUE != UIManager.get(getPropertyPrefix() + ".evenHeight")) { r.height++; }/* if(!(b instanceof JMenu && ((JMenu) b).isTopLevelMenu()) ) { // Container parent = menuItem.getParent(); JComponent p = (JComponent) parent; System.out.println("MaxText: "+p.getClientProperty(BasicMenuItemUI.MAX_TEXT_WIDTH)); System.out.println("MaxACC"+p.getClientProperty(BasicMenuItemUI.MAX_ACC_WIDTH)); System.out.println("returning pref.width: " + r.width); System.out.println("Current getSize: " + b.getSize() + "\n"); }*/ return r.getSize(); } private int max(int... values) { int maxValue = Integer.MIN_VALUE; for (int i : values) { if (i > maxValue) { maxValue = i; } } return maxValue; } // Calculates maximal width through specified parent component client property // and adds it to the width of rectangle r private void addMaxWidth(JComponent parent, String propertyName, int curWidth, int defaultTextIconGap, boolean forceGap) { // Get maximal width from parent client property Integer maxWidth = null; if (parent != null) { maxWidth = (Integer) parent.getClientProperty(propertyName); } if (maxWidth == null) { maxWidth = 0; } // Store new maximal width in parent client property if (curWidth > maxWidth) { maxWidth = curWidth; if (parent != null) { parent.putClientProperty(propertyName, maxWidth); } } // Add calculated maximal width and gap if (maxWidth > 0) { r.width += defaultTextIconGap; r.width += maxWidth; } else { if (forceGap) { r.width += defaultTextIconGap; } } } /** * We draw the background in paintMenuItem() * so override update (which fills the background of opaque * components by default) to just call paint(). * */ public void update(Graphics g, JComponent c) { paint(g, c); } public void paint(Graphics g, JComponent c) { paintMenuItem(g, c, checkIcon, arrowIcon, selectionBackground, selectionForeground, defaultTextIconGap); } protected void paintMenuItem(Graphics g, JComponent c, Icon checkIcon, Icon arrowIcon, Color background, Color foreground, int defaultTextIconGap) { JMenuItem b = (JMenuItem) c; ButtonModel model = b.getModel(); // Dimension size = b.getSize(); int menuWidth = b.getWidth(); int menuHeight = b.getHeight(); Insets i = c.getInsets(); resetRects(); viewRect.setBounds( 0, 0, menuWidth, menuHeight ); viewRect.x += i.left; viewRect.y += i.top; viewRect.width -= (i.right + viewRect.x); viewRect.height -= (i.bottom + viewRect.y); Font holdf = g.getFont(); Font f = c.getFont(); g.setFont( f ); FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f); FontMetrics fmAccel = SwingUtilities2.getFontMetrics( c, g, acceleratorFont); // get Accelerator text KeyStroke accelerator = b.getAccelerator(); String acceleratorText = ""; if (accelerator != null) { int modifiers = accelerator.getModifiers(); if (modifiers > 0) { acceleratorText = KeyEvent.getKeyModifiersText(modifiers); //acceleratorText += "-"; acceleratorText += acceleratorDelimiter; } int keyCode = accelerator.getKeyCode(); if (keyCode != 0) { acceleratorText += KeyEvent.getKeyText(keyCode); } else { acceleratorText += accelerator.getKeyChar(); } } Icon icon = null; /* * in case .checkIconFactory is defined for this UI and the icon is * compatible with it then the icon is handled by the checkIcon. */ MenuItemCheckIconFactory iconFactory = (MenuItemCheckIconFactory) UIManager.get(getPropertyPrefix() + ".checkIconFactory"); if (iconFactory == null || ! iconFactory.isCompatible(checkIcon, getPropertyPrefix())) { icon = b.getIcon(); } // layout the text and icon String text = layoutMenuItem( fm, b.getText(), fmAccel, acceleratorText, icon, checkIcon, arrowIcon, b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, acceleratorRect, checkIconRect, arrowIconRect, b.getText() == null ? 0 : defaultTextIconGap, defaultTextIconGap ); // Paint background paintBackground(g, b, background); Color holdc = g.getColor(); // Paint the Check if (checkIcon != null) { if(model.isArmed() || (c instanceof JMenu && model.isSelected())) { g.setColor(foreground); } else { g.setColor(holdc); } if( useCheckAndArrow() ) checkIcon.paintIcon(c, g, checkIconRect.x, checkIconRect.y);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -