| 1 | package org.openstreetmap.josm.gui.mappaint; |
| 2 | |
| 3 | import static org.openstreetmap.josm.tools.I18n.tr; |
| 4 | |
| 5 | import java.awt.event.ActionEvent; |
| 6 | import java.util.HashMap; |
| 7 | import java.util.HashSet; |
| 8 | import java.util.Map; |
| 9 | import java.util.Set; |
| 10 | import javax.swing.JCheckBoxMenuItem; |
| 11 | import javax.swing.JMenu; |
| 12 | import org.openstreetmap.josm.Main; |
| 13 | import org.openstreetmap.josm.actions.JosmAction; |
| 14 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener; |
| 15 | import org.openstreetmap.josm.tools.ImageProvider; |
| 16 | |
| 17 | public class MapPaintMenu extends JMenu implements MapPaintSylesUpdateListener { |
| 18 | |
| 19 | private static class MapPaintAction extends JosmAction { |
| 20 | |
| 21 | private StyleSource style; |
| 22 | private JCheckBoxMenuItem button; |
| 23 | |
| 24 | public MapPaintAction(StyleSource style) { |
| 25 | super(style.getDisplayString(), style.icon, |
| 26 | tr("Select the map painting styles"), null, style.icon != null); |
| 27 | if (style.icon == null) { |
| 28 | putValue("toolbar", "mappaint/" + style.getDisplayString()); |
| 29 | Main.toolbar.register(this); |
| 30 | } |
| 31 | this.button = new JCheckBoxMenuItem(this); |
| 32 | this.style = style; |
| 33 | updateButton(); |
| 34 | } |
| 35 | |
| 36 | private void updateButton() { |
| 37 | button.getModel().setSelected(style.active); |
| 38 | } |
| 39 | |
| 40 | private void toggleStyle() { |
| 41 | MapPaintStyles.toggleStyleActive(MapPaintStyles.getStyles().getStyleSources().indexOf(style)); |
| 42 | updateButton(); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void actionPerformed(ActionEvent ae) { |
| 47 | toggleStyle(); |
| 48 | } |
| 49 | |
| 50 | public JCheckBoxMenuItem getButton() { |
| 51 | return button; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void updateEnabledState() { |
| 56 | setEnabled(Main.map != null && Main.main.getEditLayer() != null); |
| 57 | } |
| 58 | } |
| 59 | private Map<String, MapPaintAction> actions = new HashMap<String, MapPaintAction>(); |
| 60 | |
| 61 | public MapPaintMenu() { |
| 62 | super(tr("Map Paint Styles")); |
| 63 | setIcon(ImageProvider.get("dialogs", "mapstyle")); |
| 64 | MapPaintStyles.addMapPaintSylesUpdateListener(this); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void mapPaintStylesUpdated() { |
| 69 | final Set<String> actionsToRemove = new HashSet<String>(actions.keySet()); |
| 70 | for (StyleSource style : MapPaintStyles.getStyles().getStyleSources()) { |
| 71 | final String k = style.getDisplayString(); |
| 72 | MapPaintAction a = actions.get(k); |
| 73 | if (a == null) { |
| 74 | a = new MapPaintAction(style); |
| 75 | add(a.getButton()); |
| 76 | actions.put(k, a); |
| 77 | } else { |
| 78 | a.updateButton(); |
| 79 | actionsToRemove.remove(k); |
| 80 | } |
| 81 | } |
| 82 | for (String k : actionsToRemove) { |
| 83 | final MapPaintAction a = actions.get(k); |
| 84 | if (a != null) { |
| 85 | remove(a.getButton()); |
| 86 | actions.remove(k); |
| 87 | } |
| 88 | } |
| 89 | System.out.println(actions); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void mapPaintStyleEntryUpdated(int idx) { |
| 94 | mapPaintStylesUpdated(); |
| 95 | } |
| 96 | } |