Changeset 4007 in josm


Ignore:
Timestamp:
Mar 27, 2011 1:41:58 PM (2 years ago)
Author:
bastiK
Message:

see #6150 mapcss - extended functionality for instruction text: ... (patch by Gubaer)

Location:
trunk/src/org/openstreetmap/josm/gui/mappaint
Files:
5 edited

Legend:

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

    r3987 r4007  
    4848    static public class StaticLabelCompositionStrategy extends LabelCompositionStrategy { 
    4949        private String defaultLabel; 
     50 
    5051        public StaticLabelCompositionStrategy(String defaultLabel){ 
    5152            this.defaultLabel = defaultLabel; 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r3967 r4007  
    2121import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource; 
    2222import org.openstreetmap.josm.gui.mappaint.xml.XmlStyleSource; 
     23import org.openstreetmap.josm.gui.preferences.MapPaintPreference.MapPaintPrefMigration; 
    2324import org.openstreetmap.josm.gui.preferences.SourceEntry; 
    24 import org.openstreetmap.josm.gui.preferences.MapPaintPreference.MapPaintPrefMigration; 
    2525import org.openstreetmap.josm.gui.progress.ProgressMonitor; 
    2626import org.openstreetmap.josm.io.MirroredInputStream; 
     
    4242        return styles; 
    4343    } 
    44      
     44 
     45    /** 
     46     * Value holder for a reference to a tag name. A style instruction 
     47     * <pre> 
     48     *    text: a_tag_name; 
     49     * </pre> 
     50     * results in a tag reference for the tag <tt>a_tag_name</tt> in the 
     51     * style cascade. 
     52     */ 
     53    public static class TagKeyReference { 
     54        public final String key; 
     55        public TagKeyReference(String key){ 
     56            this.key = key; 
     57        } 
     58 
     59        @Override 
     60        public String toString() { 
     61            return "TagKeyReference{" + "key='" + key + "'}"; 
     62        } 
     63    } 
     64 
    4565    public static class IconReference { 
    4666 
    47         public String iconName; 
    48         public StyleSource source; 
     67        public final String iconName; 
     68        public final StyleSource source; 
    4969 
    5070        public IconReference(String iconName, StyleSource source) { 
     
    5979    } 
    6080 
    61     public static ImageIcon getIcon(IconReference ref, boolean sanitize) 
    62     { 
     81    public static ImageIcon getIcon(IconReference ref, boolean sanitize) { 
    6382        String namespace = ref.source.getPrefName(); 
    6483        ImageIcon i = ImageProvider.getIfAvailable(getIconSourceDirs(ref.source), "mappaint."+namespace, null, ref.iconName, ref.source.zipIcons, sanitize); 
     
    112131            dirs.add("resource://images/styles/"); 
    113132        } 
    114          
     133 
    115134        return dirs; 
    116135    } 
     
    130149            source.loadStyleSource(); 
    131150        } 
    132          
     151 
    133152        fireMapPaintSylesUpdated(); 
    134153    } 
     
    139158            in = new MirroredInputStream(entry.url); 
    140159            InputStream zip = in.getZipEntry("xml", "style"); 
    141             if (zip != null) { 
     160            if (zip != null) 
    142161                return new XmlStyleSource(entry); 
    143             } 
    144162            zip = in.getZipEntry("mapcss", "style"); 
    145             if (zip != null) { 
     163            if (zip != null) 
    146164                return new MapCSSStyleSource(entry); 
    147             } 
    148             if (entry.url.toLowerCase().endsWith(".mapcss")) { 
     165            if (entry.url.toLowerCase().endsWith(".mapcss")) 
    149166                return new MapCSSStyleSource(entry); 
    150             } 
    151             if (entry.url.toLowerCase().endsWith(".xml")) { 
     167            if (entry.url.toLowerCase().endsWith(".xml")) 
    152168                return new XmlStyleSource(entry); 
    153             } else { 
     169            else { 
    154170                InputStreamReader reader = new InputStreamReader(in); 
    155171                WHILE: while (true) { 
     
    272288        Arrays.sort(selSorted); 
    273289 
    274         if (i < 0) { // Up 
     290        if (i < 0) // Up 
    275291            return selSorted[0] >= -i; 
    276         } else 
    277         if (i > 0) { // Down 
     292        else if (i > 0) // Down 
    278293            return selSorted[selSorted.length-1] <= styles.getStyleSources().size() - 1 - i; 
    279         } else 
     294        else 
    280295            return true; 
    281296    } 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/TextElement.java

    r4003 r4007  
    99import org.openstreetmap.josm.data.osm.OsmPrimitive; 
    1010import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.DeriveLabelFromNameTagsCompositionStrategy; 
     11import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.StaticLabelCompositionStrategy; 
    1112import org.openstreetmap.josm.gui.mappaint.LabelCompositionStrategy.TagLookupCompositionStrategy; 
     13import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.TagKeyReference; 
    1214import org.openstreetmap.josm.tools.CheckParameterUtil; 
    1315import org.openstreetmap.josm.tools.Utils; 
     
    8183     */ 
    8284    protected static LabelCompositionStrategy buildLabelCompositionStrategy(Cascade c, boolean defaultAnnotate){ 
    83         Keyword textKW = c.get("text", null, Keyword.class, true); 
    84         if (textKW == null) { 
    85             String textKey = c.get("text", null, String.class); 
    86             if (textKey == null)  
    87                 return defaultAnnotate ? AUTO_LABEL_COMPOSITION_STRATEGY : null; 
    88             return new TagLookupCompositionStrategy(textKey); 
    89         } else if (textKW.val.equals("auto")) 
     85        /* 
     86         * If the cascade includes a TagKeyReference we will lookup the rendered label 
     87         * from a tag value. 
     88         */ 
     89        TagKeyReference tkr = c.get("text", null, TagKeyReference.class, true); 
     90        if (tkr != null) 
     91            return new TagLookupCompositionStrategy(tkr.key); 
     92 
     93        /* 
     94         * Check whether the label composition strategy is given by 
     95         * a keyword 
     96         */ 
     97        Keyword keyword = c.get("text", null, Keyword.class, true); 
     98        if (equal(keyword, Keyword.AUTO)) 
    9099            return AUTO_LABEL_COMPOSITION_STRATEGY; 
    91         else 
    92             return new TagLookupCompositionStrategy(textKW.val); 
     100 
     101        /* 
     102         * Do we have a static text label? 
     103         */ 
     104        String text = c.get("text", null, String.class, true); 
     105        if (text != null) 
     106            return new StaticLabelCompositionStrategy(text); 
     107        return defaultAnnotate ? AUTO_LABEL_COMPOSITION_STRATEGY : null; 
    93108    } 
    94109 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Expression.java

    r3980 r4007  
    1616import org.openstreetmap.josm.actions.search.SearchCompiler.Match; 
    1717import org.openstreetmap.josm.actions.search.SearchCompiler.ParseError; 
     18import org.openstreetmap.josm.data.osm.OsmPrimitive; 
     19import org.openstreetmap.josm.data.osm.Relation; 
    1820import org.openstreetmap.josm.gui.mappaint.Cascade; 
    1921import org.openstreetmap.josm.gui.mappaint.Environment; 
     22import org.openstreetmap.josm.tools.CheckParameterUtil; 
    2023import org.openstreetmap.josm.tools.Utils; 
    2124 
    2225public interface Expression { 
    23  
    2426    public Object evaluate(Environment env); 
    2527 
     
    2729        Object literal; 
    2830 
    29         public LiteralExpression(Object lit) { 
    30             this.literal = lit; 
     31        public LiteralExpression(Object literal) { 
     32            CheckParameterUtil.ensureParameterNotNull(literal); 
     33            this.literal = literal; 
    3134        } 
    3235 
     
    3841        @Override 
    3942        public String toString() { 
    40             if (literal == null) 
    41                 return "Lit{<null>}"; 
    4243            if (literal instanceof float[]) 
    4344                return Arrays.toString((float[]) literal); 
     
    4748 
    4849    public static class FunctionExpression implements Expression { 
     50        //static Logger logger = Logger.getLogger(FunctionExpression.class.getName()); 
     51 
    4952        String name; 
    5053        List<Expression> args; 
     
    7376                if (args.length == 0) 
    7477                    return 0f; 
    75                 if (args.length == 1) { // unary minus 
     78                if (args.length == 1) 
    7679                    return -args[0]; 
    77                 } 
    7880                float res = args[0]; 
    7981                for (int i=1; i<args.length; ++i) { 
     
    160162            } 
    161163 
    162             public String get_tag_value(String key) { 
     164            public String tag(String key) { 
    163165                return env.osm.get(key); 
     166            } 
     167 
     168            // FIXME: respect parent selector chain 
     169            public String parent_tag(String key) { 
     170                for (Relation parent: OsmPrimitive.getFilteredList(env.osm.getReferrers(), Relation.class)) { 
     171                    String value = parent.get(key); 
     172                    if (value != null) return value; 
     173                } 
     174                return null; 
    164175            } 
    165176 
     
    286297            } 
    287298            for (Method m : allMethods) { 
    288                 if (!m.getName().equals(name)) 
     299                if (!m.getName().equals(name)) { 
    289300                    continue; 
     301                } 
    290302                Class<?>[] expectedParameterTypes = m.getParameterTypes(); 
    291303                Object[] convertedArgs = new Object[expectedParameterTypes.length]; 
     
    304316                    convertedArgs[0] = arrayArg; 
    305317                } else { 
    306                     if (args.size() != expectedParameterTypes.length) 
     318                    if (args.size() != expectedParameterTypes.length) { 
    307319                        continue; 
     320                    } 
    308321                    for (int i=0; i<args.size(); ++i) { 
    309322                        convertedArgs[i] = Cascade.convertTo(args.get(i).evaluate(env), expectedParameterTypes[i]); 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Instruction.java

    r4006 r4007  
    44import java.util.Arrays; 
    55 
     6import org.openstreetmap.josm.gui.mappaint.Cascade; 
    67import org.openstreetmap.josm.gui.mappaint.Environment; 
     8import org.openstreetmap.josm.gui.mappaint.Keyword; 
     9import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; 
    710import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 
    811 
     
    1215 
    1316    public static class RelativeFloat { 
    14         public float val; 
     17        public final float val; 
    1518 
    1619        public RelativeFloat(float val) { 
     
    2528 
    2629    public static class AssignmentInstruction extends Instruction { 
    27         String key; 
    28         Object val; 
     30        //static private final Logger logger = Logger.getLogger(AssignmentInstruction.class.getName()); 
     31        public final String key; 
     32        public final Object val; 
    2933 
    3034        public AssignmentInstruction(String key, Object val) { 
    3135            this.key = key; 
    3236            if (val instanceof Expression.LiteralExpression) { 
    33                 this.val = ((Expression.LiteralExpression) val).evaluate(null); 
     37                Object litValue = ((Expression.LiteralExpression) val).evaluate(null); 
     38                if (key.equals("text")) { 
     39                    /* Special case for declaration 'text: ...' 
     40                     *  
     41                     * - Treat the value 'auto' as keyword. 
     42                     * - Treat any other literal value 'litval' as as reference to tag with key 'litval' 
     43                     *  
     44                     * - Accept function expressions as is. This allows for 
     45                     *     tag(a_tag_name)                 value of a tag 
     46                     *     eval("a static text")           a static text 
     47                     *     parent_tag(a_tag_name)          value of a tag of a parent relation 
     48                     */ 
     49                    if (litValue.equals(Keyword.AUTO)) { 
     50                        this.val = Keyword.AUTO; 
     51                    } else { 
     52                        String s = Cascade.convertTo(litValue, String.class); 
     53                        if (s != null) { 
     54                            this.val = new MapPaintStyles.TagKeyReference(s); 
     55                        } else { 
     56                            this.val = litValue; 
     57                        } 
     58                    } 
     59                } else { 
     60                    this.val = litValue; 
     61                } 
    3462            } else { 
    3563                this.val = val; 
     
    3967        @Override 
    4068        public void execute(Environment env) { 
    41             Object value = (val instanceof Expression) ? ((Expression) val).evaluate(env) : val; 
     69            Object value = null; 
     70            if (val instanceof Expression) { 
     71                value = ((Expression) val).evaluate(env); 
     72            } else { 
     73                value = val; 
     74            } 
    4275            if (key.equals("icon-image") || key.equals("fill-image") || key.equals("pattern-image")) { 
    4376                if (value instanceof String) { 
Note: See TracChangeset for help on using the changeset viewer.