Ticket #6150: mapcss-parent_tag-bastiK-version.patch
File mapcss-parent_tag-bastiK-version.patch, 6.6 KB (added by , 14 years ago) |
---|
-
src/org/openstreetmap/josm/gui/mappaint/Environment.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.mappaint; 3 3 4 import java.util.List; 5 4 6 import org.openstreetmap.josm.data.osm.OsmPrimitive; 7 import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction; 8 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector; 5 9 6 10 public class Environment { 7 11 8 12 public OsmPrimitive osm; 9 13 public MultiCascade mc; 10 14 public String layer; 11 15 public StyleSource source; 16 /** 17 * <p>after the selectors of a MapCSS rule have been applied to {@code osm}, the matching 18 * parent objects of {@code osm} are remembered in this field. It can be accessed in 19 * {@link Instruction#execute(Environment)} to access tags from parent objects.</p> 20 * 21 */ 22 public List<OsmPrimitive> matchingReferrers = null; 12 23 13 24 public Environment(OsmPrimitive osm, MultiCascade mc, String layer, StyleSource source) { 14 25 this.osm = osm; … … 17 28 this.source = source; 18 29 } 19 30 31 public void setMatchingReferrers(List<OsmPrimitive> refs) { 32 matchingReferrers = refs; 33 } 34 35 public void forgetMatchingReferrers() { 36 this.matchingReferrers = null; 37 } 20 38 } -
src/org/openstreetmap/josm/gui/mappaint/mapcss/Expression.java
16 16 import org.openstreetmap.josm.actions.search.SearchCompiler.Match; 17 17 import org.openstreetmap.josm.actions.search.SearchCompiler.ParseError; 18 18 import org.openstreetmap.josm.data.osm.OsmPrimitive; 19 import org.openstreetmap.josm.data.osm.Relation;20 19 import org.openstreetmap.josm.gui.mappaint.Cascade; 21 20 import org.openstreetmap.josm.gui.mappaint.Environment; 22 21 import org.openstreetmap.josm.tools.CheckParameterUtil; … … 173 172 return env.osm.get(key); 174 173 } 175 174 176 // FIXME: respect parent selector chain177 175 public String parent_tag(String key) { 178 for (Relation parent: OsmPrimitive.getFilteredList(env.osm.getReferrers(), Relation.class)) { 176 if (env.matchingReferrers == null) { 177 for (OsmPrimitive parent : env.osm.getReferrers()) { 178 String value = parent.get(key); 179 if (value != null) return value; 180 } 181 return null; 182 } 183 for (OsmPrimitive parent: env.matchingReferrers) { 179 184 String value = parent.get(key); 180 185 if (value != null) return value; 181 186 } -
src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.mappaint.mapcss; 3 3 4 import java.util.ArrayList; 4 5 import java.util.List; 5 6 6 7 import org.openstreetmap.josm.data.osm.Node; … … 14 15 import org.openstreetmap.josm.tools.Utils; 15 16 16 17 public interface Selector { 17 18 18 19 public boolean applies(Environment e); 19 20 20 21 public String getSubpart(); 21 22 public Range getRange(); 22 23 24 /** 25 * <p>Despite its name represents a <em>child selector</em>. JOSM doesn't support 26 * descendant selectors yet.</p> 27 * 28 * <p>In addition to the standard CSS notation for child selectors, JOSM also supports 29 * an "inverse" notation:</p> 30 * <pre> 31 * // the standard notation: 32 * parentselector > childselector { ... } 33 * 34 * // the inverse notation: 35 * childselector < parentselector { ... } 36 * </pre> 37 * 38 */ 23 39 public static class DescendentSelector implements Selector { 24 40 Selector a, b; 25 boolean child; 41 /** true, if this represents a child selector in inverse notation 42 * {@code childselector < parentselector} 43 */ 44 boolean inverseNotation; 26 45 46 /** 47 * 48 * @param a the first selector 49 * @param b the second selector 50 * @param child if true {@code a} is the child selector; otherwise, {@code a} 51 * is the parent selector 52 */ 27 53 public DescendentSelector(Selector a, Selector b, boolean child) { 28 54 this.a = a; 29 55 this.b = b; 30 this. child =child;56 this.inverseNotation = !child; 31 57 } 32 58 33 59 @Override … … 36 62 return false; 37 63 38 64 Environment e2 = new Environment(null, e.mc, e.layer, e.source); 39 if (child) { 65 List<OsmPrimitive> matchingRefs = new ArrayList<OsmPrimitive>(); 66 if (!inverseNotation) { 67 boolean foundMatchingReferrer = false; 40 68 for (OsmPrimitive osm : e.osm.getReferrers()) { 41 69 e2.osm = osm; 42 if (a.applies(e2)) 43 return true; 70 if (a.applies(e2)) { 71 matchingRefs.add(osm); 72 foundMatchingReferrer = true; 73 } 44 74 } 75 if (foundMatchingReferrer) { 76 e.setMatchingReferrers(matchingRefs); 77 return true; 78 } 45 79 } else { 46 80 if (e.osm instanceof Relation) { 47 81 for (OsmPrimitive chld : ((Relation) e.osm).getMemberPrimitives()) { -
src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java
139 139 Environment env = new Environment(osm, mc, null, this); 140 140 for (MapCSSRule r : rules) { 141 141 for (Selector s : r.selectors) { 142 env.forgetMatchingReferrers(); 142 143 if (s.applies(env)) { 143 144 if (s.getRange().contains(scale)) { 144 145 mc.range = Range.cut(mc.range, s.getRange());