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

Last change on this file since 17318 was 16843, checked in by simon04, 4 years ago

fix #19574 - Add a color setting for MapCSS (patch by taylor.smock)

  • Property svn:eol-style set to native
File size: 5.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import java.awt.Color;
5import java.util.Objects;
6import java.util.Optional;
7
8import javax.swing.Icon;
9
10import org.openstreetmap.josm.data.preferences.AbstractToStringProperty;
11import org.openstreetmap.josm.tools.ImageProvider;
12import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
13import org.openstreetmap.josm.tools.Logging;
14
15/**
16 * Setting to customize a MapPaint style.
17 *
18 * Can be changed by the user in the right click menu of the mappaint style
19 * dialog.
20 *
21 * Defined in the MapCSS style, e.g.
22 * <pre>
23 * setting::highway_casing {
24 * type: boolean;
25 * label: tr("Draw highway casing");
26 * default: true;
27 * }
28 *
29 * way[highway][setting("highway_casing")] {
30 * casing-width: 2;
31 * casing-color: white;
32 * }
33 * </pre>
34 */
35public interface StyleSetting {
36
37 /**
38 * gets the value for this setting
39 * @return The value the user selected
40 */
41 Object getValue();
42
43 /**
44 * Create a matching {@link StyleSettingGui} instances for a given {@link StyleSetting} object.
45 * @return matching {@code StyleSettingGui}
46 * @throws UnsupportedOperationException when class of {@link StyleSetting} is not supported
47 */
48 default StyleSettingGui getStyleSettingGui() {
49 throw new UnsupportedOperationException(getClass() + " not supported");
50 }
51
52 /**
53 * Superclass of style settings and groups.
54 * @since 15289
55 */
56 abstract class LabeledStyleSetting implements Comparable<LabeledStyleSetting> {
57 public final StyleSource parentStyle;
58 public final String label;
59
60 LabeledStyleSetting(StyleSource parentStyle, String label) {
61 this.parentStyle = Objects.requireNonNull(parentStyle);
62 this.label = Objects.requireNonNull(label);
63 }
64
65 @Override
66 public int hashCode() {
67 return Objects.hash(label, parentStyle);
68 }
69
70 @Override
71 public boolean equals(Object obj) {
72 if (this == obj)
73 return true;
74 if (obj == null || getClass() != obj.getClass())
75 return false;
76 LabeledStyleSetting other = (LabeledStyleSetting) obj;
77 return Objects.equals(label, other.label) && Objects.equals(parentStyle, other.parentStyle);
78 }
79
80 @Override
81 public int compareTo(LabeledStyleSetting o) {
82 return label.compareTo(o.label);
83 }
84 }
85
86 /**
87 * A style setting group.
88 * @since 15289
89 */
90 class StyleSettingGroup extends LabeledStyleSetting {
91 /** group identifier */
92 public final String key;
93 /** group icon (optional) */
94 public final Icon icon;
95
96 StyleSettingGroup(StyleSource parentStyle, String label, String key, Icon icon) {
97 super(parentStyle, label);
98 this.key = Objects.requireNonNull(key);
99 this.icon = icon;
100 }
101
102 /**
103 * Creates a new {@code StyleSettingGroup}.
104 * @param c cascade
105 * @param parentStyle parent style source
106 * @param key group identifier
107 * @return newly created {@code StyleSettingGroup}
108 */
109 public static StyleSettingGroup create(Cascade c, StyleSource parentStyle, String key) {
110 String label = c.get("label", null, String.class);
111 if (label == null) {
112 Logging.warn("property 'label' required for StyleSettingGroup");
113 return null;
114 }
115 Icon icon = Optional.ofNullable(c.get("icon", null, String.class))
116 .map(s -> ImageProvider.get(s, ImageSizes.MENU)).orElse(null);
117 return new StyleSettingGroup(parentStyle, label, key, icon);
118 }
119 }
120
121 class PropertyStyleSetting<T> extends LabeledStyleSetting implements StyleSetting {
122 private final Class<T> type;
123 private final AbstractToStringProperty<T> property;
124
125 PropertyStyleSetting(StyleSource parentStyle, String label, Class<T> type, AbstractToStringProperty<T> property) {
126 super(parentStyle, label);
127 this.type = type;
128 this.property = property;
129 }
130
131 /**
132 * Replies the property key.
133 * @return The property key
134 */
135 public String getKey() {
136 return property.getKey();
137 }
138
139 @Override
140 public T getValue() {
141 return property.get();
142 }
143
144 /**
145 * Sets this property to the specified value.
146 * @param value The new value of this property
147 */
148 public void setValue(T value) {
149 property.put(value);
150 }
151
152 /**
153 * Sets this property to the specified string value.
154 * @param value The new string value of this property
155 */
156 public void setStringValue(String value) {
157 setValue(Cascade.convertTo(value, type));
158 }
159
160 @Override
161 public StyleSettingGui getStyleSettingGui() {
162 return new PropertyStyleSettingGui<>(this);
163 }
164 }
165
166 /**
167 * A style setting for boolean value (yes / no).
168 */
169 class BooleanStyleSetting extends PropertyStyleSetting<Boolean> {
170 BooleanStyleSetting(StyleSource parentStyle, String label, AbstractToStringProperty<Boolean> property) {
171 super(parentStyle, label, Boolean.class, property);
172 }
173
174 @Override
175 public StyleSettingGui getStyleSettingGui() {
176 return new BooleanStyleSettingGui(this);
177 }
178 }
179
180 /**
181 * A style setting for color values.
182 * @since 16842
183 */
184 class ColorStyleSetting extends PropertyStyleSetting<Color> {
185 ColorStyleSetting(StyleSource parentStyle, String label, AbstractToStringProperty<Color> property) {
186 super(parentStyle, label, Color.class, property);
187 }
188
189 @Override
190 public StyleSettingGui getStyleSettingGui() {
191 return new ColorStyleSettingGui(this);
192 }
193 }
194}
Note: See TracBrowser for help on using the repository browser.