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    Subpart DEFAULT_SUBPART = new StringSubpart("default");
014
015    /**
016     * Simple static subpart identifier.
017     *
018     * E.g. ::layer_1
019     */
020    class StringSubpart implements Subpart {
021        private final String id;
022
023        public StringSubpart(String id) {
024            this.id = id;
025        }
026
027        @Override
028        public String getId(Environment env) {
029            return id;
030        }
031
032        @Override
033        public String toString() {
034            return id;
035        }
036    }
037
038    /**
039     * Subpart identifier given by an expression.
040     *
041     * E.g. ::(concat("layer_", prop("i", "default")))
042     */
043    class ExpressionSubpart implements Subpart {
044        private final Expression id;
045
046        public ExpressionSubpart(Expression id) {
047            this.id = id;
048        }
049
050        @Override
051        public String getId(Environment env) {
052            return Cascade.convertTo(id.evaluate(env), String.class);
053        }
054
055        @Override
056        public String toString() {
057            return String.valueOf(id);
058        }
059    }
060}