001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint.mapcss;
003
004import org.openstreetmap.josm.gui.mappaint.Cascade;
005import org.openstreetmap.josm.gui.mappaint.Environment;
006
007/**
008 * A subpart identifies different rendering layers (<code>::subpart</code> syntax).
009 */
010public interface Subpart {
011    String getId(Environment env);
012
013    public static Subpart DEFAULT_SUBPART = new StringSubpart("default");
014
015    /**
016     * Simple static subpart identifier.
017     *
018     * E.g. ::layer_1
019     */
020    public static class StringSubpart implements Subpart {
021        private final String id;
022
023        public StringSubpart(String id) {
024            this.id = id;
025        }
026        @Override
027        public String getId(Environment env) {
028            return id;
029        }
030    }
031
032    /**
033     * Subpart identifier given by an expression.
034     *
035     * E.g. ::(concat("layer_", prop("i", "default")))
036     */
037    public static class ExpressionSubpart implements Subpart {
038        private final Expression id;
039
040        public ExpressionSubpart(Expression id) {
041            this.id = id;
042        }
043        @Override
044        public String getId(Environment env) {
045            return Cascade.convertTo(id.evaluate(env), String.class);
046        }
047    }
048}