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

Last change on this file since 17318 was 16563, checked in by simon04, 4 years ago

fix #19357 - MapPaintStylesUpdateListener: fix typo

  • 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.actions.JosmAction;
15import org.openstreetmap.josm.gui.MainApplication;
16import org.openstreetmap.josm.gui.dialogs.MapPaintDialog;
17import org.openstreetmap.josm.gui.layer.GpxLayer;
18import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
19import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintStylesUpdateListener;
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 MapPaintStylesUpdateListener {
28
29 private static class MapPaintAction extends JosmAction {
30
31 private transient StyleSource style;
32 private final JCheckBoxMenuItem button;
33
34 MapPaintAction(StyleSource style) {
35 super(style.getDisplayString(), style.getIconProvider(),
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 putValue("help", ht("/Dialog/MapPaint"));
41 }
42
43 private void updateButton() {
44 button.getModel().setSelected(style.active);
45 }
46
47 private void toggleStyle() {
48 MapPaintStyles.toggleStyleActive(MapPaintStyles.getStyles().getStyleSources().indexOf(style));
49 updateButton();
50 }
51
52 @Override
53 public void actionPerformed(ActionEvent ae) {
54 toggleStyle();
55 }
56
57 public JCheckBoxMenuItem getButton() {
58 return button;
59 }
60
61 public void setStyle(StyleSource style) {
62 this.style = style;
63 }
64
65 @Override
66 protected boolean listenToSelectionChange() {
67 return false;
68 }
69
70 @Override
71 public void updateEnabledState() {
72 setEnabled(MainApplication.isDisplayingMapView()
73 && (MainApplication.getLayerManager().getActiveData() != null || mapHasGpxOrMarkerLayer()));
74 }
75
76 private static boolean mapHasGpxOrMarkerLayer() {
77 return MainApplication.getLayerManager().getLayers().stream()
78 .anyMatch(layer -> layer instanceof GpxLayer || layer instanceof MarkerLayer);
79 }
80 }
81
82 private final transient Map<String, MapPaintAction> actions = new HashMap<>();
83
84 /**
85 * Constructs a new {@code MapPaintMenu}
86 */
87 public MapPaintMenu() {
88 super(tr("Map Paint Styles"));
89 setIcon(ImageProvider.get("dialogs", "mapstyle", ImageProvider.ImageSizes.MENU));
90 MapPaintStyles.addMapPaintStylesUpdateListener(this);
91 putClientProperty("help", ht("/Dialog/MapPaint"));
92 }
93
94 @Override
95 public void mapPaintStylesUpdated() {
96 removeAll();
97 for (StyleSource style : MapPaintStyles.getStyles().getStyleSources()) {
98 final String k = style.getDisplayString();
99 MapPaintAction a = actions.get(k);
100 if (a == null) {
101 a = new MapPaintAction(style);
102 actions.put(k, a);
103 add(a.getButton());
104 } else {
105 a.setStyle(style);
106 add(a.getButton());
107 a.updateButton();
108 }
109 }
110 addSeparator();
111 add(MapPaintDialog.PREFERENCE_ACTION);
112 }
113
114 @Override
115 public void mapPaintStyleEntryUpdated(int idx) {
116 mapPaintStylesUpdated();
117 }
118}
Note: See TracBrowser for help on using the repository browser.