Changeset 5464 in josm


Ignore:
Timestamp:
2012-08-20T23:06:41+02:00 (12 years ago)
Author:
Don-vip
Message:

Rework Properties a bit to simplify management of Color properties

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
1 deleted
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r5462 r5464  
    4949
    5050import org.openstreetmap.josm.Main;
     51import org.openstreetmap.josm.data.preferences.ColorProperty;
    5152import org.openstreetmap.josm.io.MirroredInputStream;
    5253import org.openstreetmap.josm.io.XmlWriter;
     
    241242        String getColorName();
    242243        String getSpecialName();
    243         Color getDefault();
     244        Color getDefaultValue();
    244245    }
    245246
     
    449450    }
    450451
    451     synchronized public boolean getBoolean(final String key) {
     452    synchronized public Boolean getBoolean(final String key) {
    452453        putDefault(key, null);
    453454        return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : false;
    454455    }
    455456
    456     synchronized public boolean getBoolean(final String key, final boolean def) {
     457    synchronized public Boolean getBoolean(final String key, final boolean def) {
    457458        putDefault(key, Boolean.toString(def));
    458459        return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
    459460    }
    460461
    461     synchronized public boolean getBoolean(final String key, final String specName, final boolean def) {
     462    synchronized public Boolean getBoolean(final String key, final String specName, final boolean def) {
    462463        putDefault(key, Boolean.toString(def));
    463464        String skey = key+"."+specName;
     
    802803
    803804    public Color getColor(ColorKey key) {
    804         return getColor(key.getColorName(), key.getSpecialName(), key.getDefault());
     805        return getColor(key.getColorName(), key.getSpecialName(), key.getDefaultValue());
    805806    }
    806807
     
    814815     */
    815816    synchronized public Color getColor(String colName, String specName, Color def) {
    816         String colKey = colName.toLowerCase().replaceAll("[^a-z0-9]+",".");
     817        String colKey = ColorProperty.getColorKey(colName);
    817818        if(!colKey.equals(colName)) {
    818819            colornames.put(colKey, colName);
     
    826827    }
    827828
    828     synchronized public Color getDefaultColor(String colName) {
    829         String colStr = defaults.get("color."+colName);
     829    synchronized public Color getDefaultColor(String colKey) {
     830        String colStr = defaults.get("color."+colKey);
    830831        return colStr == null || "".equals(colStr) ? null : ColorHelper.html2color(colStr);
    831832    }
    832833
    833     synchronized public boolean putColor(String colName, Color val) {
    834         return put("color."+colName, val != null ? ColorHelper.color2html(val) : null);
     834    synchronized public boolean putColor(String colKey, Color val) {
     835        return put("color."+colKey, val != null ? ColorHelper.color2html(val) : null);
    835836    }
    836837
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/PaintColors.java

    r5421 r5464  
    5757    }
    5858
     59    @Override
    5960    public String getColorName() {
    6061        return name;
    6162    }
    6263
    63     public Color getDefault() {
     64    @Override
     65    public Color getDefaultValue() {
    6466        return defaultColor;
    6567    }
    6668
     69    @Override
    6770    public String getSpecialName() {
    6871        return null;
  • trunk/src/org/openstreetmap/josm/data/preferences/AbstractProperty.java

    r4932 r5464  
    55
    66/**
    7  * captures the common functionality of preference properties
     7 * Captures the common functionality of preference properties
     8 * @param <T> The type of object accessed by this property
    89 */
    910public abstract class AbstractProperty<T> {
    1011    protected final String key;
     12    protected final T defaultValue;
    1113
    12     public AbstractProperty(String key) {
     14    /**
     15     * Constructs a new {@code AbstractProperty}.
     16     * @param key The property key
     17     * @param defaultValue The default value
     18     * @since 5464
     19     */
     20    public AbstractProperty(String key, T defaultValue) {
    1321        this.key = key;
     22        this.defaultValue = defaultValue;
    1423    }
    1524
     25    /**
     26     * Replies the property key.
     27     * @return The property key
     28     */
    1629    public String getKey() {
    1730        return key;
    1831    }
    1932
     33    /**
     34     * Determines if this property is currently set in JOSM preferences.
     35     * @return true if {@code Main.pref} contains this property.
     36     */
    2037    public boolean isSet() {
    2138        return !Main.pref.get(key).isEmpty();
    2239    }
    2340
    24     public abstract T getDefaultValue();
     41    /**
     42     * Replies the default value of this property.
     43     * @return The default value of this property
     44     */
     45    public T getDefaultValue() {
     46        return defaultValue;
     47    }
    2548
     49    /**
     50     * Removes this property from JOSM preferences (i.e replace it by its default value).
     51     */
    2652    public void remove() {
    2753        Main.pref.put(getKey(), String.valueOf(getDefaultValue()));
    2854    }
    29 
     55   
     56    /**
     57     * Replies the value of this property.
     58     * @return the value of this property
     59     * @since 5464
     60     */
     61    public abstract T get();
     62   
     63    /**
     64     * Sets this property to the specified value.
     65     * @param value The new value of this property
     66     * @return true if something has changed (i.e. value is different than before)
     67     * @since 5464
     68     */
     69    public abstract boolean put(T value);
    3070}
  • trunk/src/org/openstreetmap/josm/data/preferences/BooleanProperty.java

    r5170 r5464  
    44import org.openstreetmap.josm.Main;
    55
     6/**
     7 * A property containing a {@code Boolean} value.
     8 */
    69public class BooleanProperty extends AbstractProperty<Boolean> {
    710
    8     protected final boolean defaultValue;
    9 
     11    /**
     12     * Constructs a new {@code BooleanProperty}.
     13     * @param key The property key
     14     * @param defaultValue The default value
     15     */
    1016    public BooleanProperty(String key, boolean defaultValue) {
    11         super(key);
    12         this.defaultValue = defaultValue;
     17        super(key, defaultValue);
    1318    }
    1419
    15     public boolean get() {
     20    @Override
     21    public Boolean get() {
    1622        return Main.pref.getBoolean(getKey(), defaultValue);
    1723    }
    1824
    19     public boolean put(boolean value) {
     25    @Override
     26    public boolean put(Boolean value) {
    2027        return Main.pref.put(getKey(), value);
    2128    }
    22 
    23     @Override
    24     public Boolean getDefaultValue() {
    25         return defaultValue;
    26     }
    2729}
  • trunk/src/org/openstreetmap/josm/data/preferences/CachedProperty.java

    r4932 r5464  
    88public abstract class CachedProperty<T> extends AbstractProperty<T> implements PreferenceChangedListener {
    99
    10     protected final String defaultValue;
     10    private final String defaultValueAsString;
    1111    private T value;
    1212    private int updateCount;
    1313
    14     protected CachedProperty(String key, String defaultValue) {
    15         super(key);
     14    protected CachedProperty(String key, T defaultValue, String defaultValueAsString) {
     15        super(key, defaultValue);
    1616        Main.pref.addPreferenceChangeListener(this);
    17         this.defaultValue = defaultValue;
     17        this.defaultValueAsString = defaultValueAsString;
    1818        updateValue();
    1919    }
     
    3030    protected abstract T fromString(String s);
    3131
     32    @Override
    3233    public T get() {
    3334        return value;
     
    4041    }
    4142
     43    @Override
     44    public final boolean put(T value) {
     45        // Not used
     46        throw new IllegalAccessError("You cannot use put(T). Use put(String) instead.");
     47    }
     48
    4249    public int getUpdateCount() {
    4350        return updateCount;
    4451    }
    4552
    46     @Override
    47     public T getDefaultValue() {
    48         return fromString(getDefaultValueAsString());
    49     }
    50 
    5153    public String getDefaultValueAsString() {
    52         return defaultValue;
     54        return defaultValueAsString;
    5355    }
    5456
  • trunk/src/org/openstreetmap/josm/data/preferences/CollectionProperty.java

    r5170 r5464  
    66import org.openstreetmap.josm.Main;
    77
     8/**
     9 * A property containing a {@code Collection} of {@code String} as value.
     10 */
    811public class CollectionProperty extends AbstractProperty<Collection<String>> {
    9     protected final Collection<String> defaultValue;
    1012
     13    /**
     14     * Constructs a new {@code CollectionProperty}.
     15     * @param key The property key
     16     * @param defaultValue The default value
     17     */
    1118    public CollectionProperty(String key, Collection<String> defaultValue) {
    12         super(key);
    13         this.defaultValue = defaultValue;
     19        super(key, defaultValue);
    1420    }
    1521
     22    @Override
    1623    public Collection<String> get() {
    1724        return Main.pref.getCollection(getKey(), getDefaultValue());
    1825    }
    1926
     27    @Override
    2028    public boolean put(Collection<String> value) {
    2129        return Main.pref.putCollection(getKey(), value);
    2230    }
    23 
    24     @Override
    25     public Collection<String> getDefaultValue() {
    26         return defaultValue;
    27     }
    28 
    2931}
  • trunk/src/org/openstreetmap/josm/data/preferences/IntegerProperty.java

    r5170 r5464  
    44import org.openstreetmap.josm.Main;
    55
     6/**
     7 * A property containing an {@code Integer} value.
     8 */
    69public class IntegerProperty extends AbstractProperty<Integer> {
    710
    8     protected final int defaultValue;
    9 
     11    /**
     12     * Constructs a new {@code IntegerProperty}.
     13     * @param key The property key
     14     * @param defaultValue The default value
     15     */
    1016    public IntegerProperty(String key, int defaultValue) {
    11         super(key);
    12         this.defaultValue = defaultValue;
     17        super(key, defaultValue);
    1318    }
    1419
    15     public int get() {
     20    @Override
     21    public Integer get() {
    1622        return Main.pref.getInteger(getKey(), getDefaultValue());
    1723    }
    1824
    19     public boolean put(int value) {
     25    @Override
     26    public boolean put(Integer value) {
    2027        return Main.pref.putInteger(getKey(), value);
    2128    }
     
    3643        return put(intVal);
    3744    }
    38 
    39     @Override
    40     public Integer getDefaultValue() {
    41         return defaultValue;
    42     }
    43 
    4445}
  • trunk/src/org/openstreetmap/josm/data/preferences/StringProperty.java

    r5170 r5464  
    44import org.openstreetmap.josm.Main;
    55
     6/**
     7 * A property containing an {@code String} value.
     8 */
    69public class StringProperty extends AbstractProperty<String> {
    710
    8     protected final String defaultValue;
    9 
     11    /**
     12     * Constructs a new {@code StringProperty}.
     13     * @param key The property key
     14     * @param defaultValue The default value
     15     */
    1016    public StringProperty(String key, String defaultValue) {
    11         super(key);
    12         this.defaultValue = defaultValue;
     17        super(key, defaultValue);
    1318    }
    1419
     20    @Override
    1521    public String get() {
    1622        return Main.pref.get(getKey(), getDefaultValue());
    1723    }
    1824
     25    @Override
    1926    public boolean put(String value) {
    2027        return Main.pref.put(getKey(), value);
    2128    }
    22 
    23     @Override
    24     public String getDefaultValue() {
    25         return defaultValue;
    26     }
    27 
    2829}
  • trunk/src/org/openstreetmap/josm/gui/conflict/ConflictColors.java

    r4163 r5464  
    4545    }
    4646
     47    @Override
    4748    public String getColorName() {
    4849        return name;
    4950    }
    5051
    51     public Color getDefault() {
     52    @Override
     53    public Color getDefaultValue() {
    5254        return defaultColor;
    5355    }
    5456
     57    @Override
    5558    public String getSpecialName() {
    5659        return null;
  • trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java

    r5462 r5464  
    3535import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
    3636import org.openstreetmap.josm.data.imagery.OffsetBookmark;
     37import org.openstreetmap.josm.data.preferences.ColorProperty;
    3738import org.openstreetmap.josm.data.preferences.IntegerProperty;
    3839import org.openstreetmap.josm.gui.MenuScroller;
     
    4344public abstract class ImageryLayer extends Layer {
    4445
     46    public static final ColorProperty PROP_FADE_COLOR = new ColorProperty(marktr("Imagery fade"), Color.white);
    4547    public static final IntegerProperty PROP_FADE_AMOUNT = new IntegerProperty("imagery.fade_amount", 0);
    4648    public static final IntegerProperty PROP_SHARPEN_LEVEL = new IntegerProperty("imagery.sharpen_level", 0);
    4749
    4850    public static Color getFadeColor() {
    49         return Main.pref.getColor(marktr("Imagery fade"), Color.white);
     51        return PROP_FADE_COLOR.get();
    5052    }
    5153
    5254    public static Color getFadeColorWithAlpha() {
    53         Color c = getFadeColor();
     55        Color c = PROP_FADE_COLOR.get();
    5456        return new Color(c.getRed(),c.getGreen(),c.getBlue(),PROP_FADE_AMOUNT.get()*255/100);
    55     }
    56 
    57     public static void setFadeColor(Color color) {
    58         Main.pref.putColor("imagery.fade", color);
    5957    }
    6058
  • trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/Marker.java

    r4869 r5464  
    102102            TemplateEntryProperty result = cache.get(key);
    103103            if (result == null) {
    104                 String defaultValue = layerName == null?getDefaultLabelPattern():"";
    105                 TemplateEntryProperty parent = layerName == null?null:forMarker(null);
    106                 result = new TemplateEntryProperty(key, defaultValue, parent);
    107                 cache.put(key, result);
     104                String defaultValue = layerName == null ? getDefaultLabelPattern():"";
     105                TemplateEntryProperty parent = layerName == null ? null : forMarker(null);
     106                try {
     107                    result = new TemplateEntryProperty(key, defaultValue, parent);
     108                    cache.put(key, result);
     109                } catch (ParseError e) {
     110                    System.out.println(String.format("Unable to parse template engine pattern '%s' for property %s", defaultValue, key));
     111                }
    108112            }
    109113            return result;
     
    118122            if (result == null) {
    119123                String defaultValue = layerName == null?"?{ '{name}' | '{desc}' | '{" + Marker.MARKER_FORMATTED_OFFSET + "}' }":"";
    120                 TemplateEntryProperty parent = layerName == null?null:forAudioMarker(null);
    121                 result = new TemplateEntryProperty(key, defaultValue, parent);
    122                 cache.put(key, result);
     124                TemplateEntryProperty parent = layerName == null ? null : forAudioMarker(null);
     125                try {
     126                    result = new TemplateEntryProperty(key, defaultValue, parent);
     127                    cache.put(key, result);
     128                } catch (ParseError e) {
     129                    System.out.println(String.format("Unable to parse template engine pattern '%s' for property %s", defaultValue, key));
     130                }
    123131            }
    124132            return result;
     
    128136
    129137
    130         private TemplateEntryProperty(String key, String defaultValue, TemplateEntryProperty parent) {
    131             super(key, defaultValue);
     138        private TemplateEntryProperty(String key, String defaultValue, TemplateEntryProperty parent) throws ParseError {
     139            super(key, new TemplateParser(defaultValue).parse(), defaultValue);
    132140            this.parent = parent;
    133141            updateValue(); // Needs to be called because parent wasn't know in super constructor
     
    140148            } catch (ParseError e) {
    141149                System.out.println(String.format("Unable to parse template engine pattern '%s' for property %s. Using default ('%s') instead",
    142                         s, getKey(), defaultValue));
     150                        s, getKey(), super.getDefaultValueAsString()));
    143151                return getDefaultValue();
    144152            }
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java

    r5429 r5464  
    2626import java.util.HashSet;
    2727import java.util.List;
    28 import java.util.Locale;
    2928import java.util.Map;
    3029import java.util.Set;
     
    129128        final JPanel p = new JPanel(new GridBagLayout());
    130129
    131         this.colFadeColor = ImageryLayer.getFadeColor();
     130        this.colFadeColor = ImageryLayer.PROP_FADE_COLOR.get();
    132131        this.btnFadeColor = new JButton();
    133132
     
    196195        JLabel labelEast = new JLabel(tr("% of east:"));
    197196        JLabel labelNorth = new JLabel(tr("% of north:"));
    198         spinEast = new JSpinner(new SpinnerNumberModel(WMSLayer.PROP_OVERLAP_EAST.get(), 1, 50, 1));
    199         spinNorth = new JSpinner(new SpinnerNumberModel(WMSLayer.PROP_OVERLAP_NORTH.get(), 1, 50, 1));
     197        spinEast = new JSpinner(new SpinnerNumberModel(WMSLayer.PROP_OVERLAP_EAST.get().intValue(), 1, 50, 1));
     198        spinNorth = new JSpinner(new SpinnerNumberModel(WMSLayer.PROP_OVERLAP_NORTH.get().intValue(), 1, 50, 1));
    200199
    201200        JPanel overlapPanel = new JPanel(new FlowLayout());
     
    211210        p.add(Box.createHorizontalGlue(), GBC.eol().fill(GBC.HORIZONTAL));
    212211        JLabel labelSimConn = new JLabel(tr("Simultaneous connections"));
    213         spinSimConn = new JSpinner(new SpinnerNumberModel(WMSLayer.PROP_SIMULTANEOUS_CONNECTIONS.get(), 1, 30, 1));
     212        spinSimConn = new JSpinner(new SpinnerNumberModel(WMSLayer.PROP_SIMULTANEOUS_CONNECTIONS.get().intValue(), 1, 30, 1));
    214213        JPanel overlapPanelSimConn = new JPanel(new FlowLayout(FlowLayout.LEFT));
    215214        overlapPanelSimConn.add(labelSimConn);
     
    352351
    353352        ImageryLayer.PROP_FADE_AMOUNT.put(this.fadeAmount.getValue());
    354         ImageryLayer.setFadeColor(this.colFadeColor);
     353        ImageryLayer.PROP_FADE_COLOR.put(this.colFadeColor);
    355354        ImageryLayer.PROP_SHARPEN_LEVEL.put(sharpen.getSelectedIndex());
    356355
Note: See TracChangeset for help on using the changeset viewer.