source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/projection/CustomProjectionChoice.java@ 12846

Last change on this file since 12846 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

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