001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.event.ActionEvent;
008import java.util.HashMap;
009import java.util.Map;
010
011import javax.swing.JCheckBoxMenuItem;
012import javax.swing.JMenu;
013
014import org.openstreetmap.josm.Main;
015import org.openstreetmap.josm.actions.JosmAction;
016import org.openstreetmap.josm.gui.dialogs.MapPaintDialog;
017import org.openstreetmap.josm.gui.layer.GpxLayer;
018import org.openstreetmap.josm.gui.layer.Layer;
019import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
020import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener;
021import org.openstreetmap.josm.gui.util.StayOpenCheckBoxMenuItem;
022import org.openstreetmap.josm.tools.ImageProvider;
023
024/**
025 * The View -> Map Paint Styles menu
026 * @since 5086
027 */
028public class MapPaintMenu extends JMenu implements MapPaintSylesUpdateListener {
029
030    private static class MapPaintAction extends JosmAction {
031
032        private StyleSource style;
033        private JCheckBoxMenuItem button;
034
035        public MapPaintAction(StyleSource style) {
036            super(style.getDisplayString(), style.getIconProvider(),
037                    tr("Select the map painting styles"), null, true, "mappaint/" + style.getDisplayString(), true);
038            this.button = new StayOpenCheckBoxMenuItem(this);
039            this.style = style;
040            updateButton();
041            putValue("help", ht("/Dialog/MapPaint"));
042        }
043
044        private void updateButton() {
045            button.getModel().setSelected(style.active);
046        }
047
048        private void toggleStyle() {
049            MapPaintStyles.toggleStyleActive(MapPaintStyles.getStyles().getStyleSources().indexOf(style));
050            updateButton();
051        }
052
053        @Override
054        public void actionPerformed(ActionEvent ae) {
055            toggleStyle();
056        }
057
058        public JCheckBoxMenuItem getButton() {
059            return button;
060        }
061
062        public void setStyle(StyleSource style) {
063            this.style = style;
064        }
065
066        @Override
067        public void updateEnabledState() {
068            setEnabled(Main.isDisplayingMapView() && (Main.main.hasEditLayer() || mapHasGpxorMarkerLayer()));
069        }
070
071        private boolean mapHasGpxorMarkerLayer() {
072            for (Layer layer : Main.map.mapView.getAllLayers()) {
073                if (layer instanceof GpxLayer || layer instanceof MarkerLayer) {
074                    return true;
075                }
076            }
077            return false;
078        }
079    }
080    private final Map<String, MapPaintAction> actions = new HashMap<>();
081
082    /**
083     * Constructs a new {@code MapPaintMenu}
084     */
085    public MapPaintMenu() {
086        super(tr("Map Paint Styles"));
087        setIcon(new ImageProvider("dialogs", "mapstyle").setSize(ImageProvider.ImageSizes.MENU).get());
088        MapPaintStyles.addMapPaintSylesUpdateListener(this);
089        putClientProperty("help", ht("/Dialog/MapPaint"));
090    }
091
092    @Override
093    public void mapPaintStylesUpdated() {
094        removeAll();
095        for (StyleSource style : MapPaintStyles.getStyles().getStyleSources()) {
096            final String k = style.getDisplayString();
097            MapPaintAction a = actions.get(k);
098            if (a == null) {
099                actions.put(k, a = new MapPaintAction(style));
100                add(a.getButton());
101            } else {
102                a.setStyle(style);
103                add(a.getButton());
104                a.updateButton();
105            }
106        }
107        addSeparator();
108        add(MapPaintDialog.PREFERENCE_ACTION);
109    }
110
111    @Override
112    public void mapPaintStyleEntryUpdated(int idx) {
113        mapPaintStylesUpdated();
114    }
115}