1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint;
|
---|
3 |
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.util.Arrays;
|
---|
6 | import javax.swing.AbstractAction;
|
---|
7 | import javax.swing.Action;
|
---|
8 | import javax.swing.JCheckBoxMenuItem;
|
---|
9 | import javax.swing.JMenu;
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Setting to customize a MapPaint style.
|
---|
14 | *
|
---|
15 | * Can be changed by the user in the right click menu of the mappaint style
|
---|
16 | * dialog.
|
---|
17 | *
|
---|
18 | * Defined in the MapCSS style, e.g.
|
---|
19 | * <pre>
|
---|
20 | * setting::highway_casing {
|
---|
21 | * type: boolean;
|
---|
22 | * label: tr("Draw highway casing");
|
---|
23 | * default: true;
|
---|
24 | * }
|
---|
25 | *
|
---|
26 | * way[highway][setting("highway_casing")] {
|
---|
27 | * casing-width: 2;
|
---|
28 | * casing-color: white;
|
---|
29 | * }
|
---|
30 | * </pre>
|
---|
31 | */
|
---|
32 | public interface StyleSetting {
|
---|
33 |
|
---|
34 | void addMenuEntry(JMenu menu);
|
---|
35 |
|
---|
36 | Object getValue();
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * A style setting for boolean value (yes / no).
|
---|
40 | */
|
---|
41 | public static class BooleanStyleSetting implements StyleSetting {
|
---|
42 | public final StyleSource parentStyle;
|
---|
43 | public final String prefKey;
|
---|
44 | public final String label;
|
---|
45 | public final boolean def;
|
---|
46 |
|
---|
47 | public BooleanStyleSetting(StyleSource parentStyle, String prefKey, String label, boolean def) {
|
---|
48 | this.parentStyle = parentStyle;
|
---|
49 | this.prefKey = prefKey;
|
---|
50 | this.label = label;
|
---|
51 | this.def = def;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void addMenuEntry(JMenu menu) {
|
---|
56 | final JCheckBoxMenuItem item = new JCheckBoxMenuItem();
|
---|
57 | Action a = new AbstractAction(label) {
|
---|
58 | @Override
|
---|
59 | public void actionPerformed(ActionEvent e) {
|
---|
60 | boolean b = item.isSelected();
|
---|
61 | if (b == def) {
|
---|
62 | Main.pref.put(prefKey, null);
|
---|
63 | } else {
|
---|
64 | Main.pref.put(prefKey, b);
|
---|
65 | }
|
---|
66 | Main.worker.submit(new MapPaintStyles.MapPaintStyleLoader(Arrays.asList(parentStyle)));
|
---|
67 | }
|
---|
68 | };
|
---|
69 | item.setAction(a);
|
---|
70 | item.setSelected((boolean) getValue());
|
---|
71 | menu.add(item);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public static BooleanStyleSetting create(Cascade c, StyleSource parentStyle, String key) {
|
---|
75 | String label = c.get("label", null, String.class);
|
---|
76 | if (label == null) {
|
---|
77 | Main.warn("property 'label' required for boolean style setting");
|
---|
78 | return null;
|
---|
79 | }
|
---|
80 | Boolean def = c.get("default", null, Boolean.class);
|
---|
81 | if (def == null) {
|
---|
82 | Main.warn("property 'default' required for boolean style setting");
|
---|
83 | return null;
|
---|
84 | }
|
---|
85 | String prefKey = parentStyle.url + ":boolean:" + key;
|
---|
86 | return new BooleanStyleSetting(parentStyle, prefKey, label, def);
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public Object getValue() {
|
---|
91 | String val = Main.pref.get(prefKey, null);
|
---|
92 | if (val == null) return def;
|
---|
93 | return Boolean.parseBoolean(val);
|
---|
94 | }
|
---|
95 |
|
---|
96 | }
|
---|
97 | }
|
---|