001 package org.openstreetmap.josm.gui.mappaint;
002
003 import static org.openstreetmap.josm.tools.I18n.tr;
004
005 import java.awt.event.ActionEvent;
006 import java.util.HashMap;
007 import java.util.Map;
008 import javax.swing.JCheckBoxMenuItem;
009 import javax.swing.JMenu;
010 import org.openstreetmap.josm.Main;
011 import org.openstreetmap.josm.actions.JosmAction;
012 import org.openstreetmap.josm.gui.dialogs.MapPaintDialog;
013 import org.openstreetmap.josm.gui.dialogs.MapPaintDialog.LaunchMapPaintPreferencesAction;
014 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener;
015 import org.openstreetmap.josm.gui.util.StayOpenCheckBoxMenuItem;
016 import org.openstreetmap.josm.tools.ImageProvider;
017
018 public class MapPaintMenu extends JMenu implements MapPaintSylesUpdateListener {
019
020 private static class MapPaintAction extends JosmAction {
021
022 private StyleSource style;
023 private JCheckBoxMenuItem button;
024
025 public MapPaintAction(StyleSource style) {
026 super(style.getDisplayString(), style.icon == null ? null : ImageProvider.getIfAvailable(style.icon),
027 tr("Select the map painting styles"), null, true, "mappaint/" + style.getDisplayString(), true);
028 this.button = new StayOpenCheckBoxMenuItem(this);
029 this.style = style;
030 updateButton();
031 }
032
033 private void updateButton() {
034 button.getModel().setSelected(style.active);
035 }
036
037 private void toggleStyle() {
038 MapPaintStyles.toggleStyleActive(MapPaintStyles.getStyles().getStyleSources().indexOf(style));
039 updateButton();
040 }
041
042 @Override
043 public void actionPerformed(ActionEvent ae) {
044 toggleStyle();
045 }
046
047 public JCheckBoxMenuItem getButton() {
048 return button;
049 }
050
051 public void setStyle(StyleSource style) {
052 this.style = style;
053 }
054
055 @Override
056 public void updateEnabledState() {
057 setEnabled(Main.isDisplayingMapView() && Main.main.getEditLayer() != null);
058 }
059 }
060 private final Map<String, MapPaintAction> actions = new HashMap<String, MapPaintAction>();
061 private final LaunchMapPaintPreferencesAction mapPaintPreferencesAction = new MapPaintDialog.LaunchMapPaintPreferencesAction() {
062
063 {
064 putValue("toolbar", "mappaintpreference");
065 }
066 };
067
068 public MapPaintMenu() {
069 super(tr("Map Paint Styles"));
070 setIcon(ImageProvider.get("dialogs", "mapstyle"));
071 MapPaintStyles.addMapPaintSylesUpdateListener(this);
072 }
073
074 @Override
075 public void mapPaintStylesUpdated() {
076 removeAll();
077 for (StyleSource style : MapPaintStyles.getStyles().getStyleSources()) {
078 final String k = style.getDisplayString();
079 MapPaintAction a = actions.get(k);
080 if (a == null) {
081 actions.put(k, a = new MapPaintAction(style));
082 add(a.getButton());
083 } else {
084 a.setStyle(style);
085 add(a.getButton());
086 a.updateButton();
087 }
088 }
089 addSeparator();
090 add(mapPaintPreferencesAction);
091 }
092
093 @Override
094 public void mapPaintStyleEntryUpdated(int idx) {
095 mapPaintStylesUpdated();
096 }
097 }