001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.imagery;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.KeyAdapter;
007import java.awt.event.KeyEvent;
008import java.util.Arrays;
009
010import javax.swing.JLabel;
011
012import org.openstreetmap.josm.data.imagery.ImageryInfo;
013import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
014import org.openstreetmap.josm.gui.widgets.JosmTextArea;
015import org.openstreetmap.josm.gui.widgets.JosmTextField;
016import org.openstreetmap.josm.tools.GBC;
017import org.openstreetmap.josm.tools.Utils;
018
019/**
020 * An imagery panel used to add TMS imagery sources
021 */
022public class AddTMSLayerPanel extends AddImageryPanel {
023
024    private final JosmTextField tmsZoom = new JosmTextField();
025    private final JosmTextArea tmsUrl = new JosmTextArea(3, 40);
026    private final transient KeyAdapter keyAdapter = new KeyAdapter() {
027        @Override
028        public void keyReleased(KeyEvent e) {
029            tmsUrl.setText(buildTMSUrl());
030        }
031    };
032
033    /**
034     * Constructs a new {@code AddTMSLayerPanel}.
035     */
036    public AddTMSLayerPanel() {
037
038        add(new JLabel(tr("1. Enter URL")), GBC.eol());
039        add(new JLabel("<html>" + Utils.joinAsHtmlUnorderedList(Arrays.asList(
040                tr("{0} is replaced by tile zoom level, also supported:<br>" +
041                        "offsets to the zoom level: {1} or {2}<br>" +
042                        "reversed zoom level: {3}", "{zoom}", "{zoom+1}", "{zoom-1}", "{19-zoom}"),
043                tr("{0} is replaced by X-coordinate of the tile", "{x}"),
044                tr("{0} is replaced by Y-coordinate of the tile", "{y}"),
045                tr("{0} is replaced by {1} (Yahoo style Y coordinate)", "{!y}", "2<sup>zoom–1</sup> – 1 – Y"),
046                tr("{0} is replaced by {1} (OSGeo Tile Map Service Specification style Y coordinate)", "{-y}", "2<sup>zoom</sup> – 1 – Y"),
047                tr("{0} is replaced by a random selection from the given comma separated list, e.g. {1}", "{switch:...}", "{switch:a,b,c}")
048        )) + "</html>"), GBC.eol().fill());
049
050        add(rawUrl, GBC.eop().fill());
051        rawUrl.setLineWrap(true);
052        rawUrl.addKeyListener(keyAdapter);
053
054        add(new JLabel(tr("2. Enter maximum zoom (optional)")), GBC.eol());
055        tmsZoom.addKeyListener(keyAdapter);
056        add(tmsZoom, GBC.eop().fill());
057
058        add(new JLabel(tr("3. Verify generated TMS URL")), GBC.eol());
059        add(tmsUrl, GBC.eop().fill());
060        tmsUrl.setLineWrap(true);
061
062        add(new JLabel(tr("4. Enter name for this layer")), GBC.eol());
063        add(name, GBC.eop().fill());
064
065        registerValidableComponent(tmsUrl);
066    }
067
068    private String buildTMSUrl() {
069        StringBuilder a = new StringBuilder("tms");
070        String z = sanitize(tmsZoom.getText());
071        if (!z.isEmpty()) {
072            a.append('[').append(z).append(']');
073        }
074        a.append(':').append(sanitize(getImageryRawUrl(), ImageryType.TMS));
075        return a.toString();
076    }
077
078    @Override
079    public ImageryInfo getImageryInfo() {
080        return new ImageryInfo(getImageryName(), getTmsUrl());
081    }
082
083    protected final String getTmsUrl() {
084        return sanitize(tmsUrl.getText());
085    }
086
087    @Override
088    protected boolean isImageryValid() {
089        return !getImageryName().isEmpty() && !getTmsUrl().isEmpty();
090    }
091}