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