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