Changeset 3967 in josm for trunk/src/org


Ignore:
Timestamp:
2011-03-09T16:01:54+01:00 (13 years ago)
Author:
bastiK
Message:

MapCSS: an identifier literal at the right side of a declaration is now parsed as Keyword and not as String. This means 'color: "blue";' does not work any longer, but you have to write 'color: blue;'. This is useful for future extensions.

Location:
trunk/src/org/openstreetmap/josm/gui/mappaint
Files:
1 added
11 edited

Legend:

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

    r3888 r3967  
    7878
    7979        TextElement text = null;
    80         String textPos = c.get("text-position", null, String.class);
    81         if (textPos == null || Utils.equal(textPos, "center")) {
     80        Keyword textPos = c.get("text-position", null, Keyword.class);
     81        if (textPos == null || Utils.equal(textPos.val, "center")) {
    8282            text = TextElement.create(c, PaintColors.AREA_TEXT.get());
    8383        }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/Cascade.java

    r3904 r3967  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.mappaint;
     3
     4import static org.openstreetmap.josm.tools.Utils.equal;
    35
    46import java.awt.Color;
     
    5456    }
    5557
     58    public void put(String key, Object val) {
     59        prop.put(key, val);
     60    }
     61
     62    public void putOrClear(String key, Object val) {
     63        if (val != null) {
     64            prop.put(key, val);
     65        } else {
     66            prop.remove(key);
     67        }
     68    }
     69
     70    public void remove(String key) {
     71        prop.remove(key);
     72    }
     73
    5674    @SuppressWarnings("unchecked")
    5775    public static <T> T convertTo(Object o, Class<T> klass) {
     
    8199            return (T) toColor(o);
    82100
    83         if (klass == String.class)
     101        if (klass == String.class) {
     102            if (o instanceof Keyword)
     103                return (T) ((Keyword) o).val;
     104
    84105            return (T) o.toString();
     106        }
    85107
    86108        return null;
     
    107129        if (o instanceof Boolean)
    108130            return (Boolean) o;
    109         if (o instanceof String) {
    110             String s = (String) o;
    111             if ("true".equals(o) || "yes".equals(o))
     131        if (o instanceof Keyword) {
     132            String s = ((Keyword) o).val;
     133            if (equal(s, "true") || equal(s, "yes"))
    112134                return true;
    113             if ("false".equals(o) || "no".equals(o))
     135            if (equal(s, "false") || equal(s, "no"))
    114136                return false;
    115137        }
     
    138160    }
    139161
    140      public static Color toColor(Object o) {
     162    private static Color toColor(Object o) {
    141163        if (o instanceof Color)
    142164            return (Color) o;
    143         if (o instanceof String)
    144             return CSSColors.get((String) o);
    145         return null;
    146     }
    147 
    148     public void put(String key, Object val) {
    149         prop.put(key, val);
    150     }
    151 
    152     public void putOrClear(String key, Object val) {
    153         if (val != null) {
    154             prop.put(key, val);
    155         } else {
    156             prop.remove(key);
    157         }
    158     }
    159 
    160     public void remove(String key) {
    161         prop.remove(key);
     165        if (o instanceof Keyword)
     166            return CSSColors.get(((Keyword) o).val);
     167        return null;
    162168    }
    163169
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyle.java

    r3903 r3967  
    3333    public abstract void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member);
    3434
     35    /**
     36     * Get a property value of type Width
     37     * @param c the cascade
     38     * @param key property key for the width value
     39     * @param relativeTo reference width. Only needed, when relative width syntax
     40     *              is used, e.g. "+4".
     41     */
    3542    protected static Float getWidth(Cascade c, String key, Float relativeTo) {
    3643        Float width = c.get(key, null, Float.class, true);
     
    3946                return width;
    4047        } else {
    41             String width_key = c.get(key, null, String.class, true);
    42             if (equal(width_key, "thinnest"))
     48            Keyword widthKW = c.get(key, null, Keyword.class, true);
     49            if (equal(widthKW, Keyword.THINNEST))
    4350                return 0f;
    44             else if(equal(width_key, "default"))
     51            if (equal(widthKW, Keyword.DEFAULT))
    4552                return (float) MapPaintSettings.INSTANCE.getDefaultSegmentWidth();
    46             else if (relativeTo != null) {
     53            if (relativeTo != null) {
    4754                RelativeFloat width_rel = c.get(key, null, RelativeFloat.class, true);
    4855                if (width_rel != null)
     
    5764        float size = c.get("font-size", (float) Main.pref.getInteger("mappaint.fontsize", 8), Float.class);
    5865        int weight = Font.PLAIN;
    59         String weightStr = c.get("font-wheight", null, String.class);
    60         if (equal(weightStr, "bold")) {
     66        Keyword weightKW = c.get("font-wheight", null, Keyword.class);
     67        if (weightKW != null && equal(weightKW, "bold")) {
    6168            weight = Font.BOLD;
    6269        }
    6370        int style = Font.PLAIN;
    64         String styleStr = c.get("font-style", null, String.class);
    65         if (equal(styleStr, "italic")) {
     71        Keyword styleKW = c.get("font-style", null, Keyword.class);
     72        if (styleKW != null && equal(styleKW.val, "italic")) {
    6673            style = Font.ITALIC;
    6774        }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java

    r3907 r3967  
    2121        MultiCascade mc = new MultiCascade();
    2222        Cascade c = mc.getOrCreateCascade("default");
    23         c.put("width", "default");
     23        c.put("width", Keyword.DEFAULT);
    2424        c.put("color", color != null ? color : PaintColors.UNTAGGED.get());
    2525        return createLine(new Environment(null, mc, "default", null));
     
    103103
    104104        int alpha = 255;
    105         Integer pAlpha = Utils.color_float2int(c.get("opacity", null, float.class));
     105        Integer pAlpha = Utils.color_float2int(c.get("opacity", null, Float.class));
    106106        if (pAlpha != null) {
    107107            alpha = pAlpha;
     
    136136        }
    137137
    138         int cap;
    139         String capStr = c.get(prefix + "linecap", null, String.class);
    140         if (equal(capStr, "none")) {
    141             cap = BasicStroke.CAP_BUTT;
    142         } else if (equal(capStr, "round")) {
    143             cap = BasicStroke.CAP_ROUND;
    144         } else if (equal(capStr, "square")) {
    145             cap = BasicStroke.CAP_SQUARE;
    146         } else {
     138        Integer cap = null;
     139        Keyword capKW = c.get(prefix + "linecap", null, Keyword.class);
     140        if (capKW != null) {
     141            if (equal(capKW.val, "none")) {
     142                cap = BasicStroke.CAP_BUTT;
     143            } else if (equal(capKW.val, "round")) {
     144                cap = BasicStroke.CAP_ROUND;
     145            } else if (equal(capKW.val, "square")) {
     146                cap = BasicStroke.CAP_SQUARE;
     147            }
     148        }
     149        if (cap == null) {
    147150            cap = dashes != null ? BasicStroke.CAP_BUTT : BasicStroke.CAP_ROUND;
    148151        }
    149152
    150         int join;
    151         String joinStr = c.get(prefix + "linejoin", null, String.class);
    152         if (equal(joinStr, "round")) {
    153             join = BasicStroke.JOIN_ROUND;
    154         } else if (equal(joinStr, "miter")) {
    155             join = BasicStroke.JOIN_MITER;
    156         } else if (equal(joinStr, "bevel")) {
    157             join = BasicStroke.JOIN_BEVEL;
    158         } else {
     153        Integer join = null;
     154        Keyword joinKW = c.get(prefix + "linejoin", null, Keyword.class);
     155        if (joinKW != null) {
     156            if (equal(joinKW.val, "round")) {
     157                join = BasicStroke.JOIN_ROUND;
     158            } else if (equal(joinKW.val, "miter")) {
     159                join = BasicStroke.JOIN_MITER;
     160            } else if (equal(joinKW.val, "bevel")) {
     161                join = BasicStroke.JOIN_BEVEL;
     162            }
     163        }
     164        if (join == null) {
    159165            join = BasicStroke.JOIN_ROUND;
    160166        }
     
    177183        TextElement text = null;
    178184        if (!casing) {
    179             String textPos = c.get("text-position", null, String.class);
    180             if (textPos == null || equal(textPos, "line")) {
     185            Keyword textPos = c.get("text-position", null, Keyword.class);
     186            if (textPos == null || equal(textPos.val, "line")) {
    181187                text = TextElement.create(c, PaintColors.TEXT.get());
    182188            }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r3930 r3967  
    5555        @Override
    5656        public String toString() {
    57             return "IconReference{" + "iconName=" + iconName + " source=" + source.getDisplayString() + '}';
     57            return "IconReference{" + "iconName='" + iconName + "' source='" + source.getDisplayString() + "'}";
    5858        }
    5959    }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r3903 r3967  
    121121
    122122    }
    123    
     123
    124124    public static final NodeElemStyle SIMPLE_NODE_ELEMSTYLE;
    125125    static {
     
    171171        if (te != null) {
    172172            HorizontalTextAlignment hAlign = HorizontalTextAlignment.RIGHT;
    173             String hAlignStr = c.get("text-anchor-horizontal", null, String.class);
    174             if (equal(hAlignStr, "left")) {
     173            Keyword hAlignKW = c.get("text-anchor-horizontal", Keyword.RIGHT, Keyword.class);
     174            if (equal(hAlignKW.val, "left")) {
    175175                hAlign = HorizontalTextAlignment.LEFT;
    176             } else if (equal(hAlignStr, "center")) {
     176            } else if (equal(hAlignKW.val, "center")) {
    177177                hAlign = HorizontalTextAlignment.CENTER;
    178             } else if (equal(hAlignStr, "right")) {
     178            } else if (equal(hAlignKW.val, "right")) {
    179179                hAlign = HorizontalTextAlignment.RIGHT;
    180180            }
    181181            VerticalTextAlignment vAlign = VerticalTextAlignment.BOTTOM;
    182             String vAlignStr = c.get("text-anchor-vertical", null, String.class);
     182            String vAlignStr = c.get("text-anchor-vertical", Keyword.BOTTOM, Keyword.class).val;
    183183            if (equal(vAlignStr, "above")) {
    184184                vAlign = VerticalTextAlignment.ABOVE;
     
    203203
    204204        SymbolShape shape;
    205         String shapeStr = c.get("symbol-shape", null, String.class);
    206         if (equal(shapeStr, "square")) {
     205        Keyword shapeKW = c.get("symbol-shape", null, Keyword.class);
     206        if (shapeKW == null)
     207            return null;
     208        if (equal(shapeKW, "square")) {
    207209            shape = SymbolShape.SQUARE;
    208         } else if (equal(shapeStr, "circle")) {
     210        } else if (equal(shapeKW, "circle")) {
    209211            shape = SymbolShape.CIRCLE;
    210212        } else
  • trunk/src/org/openstreetmap/josm/gui/mappaint/TextElement.java

    r3899 r3967  
    1212
    1313public class TextElement {
     14    // textKey == null means automatic generation of text string, otherwise
     15    // the corresponding tag value is used
    1416    public String textKey;
    1517    public Font font;
     
    2931
    3032    public static TextElement create(Cascade c, Color defTextColor) {
    31         String textStr = c.get("text", null, String.class);
    32         if (textStr == null)
    33             return null;
    3433
    3534        String textKey = null;
    36         if (!"auto".equalsIgnoreCase(textStr)) {
    37             textKey = textStr;
    38         }
     35        Keyword textKW = c.get("text", null, Keyword.class, true);
     36        if (textKW == null) {
     37            textKey = c.get("text", null, String.class);
     38            if (textKey == null)
     39                return null;
     40        } else if (!textKW.val.equals("auto"))
     41            return null;
    3942
    4043        Font font = ElemStyle.getFont(c);
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/parser/MapCSSParser.java

    r3912 r3967  
    66import java.util.List;
    77
     8import org.openstreetmap.josm.gui.mappaint.Keyword;
    89import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
    910import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
     
    10391040
    10401041  final public Object literal() throws ParseException {
    1041     Object val;
     1042    String val;
    10421043    Token t;
    10431044    float f;
    10441045    switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
    10451046    case IDENT:
     1047      t = jj_consume_token(IDENT);
     1048                    {if (true) return new Keyword(t.image);}
     1049      break;
    10461050    case STRING:
    1047       val = string_or_ident();
    1048                                 {if (true) return val;}
     1051      val = string();
     1052                       {if (true) return val;}
    10491053      break;
    10501054    case PLUS:
     
    11671171  }
    11681172
    1169   private boolean jj_3R_41() {
    1170     if (jj_scan_token(DOLLAR)) return true;
    1171     if (jj_scan_token(EQUAL)) return true;
    1172     return false;
    1173   }
    1174 
    11751173  private boolean jj_3R_40() {
    11761174    if (jj_scan_token(CARET)) return true;
     
    12731271  }
    12741272
     1273  private boolean jj_3R_85() {
     1274    if (jj_scan_token(HEXCOLOR)) return true;
     1275    return false;
     1276  }
     1277
    12751278  private boolean jj_3R_27() {
    12761279    if (jj_3R_50()) return true;
     
    12791282
    12801283  private boolean jj_3R_84() {
    1281     if (jj_scan_token(HEXCOLOR)) return true;
     1284    if (jj_3R_28()) return true;
    12821285    return false;
    12831286  }
    12841287
    12851288  private boolean jj_3R_83() {
     1289    if (jj_scan_token(PLUS)) return true;
    12861290    if (jj_3R_28()) return true;
    12871291    return false;
     
    13041308
    13051309  private boolean jj_3R_82() {
    1306     if (jj_scan_token(PLUS)) return true;
    1307     if (jj_3R_28()) return true;
     1310    if (jj_3R_54()) return true;
    13081311    return false;
    13091312  }
    13101313
    13111314  private boolean jj_3R_81() {
    1312     if (jj_3R_56()) return true;
     1315    if (jj_scan_token(IDENT)) return true;
    13131316    return false;
    13141317  }
     
    13231326    if (jj_3R_83()) {
    13241327    jj_scanpos = xsp;
    1325     if (jj_3R_84()) return true;
     1328    if (jj_3R_84()) {
     1329    jj_scanpos = xsp;
     1330    if (jj_3R_85()) return true;
     1331    }
    13261332    }
    13271333    }
     
    13611367  }
    13621368
    1363   private boolean jj_3R_85() {
     1369  private boolean jj_3R_86() {
    13641370    if (jj_scan_token(COMMA)) return true;
    13651371    if (jj_3R_18()) return true;
     
    13831389    while (true) {
    13841390      xsp = jj_scanpos;
    1385       if (jj_3R_85()) { jj_scanpos = xsp; break; }
     1391      if (jj_3R_86()) { jj_scanpos = xsp; break; }
    13861392    }
    13871393    return false;
     
    18501856  private boolean jj_3R_42() {
    18511857    if (jj_scan_token(STAR)) return true;
     1858    if (jj_scan_token(EQUAL)) return true;
     1859    return false;
     1860  }
     1861
     1862  private boolean jj_3R_41() {
     1863    if (jj_scan_token(DOLLAR)) return true;
    18521864    if (jj_scan_token(EQUAL)) return true;
    18531865    return false;
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/parser/MapCSSParser.jj

    r3912 r3967  
    1111import java.util.List;
    1212
     13import org.openstreetmap.josm.gui.mappaint.Keyword;
    1314import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
    1415import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
     
    523524Object literal() :
    524525{
    525     Object val;
     526    String val;
    526527    Token t;
    527528    float f;
    528529}
    529530{
    530         val=string_or_ident() { return val; }
     531        t=<IDENT> { return new Keyword(t.image); }
     532    |
     533        val=string() { return val; }
    531534    |
    532535        <PLUS> f=ufloat() { return new Instruction.RelativeFloat(f); }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/parser/MapCSSParserTokenManager.java

    r3902 r3967  
    44import java.util.ArrayList;
    55import java.util.List;
     6import org.openstreetmap.josm.gui.mappaint.Keyword;
    67import org.openstreetmap.josm.gui.mappaint.mapcss.Condition;
    78import org.openstreetmap.josm.gui.mappaint.mapcss.Expression;
  • trunk/src/org/openstreetmap/josm/gui/mappaint/xml/XmlStyleSource.java

    r3894 r3967  
    2020import org.openstreetmap.josm.data.osm.Way;
    2121import org.openstreetmap.josm.gui.mappaint.Cascade;
     22import org.openstreetmap.josm.gui.mappaint.Keyword;
    2223import org.openstreetmap.josm.gui.mappaint.MultiCascade;
    2324import org.openstreetmap.josm.gui.mappaint.Range;
     
    288289                    if (icon.annotate != null) {
    289290                        if (icon.annotate) {
    290                             def.put("text", "auto");
     291                            def.put("text", Keyword.AUTO);
    291292                        } else {
    292293                            def.remove("text");
     
    352353            if (p.area != null) {
    353354                def.putOrClear("fill-color", p.area.color);
    354                 def.putOrClear("text-position", "center");
    355                 def.putOrClear("text", "auto");
     355                def.putOrClear("text-position", Keyword.CENTER);
     356                def.putOrClear("text", Keyword.AUTO);
    356357                def.remove("fill-image");
    357358            }
Note: See TracChangeset for help on using the changeset viewer.