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

Last change on this file was 18972, checked in by taylor.smock, 4 months ago

See #23465: Add additional javadoc comments

This also fixes some sonarlint issues

  • Property svn:eol-style set to native
File size: 6.0 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 * <p>
18 * Can be changed by the user in the right click menu of the mappaint style
19 * dialog.
20 * <p>
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 /**
122 * A setting for a style
123 * @param <T> The property type
124 */
125 class PropertyStyleSetting<T> extends LabeledStyleSetting implements StyleSetting {
126 private final Class<T> type;
127 private final AbstractToStringProperty<T> property;
128
129 PropertyStyleSetting(StyleSource parentStyle, String label, Class<T> type, AbstractToStringProperty<T> property) {
130 super(parentStyle, label);
131 this.type = type;
132 this.property = property;
133 }
134
135 /**
136 * Replies the property key.
137 * @return The property key
138 */
139 public String getKey() {
140 return property.getKey();
141 }
142
143 @Override
144 public T getValue() {
145 return property.get();
146 }
147
148 /**
149 * Sets this property to the specified value.
150 * @param value The new value of this property
151 */
152 public void setValue(T value) {
153 property.put(value);
154 }
155
156 /**
157 * Sets this property to the specified string value.
158 * @param value The new string value of this property
159 */
160 public void setStringValue(String value) {
161 setValue(Cascade.convertTo(value, type));
162 }
163
164 @Override
165 public StyleSettingGui getStyleSettingGui() {
166 return new PropertyStyleSettingGui<>(this);
167 }
168 }
169
170 /**
171 * A style setting for boolean value (yes / no).
172 */
173 class BooleanStyleSetting extends PropertyStyleSetting<Boolean> {
174 BooleanStyleSetting(StyleSource parentStyle, String label, AbstractToStringProperty<Boolean> property) {
175 super(parentStyle, label, Boolean.class, property);
176 }
177
178 @Override
179 public StyleSettingGui getStyleSettingGui() {
180 return new BooleanStyleSettingGui(this);
181 }
182 }
183
184 /**
185 * A style setting for color values.
186 * @since 16842
187 */
188 class ColorStyleSetting extends PropertyStyleSetting<Color> {
189 ColorStyleSetting(StyleSource parentStyle, String label, AbstractToStringProperty<Color> property) {
190 super(parentStyle, label, Color.class, property);
191 }
192
193 @Override
194 public StyleSettingGui getStyleSettingGui() {
195 return new ColorStyleSettingGui(this);
196 }
197 }
198}
Note: See TracBrowser for help on using the repository browser.