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.GridBagLayout;
|
---|
8 | import java.awt.Insets;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Collections;
|
---|
13 | import java.util.LinkedList;
|
---|
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 | import javax.swing.plaf.basic.BasicComboBoxEditor;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.data.projection.CustomProjection;
|
---|
24 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
25 | import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
|
---|
26 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
27 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
28 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
|
---|
29 | import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
31 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
32 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
33 | import org.openstreetmap.josm.tools.GBC;
|
---|
34 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
35 |
|
---|
36 | public class CustomProjectionChoice extends AbstractProjectionChoice implements SubPrefsOptions {
|
---|
37 |
|
---|
38 | private String pref;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructs a new {@code CustomProjectionChoice}.
|
---|
42 | */
|
---|
43 | public CustomProjectionChoice() {
|
---|
44 | super(tr("Custom Projection"), /* NO-ICON */ "core:custom");
|
---|
45 | }
|
---|
46 |
|
---|
47 | private static class PreferencePanel extends JPanel {
|
---|
48 |
|
---|
49 | public JosmTextField input;
|
---|
50 | private HistoryComboBox cbInput;
|
---|
51 |
|
---|
52 | PreferencePanel(String initialText, ActionListener listener) {
|
---|
53 | build(initialText, listener);
|
---|
54 | }
|
---|
55 |
|
---|
56 | private void build(String initialText, final ActionListener listener) {
|
---|
57 | input = new JosmTextField(30);
|
---|
58 | cbInput = new HistoryComboBox();
|
---|
59 | cbInput.setPrototypeDisplayValue(new AutoCompletionListItem("xxxx"));
|
---|
60 | cbInput.setEditor(new BasicComboBoxEditor() {
|
---|
61 | @Override
|
---|
62 | protected JosmTextField createEditorComponent() {
|
---|
63 | return input;
|
---|
64 | }
|
---|
65 | });
|
---|
66 | Collection<String> samples = Arrays.asList(
|
---|
67 | "+proj=lonlat +ellps=WGS84 +datum=WGS84 +bounds=-180,-90,180,90",
|
---|
68 | "+proj=tmerc +lat_0=0 +lon_0=9 +k_0=1 +x_0=3500000 +y_0=0 +ellps=bessel +nadgrids=BETA2007.gsb");
|
---|
69 | List<String> inputHistory = new LinkedList<>(Main.pref.getCollection("projection.custom.value.history", samples));
|
---|
70 | Collections.reverse(inputHistory);
|
---|
71 | cbInput.setPossibleItems(inputHistory);
|
---|
72 | cbInput.setText(initialText == null ? "" : initialText);
|
---|
73 |
|
---|
74 | final HtmlPanel errorsPanel = new HtmlPanel();
|
---|
75 | errorsPanel.setVisible(false);
|
---|
76 | final JLabel valStatus = new JLabel();
|
---|
77 | valStatus.setVisible(false);
|
---|
78 |
|
---|
79 | final AbstractTextComponentValidator val = new AbstractTextComponentValidator(input, false, false, false) {
|
---|
80 |
|
---|
81 | private String error;
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public void validate() {
|
---|
85 | if (!isValid()) {
|
---|
86 | feedbackInvalid(tr("Invalid projection configuration: {0}", error));
|
---|
87 | } else {
|
---|
88 | feedbackValid(tr("Projection configuration is valid."));
|
---|
89 | }
|
---|
90 | listener.actionPerformed(null);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public boolean isValid() {
|
---|
95 | try {
|
---|
96 | CustomProjection test = new CustomProjection();
|
---|
97 | test.update(input.getText());
|
---|
98 | } catch (ProjectionConfigurationException ex) {
|
---|
99 | Main.warn(ex);
|
---|
100 | error = ex.getMessage();
|
---|
101 | valStatus.setIcon(ImageProvider.get("data", "error"));
|
---|
102 | valStatus.setVisible(true);
|
---|
103 | errorsPanel.setText(error);
|
---|
104 | errorsPanel.setVisible(true);
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 | errorsPanel.setVisible(false);
|
---|
108 | valStatus.setIcon(ImageProvider.get("misc", "green_check"));
|
---|
109 | valStatus.setVisible(true);
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 | };
|
---|
113 |
|
---|
114 | JButton btnCheck = new JButton(tr("Validate"));
|
---|
115 | btnCheck.addActionListener(e -> val.validate());
|
---|
116 | btnCheck.setLayout(new BorderLayout());
|
---|
117 | btnCheck.setMargin(new Insets(-1, 0, -1, 0));
|
---|
118 |
|
---|
119 | JButton btnInfo = new JButton(tr("Parameter information..."));
|
---|
120 | btnInfo.addActionListener(e -> {
|
---|
121 | CustomProjectionChoice.ParameterInfoDialog dlg = new CustomProjectionChoice.ParameterInfoDialog();
|
---|
122 | dlg.showDialog();
|
---|
123 | dlg.toFront();
|
---|
124 | });
|
---|
125 |
|
---|
126 | this.setLayout(new GridBagLayout());
|
---|
127 | JPanel p2 = new JPanel(new GridBagLayout());
|
---|
128 | p2.add(cbInput, GBC.std().fill(GBC.HORIZONTAL).insets(0, 20, 5, 5));
|
---|
129 | p2.add(btnCheck, GBC.eol().insets(0, 20, 0, 5));
|
---|
130 | this.add(p2, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
131 | p2 = new JPanel(new GridBagLayout());
|
---|
132 | p2.add(valStatus, GBC.std().anchor(GBC.WEST).weight(0.0001, 0));
|
---|
133 | p2.add(errorsPanel, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
134 | this.add(p2, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
135 | p2 = new JPanel(new GridBagLayout());
|
---|
136 | p2.add(btnInfo, GBC.std().insets(0, 20, 0, 0));
|
---|
137 | p2.add(GBC.glue(1, 0), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
138 | this.add(p2, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
139 | }
|
---|
140 |
|
---|
141 | public void rememberHistory() {
|
---|
142 | cbInput.addCurrentItemToHistory();
|
---|
143 | Main.pref.putCollection("projection.custom.value.history", cbInput.getHistory());
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public static class ParameterInfoDialog extends ExtendedDialog {
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Constructs a new {@code ParameterInfoDialog}.
|
---|
151 | */
|
---|
152 | public ParameterInfoDialog() {
|
---|
153 | super(null, tr("Parameter information"), new String[] {tr("Close")}, false);
|
---|
154 | setContent(build());
|
---|
155 | }
|
---|
156 |
|
---|
157 | private static JComponent build() {
|
---|
158 | StringBuilder s = new StringBuilder(1024);
|
---|
159 | s.append("<b>+proj=...</b> - <i>").append(tr("Projection name"))
|
---|
160 | .append("</i><br> ").append(tr("Supported values:")).append(' ')
|
---|
161 | .append(Projections.listProjs())
|
---|
162 | .append("<br><b>+lat_0=..., +lat_1=..., +lat_2=...</b> - <i>").append(tr("Projection parameters"))
|
---|
163 | .append("</i><br><b>+x_0=..., +y_0=...</b> - <i>").append(tr("False easting and false northing"))
|
---|
164 | .append("</i><br><b>+lon_0=...</b> - <i>").append(tr("Central meridian"))
|
---|
165 | .append("</i><br><b>+k_0=...</b> - <i>").append(tr("Scaling factor"))
|
---|
166 | .append("</i><br><b>+ellps=...</b> - <i>").append(tr("Ellipsoid name"))
|
---|
167 | .append("</i><br> ").append(tr("Supported values:")).append(' ')
|
---|
168 | .append(Projections.listEllipsoids())
|
---|
169 | .append("<br><b>+a=..., +b=..., +rf=..., +f=..., +es=...</b> - <i>").append(tr("Ellipsoid parameters"))
|
---|
170 | .append("</i><br><b>+datum=...</b> - <i>").append(tr("Datum name"))
|
---|
171 | .append("</i><br> ").append(tr("Supported values:")).append(' ')
|
---|
172 | .append(Projections.listDatums())
|
---|
173 | .append("<br><b>+towgs84=...</b> - <i>").append(tr("3 or 7 term datum transform parameters"))
|
---|
174 | .append("</i><br><b>+nadgrids=...</b> - <i>").append(tr("NTv2 grid file"))
|
---|
175 | .append("</i><br> ").append(tr("Built-in:")).append(' ')
|
---|
176 | .append(Projections.listNadgrids())
|
---|
177 | .append("<br><b>+bounds=</b>minlon,minlat,maxlon,maxlat - <i>").append(tr("Projection bounds (in degrees)"))
|
---|
178 | .append("</i><br><b>+wmssrs=</b>EPSG:123456 - <i>").append(tr("Sets the SRS=... parameter in the WMS request"))
|
---|
179 | .append("</i><br>");
|
---|
180 |
|
---|
181 | return new HtmlPanel(s.toString());
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | @Override
|
---|
186 | public void setPreferences(Collection<String> args) {
|
---|
187 | if (args != null && !args.isEmpty()) {
|
---|
188 | pref = args.iterator().next();
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Override
|
---|
193 | public Projection getProjection() {
|
---|
194 | return new CustomProjection(pref);
|
---|
195 | }
|
---|
196 |
|
---|
197 | @Override
|
---|
198 | public String getCurrentCode() {
|
---|
199 | // not needed - getProjection() is overridden
|
---|
200 | throw new UnsupportedOperationException();
|
---|
201 | }
|
---|
202 |
|
---|
203 | @Override
|
---|
204 | public String getProjectionName() {
|
---|
205 | // not needed - getProjection() is overridden
|
---|
206 | throw new UnsupportedOperationException();
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override
|
---|
210 | public JPanel getPreferencePanel(ActionListener listener) {
|
---|
211 | return new PreferencePanel(pref, listener);
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public Collection<String> getPreferences(JPanel panel) {
|
---|
216 | if (!(panel instanceof PreferencePanel)) {
|
---|
217 | throw new IllegalArgumentException("Unsupported panel: "+panel);
|
---|
218 | }
|
---|
219 | PreferencePanel prefPanel = (PreferencePanel) panel;
|
---|
220 | String pref = prefPanel.input.getText();
|
---|
221 | prefPanel.rememberHistory();
|
---|
222 | return Collections.singleton(pref);
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public String[] allCodes() {
|
---|
227 | return new String[0];
|
---|
228 | }
|
---|
229 |
|
---|
230 | @Override
|
---|
231 | public Collection<String> getPreferencesFromCode(String code) {
|
---|
232 | return null;
|
---|
233 | }
|
---|
234 |
|
---|
235 | @Override
|
---|
236 | public boolean showProjectionCode() {
|
---|
237 | return false;
|
---|
238 | }
|
---|
239 |
|
---|
240 | @Override
|
---|
241 | public boolean showProjectionName() {
|
---|
242 | return false;
|
---|
243 | }
|
---|
244 | }
|
---|