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

Last change on this file since 6426 was 6426, checked in by simon04, 10 years ago

see #9309 - provide direct menu items to preset/imagery preferences

File size: 3.6 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.layer.GpxLayer;
17import org.openstreetmap.josm.gui.layer.Layer;
18import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
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.hasEditLayer() || mapHasGpxorMarkerLayer()));
67 }
68
69 private boolean mapHasGpxorMarkerLayer() {
70 for (Layer layer : Main.map.mapView.getAllLayers()) {
71 if (layer instanceof GpxLayer || layer instanceof MarkerLayer) {
72 return true;
73 }
74 }
75 return false;
76 }
77 }
78 private final Map<String, MapPaintAction> actions = new HashMap<String, MapPaintAction>();
79
80 /**
81 * Constructs a new {@code MapPaintMenu}
82 */
83 public MapPaintMenu() {
84 super(tr("Map Paint Styles"));
85 setIcon(ImageProvider.get("dialogs", "mapstyle"));
86 MapPaintStyles.addMapPaintSylesUpdateListener(this);
87 }
88
89 @Override
90 public void mapPaintStylesUpdated() {
91 removeAll();
92 for (StyleSource style : MapPaintStyles.getStyles().getStyleSources()) {
93 final String k = style.getDisplayString();
94 MapPaintAction a = actions.get(k);
95 if (a == null) {
96 actions.put(k, a = new MapPaintAction(style));
97 add(a.getButton());
98 } else {
99 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}
Note: See TracBrowser for help on using the repository browser.