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