Changeset 5089 in josm for trunk


Ignore:
Timestamp:
2012-03-16T14:26:05+01:00 (8 years ago)
Author:
simon04
Message:

give more control (esp. to plugins) where to add an entry in the main menu (i.e., add addAfter() method)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MainMenu.java

    r5086 r5089  
    257257
    258258    /**
    259      * Add a JosmAction to a menu.
     259     * Add a JosmAction at the end of a menu.
    260260     *
    261261     * This method handles all the shortcut handling. It also makes sure that actions that are
    262      * handled by the OS are not duplicated on the menu. Menu item will be added at the end of
    263      * the menu.
    264      * @param menu to add the action to
    265      * @param the action that should get a menu item
     262     * handled by the OS are not duplicated on the menu.
     263     * @param menu the menu to add the action to
     264     * @param action the action that should get a menu item
     265     * @return the created menu item
    266266     */
    267267    public static JMenuItem add(JMenu menu, JosmAction action) {
     
    269269    }
    270270
     271    /**
     272     * Add a JosmAction at the end of a menu.
     273     *
     274     * This method handles all the shortcut handling. It also makes sure that actions that are
     275     * handled by the OS are not duplicated on the menu.
     276     * @param menu the menu to add the action to
     277     * @param action the action that should get a menu item
     278     * @param isExpert whether the entry should only be visible if the expert mode is activated
     279     * @return the created menu item
     280     */
    271281    public static JMenuItem add(JMenu menu, JosmAction action, boolean isExpert) {
     282        return add(menu, action, isExpert, null);
     283    }
     284
     285    /**
     286     * Add a JosmAction at the end of a menu.
     287     *
     288     * This method handles all the shortcut handling. It also makes sure that actions that are
     289     * handled by the OS are not duplicated on the menu.
     290     * @param menu the menu to add the action to
     291     * @param action the action that should get a menu item
     292     * @param isExpert whether the entry should only be visible if the expert mode is activated
     293     * @param index  an integer specifying the position at which to add the action
     294     * @return the created menu item
     295     */
     296    public static JMenuItem add(JMenu menu, JosmAction action, boolean isExpert, Integer index) {
    272297        if (action.getShortcut().getAutomatic())
    273298            return null;
    274         JMenuItem menuitem = menu.add(action);
     299        final JMenuItem menuitem;
     300        if (index == null) {
     301            menuitem = menu.add(action);
     302        } else {
     303            menuitem = menu.insert(action, index);
     304        }
    275305        if (isExpert) {
    276306            ExpertToggleAction.addVisibilitySwitcher(menuitem);
     
    281311        }
    282312        return menuitem;
     313    }
     314
     315    /**
     316     * Add the JosmAction {@code actionToBeInserted} directly below {@code existingMenuEntryAction}.
     317     *
     318     * This method handles all the shortcut handling. It also makes sure that actions that are
     319     * handled by the OS are not duplicated on the menu.
     320     * @param menu the menu to add the action to
     321     * @param actionToBeInserted the action that should get a menu item directly below {@code existingMenuEntryAction}
     322     * @param isExpert whether the entry should only be visible if the expert mode is activated
     323     * @param existingMenuEntryAction an action already added to the menu {@code menu}, the action {@code actionToBeInserted} is added directly below
     324     * @return the created menu item
     325     */
     326    public static JMenuItem addAfter(JMenu menu, JosmAction actionToBeInserted, boolean isExpert, JosmAction existingMenuEntryAction) {
     327        int i = 0;
     328        for (Component c : menu.getMenuComponents()) {
     329            if (c instanceof JMenuItem && ((JMenuItem) c).getAction() == existingMenuEntryAction) {
     330                break;
     331            }
     332            i++;
     333        }
     334        return add(menu, actionToBeInserted, isExpert, i + 1);
    283335    }
    284336
     
    289341     * handled by the OS are not duplicated on the menu.
    290342     * @param menu to add the action to
    291      * @param the action that should get a menu item
     343     * @param action the action that should get a menu item
    292344     * @param group the item should be added to. Groups are split by a separator.
    293345     *        0 is the first group, -1 will add the item to the end.
     
    309361     * Also adds a checkbox that may be toggled.
    310362     * @param menu to add the action to
    311      * @param the action that should get a menu item
     363     * @param action the action that should get a menu item
    312364     * @param group the item should be added to. Groups are split by a separator. Use
    313365     *        one of the enums that are defined for some of the menus to tell in which
Note: See TracChangeset for help on using the changeset viewer.