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

Last change on this file since 10145 was 9966, checked in by Don-vip, 8 years ago

sonar - remove redundant null-checks

  • Property svn:eol-style set to native
File size: 3.1 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 void addMenuEntry(JMenu menu);
37
38 Object getValue();
39
40 /**
41 * A style setting for boolean value (yes / no).
42 */
43 class BooleanStyleSetting implements StyleSetting {
44 public final StyleSource parentStyle;
45 public final String prefKey;
46 public final String label;
47 public final boolean def;
48
49 public BooleanStyleSetting(StyleSource parentStyle, String prefKey, String label, boolean def) {
50 this.parentStyle = parentStyle;
51 this.prefKey = prefKey;
52 this.label = label;
53 this.def = def;
54 }
55
56 @Override
57 public void addMenuEntry(JMenu menu) {
58 final JCheckBoxMenuItem item = new JCheckBoxMenuItem();
59 Action a = new AbstractAction(label) {
60 @Override
61 public void actionPerformed(ActionEvent e) {
62 setValue(item.isSelected());
63 Main.worker.submit(new MapPaintStyles.MapPaintStyleLoader(Arrays.asList(parentStyle)));
64 }
65 };
66 item.setAction(a);
67 item.setSelected((boolean) getValue());
68 menu.add(item);
69 }
70
71 public static BooleanStyleSetting create(Cascade c, StyleSource parentStyle, String key) {
72 String label = c.get("label", null, String.class);
73 if (label == null) {
74 Main.warn("property 'label' required for boolean style setting");
75 return null;
76 }
77 Boolean def = c.get("default", null, Boolean.class);
78 if (def == null) {
79 Main.warn("property 'default' required for boolean style setting");
80 return null;
81 }
82 String prefKey = parentStyle.url + ":boolean:" + key;
83 return new BooleanStyleSetting(parentStyle, prefKey, label, def);
84 }
85
86 @Override
87 public Object getValue() {
88 String val = Main.pref.get(prefKey, null);
89 if (val == null) return def;
90 return Boolean.valueOf(val);
91 }
92
93 public void setValue(Object o) {
94 if (!(o instanceof Boolean)) {
95 throw new IllegalArgumentException();
96 }
97 boolean b = (Boolean) o;
98 if (b == def) {
99 Main.pref.put(prefKey, null);
100 } else {
101 Main.pref.put(prefKey, b);
102 }
103 }
104 }
105}
Note: See TracBrowser for help on using the repository browser.