Ticket #19574: 10435.colors.patch

File 10435.colors.patch, 7.0 KB (added by taylor.smock, 4 years ago)

Initial colors setting for mapcss

  • src/org/openstreetmap/josm/data/preferences/NamedColorProperty.java

     
    1616 * and customized by the user.
    1717 * @since 12987
    1818 */
    19 public class NamedColorProperty extends AbstractProperty<Color> {
     19public class NamedColorProperty extends AbstractToStringProperty<Color> {
    2020
    2121    public static final String NAMED_COLOR_PREFIX = "clr.";
    2222
     
    6464    }
    6565
    6666    @Override
     67    protected void storeDefaultValue() {
     68        // This is required due to the super() initializer calling this method.
     69        if (category != null) {
     70            super.storeDefaultValue();
     71        }
     72    }
     73
     74    @Override
    6775    public Color get() {
    6876        List<String> data = getPreferences().getList(getKey(), getDefaultValuePref()); // store default value
    6977        if (super.isSet() && data != null && !data.isEmpty()) {
     
    136144    public FallbackProperty<Color> getChildColor(String name) {
    137145        return getChildColor(category, source, name);
    138146    }
     147
     148    @Override
     149    protected Color fromString(String string) {
     150        return ColorHelper.html2color(string);
     151    }
     152
     153    @Override
     154    protected String toString(Color color) {
     155        return ColorHelper.color2html(color);
     156    }
    139157}
  • src/org/openstreetmap/josm/gui/mappaint/ColorStyleSettingGui.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.gui.mappaint;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import java.awt.Color;
     7import java.awt.Component;
     8import java.awt.Graphics;
     9import java.awt.event.ActionEvent;
     10import java.util.Arrays;
     11import java.util.Objects;
     12
     13import javax.swing.AbstractAction;
     14import javax.swing.Icon;
     15import javax.swing.JColorChooser;
     16import javax.swing.JMenu;
     17
     18import org.openstreetmap.josm.gui.MainApplication;
     19import org.openstreetmap.josm.gui.mappaint.StyleSetting.ColorStyleSetting;
     20import org.openstreetmap.josm.gui.mappaint.loader.MapPaintStyleLoader;
     21import org.openstreetmap.josm.tools.ImageProvider;
     22import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
     23
     24/**
     25 * A GUI to set a color style
     26 * @author Taylor Smock
     27 * @since xxx
     28 */
     29public class ColorStyleSettingGui implements StyleSettingGui {
     30
     31    private ColorStyleSetting setting;
     32
     33    /**
     34     * Create a new ColorStyleSettingGui
     35     * @param setting The setting to create the GUI for
     36     */
     37    public ColorStyleSettingGui(ColorStyleSetting setting) {
     38        this.setting = Objects.requireNonNull(setting);
     39    }
     40
     41    static class ColorIcon implements Icon {
     42
     43        private Color color;
     44        private ImageSizes size;
     45
     46        ColorIcon(Color color, ImageProvider.ImageSizes size) {
     47            this.color = color;
     48            this.size = size;
     49        }
     50
     51        @Override
     52        public void paintIcon(Component c, Graphics g, int x, int y) {
     53            Color current = g.getColor();
     54            g.setColor(color);
     55            g.drawRect(x, y, getIconWidth(), getIconHeight());
     56            g.fillRect(x, y, getIconWidth(), getIconHeight());
     57            g.setColor(current); // So that the text is still black
     58        }
     59
     60        @Override
     61        public int getIconWidth() {
     62            return size.getAdjustedWidth();
     63        }
     64
     65        @Override
     66        public int getIconHeight() {
     67            return size.getAdjustedHeight();
     68        }
     69
     70    }
     71
     72    class ColorStyleSettingAction extends AbstractAction {
     73        ColorStyleSettingAction() {
     74            super(setting.label, new ColorIcon(setting.getValue(), ImageSizes.SMALLICON));
     75        }
     76
     77        @Override
     78        public void actionPerformed(ActionEvent e) {
     79            setting.setValue(JColorChooser.showDialog(MainApplication.getMainPanel(), tr("Choose a color"), setting.getValue()));
     80            MainApplication.worker.submit(new MapPaintStyleLoader(Arrays.asList(setting.parentStyle)));
     81        }
     82    }
     83
     84    @Override
     85    public void addMenuEntry(JMenu menu) {
     86        menu.add(new ColorStyleSettingAction());
     87    }
     88
     89}
  • src/org/openstreetmap/josm/gui/mappaint/StyleSetting.java

     
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.mappaint;
    33
     4import java.awt.Color;
    45import java.util.Objects;
    56import java.util.Optional;
    67
     
    175176            return new BooleanStyleSettingGui(this);
    176177        }
    177178    }
     179
     180    /**
     181     * A style setting for color values.
     182     * @since xxx
     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    }
    178194}
  • src/org/openstreetmap/josm/gui/mappaint/StyleSettingFactory.java

     
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.mappaint;
    33
     4import java.awt.Color;
    45import java.util.function.BiFunction;
    56
    67import org.openstreetmap.josm.data.preferences.BooleanProperty;
    78import org.openstreetmap.josm.data.preferences.DoubleProperty;
     9import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    810import org.openstreetmap.josm.data.preferences.StringProperty;
    911import org.openstreetmap.josm.tools.Logging;
    1012
     
    4547                    final StringProperty property = new StringProperty(qualifiedKey, defaultValue);
    4648                    return new StyleSetting.PropertyStyleSetting<>(parentStyle, label, String.class, property);
    4749                });
     50            case "color":
     51                return forLabelAndDefault(c, Color.class, (label, defaultValue) -> {
     52                    final NamedColorProperty property = new NamedColorProperty(NamedColorProperty.COLOR_CATEGORY_MAPPAINT,
     53                            parentStyle.getFileNamePart(), label, defaultValue);
     54                    return new StyleSetting.ColorStyleSetting(parentStyle, label, property);
     55                });
    4856            default:
    4957                Logging.warn("Unknown setting type {0} for style {1}", type, parentStyle.url);
    5058                return null;