public void testCreatesTheItemShopMenu() {
    ItemShop building = new ItemShop();
    building.stockItems(Item.BLOCKABALL, Item.HEALVIAL);
    JMenu menu = _constructor.createBuildingMenu(building, null);

    Assert.assertNotNull(menu);
    Assert.assertEquals(building.getName(), menu.getName());
    Assert.assertEquals(building.getName(), menu.getText());
    Assert.assertEquals(2, menu.getItemCount());
    for (int i = 0; i < menu.getItemCount(); i++) {
      JMenuItem menuItem = menu.getItem(i);
      Action action = menuItem.getAction();
      Assert.assertNotNull(action);
      Assert.assertEquals(ItemAction.class, action.getClass());

      String name = menuItem.getName();
      if (name.equals(Item.BLOCKABALL.toString())) {
        Assert.assertEquals(Item.BLOCKABALL.getWellFormattedString(), menuItem.getText());
      } else if (name.equals(Item.HEALVIAL.toString())) {
        Assert.assertEquals(Item.HEALVIAL.getWellFormattedString(), menuItem.getText());
      } else {
        Assert.fail("Name: " + name + " Text: " + menuItem.getText());
      }
    }
  }
 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();
 }
Exemple #3
0
 public static void addFastKeys(JMenuBar menuBar) {
   for (Component menuComponent : menuBar.getComponents()) { // iterate over menus
     JMenu menu = (JMenu) menuComponent;
     menu.setMnemonic(menu.getText().charAt(menu.getText().indexOf('&') + 1));
     menu.setText(menu.getText().replace("&", ""));
     for (Component menuItemComponent : menu.getMenuComponents())
       if (menuItemComponent instanceof JMenuItem) { // skip separators
         JMenuItem menuItem = (JMenuItem) menuItemComponent;
         menuItem.setMnemonic(menuItem.getText().charAt(menuItem.getText().indexOf('&') + 1));
         menuItem.setText(menuItem.getText().replace("&", ""));
       }
     /*
     for (int count = 0; count < menu.getMenuComponentCount(); count++) {  //iterate over menu items
        Component menuItemComponent = menu.getMenuComponent(count);
        if (menuItemComponent instanceof JMenuItem) {   //skip separators
           JMenuItem menuItem = (JMenuItem)menuItemComponent;
           menuItem.setMnemonic(menuItem.getText().charAt(
                 menuItem.getText().indexOf('&') + 1));
           menuItem.setText(menuItem.getText().replace("&", ""));
           }
        }
     */
   }
 }
  public void testCreatesTheHealingCenterMenu() {
    Building building = new HealingCenter();
    JMenu menu = _constructor.createBuildingMenu(building, null);

    Assert.assertNotNull(menu);
    Assert.assertEquals(building.getName(), menu.getName());
    Assert.assertEquals(building.getName(), menu.getText());
    Assert.assertEquals(1, menu.getItemCount());
    for (int i = 0; i < menu.getItemCount(); i++) {
      JMenuItem menuItem = menu.getItem(i);
      Action action = menuItem.getAction();
      Assert.assertNotNull(action);
      Assert.assertEquals(HealAction.class, action.getClass());

      Assert.assertEquals("Heal", menuItem.getName());
      Assert.assertEquals("Heal", menuItem.getText());
    }
  }
 private void assertNameAndText(JMenu menu, String name, String text) {
   Assert.assertEquals("Name of the Menu should be " + name, name, menu.getName());
   Assert.assertEquals("Text of the Menu should be " + text, text, menu.getText());
 }
 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);
 }