MenuCommonFunctions { public static void setMnemonic(Action action) { String actionLabel = (String)action.getValue(Action.NAME); if(actionLabel!=null) { int charPosition=getMnemonicCharPos(actionLabel); if(charPosition>=0) { action.putValue(Action.MNEMONIC_KEY,new Integer(Character.toUpperCase(actionLabel.charAt(charPosition+1)))); action.putValue(Action.NAME,clearMnemonic(charPosition,actionLabel)); } } } private  MenuCommonFunctions(); static void setMnemonic(Action action); static void setMnemonic(AbstractButton actionComponent); static void updateOrInsertMenuItem(JPopupMenu menu, JMenuItem menuItem); static void updateOrInsertMenuItem(JPopupMenu menu, JMenuItem menuItem, boolean hideOnUpdate); static final Character MNEMONIC_CHARACTER; static final String MNEMONIC_STRING; }
@Test public void testSetMnemonicAction() throws Exception { UnitTestAction action = new UnitTestAction("H&ello"); MenuCommonFunctions.setMnemonic(action); assertEquals(KeyEvent.VK_E,action.getMnemonic()); assertEquals("Hello",action.getValue(Action.NAME)); }
@Test public void testSetMnemonicButton() throws Exception { UnitTestAction action = new UnitTestAction("H&ello"); JMenuItem menu = new JMenuItem(action); assertEquals("H&ello",menu.getText()); MenuCommonFunctions.setMnemonic(menu); assertEquals(KeyEvent.VK_E,menu.getMnemonic()); assertEquals("Hello",menu.getText()); }
@Test public void testSetMnemonicButtonSpecial() throws Exception { UnitTestAction action = new UnitTestAction("Smith && &Wesson"); JMenuItem menu = new JMenuItem(action); MenuCommonFunctions.setMnemonic(menu); assertEquals(KeyEvent.VK_W,menu.getMnemonic()); assertEquals("Smith & Wesson",menu.getText()); }