📄 xpmenuitemui.java
字号:
//Get widest text so far from parent, if no one exists null is returned. Integer maxTextWidth = (Integer) p.getClientProperty(MAX_TEXT_WIDTH); Integer maxAccWidth = (Integer) p.getClientProperty(MAX_ACC_WIDTH); Integer maxIconWidth = (Integer) p.getClientProperty(MAX_ICON_WIDTH); // WAS ME int maxTextValue = maxTextWidth != null ? maxTextWidth.intValue() : 0; int maxAccValue = maxAccWidth != null ? maxAccWidth.intValue() : 0; int maxIconValue = maxIconWidth != null ? maxIconWidth.intValue() : 0; //Compare the text widths, and adjust the r.width to the widest. if (r.width < maxTextValue) { r.width = maxTextValue; } else { p.putClientProperty(XPMenuItemUI.MAX_TEXT_WIDTH, new Integer(r.width)); } //Compare the accelarator widths. if (acceleratorRect.width > maxAccValue) { maxAccValue = acceleratorRect.width; p.putClientProperty(XPMenuItemUI.MAX_ACC_WIDTH, new Integer(acceleratorRect.width)); } //Compare the accelarator widths. if (icon != null && icon.getIconWidth() > maxIconValue) { maxIconValue = icon.getIconWidth(); p.putClientProperty(XPMenuItemUI.MAX_ICON_WIDTH, new Integer(maxIconValue)); } //Add on the widest accelerator r.width += maxAccValue; r.width += defaultTextIconGap; } 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(); } 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(); Icon bicon = b.getIcon(); JComponent p = (JComponent) b.getParent(); Integer maxValueInt = (Integer) p.getClientProperty(XPMenuItemUI.MAX_ICON_WIDTH); int maxValue = maxValueInt == null ? 16 : maxValueInt.intValue(); int menuWidth = b.getWidth(); int menuHeight = b.getHeight(); Insets i = c.getInsets(); resetRects(); viewRect.setBounds(0, 0, menuWidth, menuHeight); viewRect.x += 0; 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 = g.getFontMetrics(f); FontMetrics fmAccel = g.getFontMetrics(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(); } } int offset = 0; // layout the text and icon Icon ic = b.getIcon(); Icon iCheck = checkIcon; Icon paintIcon = ic; if (useCheckAndArrow()) { if (c instanceof JCheckBoxMenuItem || c instanceof JRadioButtonMenuItem) { ic = checkIcon; if (checkIcon.getIconWidth() < maxValue) { ic = new EmptyIcon(maxValue, checkIcon.getIconHeight()); offset = (maxValue - checkIcon.getIconWidth()) / 2; } paintIcon = null; } else if (c instanceof JMenuItem) { if (ic == null || ic.getIconWidth() < maxValue) { int height = (ic == null) ? 2 : b.getIcon().getIconHeight(); int width = (ic == null) ? 2 : b.getIcon().getIconWidth(); offset = (maxValue - width) / 2; ic = new EmptyIcon(maxValue, height); } } } String text = layoutMenuItem(fm, b.getText(), fmAccel, acceleratorText, ic, null /*iCheck*/ , arrowIcon, b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, acceleratorRect, checkIconRect, arrowIconRect, b.getText() == null ? 0 : defaultTextIconGap, defaultIconGap); // 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, iconRect.x + offset, iconRect.y); } g.setColor(holdc); } // Paint the Icon if (paintIcon != null) { Icon icon; if (!model.isEnabled()) { icon = (Icon) b.getDisabledIcon(); if (icon != null) icon.paintIcon(c, g, iconRect.x + offset, iconRect.y); } else if (model.isPressed() && model.isArmed()) { icon = (Icon) b.getPressedIcon(); if (icon == null) { // Use default icon icon = (Icon) b.getIcon(); } if (icon != null) icon.paintIcon(c, g, iconRect.x + offset, iconRect.y); } else if (model.isArmed() || model.isSelected()) { icon = (Icon) b.getIcon(); if (icon != null) { Icon disabled = b.getDisabledIcon(); if (disabled != null) disabled.paintIcon(c, g, iconRect.x + offset+1, iconRect.y+1); icon.paintIcon(c, g, iconRect.x + offset - 1, iconRect.y - 1); } } else { icon = (Icon) b.getIcon(); if (icon != null) icon.paintIcon(c, g, iconRect.x + offset, iconRect.y); } } // Draw the Text if (text != null) { View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { v.paint(g, textRect); } else { paintText(g, b, textRect, text); } } // Draw the Accelerator Text if (acceleratorText != null && !acceleratorText.equals("")) { //Get the maxAccWidth from the parent to calculate the offset. int accOffset = 0; Container parent = menuItem.getParent(); if (parent != null && parent instanceof JComponent) { Integer amaxValueInt = (Integer) p.getClientProperty(XPMenuItemUI.MAX_ACC_WIDTH); int amaxValue = amaxValueInt != null ? amaxValueInt.intValue() : acceleratorRect.width; //Calculate the offset, with which the accelerator texts will be drawn with. accOffset = amaxValue - acceleratorRect.width; } g.setFont(acceleratorFont); if (!model.isEnabled()) { // *** paint the acceleratorText disabled if (disabledForeground != null) { g.setColor(disabledForeground); BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x - accOffset, acceleratorRect.y + fmAccel.getAscent()); } else { g.setColor(b.getBackground().brighter()); BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x - accOffset, acceleratorRect.y + fmAccel.getAscent()); g.setColor(b.getBackground().darker()); BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x - accOffset - 1, acceleratorRect.y + fmAccel.getAscent() - 1); } } else { // *** paint the acceleratorText normally if (model.isArmed() || (c instanceof JMenu && model.isSelected())) { g.setColor(acceleratorSelectionForeground); } else { g.setColor(acceleratorForeground); } BasicGraphicsUtils.drawString(g, acceleratorText, 0, acceleratorRect.x - accOffset, acceleratorRect.y + fmAccel.getAscent()); } } // Paint the Arrow if (arrowIcon != null) { if (model.isArmed() || (c instanceof JMenu && model.isSelected())) g.setColor(foreground); if (useCheckAndArrow()) {// offset=(maxValue - arrowIconRect.width); arrowIcon.paintIcon(c, g, arrowIconRect.x, arrowIconRect.y); } } g.setColor(holdc); g.setFont(holdf); } /** * Draws the background of the menu item. * * @param g the paint graphics * @param menuItem menu item to be painted * @param bgColor selection background color * @since 1.4 */ protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) { ButtonModel model = menuItem.getModel(); Color oldColor = g.getColor(); int menuWidth = menuItem.getWidth(); int menuHeight = menuItem.getHeight(); boolean mouseOver=model.isArmed() || (menuItem instanceof JMenu && model.isSelected()); int maxValue=menuWidth; if (useCheckAndArrow()) { JComponent p = (JComponent) menuItem.getParent(); Integer maxValueInt = (Integer) p.getClientProperty(XPMenuItemUI.MAX_ICON_WIDTH); maxValue = (maxValueInt == null) ? 16 : maxValueInt.intValue(); maxValue += defaultTextIconGap; skin.draw(g,model.isEnabled(),mouseOver,false,false,menuWidth,maxValue,menuHeight); } else { boolean isRollover=(menuItem.getClientProperty("rollover")==Boolean.TRUE) ? true : false; int index=2; if (!model.isEnabled()) index=2; else if (model.isSelected()) index=1; else if (isRollover) index=0; topSkin.draw(g,index,menuWidth,menuHeight); } } /** * 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 String layoutMenuItem(FontMetrics fm, 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) { SwingUtilities.layoutCompoundLabel(menuItem, fm, text, icon, verticalAlignment, horizontalAlignment, verticalTextPosition, horizontalTextPosition, 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 = ""; } else { acceleratorRect.width = SwingUtilities.computeStringWidth(fmAccel, acceleratorText); acceleratorRect.height = fmAccel.getHeight(); } /* Initialize the checkIcon bounds rectangle's width & height. */ if (useCheckAndArrow()) { if (checkIcon != null) { checkIconRect.height = checkIcon.getIconHeight(); checkIconRect.width = checkIcon.getIconWidth(); } else { checkIconRect.width = checkIconRect.height = 0; } /* Initialize the arrowIcon bounds rectangle width & height. */ if (arrowIcon != null) { arrowIconRect.width = arrowIcon.getIconWidth(); arrowIconRect.height = arrowIcon.getIconHeight(); } else { arrowIconRect.width = arrowIconRect.height = 0; } } else { checkIconRect.width = checkIconRect.height = 0; arrowIconRect.width = arrowIconRect.height = 0; } Rectangle labelRect = iconRect.union(textRect); if (true) { //useCheckAndArrow()) { //BasicGraphicsUtils.isLeftToRight(menuItem) ) { // FIX ME if (checkIcon != null) { checkIconRect.x += menuItemGap; } else { textRect.x += menuItemGap; iconRect.x += menuItemGap; } // Position the Accelerator text rect acceleratorRect.x = viewRect.x + viewRect.width - arrowIconRect.width - menuItemGap - acceleratorRect.width; } // Align the accelertor text and the check and arrow icons vertically // with the center of the label rect. acceleratorRect.y = labelRect.y + (labelRect.height / 2) - (acceleratorRect.height / 2); if (useCheckAndArrow()) { arrowIconRect.y = labelRect.y + (labelRect.height / 2) - (arrowIconRect.height / 2); checkIconRect.y = labelRect.y + (labelRect.height / 2) - (checkIconRect.height / 2); arrowIconRect.x = viewRect.x + viewRect.width - menuItemGap - arrowIconRect.width; } // iconRect.y = labelRect.y + 10; //(labelRect.height/2) - (iconRect.height/2); return text; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -