Ticket #19574: 10435.colors.patch
File 10435.colors.patch, 7.0 KB (added by , 5 years ago) |
---|
-
src/org/openstreetmap/josm/data/preferences/NamedColorProperty.java
16 16 * and customized by the user. 17 17 * @since 12987 18 18 */ 19 public class NamedColorProperty extends Abstract Property<Color> {19 public class NamedColorProperty extends AbstractToStringProperty<Color> { 20 20 21 21 public static final String NAMED_COLOR_PREFIX = "clr."; 22 22 … … 64 64 } 65 65 66 66 @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 67 75 public Color get() { 68 76 List<String> data = getPreferences().getList(getKey(), getDefaultValuePref()); // store default value 69 77 if (super.isSet() && data != null && !data.isEmpty()) { … … 136 144 public FallbackProperty<Color> getChildColor(String name) { 137 145 return getChildColor(category, source, name); 138 146 } 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 } 139 157 } -
src/org/openstreetmap/josm/gui/mappaint/ColorStyleSettingGui.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.gui.mappaint; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 import java.awt.Color; 7 import java.awt.Component; 8 import java.awt.Graphics; 9 import java.awt.event.ActionEvent; 10 import java.util.Arrays; 11 import java.util.Objects; 12 13 import javax.swing.AbstractAction; 14 import javax.swing.Icon; 15 import javax.swing.JColorChooser; 16 import javax.swing.JMenu; 17 18 import org.openstreetmap.josm.gui.MainApplication; 19 import org.openstreetmap.josm.gui.mappaint.StyleSetting.ColorStyleSetting; 20 import org.openstreetmap.josm.gui.mappaint.loader.MapPaintStyleLoader; 21 import org.openstreetmap.josm.tools.ImageProvider; 22 import org.openstreetmap.josm.tools.ImageProvider.ImageSizes; 23 24 /** 25 * A GUI to set a color style 26 * @author Taylor Smock 27 * @since xxx 28 */ 29 public 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
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.mappaint; 3 3 4 import java.awt.Color; 4 5 import java.util.Objects; 5 6 import java.util.Optional; 6 7 … … 175 176 return new BooleanStyleSettingGui(this); 176 177 } 177 178 } 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 } 178 194 } -
src/org/openstreetmap/josm/gui/mappaint/StyleSettingFactory.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.mappaint; 3 3 4 import java.awt.Color; 4 5 import java.util.function.BiFunction; 5 6 6 7 import org.openstreetmap.josm.data.preferences.BooleanProperty; 7 8 import org.openstreetmap.josm.data.preferences.DoubleProperty; 9 import org.openstreetmap.josm.data.preferences.NamedColorProperty; 8 10 import org.openstreetmap.josm.data.preferences.StringProperty; 9 11 import org.openstreetmap.josm.tools.Logging; 10 12 … … 45 47 final StringProperty property = new StringProperty(qualifiedKey, defaultValue); 46 48 return new StyleSetting.PropertyStyleSetting<>(parentStyle, label, String.class, property); 47 49 }); 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 }); 48 56 default: 49 57 Logging.warn("Unknown setting type {0} for style {1}", type, parentStyle.url); 50 58 return null;