Changeset 4590 in josm


Ignore:
Timestamp:
2011-11-12T12:54:49+01:00 (9 years ago)
Author:
stoecker
Message:

see #59 - patch by xeen - apply parts of the new Windows menu stuff

Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/ChangesetManagerToggleAction.java

    r3083 r4590  
    3434                tr("Toggle visibility of Changeset Manager window"),
    3535                Shortcut.registerShortcut(
    36                         "menu:view:changesetdialog",
     36                        "menu:windows:changesetdialog",
    3737                        tr("Toggle visibility of Changeset Manager window"),
    3838                        KeyEvent.VK_C,
  • trunk/src/org/openstreetmap/josm/gui/MainMenu.java

    r4500 r4590  
    1212import javax.swing.JMenuBar;
    1313import javax.swing.JMenuItem;
     14import javax.swing.JPopupMenu;
     15import javax.swing.JSeparator;
    1416import javax.swing.KeyStroke;
     17import javax.swing.event.MenuEvent;
     18import javax.swing.event.MenuListener;
    1519
    1620import org.openstreetmap.josm.Main;
     
    5660import org.openstreetmap.josm.actions.OpenLocationAction;
    5761import org.openstreetmap.josm.actions.OrthogonalizeAction;
    58 import org.openstreetmap.josm.actions.OrthogonalizeAction.Undo;
    5962import org.openstreetmap.josm.actions.PasteAction;
    6063import org.openstreetmap.josm.actions.PasteTagsAction;
     
    8285import org.openstreetmap.josm.actions.ZoomInAction;
    8386import org.openstreetmap.josm.actions.ZoomOutAction;
     87import org.openstreetmap.josm.actions.OrthogonalizeAction.Undo;
    8488import org.openstreetmap.josm.actions.audio.AudioBackAction;
    8589import org.openstreetmap.josm.actions.audio.AudioFasterAction;
     
    191195    public final ImageryMenu imageryMenu =
    192196        (ImageryMenu)addMenu(new ImageryMenu(), marktr("Imagery"), KeyEvent.VK_I, 5, ht("/Menu/Imagery"));
     197    /** the window menu is split into several groups. The first is for windows that can be opened from
     198     * this menu any time, e.g. the changeset editor. The second group is for toggle dialogs and the third
     199     * group is for currently open windows that cannot be toggled, e.g. relation editors. It's recommended
     200     * to use WINDOW_MENU_GROUP to determine the group integer.
     201     */
     202    public final JMenu windowMenu = addMenu(marktr("Windows"), KeyEvent.VK_W, 6, ht("/Menu/Windows"));
     203    public static enum WINDOW_MENU_GROUP { ALWAYS, TOGGLE_DIALOG, VOLATILE }
     204
    193205    public JMenu audioMenu = null;
    194     public final JMenu helpMenu = addMenu(marktr("Help"), KeyEvent.VK_H, 6, ht("/Menu/Help"));
    195     public final int defaultMenuPos = 6;
     206    public final JMenu helpMenu = addMenu(marktr("Help"), KeyEvent.VK_H, 7, ht("/Menu/Help"));
     207    public final int defaultMenuPos = 7;
    196208
    197209    public final JosmAction moveUpAction = new MoveAction(MoveAction.Direction.UP);
     
    203215    public final TaggingPresetSearchAction presetSearchAction = new TaggingPresetSearchAction();
    204216    public FullscreenToggleAction fullscreenToggleAction = null;
     217
     218    /** this menu listener hides unnecessary JSeparators in a menu list but does not remove them.
     219     * If at a later time the separators are required, they will be made visible again. Intended
     220     * usage is make menus not look broken if separators are used to group the menu and some of
     221     * these groups are empty.
     222     */
     223    public final static MenuListener menuSeparatorHandler = new MenuListener() {
     224        @Override
     225        public void menuCanceled(MenuEvent arg0) {}
     226        @Override
     227        public void menuDeselected(MenuEvent arg0) {}
     228        @Override
     229        public void menuSelected(MenuEvent a) {
     230            if(!(a.getSource() instanceof JMenu))
     231                return;
     232            final JPopupMenu m = ((JMenu) a.getSource()).getPopupMenu();
     233            for(int i=0; i < m.getComponentCount()-1; i++) {
     234                if(!(m.getComponent(i) instanceof JSeparator)) {
     235                    continue;
     236                }
     237                // hide separator if the next menu item is one as well
     238                ((JSeparator) m.getComponent(i)).setVisible(!(m.getComponent(i+1) instanceof JSeparator));
     239            }
     240            // hide separator at the end of the menu
     241            if(m.getComponent(m.getComponentCount()-1) instanceof JSeparator) {
     242                ((JSeparator) m.getComponent(m.getComponentCount()-1)).setVisible(false);
     243            }
     244        }
     245    };
     246
     247    /**
     248     * Add a JosmAction to a menu.
     249     *
     250     * This method handles all the shortcut handling. It also makes sure that actions that are
     251     * handled by the OS are not duplicated on the menu. Menu item will be added at the end of
     252     * the menu.
     253     * @param menu to add the action to
     254     * @param the action that should get a menu item
     255     */
     256    public static JMenuItem add(JMenu menu, JosmAction action) {
     257        if (action.getShortcut().getAutomatic())
     258            return null;
     259        JMenuItem menuitem = menu.add(action);
     260            KeyStroke ks = action.getShortcut().getKeyStroke();
     261            if (ks != null) {
     262                menuitem.setAccelerator(ks);
     263            }
     264        return menuitem;
     265    }
     266
    205267    /**
    206268     * Add a JosmAction to a menu.
     
    208270     * This method handles all the shortcut handling. It also makes sure that actions that are
    209271     * handled by the OS are not duplicated on the menu.
     272     * @param menu to add the action to
     273     * @param the action that should get a menu item
     274     * @param group the item should be added to. Groups are split by a separator.
     275     *        0 is the first group, -1 will add the item to the end.
    210276     */
    211     public static JMenuItem add(JMenu menu, JosmAction action) {
    212         JMenuItem menuitem = null;
    213         if (!action.getShortcut().getAutomatic()) {
    214             menuitem = menu.add(action);
    215             KeyStroke ks = action.getShortcut().getKeyStroke();
    216             if (ks != null) {
    217                 menuitem.setAccelerator(ks);
     277    public static <E extends Enum<E>> JMenuItem add(JMenu menu, JosmAction action, Enum<E> group) {
     278        if (action.getShortcut().getAutomatic())
     279            return null;
     280        int i = getInsertionIndexForGroup(menu, group.ordinal());
     281        JMenuItem menuitem = (JMenuItem) menu.add(new JMenuItem(action), i);
     282        KeyStroke ks = action.getShortcut().getKeyStroke();
     283        if (ks != null) {
     284            menuitem.setAccelerator(ks);
     285        }
     286        return menuitem;
     287    }
     288
     289    /**
     290     * Add a JosmAction to a menu and automatically prints accelerator if available.
     291     * Also adds a checkbox that may be toggled.
     292     * @param menu to add the action to
     293     * @param the action that should get a menu item
     294     * @param group the item should be added to. Groups are split by a separator. Use
     295     *        one of the enums that are defined for some of the menus to tell in which
     296     *        group the item should go.
     297     */
     298    public static <E extends Enum<E>> JCheckBoxMenuItem addWithCheckbox(JMenu menu, JosmAction action, Enum<E> group) {
     299        int i = getInsertionIndexForGroup(menu, group.ordinal());
     300        final JCheckBoxMenuItem mi = (JCheckBoxMenuItem) menu.add(new JCheckBoxMenuItem(action), i);
     301        final KeyStroke ks = action.getShortcut().getKeyStroke();
     302        if (ks != null) {
     303            mi.setAccelerator(ks);
     304        }
     305        return mi;
     306    }
     307
     308    /** finds the correct insertion index for a given group and adds separators if necessary */
     309    private static int getInsertionIndexForGroup(JMenu menu, int group) {
     310        if(group < 0)
     311            return -1;
     312        // look for separator that *ends* the group (or stop at end of menu)
     313        int i;
     314        for(i=0; i < menu.getItemCount() && group >= 0; i++) {
     315            if(menu.getItem(i) == null) {
     316                group--;
    218317            }
    219318        }
    220         return menuitem;
     319        // insert before separator that ends the group
     320        if(group < 0) {
     321            i--;
     322        }
     323        // not enough separators have been found, add them
     324        while(group > 0) {
     325            menu.addSeparator();
     326            group--;
     327            i++;
     328        }
     329        return i;
    221330    }
    222331
     
    301410        vft.setAccelerator(viewportFollowToggleAction.getShortcut().getKeyStroke());
    302411        viewportFollowToggleAction.addButtonModel(vft.getModel());
    303 
    304         // -- changeset manager toggle action
    305         ChangesetManagerToggleAction changesetManagerToggleAction = new ChangesetManagerToggleAction();
    306         final JCheckBoxMenuItem mi = new JCheckBoxMenuItem(changesetManagerToggleAction);
    307         viewMenu.addSeparator();
    308         viewMenu.add(mi);
    309         mi.setAccelerator(changesetManagerToggleAction.getShortcut().getKeyStroke());
    310         changesetManagerToggleAction.addButtonModel(mi.getModel());
    311412
    312413        if(!Main.applet && Main.platform.canFullscreen()) {
     
    352453        add(toolsMenu, createMultipolygon);
    353454
     455        // -- changeset manager toggle action
     456        ChangesetManagerToggleAction changesetManagerToggleAction = new ChangesetManagerToggleAction();
     457        final JCheckBoxMenuItem mi = MainMenu.addWithCheckbox(windowMenu, changesetManagerToggleAction,
     458                MainMenu.WINDOW_MENU_GROUP.ALWAYS);
     459        changesetManagerToggleAction.addButtonModel(mi.getModel());
     460
     461
    354462        if (!Main.pref.getBoolean("audio.menuinvisible", false)) {
    355463            audioMenu = addMenu(marktr("Audio"), KeyEvent.VK_U, defaultMenuPos, ht("/Menu/Audio"));
     
    370478        add(helpMenu, about);
    371479
     480
     481        windowMenu.addMenuListener(menuSeparatorHandler);
     482
    372483        new PresetsMenuEnabler(presetsMenu).refreshEnabled();
    373484    }
  • trunk/src/org/openstreetmap/josm/gui/MapFrame.java

    r4585 r4590  
    364364        jb.add(toolBarActions);
    365365
    366         jb.addSeparator(new Dimension(0,18));
    367         toolBarToggle.setAlignmentX(0.5f);
    368         jb.add(toolBarToggle);
    369         otherButton.setAlignmentX(0.5f);
    370         otherButton.setBorder(null);
    371         otherButton.setFont(otherButton.getFont().deriveFont(Font.PLAIN));
    372         jb.add(otherButton);
     366        if(Main.pref.getBoolean("sidetoolbar.togglevisible", true)) {
     367            jb.addSeparator(new Dimension(0,18));
     368            toolBarToggle.setAlignmentX(0.5f);
     369            jb.add(toolBarToggle);
     370            otherButton.setAlignmentX(0.5f);
     371            otherButton.setBorder(null);
     372            otherButton.setFont(otherButton.getFont().deriveFont(Font.PLAIN));
     373            jb.add(otherButton);
     374        }
    373375
    374376        if(Main.pref.getBoolean("sidetoolbar.visible", true))
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java

    r4512 r4590  
    3232import javax.swing.ImageIcon;
    3333import javax.swing.JButton;
     34import javax.swing.JCheckBoxMenuItem;
    3435import javax.swing.JComponent;
    3536import javax.swing.JDialog;
     
    4243import org.openstreetmap.josm.Main;
    4344import org.openstreetmap.josm.actions.JosmAction;
     45import org.openstreetmap.josm.gui.MainMenu;
    4446import org.openstreetmap.josm.gui.dialogs.DialogsPanel.Action;
    4547import org.openstreetmap.josm.gui.help.HelpUtil;
     
    9597    private JPanel buttonsPanel;
    9698
     99    /** holds the menu entry in the windows menu. Required to properly
     100     * toggle the checkbox on show/hide
     101     */
     102    protected JCheckBoxMenuItem windowMenuItem;
     103
    97104    /**
    98105     * Constructor
     
    140147
    141148        RedirectInputMap.redirectToMainContentPane(this);
     149
     150        windowMenuItem = MainMenu.addWithCheckbox(Main.main.menu.windowMenu,
     151                (JosmAction) getToggleAction(),
     152                MainMenu.WINDOW_MENU_GROUP.TOGGLE_DIALOG);
    142153    }
    143154
     
    160171        public void actionPerformed(ActionEvent e) {
    161172            toggleButtonHook();
     173            if(getValue("toolbarbutton") != null && getValue("toolbarbutton") instanceof JButton) {
     174                ((JButton) getValue("toolbarbutton")).setSelected(!isShowing);
     175            }
    162176            if (isShowing) {
    163177                hideDialog();
     
    195209        // toggling the selected value in order to enforce PropertyChangeEvents
    196210        setIsShowing(true);
     211        windowMenuItem.setState(true);
    197212        toggleAction.putValue("selected", false);
    198213        toggleAction.putValue("selected", true);
     
    252267        closeDetachedDialog();
    253268        this.setVisible(false);
     269        windowMenuItem.setState(false);
    254270        setIsShowing(false);
    255271        toggleAction.putValue("selected", false);
     
    330346        closeDetachedDialog();
    331347        hideNotify();
     348        Main.main.menu.windowMenu.remove(windowMenuItem);
    332349    }
    333350
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java

    r4563 r4590  
    3535import javax.swing.JComponent;
    3636import javax.swing.JLabel;
     37import javax.swing.JMenu;
     38import javax.swing.JMenuItem;
    3739import javax.swing.JOptionPane;
    3840import javax.swing.JPanel;
     
    5456import org.openstreetmap.josm.Main;
    5557import org.openstreetmap.josm.actions.CopyAction;
     58import org.openstreetmap.josm.actions.JosmAction;
    5659import org.openstreetmap.josm.actions.PasteTagsAction.TagPaster;
    5760import org.openstreetmap.josm.command.AddCommand;
     
    6871import org.openstreetmap.josm.gui.DefaultNameFormatter;
    6972import org.openstreetmap.josm.gui.HelpAwareOptionPane;
     73import org.openstreetmap.josm.gui.MainMenu;
    7074import org.openstreetmap.josm.gui.SideButton;
    7175import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
     
    101105
    102106    private AutoCompletingTextField tfRole;
     107
     108    /** the menu item in the windows menu. Required to properly
     109     * hide on dialog close.
     110     */
     111    private JMenuItem windowMenuItem;
    103112
    104113    /**
     
    575584        if (visible) {
    576585            RelationDialogManager.getRelationDialogManager().positionOnScreen(this);
     586            if(windowMenuItem == null) {
     587                addToWindowMenu();
     588            }
    577589        } else {
    578590            // make sure all registered listeners are unregistered
     
    581593            memberTableModel.unregister();
    582594            memberTable.unlinkAsListener();
     595            if(windowMenuItem != null) {
     596                Main.main.menu.windowMenu.remove(windowMenuItem);
     597                windowMenuItem = null;
     598            }
    583599            dispose();
    584600        }
     601    }
     602
     603    /** adds current relation editor to the windows menu (in the "volatile" group) o*/
     604    protected void addToWindowMenu() {
     605        String name = getRelation() == null ? tr("New Relation") : getRelation().getLocalName();
     606        final String tt = tr("Focus Relation Editor with relation ''{0}'' in layer ''{1}''",
     607                name, getLayer().getName());
     608        name = tr("Relation Editor: {0}", name == null ? getRelation().getId() : name);
     609        final JMenu wm = Main.main.menu.windowMenu;
     610        final JosmAction focusAction = new JosmAction(name, "dialogs/relationlist", tt, null, false, false) {
     611            @Override
     612            public void actionPerformed(ActionEvent e) {
     613                final RelationEditor r = (RelationEditor) getValue("relationEditor");
     614                r.setVisible(true);
     615            }
     616        };
     617        focusAction.putValue("relationEditor", this);
     618        windowMenuItem = MainMenu.add(wm, focusAction, MainMenu.WINDOW_MENU_GROUP.VOLATILE);
    585619    }
    586620
  • trunk/src/org/openstreetmap/josm/gui/help/HelpBrowser.java

    r4191 r4590  
    2525import javax.swing.JDialog;
    2626import javax.swing.JEditorPane;
     27import javax.swing.JMenuItem;
    2728import javax.swing.JOptionPane;
    2829import javax.swing.JPanel;
     
    4546
    4647import org.openstreetmap.josm.Main;
     48import org.openstreetmap.josm.actions.JosmAction;
    4749import org.openstreetmap.josm.gui.HelpAwareOptionPane;
     50import org.openstreetmap.josm.gui.MainMenu;
    4851import org.openstreetmap.josm.tools.ImageProvider;
    4952import org.openstreetmap.josm.tools.OpenBrowser;
     
    5356    /** the unique instance */
    5457    private static HelpBrowser instance;
     58
     59    /** the menu item in the windows menu. Required to properly
     60     * hide on dialog close.
     61     */
     62    private JMenuItem windowMenuItem;
    5563
    5664    /**
     
    106114
    107115    private HelpContentReader reader;
     116
     117    private static final JosmAction focusAction = new JosmAction(tr("JOSM Help Browser"), "help", "", null, false, false) {
     118        @Override
     119        public void actionPerformed(ActionEvent e) {
     120            HelpBrowser.getInstance().setVisible(true);
     121        }
     122    };
    108123
    109124    /**
     
    193208        } else if (!visible && isShowing()){
    194209            new WindowGeometry(this).remember(getClass().getName() + ".geometry");
     210        }
     211        if(windowMenuItem != null && !visible) {
     212            Main.main.menu.windowMenu.remove(windowMenuItem);
     213            windowMenuItem = null;
     214        }
     215        if(windowMenuItem == null && visible) {
     216            windowMenuItem = MainMenu.add(Main.main.menu.windowMenu, focusAction, MainMenu.WINDOW_MENU_GROUP.VOLATILE);
    195217        }
    196218        super.setVisible(visible);
Note: See TracChangeset for help on using the changeset viewer.