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

Last change on this file since 12378 was 10453, checked in by Don-vip, 8 years ago

fix #13023 - Replace uses of hasEditLayer() with new layer manager (patch by michael2402, modified) - gsoc-core

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