1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import javax.swing.JCheckBoxMenuItem;
|
---|
12 | import javax.swing.JMenu;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
15 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
16 | import org.openstreetmap.josm.gui.dialogs.MapPaintDialog;
|
---|
17 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
18 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
19 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintStylesUpdateListener;
|
---|
20 | import org.openstreetmap.josm.gui.util.StayOpenCheckBoxMenuItem;
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * The View -> Map Paint Styles menu
|
---|
25 | * @since 5086
|
---|
26 | */
|
---|
27 | public 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 | int index = MapPaintStyles.getStyles().getStyleSources().indexOf(style);
|
---|
49 | if (index >= 0) {
|
---|
50 | MapPaintStyles.toggleStyleActive(index);
|
---|
51 | }
|
---|
52 | updateButton();
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void actionPerformed(ActionEvent ae) {
|
---|
57 | toggleStyle();
|
---|
58 | }
|
---|
59 |
|
---|
60 | public JCheckBoxMenuItem getButton() {
|
---|
61 | return button;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void setStyle(StyleSource style) {
|
---|
65 | this.style = style;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | protected boolean listenToSelectionChange() {
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public void updateEnabledState() {
|
---|
75 | setEnabled(MainApplication.isDisplayingMapView()
|
---|
76 | && (MainApplication.getLayerManager().getActiveData() != null || mapHasGpxOrMarkerLayer()));
|
---|
77 | }
|
---|
78 |
|
---|
79 | private static boolean mapHasGpxOrMarkerLayer() {
|
---|
80 | return MainApplication.getLayerManager().getLayers().stream()
|
---|
81 | .anyMatch(layer -> layer instanceof GpxLayer || layer instanceof MarkerLayer);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | private final transient Map<String, MapPaintAction> actions = new HashMap<>();
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Constructs a new {@code MapPaintMenu}
|
---|
89 | */
|
---|
90 | public MapPaintMenu() {
|
---|
91 | super(tr("Map Paint Styles"));
|
---|
92 | setIcon(ImageProvider.get("dialogs", "mapstyle", ImageProvider.ImageSizes.MENU));
|
---|
93 | MapPaintStyles.addMapPaintStylesUpdateListener(this);
|
---|
94 | putClientProperty("help", ht("/Dialog/MapPaint"));
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public void mapPaintStylesUpdated() {
|
---|
99 | removeAll();
|
---|
100 | for (StyleSource style : MapPaintStyles.getStyles().getStyleSources()) {
|
---|
101 | final String k = style.getDisplayString();
|
---|
102 | MapPaintAction a = actions.get(k);
|
---|
103 | if (a == null) {
|
---|
104 | a = new MapPaintAction(style);
|
---|
105 | actions.put(k, a);
|
---|
106 | add(a.getButton());
|
---|
107 | } else {
|
---|
108 | a.setStyle(style);
|
---|
109 | add(a.getButton());
|
---|
110 | a.updateButton();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | addSeparator();
|
---|
114 | add(MapPaintDialog.PREFERENCE_ACTION);
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public void mapPaintStyleEntryUpdated(int idx) {
|
---|
119 | mapPaintStylesUpdated();
|
---|
120 | }
|
---|
121 | }
|
---|