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

Last change on this file since 8674 was 8674, checked in by Don-vip, 9 years ago

fix Checkstyle issues

  • Property svn:eol-style set to native
File size: 3.0 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 boolean b = item.isSelected();
63 if (b == def) {
64 Main.pref.put(prefKey, null);
65 } else {
66 Main.pref.put(prefKey, b);
67 }
68 Main.worker.submit(new MapPaintStyles.MapPaintStyleLoader(Arrays.asList(parentStyle)));
69 }
70 };
71 item.setAction(a);
72 item.setSelected((boolean) getValue());
73 menu.add(item);
74 }
75
76 public static BooleanStyleSetting create(Cascade c, StyleSource parentStyle, String key) {
77 String label = c.get("label", null, String.class);
78 if (label == null) {
79 Main.warn("property 'label' required for boolean style setting");
80 return null;
81 }
82 Boolean def = c.get("default", null, Boolean.class);
83 if (def == null) {
84 Main.warn("property 'default' required for boolean style setting");
85 return null;
86 }
87 String prefKey = parentStyle.url + ":boolean:" + key;
88 return new BooleanStyleSetting(parentStyle, prefKey, label, def);
89 }
90
91 @Override
92 public Object getValue() {
93 String val = Main.pref.get(prefKey, null);
94 if (val == null) return def;
95 return Boolean.valueOf(val);
96 }
97 }
98}
Note: See TracBrowser for help on using the repository browser.