Ticket #17021: base_constants.patch

File base_constants.patch, 8.0 KB (added by GerdP, 7 years ago)
  • src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

     
    471471                MapCSSRule optRule = new MapCSSRule(r.selector.optimizedBaseCheck(), r.declaration);
    472472                final String base = ((GeneralSelector) selRightmost).getBase();
    473473                switch (base) {
    474                     case "node":
     474                    case Selector.BASE_NODE:
    475475                        nodeRules.add(optRule);
    476476                        break;
    477                     case "way":
     477                    case Selector.BASE_WAY:
    478478                        wayNoAreaRules.add(optRule);
    479479                        wayRules.add(optRule);
    480480                        break;
    481                     case "area":
     481                    case Selector.BASE_AREA:
    482482                        wayRules.add(optRule);
    483483                        multipolygonRules.add(optRule);
    484484                        break;
    485                     case "relation":
     485                    case Selector.BASE_RELATION:
    486486                        relationRules.add(optRule);
    487487                        multipolygonRules.add(optRule);
    488488                        break;
    489                     case "*":
     489                    case Selector.BASE_ANY:
    490490                        nodeRules.add(optRule);
    491491                        wayRules.add(optRule);
    492492                        wayNoAreaRules.add(optRule);
     
    493493                        relationRules.add(optRule);
    494494                        multipolygonRules.add(optRule);
    495495                        break;
    496                     case "canvas":
     496                    case Selector.BASE_CANVAS:
    497497                        canvasRules.add(r);
    498498                        break;
    499                     case "meta":
    500                     case "setting":
     499                    case Selector.BASE_META:
     500                    case Selector.BASE_SETTING:
    501501                        break;
    502502                    default:
    503503                        final RuntimeException e = new JosmRuntimeException(MessageFormat.format("Unknown MapCSS base selector {0}", base));
     
    556556     * load meta info from a selector "meta"
    557557     */
    558558    private void loadMeta() {
    559         Cascade c = constructSpecial("meta");
     559        Cascade c = constructSpecial(Selector.BASE_META);
    560560        String pTitle = c.get("title", null, String.class);
    561561        if (title == null) {
    562562            title = pTitle;
     
    568568    }
    569569
    570570    private void loadCanvas() {
    571         Cascade c = constructSpecial("canvas");
     571        Cascade c = constructSpecial(Selector.BASE_CANVAS);
    572572        backgroundColorOverride = c.get("fill-color", null, Color.class);
    573573    }
    574574
     
    585585        for (MapCSSRule r : rules) {
    586586            if (r.selector instanceof GeneralSelector) {
    587587                GeneralSelector gs = (GeneralSelector) r.selector;
    588                 if ("setting".equals(gs.getBase())) {
     588                if (Selector.BASE_SETTING.equals(gs.getBase())) {
    589589                    if (!gs.matchesConditions(env)) {
    590590                        continue;
    591591                    }
     
    737737            MapCSSRule x = it.next();
    738738            if (x.selector instanceof GeneralSelector) {
    739739                GeneralSelector gs = (GeneralSelector) x.selector;
    740                 if ("meta".equals(gs.base)) {
     740                if (Selector.BASE_META.equals(gs.base)) {
    741741                    it.remove();
    742742                }
    743743            }
  • src/org/openstreetmap/josm/gui/mappaint/mapcss/Selector.java

     
    5050 */
    5151public interface Selector {
    5252
     53    /** selector base that matches anything. */
     54    public static final String BASE_ANY = "*";
     55    /** selector base that matches on OSM object node. */
     56    public static final String BASE_NODE = "node";
     57    /** selector base that matches on OSM object way. */
     58    public static final String BASE_WAY = "way";
     59    /** selector base that matches on OSM object relation. */
     60    public static final String BASE_RELATION = "relation";
     61
     62    /** selector base that matches with any area regardless of whether the area border is only modelled with a single way or with a set of ways glued together with a relation.*/
     63    public static final String BASE_AREA = "area";
     64
     65    /** selector base for special rules containing meta information. */
     66    public static final String BASE_META = "meta";
     67
     68    /** selector base for style information not specific to nodes, ways or relations. */
     69    public static final String BASE_CANVAS = "canvas";
     70
     71    /** selector base for artificial bases created to use preferences. */
     72    public static final String BASE_SETTING = "setting";
     73
    5374    /**
    5475     * Apply the selector to the primitive and check if it matches.
    5576     *
     
    575596
    576597        public OptimizedGeneralSelector(String base, Pair<Integer, Integer> zoom, List<Condition> conds, Subpart subpart) {
    577598            super(conds);
    578             this.base = base;
     599            this.base = checkBase(base);
    579600            if (zoom != null) {
    580601                int a = zoom.a == null ? 0 : zoom.a;
    581602                int b = zoom.b == null ? Integer.MAX_VALUE : zoom.b;
     
    592613
    593614        public OptimizedGeneralSelector(String base, Range range, List<Condition> conds, Subpart subpart) {
    594615            super(conds);
    595             this.base = base;
     616            this.base = checkBase(base);
    596617            this.range = range;
    597618            this.subpart = subpart != null ? subpart : Subpart.DEFAULT_SUBPART;
    598619        }
     
    611632            return range;
    612633        }
    613634
     635        /**
     636         * Check if this is a known base and return the corresponding string constant.
     637         * @param base
     638         * @return the matching String constant
     639         */
     640        private static String checkBase(String base) {
     641            switch(base) {
     642            case "*": return BASE_ANY;
     643            case "node": return BASE_NODE;
     644            case "way": return BASE_WAY;
     645            case "relation": return BASE_RELATION;
     646            case "area": return BASE_AREA;
     647            case "meta": return BASE_META;
     648            case "canvas": return BASE_CANVAS;
     649            case "setting": return BASE_SETTING;
     650            default:
     651                throw new IllegalArgumentException("unknown selector " + base);
     652            }
     653        }
     654
    614655        public String getBase() {
    615656            return base;
    616657        }
    617658
    618659        public boolean matchesBase(OsmPrimitiveType type) {
    619             if ("*".equals(base)) {
     660            if (BASE_ANY.equals(base)) {
    620661                return true;
    621662            } else if (OsmPrimitiveType.NODE == type) {
    622                 return "node".equals(base);
     663                return BASE_NODE.equals(base);
    623664            } else if (OsmPrimitiveType.WAY == type) {
    624                 return "way".equals(base) || "area".equals(base);
     665                return BASE_WAY.equals(base) || BASE_AREA.equals(base);
    625666            } else if (OsmPrimitiveType.RELATION == type) {
    626                 return "area".equals(base) || "relation".equals(base) || "canvas".equals(base);
     667                return BASE_AREA.equals(base) || BASE_RELATION.equals(base) || BASE_CANVAS.equals(base);
    627668            }
    628669            return false;
    629670        }
     
    633674                return false;
    634675            } else {
    635676                if (p instanceof IRelation) {
    636                     if ("area".equals(base)) {
     677                    if (BASE_AREA.equals(base)) {
    637678                        return ((IRelation<?>) p).isMultipolygon();
    638                     } else if ("canvas".equals(base)) {
     679                    } else if (BASE_CANVAS.equals(base)) {
    639680                        return p.get("#canvas") != null;
    640681                    }
    641682                }