001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint;
003
004import java.awt.Color;
005import java.util.Objects;
006import java.util.Optional;
007
008import javax.swing.Icon;
009
010import org.openstreetmap.josm.data.preferences.AbstractToStringProperty;
011import org.openstreetmap.josm.tools.ImageProvider;
012import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
013import org.openstreetmap.josm.tools.Logging;
014
015/**
016 * Setting to customize a MapPaint style.
017 * <p>
018 * Can be changed by the user in the right click menu of the mappaint style
019 * dialog.
020 * <p>
021 * Defined in the MapCSS style, e.g.
022 * <pre>
023 * setting::highway_casing {
024 *   type: boolean;
025 *   label: tr("Draw highway casing");
026 *   default: true;
027 * }
028 *
029 * way[highway][setting("highway_casing")] {
030 *   casing-width: 2;
031 *   casing-color: white;
032 * }
033 * </pre>
034 */
035@SuppressWarnings("PMD.ImplicitFunctionalInterface")
036public interface StyleSetting {
037
038    /**
039     * gets the value for this setting
040     * @return The value the user selected
041     */
042    Object getValue();
043
044    /**
045     * Create a matching {@link StyleSettingGui} instances for a given {@link StyleSetting} object.
046     * @return matching {@code StyleSettingGui}
047     * @throws UnsupportedOperationException when class of {@link StyleSetting} is not supported
048     */
049    default StyleSettingGui getStyleSettingGui() {
050        throw new UnsupportedOperationException(getClass() + " not supported");
051    }
052
053    /**
054     * Superclass of style settings and groups.
055     * @since 15289
056     */
057    abstract class LabeledStyleSetting implements Comparable<LabeledStyleSetting> {
058        public final StyleSource parentStyle;
059        public final String label;
060
061        LabeledStyleSetting(StyleSource parentStyle, String label) {
062            this.parentStyle = Objects.requireNonNull(parentStyle);
063            this.label = Objects.requireNonNull(label);
064        }
065
066        @Override
067        public int hashCode() {
068            return Objects.hash(label, parentStyle);
069        }
070
071        @Override
072        public boolean equals(Object obj) {
073            if (this == obj)
074                return true;
075            if (obj == null || getClass() != obj.getClass())
076                return false;
077            LabeledStyleSetting other = (LabeledStyleSetting) obj;
078            return Objects.equals(label, other.label) && Objects.equals(parentStyle, other.parentStyle);
079        }
080
081        @Override
082        public int compareTo(LabeledStyleSetting o) {
083            return label.compareTo(o.label);
084        }
085    }
086
087    /**
088     * A style setting group.
089     * @since 15289
090     */
091    class StyleSettingGroup extends LabeledStyleSetting {
092        /** group identifier */
093        public final String key;
094        /** group icon (optional) */
095        public final Icon icon;
096
097        StyleSettingGroup(StyleSource parentStyle, String label, String key, Icon icon) {
098            super(parentStyle, label);
099            this.key = Objects.requireNonNull(key);
100            this.icon = icon;
101        }
102
103        /**
104         * Creates a new {@code StyleSettingGroup}.
105         * @param c cascade
106         * @param parentStyle parent style source
107         * @param key group identifier
108         * @return newly created {@code StyleSettingGroup}
109         */
110        public static StyleSettingGroup create(Cascade c, StyleSource parentStyle, String key) {
111            String label = c.get("label", null, String.class);
112            if (label == null) {
113                Logging.warn("property 'label' required for StyleSettingGroup");
114                return null;
115            }
116            Icon icon = Optional.ofNullable(c.get("icon", null, String.class))
117                    .map(s -> ImageProvider.get(s, ImageSizes.MENU)).orElse(null);
118            return new StyleSettingGroup(parentStyle, label, key, icon);
119        }
120    }
121
122    /**
123     * A setting for a style
124     * @param <T> The property type
125     */
126    class PropertyStyleSetting<T> extends LabeledStyleSetting implements StyleSetting {
127        private final Class<T> type;
128        private final AbstractToStringProperty<T> property;
129
130        PropertyStyleSetting(StyleSource parentStyle, String label, Class<T> type, AbstractToStringProperty<T> property) {
131            super(parentStyle, label);
132            this.type = type;
133            this.property = property;
134        }
135
136        /**
137         * Replies the property key.
138         * @return The property key
139         */
140        public String getKey() {
141            return property.getKey();
142        }
143
144        @Override
145        public T getValue() {
146            return property.get();
147        }
148
149        /**
150         * Sets this property to the specified value.
151         * @param value The new value of this property
152         */
153        public void setValue(T value) {
154            property.put(value);
155        }
156
157        /**
158         * Sets this property to the specified string value.
159         * @param value The new string value of this property
160         */
161        public void setStringValue(String value) {
162            setValue(Cascade.convertTo(value, type));
163        }
164
165        @Override
166        public StyleSettingGui getStyleSettingGui() {
167            return new PropertyStyleSettingGui<>(this);
168        }
169    }
170
171    /**
172     * A style setting for boolean value (yes / no).
173     */
174    class BooleanStyleSetting extends PropertyStyleSetting<Boolean> {
175        BooleanStyleSetting(StyleSource parentStyle, String label, AbstractToStringProperty<Boolean> property) {
176            super(parentStyle, label, Boolean.class, property);
177        }
178
179        @Override
180        public StyleSettingGui getStyleSettingGui() {
181            return new BooleanStyleSettingGui(this);
182        }
183    }
184
185    /**
186     * A style setting for color values.
187     * @since 16842
188     */
189    class ColorStyleSetting extends PropertyStyleSetting<Color> {
190        ColorStyleSetting(StyleSource parentStyle, String label, AbstractToStringProperty<Color> property) {
191            super(parentStyle, label, Color.class, property);
192        }
193
194        @Override
195        public StyleSettingGui getStyleSettingGui() {
196            return new ColorStyleSettingGui(this);
197        }
198    }
199}