Ignore:
Timestamp:
2011-02-05T14:07:23+01:00 (13 years ago)
Author:
bastiK
Message:

improve mapcss support

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/Cascade.java

    r3848 r3856  
    55import java.util.Arrays;
    66import java.util.HashMap;
     7import java.util.List;
    78import java.util.Map;
    89
     
    2627     *      value, def otherwise
    2728     */
    28     public <T> T get(String key, T def, Class klass) {
     29    public <T> T get(String key, T def, Class<T> klass) {
    2930        if (def != null && !klass.isInstance(def))
    3031            throw new IllegalArgumentException();
     
    3233        if (o == null)
    3334            return def;
    34         if (klass.isInstance(o)) {
    35             @SuppressWarnings("unchecked") T res = (T) klass.cast(o);
     35        T res = convertTo(o, klass);
     36        if (res == null) {
     37            System.err.println(String.format("Warning: unable to convert property %s to type %s: found %s of type %s!", key, klass, o, o.getClass()));
     38            return def;
     39        } else
    3640            return res;
    37         }
    38         System.err.println(String.format("Warning: wrong type for mappaint property %s: %s expected, but %s of type %s found!", key, klass, o, o.getClass()));
    39         return def;
    4041    }
    4142
     
    4445    }
    4546
    46     public Float getFloat(String key, Float def) {
    47         Object o = prop.get(key);
     47    @SuppressWarnings("unchecked")
     48    public static <T> T convertTo(Object o, Class<T> klass) {
    4849        if (o == null)
    49             return def;
     50            return null;
     51        if (klass.isInstance(o))
     52            return (T) o;
     53
     54        if (klass == float.class || klass == Float.class)
     55            return (T) toFloat(o);
     56
     57        if (klass == double.class || klass == Double.class) {
     58            o = toFloat(o);
     59            if (o != null) {
     60                o = new Double((Float) o);
     61            }
     62            return (T) o;
     63        }
     64
     65        if (klass == boolean.class || klass == Boolean.class)
     66            return (T) toBool(o);
     67
     68        if (klass == float[].class) {
     69            return (T) toFloatArray(o);
     70        }
     71
     72        if (klass == Color.class) {
     73            return (T) toColor(o);
     74        }
     75        return null;
     76    }
     77
     78    private static Float toFloat(Object o) {
    5079        if (o instanceof Float)
    5180            return (Float) o;
     81        if (o instanceof Double)
     82            return new Float((Double) o);
    5283        if (o instanceof Integer)
    5384            return new Float((Integer) o);
    54         return def;
     85        if (o instanceof String) {
     86            try {
     87                float f = Float.parseFloat((String) o);
     88                return f;
     89            } catch (NumberFormatException e) {
     90            }
     91        }
     92        return null;
    5593    }
    5694
    57     public Color getColor(String key, Color def) {
    58         Object o = prop.get(key);
    59         if (o == null)
    60             return def;
     95    private static Boolean toBool(Object o) {
     96        if (o instanceof Boolean)
     97            return (Boolean) o;
     98        if (o instanceof String)
     99            return Boolean.parseBoolean((String) o);
     100        return null;
     101    }
     102
     103    private static float[] toFloatArray(Object o) {
     104        if (o instanceof float[])
     105            return (float[]) o;
     106        if (o instanceof List) {
     107            List l = (List) o;
     108            float[] a = new float[l.size()];
     109            for (int i=0; i<l.size(); ++i) {
     110                Float f = toFloat(l.get(i));
     111                if (f == null)
     112                    return null;
     113                else
     114                    a[i] = f;
     115            }
     116            return a;
     117        }
     118        return null;
     119    }
     120
     121     public static Color toColor(Object o) {
    61122        if (o instanceof Color)
    62123            return (Color) o;
    63         if (o instanceof String) {
    64             Color clr = CSSColors.get((String) o);
    65             if (clr != null)
    66                 return clr;
    67             else
    68                 return def;
    69         }
    70         return def;
     124        if (o instanceof String)
     125            return CSSColors.get((String) o);
     126        return null;
    71127    }
    72128
     
    111167        return res.append("}").toString();
    112168    }
     169
     170    public boolean containsKey(String key) {
     171        return prop.containsKey(key);
     172    }
    113173}
Note: See TracChangeset for help on using the changeset viewer.