Ejemplo n.º 1
0
 protected Dimension getPreferredMenuItemSize(
     JComponent comp, Icon checkIcon, Icon arrowIcon, int defaultTextIconGap) {
   JMenu jMenu = (JMenu) comp;
   Icon icon1 = getIcon();
   Icon icon2 = getAllowedIcon();
   String text = jMenu.getText();
   Font font = jMenu.getFont();
   FontMetrics fontmetrics = jMenu.getToolkit().getFontMetrics(font);
   resetRects();
   layoutMenuItem(
       fontmetrics,
       text,
       icon1,
       icon2,
       arrowIcon,
       jMenu.getVerticalAlignment(),
       jMenu.getHorizontalAlignment(),
       jMenu.getVerticalTextPosition(),
       jMenu.getHorizontalTextPosition(),
       ourViewRect,
       ourIconRect,
       ourTextRect,
       ourAcceleratorRect,
       ourCheckIconRect,
       ourArrowIconRect,
       text != null ? defaultTextIconGap : 0,
       defaultTextIconGap);
   ourPreferredSizeRect.setBounds(ourTextRect);
   ourPreferredSizeRect =
       SwingUtilities.computeUnion(
           ourIconRect.x,
           ourIconRect.y,
           ourIconRect.width,
           ourIconRect.height,
           ourPreferredSizeRect);
   if (useCheckAndArrow()) {
     ourPreferredSizeRect.width += myMaxGutterIconWidth;
     ourPreferredSizeRect.width += defaultTextIconGap;
     ourPreferredSizeRect.width += defaultTextIconGap;
     ourPreferredSizeRect.width += ourArrowIconRect.width;
   }
   ourPreferredSizeRect.width += 2 * defaultTextIconGap;
   Insets insets = jMenu.getInsets();
   if (insets != null) {
     ourPreferredSizeRect.width += insets.left + insets.right;
     ourPreferredSizeRect.height += insets.top + insets.bottom;
   }
   if (ourPreferredSizeRect.width % 2 == 0) {
     ourPreferredSizeRect.width++;
   }
   if (ourPreferredSizeRect.height % 2 == 0) {
     ourPreferredSizeRect.height++;
   }
   return ourPreferredSizeRect.getSize();
 }
Ejemplo n.º 2
0
 public void paint(Graphics g, JComponent comp) {
   UIUtil.applyRenderingHints(g);
   JMenu jMenu = (JMenu) comp;
   ButtonModel buttonmodel = jMenu.getModel();
   int mnemonicIndex = jMenu.getDisplayedMnemonicIndex();
   Icon icon = getIcon();
   Icon allowedIcon = getAllowedIcon();
   Insets insets = comp.getInsets();
   resetRects();
   ourViewRect.setBounds(0, 0, jMenu.getWidth(), jMenu.getHeight());
   ourViewRect.x += insets.left;
   ourViewRect.y += insets.top;
   ourViewRect.width -= insets.right + ourViewRect.x;
   ourViewRect.height -= insets.bottom + ourViewRect.y;
   Font font = g.getFont();
   Font font1 = comp.getFont();
   g.setFont(font1);
   FontMetrics fontmetrics = g.getFontMetrics(font1);
   String s1 =
       layoutMenuItem(
           fontmetrics,
           jMenu.getText(),
           icon,
           allowedIcon,
           arrowIcon,
           jMenu.getVerticalAlignment(),
           jMenu.getHorizontalAlignment(),
           jMenu.getVerticalTextPosition(),
           jMenu.getHorizontalTextPosition(),
           ourViewRect,
           ourIconRect,
           ourTextRect,
           ourAcceleratorRect,
           ourCheckIconRect,
           ourArrowIconRect,
           jMenu.getText() != null ? defaultTextIconGap : 0,
           defaultTextIconGap);
   Color color2 = g.getColor();
   if (comp.isOpaque()) {
     g.setColor(jMenu.getBackground());
     g.fillRect(0, 0, jMenu.getWidth(), jMenu.getHeight());
     if (buttonmodel.isArmed() || buttonmodel.isSelected()) {
       if (UIUtil.isUnderAquaLookAndFeel()) {
         myAquaSelectedBackgroundPainter.paintBorder(
             comp, g, 0, 0, jMenu.getWidth(), jMenu.getHeight());
       } else {
         g.setColor(selectionBackground);
         if (allowedIcon != null) {
           g.fillRect(k, 0, jMenu.getWidth() - k, jMenu.getHeight());
         } else {
           g.fillRect(0, 0, jMenu.getWidth(), jMenu.getHeight());
           g.setColor(selectionBackground);
         }
       }
     }
     g.setColor(color2);
   }
   if (allowedIcon != null) {
     if (buttonmodel.isArmed() || buttonmodel.isSelected()) {
       g.setColor(selectionForeground);
     } else {
       g.setColor(jMenu.getForeground());
     }
     if (useCheckAndArrow()) {
       allowedIcon.paintIcon(comp, g, ourCheckIconRect.x, ourCheckIconRect.y);
     }
     g.setColor(color2);
     if (menuItem.isArmed()) {
       drawIconBorder(g);
     }
   }
   if (icon != null) {
     if (!buttonmodel.isEnabled()) {
       icon = jMenu.getDisabledIcon();
     } else if (buttonmodel.isPressed() && buttonmodel.isArmed()) {
       icon = jMenu.getPressedIcon();
       if (icon == null) {
         icon = jMenu.getIcon();
       }
     }
     if (icon != null) {
       icon.paintIcon(comp, g, ourIconRect.x, ourIconRect.y);
     }
   }
   if (s1 != null && s1.length() > 0) {
     if (buttonmodel.isEnabled()) {
       if (buttonmodel.isArmed() || buttonmodel.isSelected()) {
         g.setColor(selectionForeground);
       } else {
         g.setColor(jMenu.getForeground());
       }
       BasicGraphicsUtils.drawStringUnderlineCharAt(
           g, s1, mnemonicIndex, ourTextRect.x, ourTextRect.y + fontmetrics.getAscent());
     } else {
       final Object disabledForeground = UIUtil.getMenuItemDisabledForeground();
       if (disabledForeground instanceof Color) {
         g.setColor((Color) disabledForeground);
         BasicGraphicsUtils.drawStringUnderlineCharAt(
             g, s1, mnemonicIndex, ourTextRect.x, ourTextRect.y + fontmetrics.getAscent());
       } else {
         g.setColor(jMenu.getBackground().brighter());
         BasicGraphicsUtils.drawStringUnderlineCharAt(
             g, s1, mnemonicIndex, ourTextRect.x, ourTextRect.y + fontmetrics.getAscent());
         g.setColor(jMenu.getBackground().darker());
         BasicGraphicsUtils.drawStringUnderlineCharAt(
             g,
             s1,
             mnemonicIndex,
             ourTextRect.x - 1,
             (ourTextRect.y + fontmetrics.getAscent()) - 1);
       }
     }
   }
   if (arrowIcon != null) {
     if (buttonmodel.isArmed() || buttonmodel.isSelected()) {
       g.setColor(selectionForeground);
     }
     if (useCheckAndArrow()) {
       try {
         if (SystemInfo.isMac
             && myAquaInvertedArrowIcon != null
             && (buttonmodel.isArmed() || buttonmodel.isSelected())
             && UIUtil.isUnderAquaLookAndFeel()) {
           myAquaInvertedArrowIcon.paintIcon(comp, g, ourArrowIconRect.x, ourArrowIconRect.y);
         } else if (SystemInfo.isMac
             && myAquaDisabledArrowIcon != null
             && !buttonmodel.isEnabled()
             && UIUtil.isUnderAquaLookAndFeel()) {
           myAquaDisabledArrowIcon.paintIcon(comp, g, ourArrowIconRect.x, ourArrowIconRect.y);
         } else arrowIcon.paintIcon(comp, g, ourArrowIconRect.x, ourArrowIconRect.y);
       } catch (NullPointerException npe) {
         // GTKIconFactory$MenuArrowIcon.paintIcon since it doesn't expect to be given a null
         // instead of SynthContext
         // http://www.jetbrains.net/jira/browse/IDEADEV-22360
       }
     }
   }
   g.setColor(color2);
   g.setFont(font);
 }