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 final 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>"+tr("Projection name")+"</i><br>");
|
---|
172 | s.append(" "+tr("Supported values:")+" ");
|
---|
173 | s.append(listKeys(Projections.projs)+"<br>");
|
---|
174 | s.append("<b>+lat_0=..., +lat_1=..., +lat_2=...</b> - <i>"+tr("Projection parameters")+"</i><br>");
|
---|
175 | s.append("<b>+x_0=..., +y_0=...</b> - <i>"+tr("False easting and false northing")+"</i><br>");
|
---|
176 | s.append("<b>+lon_0=...</b> - <i>"+tr("Central meridian")+"</i><br>");
|
---|
177 | s.append("<b>+k_0=...</b> - <i>"+tr("Scaling factor")+"</i><br>");
|
---|
178 | s.append("<b>+ellps=...</b> - <i>"+tr("Ellipsoid name")+"</i><br>");
|
---|
179 | s.append(" "+tr("Supported values:")+" ");
|
---|
180 | s.append(listKeys(Projections.ellipsoids)+"<br>");
|
---|
181 | s.append("<b>+a=..., +b=..., +rf=..., +f=..., +es=...</b> - <i>"+tr("Ellipsoid parameters")+"</i><br>");
|
---|
182 | s.append("<b>+datum=...</b> - <i>"+tr("Datum name")+"</i><br>");
|
---|
183 | s.append(" "+tr("Supported values:")+" ");
|
---|
184 | s.append(listKeys(Projections.datums)+"<br>");
|
---|
185 | s.append("<b>+towgs84=...</b> - <i>"+tr("3 or 7 term datum transform parameters")+"</i><br>");
|
---|
186 | s.append("<b>+nadgrids=...</b> - <i>"+tr("NTv2 grid file")+"</i><br>");
|
---|
187 | s.append(" "+tr("Built-in:")+" ");
|
---|
188 | s.append(listKeys(Projections.nadgrids)+"<br>");
|
---|
189 | s.append("<b>+bounds=</b>minlon,minlat,maxlon,maxlat - <i>"+tr("Projection bounds (in degrees)")+"</i><br>");
|
---|
190 | s.append("<b>+wmssrs=</b>EPSG:123456 - <i>"+tr("Sets the SRS=... parameter in the WMS request")+"</i><br>");
|
---|
191 |
|
---|
192 | return new HtmlPanel(s.toString());
|
---|
193 | }
|
---|
194 |
|
---|
195 | private String listKeys(Map<String, ?> map) {
|
---|
196 | List<String> keys = new ArrayList<>(map.keySet());
|
---|
197 | Collections.sort(keys);
|
---|
198 | return Utils.join(", ", keys);
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | @Override
|
---|
203 | public void setPreferences(Collection<String> args) {
|
---|
204 | if (args != null && !args.isEmpty()) {
|
---|
205 | pref = args.iterator().next();
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override
|
---|
210 | public Projection getProjection() {
|
---|
211 | return new CustomProjection(pref);
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public String getCurrentCode() {
|
---|
216 | // not needed - getProjection() is overridden
|
---|
217 | throw new UnsupportedOperationException();
|
---|
218 | }
|
---|
219 |
|
---|
220 | @Override
|
---|
221 | public String getProjectionName() {
|
---|
222 | // not needed - getProjection() is overridden
|
---|
223 | throw new UnsupportedOperationException();
|
---|
224 | }
|
---|
225 |
|
---|
226 | @Override
|
---|
227 | public JPanel getPreferencePanel(ActionListener listener) {
|
---|
228 | return new PreferencePanel(pref, listener);
|
---|
229 | }
|
---|
230 |
|
---|
231 | @Override
|
---|
232 | public Collection<String> getPreferences(JPanel panel) {
|
---|
233 | if (!(panel instanceof PreferencePanel)) {
|
---|
234 | throw new IllegalArgumentException("Unsupported panel: "+panel);
|
---|
235 | }
|
---|
236 | PreferencePanel prefPanel = (PreferencePanel) panel;
|
---|
237 | String pref = prefPanel.input.getText();
|
---|
238 | prefPanel.rememberHistory();
|
---|
239 | return Collections.singleton(pref);
|
---|
240 | }
|
---|
241 |
|
---|
242 | @Override
|
---|
243 | public String[] allCodes() {
|
---|
244 | return new String[0];
|
---|
245 | }
|
---|
246 |
|
---|
247 | @Override
|
---|
248 | public Collection<String> getPreferencesFromCode(String code) {
|
---|
249 | return null;
|
---|
250 | }
|
---|
251 |
|
---|
252 | @Override
|
---|
253 | public boolean showProjectionCode() {
|
---|
254 | return false;
|
---|
255 | }
|
---|
256 |
|
---|
257 | @Override
|
---|
258 | public boolean showProjectionName() {
|
---|
259 | return false;
|
---|
260 | }
|
---|
261 | }
|
---|