Changeset 15438 in josm for trunk


Ignore:
Timestamp:
2019-10-07T01:41:24+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #12662 - add new Mode menu, listing map modes. Also allows to find them via ctrl+spacebar

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
2 edited

Legend:

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

    r15028 r15438  
    1010import java.awt.event.KeyEvent;
    1111import java.util.ArrayList;
     12import java.util.Arrays;
     13import java.util.Collection;
    1214import java.util.HashMap;
    1315import java.util.List;
    1416import java.util.Locale;
    1517import java.util.Map;
     18import java.util.Objects;
     19import java.util.Optional;
    1620
    1721import javax.swing.JCheckBoxMenuItem;
     
    116120import org.openstreetmap.josm.actions.audio.AudioPrevAction;
    117121import org.openstreetmap.josm.actions.audio.AudioSlowerAction;
     122import org.openstreetmap.josm.actions.mapmode.MapMode;
    118123import org.openstreetmap.josm.actions.search.SearchAction;
    119124import org.openstreetmap.josm.data.UndoRedoHandler;
     
    337342    public final JMenu viewMenu = addMenu("View", /* I18N: mnemonic: V */ trc("menu", "View"), KeyEvent.VK_V, 2, ht("/Menu/View"));
    338343    /**
     344     * modeMenu contains map modes
     345     */
     346    public final JMenu modeMenu = addMenu("Mode", /* I18N: mnemonic: M */ trc("menu", "Mode"), KeyEvent.VK_M, 3, ht("/Menu/Mode"));
     347    /**
    339348     * toolsMenu contains different geometry manipulation actions from JOSM core (most used)
    340349     * The plugins should use other menus
    341350     */
    342     public final JMenu toolsMenu = addMenu("Tools", /* I18N: mnemonic: T */ trc("menu", "Tools"), KeyEvent.VK_T, 3, ht("/Menu/Tools"));
     351    public final JMenu toolsMenu = addMenu("Tools", /* I18N: mnemonic: T */ trc("menu", "Tools"), KeyEvent.VK_T, 4, ht("/Menu/Tools"));
    343352    /**
    344353     * moreToolsMenu contains geometry-related actions from all the plugins
     
    346355     */
    347356    // CHECKSTYLE.OFF: LineLength
    348     public final JMenu moreToolsMenu = addMenu("More tools", /* I18N: mnemonic: M */ trc("menu", "More tools"), KeyEvent.VK_M, 4, ht("/Menu/MoreTools"));
     357    public final JMenu moreToolsMenu = addMenu("More tools", /* I18N: mnemonic: O */ trc("menu", "More tools"), KeyEvent.VK_O, 5, ht("/Menu/MoreTools"));
    349358    /**
    350359     * dataMenu contains plugin actions that are related to certain tagging schemes (addressing opening hours),
     
    352361     * @since 6082
    353362     */
    354     public final JMenu dataMenu = addMenu("Data", /* I18N: mnemonic: D */ trc("menu", "Data"), KeyEvent.VK_D, 5, ht("/Menu/Data"));
     363    public final JMenu dataMenu = addMenu("Data", /* I18N: mnemonic: D */ trc("menu", "Data"), KeyEvent.VK_D, 6, ht("/Menu/Data"));
    355364    /**
    356365     * selectionMenu contains all actions related to selecting different objects
    357366     * @since 6082 (moved from Utilsplugin2)
    358367     */
    359     public final JMenu selectionMenu = addMenu("Selection", /* I18N: mnemonic: N */ trc("menu", "Selection"), KeyEvent.VK_N, 6, ht("/Menu/Selection"));
     368    public final JMenu selectionMenu = addMenu("Selection", /* I18N: mnemonic: N */ trc("menu", "Selection"), KeyEvent.VK_N, 7, ht("/Menu/Selection"));
    360369    /**
    361370     * presetsMenu contains presets actions (search, presets tree)
    362371     */
    363     public final JMenu presetsMenu = addMenu("Presets", /* I18N: mnemonic: P */ trc("menu", "Presets"), KeyEvent.VK_P, 7, ht("/Menu/Presets"));
     372    public final JMenu presetsMenu = addMenu("Presets", /* I18N: mnemonic: P */ trc("menu", "Presets"), KeyEvent.VK_P, 8, ht("/Menu/Presets"));
    364373    /**
    365374     * submenu in Imagery menu that contains plugin-managed additional imagery layers
     
    370379     * imageryMenu contains all imagery-related actions
    371380     */
    372     public final ImageryMenu imageryMenu = addMenu(new ImageryMenu(imagerySubMenu), /* untranslated name */ "Imagery", KeyEvent.VK_I, 8, ht("/Menu/Imagery"));
     381    public final ImageryMenu imageryMenu = addMenu(new ImageryMenu(imagerySubMenu), /* untranslated name */ "Imagery", KeyEvent.VK_I, 9, ht("/Menu/Imagery"));
    373382    // CHECKSTYLE.ON: LineLength
    374383    /**
     
    377386     * @since 6082
    378387     */
    379     public final JMenu gpsMenu = addMenu("GPS", /* I18N: mnemonic: G */ trc("menu", "GPS"), KeyEvent.VK_G, 9, ht("/Menu/GPS"));
     388    public final JMenu gpsMenu = addMenu("GPS", /* I18N: mnemonic: G */ trc("menu", "GPS"), KeyEvent.VK_G, 10, ht("/Menu/GPS"));
    380389    /** the window menu is split into several groups. The first is for windows that can be opened from
    381390     * this menu any time, e.g. the changeset editor. The second group is for toggle dialogs and the third
     
    383392     * to use WINDOW_MENU_GROUP to determine the group integer.
    384393     */
    385     public final JMenu windowMenu = addMenu("Windows", /* I18N: mnemonic: W */ trc("menu", "Windows"), KeyEvent.VK_W, 10, ht("/ToggleDialogs"));
     394    public final JMenu windowMenu = addMenu("Windows", /* I18N: mnemonic: W */ trc("menu", "Windows"), KeyEvent.VK_W, 11, ht("/ToggleDialogs"));
    386395
    387396    /**
     
    392401     * helpMenu contains JOSM general actions (Help, About, etc.)
    393402     */
    394     public final JMenu helpMenu = addMenu("Help", /* I18N: mnemonic: H */ trc("menu", "Help"), KeyEvent.VK_H, 11, ht("/Menu/Help"));
    395 
    396     private static final int defaultMenuPos = 11;
     403    public final JMenu helpMenu = addMenu("Help", /* I18N: mnemonic: H */ trc("menu", "Help"), KeyEvent.VK_H, 12, ht("/Menu/Help"));
     404
     405    private static final int defaultMenuPos = 12;
    397406
    398407    /** Move the selection up */
     
    847856        windowMenu.addMenuListener(menuSeparatorHandler);
    848857
    849         new PresetsMenuEnabler(presetsMenu);
     858        new EditLayerMenuEnabler(Arrays.asList(modeMenu, presetsMenu));
    850859    }
    851860    // CHECKSTYLE.ON: ExecutableStatementCountCheck
     
    867876        }
    868877        return result;
     878    }
     879
     880    /**
     881     * Returns the {@link JCheckBoxMenuItem} for the given {@link MapMode}.
     882     * @param mode map mode
     883     * @return the {@code JCheckBoxMenuItem} for the given {@code MapMode}
     884     * @since 15438
     885     */
     886    public Optional<JCheckBoxMenuItem> findMapModeMenuItem(MapMode mode) {
     887        return Arrays.stream(modeMenu.getMenuComponents())
     888                .filter(m -> m instanceof JCheckBoxMenuItem)
     889                .map(m -> (JCheckBoxMenuItem) m)
     890                .filter(m -> Objects.equals(mode, m.getAction()))
     891                .findFirst();
    869892    }
    870893
     
    912935    }
    913936
    914     static class PresetsMenuEnabler implements ActiveLayerChangeListener {
    915         private final JMenu presetsMenu;
    916 
    917         PresetsMenuEnabler(JMenu presetsMenu) {
    918             this.presetsMenu = presetsMenu;
     937    static class EditLayerMenuEnabler implements ActiveLayerChangeListener {
     938        private final Collection<JMenu> menus;
     939
     940        EditLayerMenuEnabler(Collection<JMenu> menus) {
     941            this.menus = Objects.requireNonNull(menus);
    919942            MainApplication.getLayerManager().addAndFireActiveLayerChangeListener(this);
    920943        }
     
    922945        @Override
    923946        public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
    924             presetsMenu.setEnabled(e.getSource().getEditLayer() != null);
     947            menus.forEach(m -> m.setEnabled(e.getSource().getEditLayer() != null));
    925948        }
    926949    }
  • trunk/src/org/openstreetmap/josm/gui/MapFrame.java

    r14248 r15438  
    1818import java.util.List;
    1919import java.util.Map;
     20import java.util.Optional;
    2021import java.util.concurrent.CopyOnWriteArrayList;
    2122
     
    8384import org.openstreetmap.josm.tools.GBC;
    8485import org.openstreetmap.josm.tools.ImageProvider;
     86import org.openstreetmap.josm.tools.Logging;
    8587import org.openstreetmap.josm.tools.Shortcut;
    8688
     
    345347        MainApplication.getLayerManager().removeLayerChangeListener(this);
    346348        MainApplication.getLayerManager().removeActiveLayerChangeListener(this);
     349        MainApplication.getMenu().modeMenu.removeAll();
    347350        dialogsPanel.destroy();
    348351        Config.getPref().removePreferenceChangeListener(sidetoolbarPreferencesChangedListener);
     
    436439        if (!(b.getAction() instanceof MapMode))
    437440            throw new IllegalArgumentException("MapMode action must be subclass of MapMode");
     441        MainApplication.getMenu().modeMenu.add(new JCheckBoxMenuItem(b.getAction()));
    438442        allMapModeButtons.add(b);
    439443        toolBarActionsGroup.add(b);
     
    481485            newMapMode = null;
    482486        }
     487        Logging.info("Switching map mode from {0} to {1}",
     488                Optional.ofNullable(oldMapMode).map(m -> m.getClass().getSimpleName()).orElse("(none)"),
     489                Optional.ofNullable(newMapMode).map(m -> m.getClass().getSimpleName()).orElse("(none)"));
    483490
    484491        if (oldMapMode != null) {
     492            MainApplication.getMenu().findMapModeMenuItem(oldMapMode).ifPresent(m -> m.setSelected(false));
    485493            oldMapMode.exitMode();
    486494        }
     
    488496        if (newMapMode != null) {
    489497            newMapMode.enterMode();
     498            MainApplication.getMenu().findMapModeMenuItem(newMapMode).ifPresent(m -> m.setSelected(true));
    490499        }
    491500        lastMapMode.put(newLayer, newMapMode);
Note: See TracChangeset for help on using the changeset viewer.