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.Main;
|
---|
15 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
16 | import org.openstreetmap.josm.gui.dialogs.MapPaintDialog;
|
---|
17 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
18 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
19 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
20 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener;
|
---|
21 | import org.openstreetmap.josm.gui.util.StayOpenCheckBoxMenuItem;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * The View -> Map Paint Styles menu
|
---|
26 | * @since 5086
|
---|
27 | */
|
---|
28 | public class MapPaintMenu extends JMenu implements MapPaintSylesUpdateListener {
|
---|
29 |
|
---|
30 | private static class MapPaintAction extends JosmAction {
|
---|
31 |
|
---|
32 | private StyleSource style;
|
---|
33 | private JCheckBoxMenuItem button;
|
---|
34 |
|
---|
35 | public MapPaintAction(StyleSource style) {
|
---|
36 | super(style.getDisplayString(), style.getIcon(),
|
---|
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.main.hasEditLayer() || mapHasGpxorMarkerLayer()));
|
---|
69 | }
|
---|
70 |
|
---|
71 | private boolean mapHasGpxorMarkerLayer() {
|
---|
72 | for (Layer layer : Main.map.mapView.getAllLayers()) {
|
---|
73 | if (layer instanceof GpxLayer || layer instanceof MarkerLayer) {
|
---|
74 | return true;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | private final Map<String, MapPaintAction> actions = new HashMap<>();
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Constructs a new {@code MapPaintMenu}
|
---|
84 | */
|
---|
85 | public MapPaintMenu() {
|
---|
86 | super(tr("Map Paint Styles"));
|
---|
87 | setIcon(new ImageProvider("dialogs", "mapstyle").setSize(ImageProvider.ImageSizes.MENU).get());
|
---|
88 | MapPaintStyles.addMapPaintSylesUpdateListener(this);
|
---|
89 | putClientProperty("help", ht("/Dialog/MapPaint"));
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public void mapPaintStylesUpdated() {
|
---|
94 | removeAll();
|
---|
95 | for (StyleSource style : MapPaintStyles.getStyles().getStyleSources()) {
|
---|
96 | final String k = style.getDisplayString();
|
---|
97 | MapPaintAction a = actions.get(k);
|
---|
98 | if (a == null) {
|
---|
99 | actions.put(k, a = new MapPaintAction(style));
|
---|
100 | add(a.getButton());
|
---|
101 | } else {
|
---|
102 | a.setStyle(style);
|
---|
103 | add(a.getButton());
|
---|
104 | a.updateButton();
|
---|
105 | }
|
---|
106 | }
|
---|
107 | addSeparator();
|
---|
108 | add(MapPaintDialog.PREFERENCE_ACTION);
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public void mapPaintStyleEntryUpdated(int idx) {
|
---|
113 | mapPaintStylesUpdated();
|
---|
114 | }
|
---|
115 | }
|
---|