001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.preferences.projection; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.BorderLayout; 007import java.awt.GridBagConstraints; 008import java.awt.GridBagLayout; 009import java.awt.Insets; 010import java.awt.event.ActionListener; 011import java.util.Arrays; 012import java.util.Collection; 013import java.util.Collections; 014import java.util.List; 015 016import javax.swing.JButton; 017import javax.swing.JComponent; 018import javax.swing.JLabel; 019import javax.swing.JPanel; 020 021import org.openstreetmap.josm.data.projection.CustomProjection; 022import org.openstreetmap.josm.data.projection.Projection; 023import org.openstreetmap.josm.data.projection.ProjectionConfigurationException; 024import org.openstreetmap.josm.data.projection.Projections; 025import org.openstreetmap.josm.gui.ExtendedDialog; 026import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator; 027import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 028import org.openstreetmap.josm.gui.widgets.HtmlPanel; 029import org.openstreetmap.josm.tools.GBC; 030import org.openstreetmap.josm.tools.ImageProvider; 031import org.openstreetmap.josm.tools.Logging; 032import org.openstreetmap.josm.tools.Utils; 033 034/** 035 * ProjectionChoice where a CRS can be defined using various parameters. 036 * <p> 037 * The configuration string mimics the syntax of the PROJ.4 project and should 038 * be mostly compatible. 039 * @see CustomProjection 040 */ 041public class CustomProjectionChoice extends AbstractProjectionChoice implements SubPrefsOptions { 042 043 private String pref; 044 045 /** 046 * Constructs a new {@code CustomProjectionChoice}. 047 */ 048 public CustomProjectionChoice() { 049 super(tr("Custom Projection"), /* NO-ICON */ "core:custom"); 050 } 051 052 private static class PreferencePanel extends JPanel { 053 054 private HistoryComboBox cbInput; 055 056 PreferencePanel(String initialText, ActionListener listener) { 057 build(initialText, listener); 058 } 059 060 private void build(String initialText, final ActionListener listener) { 061 cbInput = new HistoryComboBox(); 062 cbInput.getEditorComponent().setColumns(30); 063 List<String> samples = Arrays.asList( 064 "+proj=lonlat +ellps=WGS84 +datum=WGS84 +bounds=-180,-90,180,90", 065 "+proj=tmerc +lat_0=0 +lon_0=9 +k_0=1 +x_0=3500000 +y_0=0 +ellps=bessel +nadgrids=BETA2007.gsb"); 066 cbInput.getModel().prefs().load("projection.custom.value.history", samples); 067 cbInput.setText(initialText == null ? "" : initialText); 068 069 final HtmlPanel errorsPanel = new HtmlPanel(); 070 errorsPanel.setVisible(false); 071 final JLabel valStatus = new JLabel(); 072 valStatus.setVisible(false); 073 074 final AbstractTextComponentValidator val = new AbstractTextComponentValidator(cbInput.getEditorComponent(), false, false, false) { 075 076 private String error; 077 078 @Override 079 public void validate() { 080 if (!isValid()) { 081 feedbackInvalid(tr("Invalid projection configuration: {0}", error)); 082 } else { 083 feedbackValid(tr("Projection configuration is valid.")); 084 } 085 listener.actionPerformed(null); 086 } 087 088 @Override 089 public boolean isValid() { 090 try { 091 CustomProjection test = new CustomProjection(); 092 test.update(cbInput.getEditorComponent().getText()); 093 } catch (ProjectionConfigurationException ex) { 094 Logging.warn(ex); 095 error = ex.getMessage(); 096 valStatus.setIcon(ImageProvider.get("data", "error")); 097 valStatus.setVisible(true); 098 errorsPanel.setText(error); 099 errorsPanel.setVisible(true); 100 return false; 101 } 102 errorsPanel.setVisible(false); 103 valStatus.setIcon(ImageProvider.get("misc", "green_check")); 104 valStatus.setVisible(true); 105 return true; 106 } 107 }; 108 109 JButton btnCheck = new JButton(tr("Validate")); 110 btnCheck.addActionListener(e -> val.validate()); 111 btnCheck.setLayout(new BorderLayout()); 112 btnCheck.setMargin(new Insets(-1, 0, -1, 0)); 113 114 JButton btnInfo = new JButton(tr("Parameter information...")); 115 btnInfo.addActionListener(e -> { 116 CustomProjectionChoice.ParameterInfoDialog dlg = new CustomProjectionChoice.ParameterInfoDialog(); 117 dlg.showDialog(); 118 dlg.toFront(); 119 }); 120 121 this.setLayout(new GridBagLayout()); 122 JPanel p2 = new JPanel(new GridBagLayout()); 123 p2.add(cbInput, GBC.std().fill(GridBagConstraints.HORIZONTAL).insets(0, 20, 5, 5)); 124 p2.add(btnCheck, GBC.eol().insets(0, 20, 0, 5)); 125 this.add(p2, GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 126 p2 = new JPanel(new GridBagLayout()); 127 p2.add(valStatus, GBC.std().anchor(GridBagConstraints.WEST).weight(0.0001, 0)); 128 p2.add(errorsPanel, GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 129 this.add(p2, GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 130 p2 = new JPanel(new GridBagLayout()); 131 p2.add(btnInfo, GBC.std().insets(0, 20, 0, 0)); 132 p2.add(GBC.glue(1, 0), GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 133 this.add(p2, GBC.eol().fill(GridBagConstraints.HORIZONTAL)); 134 } 135 136 /** 137 * Remember the current input 138 */ 139 public void rememberHistory() { 140 cbInput.addCurrentItemToHistory(); 141 cbInput.getModel().prefs().save("projection.custom.value.history"); 142 } 143 } 144 145 /** 146 * A dialog for the available parameters of the custom projection 147 */ 148 public static class ParameterInfoDialog extends ExtendedDialog { 149 150 /** 151 * Constructs a new {@code ParameterInfoDialog}. 152 */ 153 public ParameterInfoDialog() { 154 super(null, tr("Parameter information"), new String[] {tr("Close")}, false); 155 setContent(build()); 156 } 157 158 private static JComponent build() { 159 StringBuilder s = new StringBuilder(1024); 160 s.append("<b>+proj=...</b> - <i>").append(tr("Projection name")) 161 .append("</i><br> ").append(tr("Supported values:")).append(' ') 162 .append(Projections.listProjs()) 163 .append("<br><b>+lat_0=..., +lat_1=..., +lat_2=...</b> - <i>").append(tr("Projection parameters")) 164 .append("</i><br><b>+x_0=..., +y_0=...</b> - <i>").append(tr("False easting and false northing")) 165 .append("</i><br><b>+lon_0=...</b> - <i>").append(tr("Central meridian")) 166 .append("</i><br><b>+k_0=...</b> - <i>").append(tr("Scaling factor")) 167 .append("</i><br><b>+ellps=...</b> - <i>").append(tr("Ellipsoid name")) 168 .append("</i><br> ").append(tr("Supported values:")).append(' ') 169 .append(Projections.listEllipsoids()) 170 .append("<br><b>+a=..., +b=..., +rf=..., +f=..., +es=...</b> - <i>").append(tr("Ellipsoid parameters")) 171 .append("</i><br><b>+datum=...</b> - <i>").append(tr("Datum name")) 172 .append("</i><br> ").append(tr("Supported values:")).append(' ') 173 .append(Projections.listDatums()) 174 .append("<br><b>+towgs84=...</b> - <i>").append(tr("3 or 7 term datum transform parameters")) 175 .append("</i><br><b>+nadgrids=...</b> - <i>").append(tr("NTv2 grid file")) 176 .append("</i><br> ").append(tr("Built-in:")).append(' ') 177 .append(Projections.listNadgrids()) 178 .append("<br><b>+bounds=</b>minlon,minlat,maxlon,maxlat - <i>").append(tr("Projection bounds (in degrees)")) 179 .append("</i><br><b>+wmssrs=</b>EPSG:123456 - <i>").append(tr("Sets the SRS=... parameter in the WMS request")) 180 .append("</i><br>"); 181 182 return new HtmlPanel(s.toString()); 183 } 184 } 185 186 @Override 187 public void setPreferences(Collection<String> args) { 188 if (!Utils.isEmpty(args)) { 189 pref = args.iterator().next(); 190 } 191 } 192 193 @Override 194 public Projection getProjection() { 195 return new CustomProjection(pref); 196 } 197 198 @Override 199 public String getCurrentCode() { 200 // not needed - getProjection() is overridden 201 throw new UnsupportedOperationException(); 202 } 203 204 @Override 205 public String getProjectionName() { 206 // not needed - getProjection() is overridden 207 throw new UnsupportedOperationException(); 208 } 209 210 @Override 211 public JPanel getPreferencePanel(ActionListener listener) { 212 return new PreferencePanel(pref, listener); 213 } 214 215 @Override 216 public Collection<String> getPreferences(JPanel panel) { 217 if (!(panel instanceof PreferencePanel)) { 218 throw new IllegalArgumentException("Unsupported panel: "+panel); 219 } 220 PreferencePanel prefPanel = (PreferencePanel) panel; 221 String savePreferences = prefPanel.cbInput.getEditorComponent().getText(); 222 prefPanel.rememberHistory(); 223 return Collections.singleton(savePreferences); 224 } 225 226 @Override 227 public String[] allCodes() { 228 return new String[0]; 229 } 230 231 @Override 232 public Collection<String> getPreferencesFromCode(String code) { 233 return null; 234 } 235 236 @Override 237 public boolean showProjectionCode() { 238 return false; 239 } 240 241 @Override 242 public boolean showProjectionName() { 243 return false; 244 } 245}