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     * Returns layer name of the default layer.
044     * @return layer name of the default layer
045     */
046    public String getLayerName() {
047        return layerName;
048    }
049
050    /**
051     * Returns default tileMatrixSet. Only usable for WMTS
052     * @return default tileMatrixSet. Only usable for WMTS
053     */
054    public String getTileMatrixSet() {
055        return tileMatrixSet;
056    }
057
058    /**
059     * Returns style for this WMS / WMTS layer to use.
060     * @return style for this WMS / WMTS layer to use
061     */
062    public String getStyle() {
063        return style;
064    }
065
066    /**
067     * Returns JSON representation of the default layer object.
068     * @return JSON representation of the default layer object
069     */
070    public JsonObject toJson() {
071        JsonObjectBuilder ret = Json.createObjectBuilder();
072        ret.add("layerName", layerName);
073        ret.add("style", style);
074        ret.add("tileMatrixSet", tileMatrixSet);
075        return ret.build();
076    }
077
078    /**
079     * Factory method creating DefaultLayer from JSON objects
080     * @param o serialized DefaultLayer object
081     * @param type of ImageryType serialized
082     * @return DefaultLayer instance based on JSON object
083     */
084    public static DefaultLayer fromJson(JsonObject o, ImageryType type) {
085        return new DefaultLayer(type, o.getString("layerName"), o.getString("style"), o.getString("tileMatrixSet"));
086    }
087}