001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.imagery;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import javax.json.Json;
007import javax.json.JsonObject;
008import javax.json.JsonObjectBuilder;
009
010import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
011
012/**
013 *
014 * Simple class representing default layer that might be set in imagery information
015 *
016 * This simple class is needed - as for WMS there is different information needed to specify layer than for WMTS
017 *
018 * @author Wiktor Niesiobedzki
019 *
020 */
021public class DefaultLayer {
022    private final String layerName;
023    private final String tileMatrixSet;
024    private final String style;
025
026    /**
027     * Constructor
028     * @param imageryType for which this layer is defined
029     * @param layerName as returned by getIdentifier for WMTS and getName for WMS
030     * @param style of the layer
031     * @param tileMatrixSet only for WMTS - tileMatrixSet to use
032     */
033    public DefaultLayer(ImageryType imageryType, String layerName, String style, String tileMatrixSet) {
034        this.layerName = layerName == null ? "" : layerName;
035        this.style = style == null ? "" : style;
036        if (imageryType != ImageryType.WMTS && !(tileMatrixSet == null || "".equals(tileMatrixSet))) {
037            throw new IllegalArgumentException(tr("{0} imagery has tileMatrixSet defined to: {1}", imageryType, tileMatrixSet));
038        }
039        this.tileMatrixSet = tileMatrixSet == null ? "" : tileMatrixSet;
040    }
041
042    /**
043     * @return layer name of the default layer
044     */
045    public String getLayerName() {
046        return layerName;
047    }
048
049    /**
050     * @return default tileMatrixSet. Only usable for WMTS
051     */
052    public String getTileMatrixSet() {
053        return tileMatrixSet;
054    }
055
056    /**
057     * @return style for this WMS / WMTS layer to use
058     */
059    public String getStyle() {
060        return style;
061    }
062
063    /**
064     * @return JSON representation of the default layer object
065     */
066    public JsonObject toJson() {
067        JsonObjectBuilder ret = Json.createObjectBuilder();
068        ret.add("layerName", layerName);
069        ret.add("style", style);
070        ret.add("tileMatrixSet", tileMatrixSet);
071        return ret.build();
072    }
073
074    /**
075     * Factory method creating DefaultLayer from JSON objects
076     * @param o serialized DefaultLayer object
077     * @param type of ImageryType serialized
078     * @return DefaultLayer instance based on JSON object
079     */
080    public static DefaultLayer fromJson(JsonObject o, ImageryType type) {
081        return new DefaultLayer(type, o.getString("layerName"), o.getString("style"), o.getString("tileMatrixSet"));
082    }
083}