synthmenuitemui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,066 行 · 第 1/3 页
JAVA
1,066 行
Icon checkIcon, Icon arrowIcon, int defaultTextIconGap) { 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 += "-"; acceleratorText += acceleratorDelimiter; } int keyCode = accelerator.getKeyCode(); if (keyCode != 0) { acceleratorText += KeyEvent.getKeyText(keyCode); } else { acceleratorText += accelerator.getKeyChar(); } } SynthContext context = getContext(c); SynthContext accContext = getContext(c, Region.MENU_ITEM_ACCELERATOR); Font font = context.getStyle().getFont(context); FontMetrics fm = b.getToolkit().getFontMetrics(font); FontMetrics fmAccel = b.getToolkit().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 ); // 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 = menuItem.getParent(); if (parent instanceof JPopupMenu) { JPopupMenu popup = (JPopupMenu)parent; ComponentUI ui = popup.getUI(); if (ui instanceof SynthPopupMenuUI) { SynthPopupMenuUI popupUI = (SynthPopupMenuUI)ui; r.width = popupUI.adjustTextWidth(r.width); popupUI.adjustAcceleratorWidth(acceleratorRect.width); r.width += popupUI.getMaxAcceleratorWidth(); } else { // We don't have a SynthPopupMenuUI, fallback to storing the // values in the client properties of the JPopupMenu. //Get widest text so far from parent, if no one exists null is returned. Integer maxTextWidth = (Integer)popup.getClientProperty( MAX_TEXT_WIDTH); Integer maxAccWidth = (Integer)popup. getClientProperty(MAX_ACC_WIDTH); int maxTextValue = maxTextWidth!=null ? maxTextWidth.intValue() : 0; int maxAccValue = maxAccWidth!=null ? maxAccWidth.intValue() : 0; // Compare the text widths, and adjust the r.width to the // widest. if (r.width < maxTextValue) { r.width = maxTextValue; } else { popup.putClientProperty(MAX_TEXT_WIDTH, new Integer(r.width)); } // Compare the accelarator widths. if (acceleratorRect.width > maxAccValue) { maxAccValue = acceleratorRect.width; popup.putClientProperty(MAX_ACC_WIDTH, new Integer( acceleratorRect.width)); } //Add on the widest accelerator r.width += maxAccValue; } } 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++; }/* 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"); }*/ context.dispose(); accContext.dispose(); return r.getSize(); } public void update(Graphics g, JComponent c) { SynthContext context = getContext(c); SynthLookAndFeel.update(context, g); paint(context, g); context.dispose(); } public void paint(Graphics g, JComponent c) { SynthContext context = getContext(c); paint(context, g); context.dispose(); } protected void paint(SynthContext context, Graphics g) { 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); SynthContext accContext = getContext(menuItem, Region.MENU_ITEM_ACCELERATOR); SynthStyle style = context.getStyle(); Font f = style.getFont(context); g.setFont(f); FontMetrics fm = g.getFontMetrics(f); FontMetrics accFM = g.getFontMetrics(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 += "-"; acceleratorText += acceleratorDelimiter; } int keyCode = accelerator.getKeyCode(); if (keyCode != 0) { acceleratorText += KeyEvent.getKeyText(keyCode); } else { acceleratorText += accelerator.getKeyChar(); } } // layout 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 ); // 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.getSynthGraphics(context).paintText(context, g, text, textRect.x, textRect.y, menuItem. 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 = menuItem.getParent(); if (parent != null && parent instanceof JPopupMenu) { ComponentUI ui = ((JPopupMenu)parent).getUI(); if (ui instanceof SynthPopupMenuUI) { SynthPopupMenuUI popupUI = (SynthPopupMenuUI)ui; // Calculate the offset, with which the accelerator texts // will be drawn with. int max = popupUI.getMaxAcceleratorWidth(); if (max > 0) { accOffset = max - acceleratorRect.width; } } else { // We don't have a SynthPopupMenuUI, fallback to using the // values in the client properties of the JPopupMenu. Integer maxValueInt = (Integer)((JComponent)parent). getClientProperty(MAX_ACC_WIDTH); int maxValue = maxValueInt != null ? maxValueInt.intValue() : acceleratorRect.width; // Calculate the offset, with which the accelerator texts // will be drawn with. accOffset = maxValue - acceleratorRect.width; } } SynthStyle accStyle = accContext.getStyle(); g.setColor(accStyle.getColor(accContext, ColorType.TEXT_FOREGROUND)); g.setFont(accStyle.getFont(accContext)); accStyle.getSynthGraphics(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); } accContext.dispose(); } /** * 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( 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 ) { context.getStyle().getSynthGraphics(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 = ""; } else { SynthStyle style = accContext.getStyle(); acceleratorRect.width = style.getSynthGraphics(accContext). computeStringWidth(accContext, fmAccel.getFont(), fmAccel, acceleratorText);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?