< prev index next >

src/share/classes/javax/swing/plaf/basic/BasicMenuUI.java

Print this page
rev 1580 : 6727661: Code improvement and warnings removing from the swing/plaf packages
Summary: Removed unnecessary castings and other warnings
Reviewed-by: alexp
Contributed-by: Florian Brunner <fbrunnerlist@gmx.ch>


 106     }
 107 
 108     void installLazyActionMap() {
 109         LazyActionMap.installLazyActionMap(menuItem, BasicMenuUI.class,
 110                                            getPropertyPrefix() + ".actionMap");
 111     }
 112 
 113     void updateMnemonicBinding() {
 114         int mnemonic = menuItem.getModel().getMnemonic();
 115         int[] shortcutKeys = (int[])DefaultLookup.get(menuItem, this,
 116                                                    "Menu.shortcutKeys");
 117         if (shortcutKeys == null) {
 118             shortcutKeys = new int[] {KeyEvent.ALT_MASK};
 119         }
 120         if (mnemonic == lastMnemonic) {
 121             return;
 122         }
 123         InputMap windowInputMap = SwingUtilities.getUIInputMap(
 124                        menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW);
 125         if (lastMnemonic != 0 && windowInputMap != null) {
 126             for (int i=0; i<shortcutKeys.length; i++) {
 127                 windowInputMap.remove(KeyStroke.getKeyStroke
 128                                       (lastMnemonic, shortcutKeys[i], false));
 129             }
 130         }
 131         if (mnemonic != 0) {
 132             if (windowInputMap == null) {
 133                 windowInputMap = createInputMap(JComponent.
 134                                               WHEN_IN_FOCUSED_WINDOW);
 135                 SwingUtilities.replaceUIInputMap(menuItem, JComponent.
 136                                        WHEN_IN_FOCUSED_WINDOW, windowInputMap);
 137             }
 138             for (int i=0; i<shortcutKeys.length; i++) {
 139                 windowInputMap.put(KeyStroke.getKeyStroke(mnemonic,
 140                                          shortcutKeys[i], false),
 141                                    "selectMenu");
 142             }
 143         }
 144         lastMnemonic = mnemonic;
 145     }
 146 
 147     protected void uninstallKeyboardActions() {
 148         super.uninstallKeyboardActions();
 149         lastMnemonic = 0;
 150     }
 151 
 152     protected MouseInputListener createMouseInputListener(JComponent c) {
 153         return getHandler();
 154     }
 155 
 156     protected MenuListener createMenuListener(JComponent c) {
 157         return null;
 158     }
 159 
 160     protected ChangeListener createChangeListener(JComponent c) {
 161         return null;


 247         public void actionPerformed(ActionEvent e) {
 248             JMenu menu = getMenu(e);
 249             if (!crossMenuMnemonic) {
 250                 JPopupMenu pm = BasicPopupMenuUI.getLastPopup();
 251                 if (pm != null && pm != menu.getParent()) {
 252                     return;
 253                 }
 254             }
 255 
 256             final MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager();
 257             if(force) {
 258                 Container cnt = menu.getParent();
 259                 if(cnt != null && cnt instanceof JMenuBar) {
 260                     MenuElement me[];
 261                     MenuElement subElements[];
 262 
 263                     subElements = menu.getPopupMenu().getSubElements();
 264                     if(subElements.length > 0) {
 265                         me = new MenuElement[4];
 266                         me[0] = (MenuElement) cnt;
 267                         me[1] = (MenuElement) menu;
 268                         me[2] = (MenuElement) menu.getPopupMenu();
 269                         me[3] = subElements[0];
 270                     } else {
 271                         me = new MenuElement[3];
 272                         me[0] = (MenuElement)cnt;
 273                         me[1] = menu;
 274                         me[2] = (MenuElement) menu.getPopupMenu();
 275                     }
 276                     defaultManager.setSelectedPath(me);
 277                 }
 278             } else {
 279                 MenuElement path[] = defaultManager.getSelectedPath();
 280                 if(path.length > 0 && path[path.length-1] == menu) {
 281                     appendPath(path, menu.getPopupMenu());
 282                 }
 283             }
 284         }
 285 
 286         public boolean isEnabled(Object c) {
 287             if (c instanceof JMenu) {
 288                 return ((JMenu)c).isEnabled();
 289             }
 290             return true;
 291         }
 292     }
 293 
 294     /*


 589 
 590 
 591         //
 592         // MenuKeyListener
 593         //
 594         /**
 595          * Open the Menu
 596          */
 597         public void menuKeyTyped(MenuKeyEvent e) {
 598             if (!crossMenuMnemonic && BasicPopupMenuUI.getLastPopup() != null) {
 599                 // when crossMenuMnemonic is not set, we don't open a toplevel
 600                 // menu if another toplevel menu is already open
 601                     return;
 602                 }
 603 
 604             char key = Character.toLowerCase((char)menuItem.getMnemonic());
 605             MenuElement path[] = e.getPath();
 606             MenuSelectionManager manager = e.getMenuSelectionManager();
 607             if (key == Character.toLowerCase(e.getKeyChar())) {
 608                 JPopupMenu popupMenu = ((JMenu)menuItem).getPopupMenu();
 609                 ArrayList newList = new ArrayList(Arrays.asList(path));
 610                 newList.add(popupMenu);
 611                 MenuElement subs[] = popupMenu.getSubElements();
 612                 MenuElement sub =
 613                         BasicPopupMenuUI.findEnabledChild(subs, -1, true);
 614                 if(sub != null) {
 615                     newList.add(sub);
 616                 }
 617                 MenuElement newPath[] = new MenuElement[0];;
 618                 newPath = (MenuElement[]) newList.toArray(newPath);
 619                 manager.setSelectedPath(newPath);
 620                 e.consume();
 621             } else if (((JMenu)menuItem).isTopLevelMenu()
 622                     && BasicPopupMenuUI.getLastPopup() == null) {
 623                 manager.clearSelectedPath();
 624             }
 625         }
 626 
 627         public void menuKeyPressed(MenuKeyEvent e) {}
 628         public void menuKeyReleased(MenuKeyEvent e) {}
 629     }
 630 }


 106     }
 107 
 108     void installLazyActionMap() {
 109         LazyActionMap.installLazyActionMap(menuItem, BasicMenuUI.class,
 110                                            getPropertyPrefix() + ".actionMap");
 111     }
 112 
 113     void updateMnemonicBinding() {
 114         int mnemonic = menuItem.getModel().getMnemonic();
 115         int[] shortcutKeys = (int[])DefaultLookup.get(menuItem, this,
 116                                                    "Menu.shortcutKeys");
 117         if (shortcutKeys == null) {
 118             shortcutKeys = new int[] {KeyEvent.ALT_MASK};
 119         }
 120         if (mnemonic == lastMnemonic) {
 121             return;
 122         }
 123         InputMap windowInputMap = SwingUtilities.getUIInputMap(
 124                        menuItem, JComponent.WHEN_IN_FOCUSED_WINDOW);
 125         if (lastMnemonic != 0 && windowInputMap != null) {
 126             for (int shortcutKey : shortcutKeys) {
 127                 windowInputMap.remove(KeyStroke.getKeyStroke
 128                         (lastMnemonic, shortcutKey, false));
 129             }
 130         }
 131         if (mnemonic != 0) {
 132             if (windowInputMap == null) {
 133                 windowInputMap = createInputMap(JComponent.
 134                                               WHEN_IN_FOCUSED_WINDOW);
 135                 SwingUtilities.replaceUIInputMap(menuItem, JComponent.
 136                                        WHEN_IN_FOCUSED_WINDOW, windowInputMap);
 137             }
 138             for (int shortcutKey : shortcutKeys) {
 139                 windowInputMap.put(KeyStroke.getKeyStroke(mnemonic,
 140                         shortcutKey, false), "selectMenu");

 141             }
 142         }
 143         lastMnemonic = mnemonic;
 144     }
 145 
 146     protected void uninstallKeyboardActions() {
 147         super.uninstallKeyboardActions();
 148         lastMnemonic = 0;
 149     }
 150 
 151     protected MouseInputListener createMouseInputListener(JComponent c) {
 152         return getHandler();
 153     }
 154 
 155     protected MenuListener createMenuListener(JComponent c) {
 156         return null;
 157     }
 158 
 159     protected ChangeListener createChangeListener(JComponent c) {
 160         return null;


 246         public void actionPerformed(ActionEvent e) {
 247             JMenu menu = getMenu(e);
 248             if (!crossMenuMnemonic) {
 249                 JPopupMenu pm = BasicPopupMenuUI.getLastPopup();
 250                 if (pm != null && pm != menu.getParent()) {
 251                     return;
 252                 }
 253             }
 254 
 255             final MenuSelectionManager defaultManager = MenuSelectionManager.defaultManager();
 256             if(force) {
 257                 Container cnt = menu.getParent();
 258                 if(cnt != null && cnt instanceof JMenuBar) {
 259                     MenuElement me[];
 260                     MenuElement subElements[];
 261 
 262                     subElements = menu.getPopupMenu().getSubElements();
 263                     if(subElements.length > 0) {
 264                         me = new MenuElement[4];
 265                         me[0] = (MenuElement) cnt;
 266                         me[1] = menu;
 267                         me[2] = menu.getPopupMenu();
 268                         me[3] = subElements[0];
 269                     } else {
 270                         me = new MenuElement[3];
 271                         me[0] = (MenuElement)cnt;
 272                         me[1] = menu;
 273                         me[2] = menu.getPopupMenu();
 274                     }
 275                     defaultManager.setSelectedPath(me);
 276                 }
 277             } else {
 278                 MenuElement path[] = defaultManager.getSelectedPath();
 279                 if(path.length > 0 && path[path.length-1] == menu) {
 280                     appendPath(path, menu.getPopupMenu());
 281                 }
 282             }
 283         }
 284 
 285         public boolean isEnabled(Object c) {
 286             if (c instanceof JMenu) {
 287                 return ((JMenu)c).isEnabled();
 288             }
 289             return true;
 290         }
 291     }
 292 
 293     /*


 588 
 589 
 590         //
 591         // MenuKeyListener
 592         //
 593         /**
 594          * Open the Menu
 595          */
 596         public void menuKeyTyped(MenuKeyEvent e) {
 597             if (!crossMenuMnemonic && BasicPopupMenuUI.getLastPopup() != null) {
 598                 // when crossMenuMnemonic is not set, we don't open a toplevel
 599                 // menu if another toplevel menu is already open
 600                     return;
 601                 }
 602 
 603             char key = Character.toLowerCase((char)menuItem.getMnemonic());
 604             MenuElement path[] = e.getPath();
 605             MenuSelectionManager manager = e.getMenuSelectionManager();
 606             if (key == Character.toLowerCase(e.getKeyChar())) {
 607                 JPopupMenu popupMenu = ((JMenu)menuItem).getPopupMenu();
 608                 ArrayList<MenuElement> newList = new ArrayList<MenuElement>(Arrays.asList(path));
 609                 newList.add(popupMenu);
 610                 MenuElement subs[] = popupMenu.getSubElements();
 611                 MenuElement sub =
 612                         BasicPopupMenuUI.findEnabledChild(subs, -1, true);
 613                 if(sub != null) {
 614                     newList.add(sub);
 615                 }
 616                 MenuElement newPath[] = new MenuElement[0];
 617                 newPath = newList.toArray(newPath);
 618                 manager.setSelectedPath(newPath);
 619                 e.consume();
 620             } else if (((JMenu)menuItem).isTopLevelMenu()
 621                     && BasicPopupMenuUI.getLastPopup() == null) {
 622                 manager.clearSelectedPath();
 623             }
 624         }
 625 
 626         public void menuKeyPressed(MenuKeyEvent e) {}
 627         public void menuKeyReleased(MenuKeyEvent e) {}
 628     }
 629 }
< prev index next >