?? basicmenuitemui.java
字號:
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 ); // find the union of the icon and text rects r.setBounds(textRect); r = SwingUtilities.computeUnion(iconRect.x, iconRect.y, iconRect.width, iconRect.height, r); // Find the result height r.height = Math.max( Math.max(r.height, checkIconRect.height), Math.max(arrowIconRect.height, acceleratorRect.height)); // r = iconRect.union(textRect); // 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(); //Check the parent, and see that it is not a top-level menu. if (parent != null && parent instanceof JComponent && !(menuItem instanceof JMenu && ((JMenu) menuItem).isTopLevelMenu())) { JComponent p = (JComponent) parent; //Get widest text so far from parent, if no one exists null is returned. Integer maxTextWidth = (Integer) p.getClientProperty(BasicMenuItemUI.MAX_TEXT_WIDTH); Integer maxAccWidth = (Integer) p.getClientProperty(BasicMenuItemUI.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 { p.putClientProperty(BasicMenuItemUI.MAX_TEXT_WIDTH, new Integer(r.width) ); } //Compare the accelarator widths. if (acceleratorRect.width > maxAccValue) { maxAccValue = acceleratorRect.width; p.putClientProperty(BasicMenuItemUI.MAX_ACC_WIDTH, new Integer(acceleratorRect.width) ); } //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 && 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(); } /** * 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); g.setColor(holdc); } // Paint the Icon if(icon != null ) { 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) icon.paintIcon(c, g, iconRect.x, 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) { JComponent p = (JComponent) parent; Integer maxValueInt = (Integer) p.getClientProperty(BasicMenuItemUI.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; } g.setFont( acceleratorFont ); if(!model.isEnabled()) { // *** paint the acceleratorText disabled if ( disabledForeground != null ) { g.setColor( disabledForeground ); SwingUtilities2.drawString(b, g,acceleratorText, acceleratorRect.x - accOffset, acceleratorRect.y + fmAccel.getAscent()); } else { g.setColor(b.getBackground().brighter()); SwingUtilities2.drawString(b, g,acceleratorText, acceleratorRect.x - accOffset, acceleratorRect.y + fmAccel.getAscent()); g.setColor(b.getBackground().darker()); SwingUtilities2.drawString(b, g,acceleratorText, 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 ); } SwingUtilities2.drawString(b, g,acceleratorText, 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()) 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(); if(menuItem.isOpaque()) { if (model.isArmed()|| (menuItem instanceof JMenu && model.isSelected())) { g.setColor(bgColor); g.fillRect(0,0, menuWidth, menuHeight); } else { g.setColor(menuItem.getBackground()); g.fillRect(0,0, menuWidth, menuHeight); } g.setColor(oldColor); } else if (model.isArmed() || (menuItem instanceof JMenu && model.isSelected())) { g.setColor(bgColor); g.fillRect(0,0, menuWidth, menuHeight); g.setColor(oldColor); } } /** * Renders the text of the current menu item. * <p> * @param g graphics context * @param menuItem menu item to render * @param textRect bounding rectangle for rendering the text * @param text string to render * @since 1.4 */ protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect, String text) { ButtonModel model = menuItem.getModel(); FontMetrics fm = SwingUtilities2.getFontMetrics(menuItem, g); int mnemIndex = menuItem.getDisplayedMnemonicIndex(); if(!model.isEnabled()) { // *** paint the text disabled if ( UIManager.get("MenuItem.disabledForeground") instanceof Color ) { g.setColor( UIManager.getColor("MenuItem.disabledForeground") ); SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text, mnemIndex, textRect.x, textRect.y + fm.getAscent()); } else { g.setColor(menuItem.getBackground().brighter()); SwingUtilities2.drawStringUnderlineCharAt(menuItem, g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent()); g.setColor(menuItem.getBackground().darker()); SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text, mnemIndex, textRect.x - 1, textRect.y + fm.getAscent() - 1); } } else { // *** paint the text normally if (model.isArmed()|| (menuItem instanceof JMenu && model.isSelected())) { g.setColor(selectionForeground); // Uses protected field. } SwingUtilities2.drawStringUnderlineCharAt(menuItem, g,text, mnemIndex, textRect.x, textRect.y + fm.getAscent()); } } /** * 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,
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -