source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintMenu.java@ 6034

Last change on this file since 6034 was 6034, checked in by Don-vip, 11 years ago

fix #8823 - enable map paint styles switching with GPX layers

File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.util.HashMap;
8import java.util.Map;
9
10import javax.swing.JCheckBoxMenuItem;
11import javax.swing.JMenu;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.JosmAction;
15import org.openstreetmap.josm.gui.dialogs.MapPaintDialog;
16import org.openstreetmap.josm.gui.dialogs.MapPaintDialog.LaunchMapPaintPreferencesAction;
17import org.openstreetmap.josm.gui.layer.GpxLayer;
18import org.openstreetmap.josm.gui.layer.Layer;
19import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener;
20import org.openstreetmap.josm.gui.util.StayOpenCheckBoxMenuItem;
21import org.openstreetmap.josm.tools.ImageProvider;
22
23/**
24 * The View -> Map Paint Styles menu
25 * @since 5086
26 */
27public class MapPaintMenu extends JMenu implements MapPaintSylesUpdateListener {
28
29 private static class MapPaintAction extends JosmAction {
30
31 private StyleSource style;
32 private JCheckBoxMenuItem button;
33
34 public MapPaintAction(StyleSource style) {
35 super(style.getDisplayString(), style.icon == null ? null : ImageProvider.getIfAvailable(style.icon),
36 tr("Select the map painting styles"), null, true, "mappaint/" + style.getDisplayString(), true);
37 this.button = new StayOpenCheckBoxMenuItem(this);
38 this.style = style;
39 updateButton();
40 }
41
42 private void updateButton() {
43 button.getModel().setSelected(style.active);
44 }
45
46 private void toggleStyle() {
47 MapPaintStyles.toggleStyleActive(MapPaintStyles.getStyles().getStyleSources().indexOf(style));
48 updateButton();
49 }
50
51 @Override
52 public void actionPerformed(ActionEvent ae) {
53 toggleStyle();
54 }
55
56 public JCheckBoxMenuItem getButton() {
57 return button;
58 }
59
60 public void setStyle(StyleSource style) {
61 this.style = style;
62 }
63
64 @Override
65 public void updateEnabledState() {
66 setEnabled(Main.isDisplayingMapView() && (Main.main.getEditLayer() != null || mapHasGpxLayer()));
67 }
68
69 private boolean mapHasGpxLayer() {
70 for (Layer layer : Main.map.mapView.getAllLayers()) {
71 if (layer instanceof GpxLayer) {
72 return true;
73 }
74 }
75 return false;
76 }
77 }
78 private final Map<String, MapPaintAction> actions = new HashMap<String, MapPaintAction>();
79 private final LaunchMapPaintPreferencesAction mapPaintPreferencesAction = new MapPaintDialog.LaunchMapPaintPreferencesAction() {
80
81 {
82 putValue("toolbar", "mappaintpreference");
83 }
84 };
85
86 /**
87 * Constructs a new {@code MapPaintMenu}
88 */
89 public MapPaintMenu() {
90 super(tr("Map Paint Styles"));
91 setIcon(ImageProvider.get("dialogs", "mapstyle"));
92 MapPaintStyles.addMapPaintSylesUpdateListener(this);
93 }
94
95 @Override
96 public void mapPaintStylesUpdated() {
97 removeAll();
98 for (StyleSource style : MapPaintStyles.getStyles().getStyleSources()) {
99 final String k = style.getDisplayString();
100 MapPaintAction a = actions.get(k);
101 if (a == null) {
102 actions.put(k, a = new MapPaintAction(style));
103 add(a.getButton());
104 } else {
105 a.setStyle(style);
106 add(a.getButton());
107 a.updateButton();
108 }
109 }
110 addSeparator();
111 add(mapPaintPreferencesAction);
112 }
113
114 @Override
115 public void mapPaintStyleEntryUpdated(int idx) {
116 mapPaintStylesUpdated();
117 }
118}
Note: See TracBrowser for help on using the repository browser.