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