source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSetting.java@ 12378

Last change on this file since 12378 was 12378, checked in by michael2402, 7 years ago

Document the gui.mappaint package

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