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.gui.widgets.JosmTextArea; 014import org.openstreetmap.josm.gui.widgets.JosmTextField; 015import org.openstreetmap.josm.tools.GBC; 016import org.openstreetmap.josm.tools.Utils; 017 018/** 019 * An imagery panel used to add TMS imagery sources 020 */ 021public class AddTMSLayerPanel extends AddImageryPanel { 022 023 private final JosmTextField tmsZoom = new JosmTextField(); 024 private final JosmTextArea tmsUrl = new JosmTextArea(3, 40); 025 private final KeyAdapter keyAdapter = new KeyAdapter() { 026 @Override 027 public void keyReleased(KeyEvent e) { 028 tmsUrl.setText(buildTMSUrl()); 029 } 030 }; 031 032 /** 033 * Constructs a new {@code AddTMSLayerPanel}. 034 */ 035 public AddTMSLayerPanel() { 036 037 add(new JLabel(tr("1. Enter URL")), GBC.eol()); 038 add(new JLabel("<html>" + Utils.joinAsHtmlUnorderedList(Arrays.asList( 039 tr("{0} is replaced by tile zoom level, also supported:<br>" + 040 "offsets to the zoom level: {1} or {2}<br>" + 041 "reversed zoom level: {3}", "{zoom}", "{zoom+1}", "{zoom-1}", "{19-zoom}"), 042 tr("{0} is replaced by X-coordinate of the tile", "{x}"), 043 tr("{0} is replaced by Y-coordinate of the tile", "{y}"), 044 tr("{0} is replaced by {1} (Yahoo style Y coordinate)", "{!y}", "2<sup>zoom–1</sup> – 1 – Y"), 045 tr("{0} is replaced by {1} (OSGeo Tile Map Service Specification style Y coordinate)", "{-y}", "2<sup>zoom</sup> – 1 – Y"), 046 tr("{0} is replaced by a random selection from the given comma separated list, e.g. {1}", "{switch:...}", "{switch:a,b,c}") 047 )) + "</html>"), GBC.eol().fill()); 048 049 add(rawUrl, GBC.eop().fill()); 050 rawUrl.setLineWrap(true); 051 rawUrl.addKeyListener(keyAdapter); 052 053 add(new JLabel(tr("2. Enter maximum zoom (optional)")), GBC.eol()); 054 tmsZoom.addKeyListener(keyAdapter); 055 add(tmsZoom, GBC.eop().fill()); 056 057 add(new JLabel(tr("3. Verify generated TMS URL")), GBC.eol()); 058 add(tmsUrl, GBC.eop().fill()); 059 tmsUrl.setLineWrap(true); 060 061 add(new JLabel(tr("4. Enter name for this layer")), GBC.eol()); 062 add(name, GBC.eop().fill()); 063 064 registerValidableComponent(tmsUrl); 065 } 066 067 private String buildTMSUrl() { 068 StringBuilder a = new StringBuilder("tms"); 069 String z = sanitize(tmsZoom.getText()); 070 if (!z.isEmpty()) { 071 a.append("[").append(z).append("]"); 072 } 073 a.append(":"); 074 a.append(getImageryRawUrl()); 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}