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

Last change on this file since 12675 was 12651, checked in by Don-vip, 7 years ago

see #15182 - code refactoring to avoid dependence on GUI packages from MapPaintStyles

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