| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.preferences.projection;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.BorderLayout;
|
|---|
| 7 | import java.awt.GridBagConstraints;
|
|---|
| 8 | import java.awt.GridBagLayout;
|
|---|
| 9 | import java.awt.Insets;
|
|---|
| 10 | import java.awt.event.ActionListener;
|
|---|
| 11 | import java.util.Arrays;
|
|---|
| 12 | import java.util.Collection;
|
|---|
| 13 | import java.util.Collections;
|
|---|
| 14 | import java.util.List;
|
|---|
| 15 |
|
|---|
| 16 | import javax.swing.JButton;
|
|---|
| 17 | import javax.swing.JComponent;
|
|---|
| 18 | import javax.swing.JLabel;
|
|---|
| 19 | import javax.swing.JPanel;
|
|---|
| 20 |
|
|---|
| 21 | import org.openstreetmap.josm.data.projection.CustomProjection;
|
|---|
| 22 | import org.openstreetmap.josm.data.projection.Projection;
|
|---|
| 23 | import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
|
|---|
| 24 | import org.openstreetmap.josm.data.projection.Projections;
|
|---|
| 25 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 26 | import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
|
|---|
| 27 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
|---|
| 28 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
|---|
| 29 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 30 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 31 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 32 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * ProjectionChoice where a CRS can be defined using various parameters.
|
|---|
| 36 | * <p>
|
|---|
| 37 | * The configuration string mimics the syntax of the PROJ.4 project and should
|
|---|
| 38 | * be mostly compatible.
|
|---|
| 39 | * @see CustomProjection
|
|---|
| 40 | */
|
|---|
| 41 | public class CustomProjectionChoice extends AbstractProjectionChoice implements SubPrefsOptions {
|
|---|
| 42 |
|
|---|
| 43 | private String pref;
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * Constructs a new {@code CustomProjectionChoice}.
|
|---|
| 47 | */
|
|---|
| 48 | public CustomProjectionChoice() {
|
|---|
| 49 | super(tr("Custom Projection"), /* NO-ICON */ "core:custom");
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | private static class PreferencePanel extends JPanel {
|
|---|
| 53 |
|
|---|
| 54 | private HistoryComboBox cbInput;
|
|---|
| 55 |
|
|---|
| 56 | PreferencePanel(String initialText, ActionListener listener) {
|
|---|
| 57 | build(initialText, listener);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | private void build(String initialText, final ActionListener listener) {
|
|---|
| 61 | cbInput = new HistoryComboBox();
|
|---|
| 62 | cbInput.getEditorComponent().setColumns(30);
|
|---|
| 63 | List<String> samples = Arrays.asList(
|
|---|
| 64 | "+proj=lonlat +ellps=WGS84 +datum=WGS84 +bounds=-180,-90,180,90",
|
|---|
| 65 | "+proj=tmerc +lat_0=0 +lon_0=9 +k_0=1 +x_0=3500000 +y_0=0 +ellps=bessel +nadgrids=BETA2007.gsb");
|
|---|
| 66 | cbInput.getModel().prefs().load("projection.custom.value.history", samples);
|
|---|
| 67 | cbInput.setText(initialText == null ? "" : initialText);
|
|---|
| 68 |
|
|---|
| 69 | final HtmlPanel errorsPanel = new HtmlPanel();
|
|---|
| 70 | errorsPanel.setVisible(false);
|
|---|
| 71 | final JLabel valStatus = new JLabel();
|
|---|
| 72 | valStatus.setVisible(false);
|
|---|
| 73 |
|
|---|
| 74 | final AbstractTextComponentValidator val = new AbstractTextComponentValidator(cbInput.getEditorComponent(), false, false, false) {
|
|---|
| 75 |
|
|---|
| 76 | private String error;
|
|---|
| 77 |
|
|---|
| 78 | @Override
|
|---|
| 79 | public void validate() {
|
|---|
| 80 | if (!isValid()) {
|
|---|
| 81 | feedbackInvalid(tr("Invalid projection configuration: {0}", error));
|
|---|
| 82 | } else {
|
|---|
| 83 | feedbackValid(tr("Projection configuration is valid."));
|
|---|
| 84 | }
|
|---|
| 85 | listener.actionPerformed(null);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | @Override
|
|---|
| 89 | public boolean isValid() {
|
|---|
| 90 | try {
|
|---|
| 91 | CustomProjection test = new CustomProjection();
|
|---|
| 92 | test.update(cbInput.getEditorComponent().getText());
|
|---|
| 93 | } catch (ProjectionConfigurationException ex) {
|
|---|
| 94 | Logging.warn(ex);
|
|---|
| 95 | error = ex.getMessage();
|
|---|
| 96 | valStatus.setIcon(ImageProvider.get("data", "error"));
|
|---|
| 97 | valStatus.setVisible(true);
|
|---|
| 98 | errorsPanel.setText(error);
|
|---|
| 99 | 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 | }
|
|---|