1 | package org.openstreetmap.josm.gui.mappaint;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.Map;
|
---|
8 | import javax.swing.JCheckBoxMenuItem;
|
---|
9 | import javax.swing.JMenu;
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
12 | import org.openstreetmap.josm.gui.dialogs.MapPaintDialog;
|
---|
13 | import org.openstreetmap.josm.gui.dialogs.MapPaintDialog.LaunchMapPaintPreferencesAction;
|
---|
14 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.MapPaintSylesUpdateListener;
|
---|
15 | import org.openstreetmap.josm.gui.util.StayOpenCheckBoxMenuItem;
|
---|
16 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
17 |
|
---|
18 | public class MapPaintMenu extends JMenu implements MapPaintSylesUpdateListener {
|
---|
19 |
|
---|
20 | private static class MapPaintAction extends JosmAction {
|
---|
21 |
|
---|
22 | private StyleSource style;
|
---|
23 | private JCheckBoxMenuItem button;
|
---|
24 |
|
---|
25 | public MapPaintAction(StyleSource style) {
|
---|
26 | super(style.getDisplayString(), style.icon == null ? null : ImageProvider.getIfAvailable(style.icon),
|
---|
27 | tr("Select the map painting styles"), null, true, "mappaint/" + style.getDisplayString(), true);
|
---|
28 | this.button = new StayOpenCheckBoxMenuItem(this);
|
---|
29 | this.style = style;
|
---|
30 | updateButton();
|
---|
31 | }
|
---|
32 |
|
---|
33 | private void updateButton() {
|
---|
34 | button.getModel().setSelected(style.active);
|
---|
35 | }
|
---|
36 |
|
---|
37 | private void toggleStyle() {
|
---|
38 | MapPaintStyles.toggleStyleActive(MapPaintStyles.getStyles().getStyleSources().indexOf(style));
|
---|
39 | updateButton();
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void actionPerformed(ActionEvent ae) {
|
---|
44 | toggleStyle();
|
---|
45 | }
|
---|
46 |
|
---|
47 | public JCheckBoxMenuItem getButton() {
|
---|
48 | return button;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void setStyle(StyleSource style) {
|
---|
52 | this.style = style;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void updateEnabledState() {
|
---|
57 | setEnabled(Main.isDisplayingMapView() && Main.main.getEditLayer() != null);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | private final Map<String, MapPaintAction> actions = new HashMap<String, MapPaintAction>();
|
---|
61 | private final LaunchMapPaintPreferencesAction mapPaintPreferencesAction = new MapPaintDialog.LaunchMapPaintPreferencesAction() {
|
---|
62 |
|
---|
63 | {
|
---|
64 | putValue("toolbar", "mappaintpreference");
|
---|
65 | }
|
---|
66 | };
|
---|
67 |
|
---|
68 | public MapPaintMenu() {
|
---|
69 | super(tr("Map Paint Styles"));
|
---|
70 | setIcon(ImageProvider.get("dialogs", "mapstyle"));
|
---|
71 | MapPaintStyles.addMapPaintSylesUpdateListener(this);
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void mapPaintStylesUpdated() {
|
---|
76 | removeAll();
|
---|
77 | for (StyleSource style : MapPaintStyles.getStyles().getStyleSources()) {
|
---|
78 | final String k = style.getDisplayString();
|
---|
79 | MapPaintAction a = actions.get(k);
|
---|
80 | if (a == null) {
|
---|
81 | actions.put(k, a = new MapPaintAction(style));
|
---|
82 | add(a.getButton());
|
---|
83 | } else {
|
---|
84 | a.setStyle(style);
|
---|
85 | add(a.getButton());
|
---|
86 | a.updateButton();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | addSeparator();
|
---|
90 | add(mapPaintPreferencesAction);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public void mapPaintStyleEntryUpdated(int idx) {
|
---|
95 | mapPaintStylesUpdated();
|
---|
96 | }
|
---|
97 | }
|
---|