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

Last change on this file since 12841 was 12841, checked in by bastiK, 8 years ago

see #15229 - fix deprecations caused by [12840]

  • Property svn:eol-style set to native
File size: 10.0 KB
RevLine 
[5234]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.Main;
23import org.openstreetmap.josm.data.projection.CustomProjection;
24import org.openstreetmap.josm.data.projection.Projection;
25import org.openstreetmap.josm.data.projection.ProjectionConfigurationException;
26import org.openstreetmap.josm.data.projection.Projections;
27import org.openstreetmap.josm.gui.ExtendedDialog;
28import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
29import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
30import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
31import org.openstreetmap.josm.gui.widgets.HtmlPanel;
[6792]32import org.openstreetmap.josm.gui.widgets.JosmTextField;
[5234]33import org.openstreetmap.josm.tools.GBC;
34import org.openstreetmap.josm.tools.ImageProvider;
[12620]35import org.openstreetmap.josm.tools.Logging;
[5234]36
[12148]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 */
[5546]44public class CustomProjectionChoice extends AbstractProjectionChoice implements SubPrefsOptions {
[5234]45
46 private String pref;
47
[6295]48 /**
49 * Constructs a new {@code CustomProjectionChoice}.
50 */
[5234]51 public CustomProjectionChoice() {
[7668]52 super(tr("Custom Projection"), /* NO-ICON */ "core:custom");
[5234]53 }
54
55 private static class PreferencePanel extends JPanel {
56
[5886]57 public JosmTextField input;
[5234]58 private HistoryComboBox cbInput;
59
[8836]60 PreferencePanel(String initialText, ActionListener listener) {
[5234]61 build(initialText, listener);
62 }
63
[8512]64 private void build(String initialText, final ActionListener listener) {
[5886]65 input = new JosmTextField(30);
[5234]66 cbInput = new HistoryComboBox();
67 cbInput.setPrototypeDisplayValue(new AutoCompletionListItem("xxxx"));
68 cbInput.setEditor(new BasicComboBoxEditor() {
69 @Override
[5886]70 protected JosmTextField createEditorComponent() {
[5234]71 return input;
72 }
73 });
[12841]74 List<String> samples = Arrays.asList(
[5234]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");
[12841]77 List<String> inputHistory = new LinkedList<>(Main.pref.getList("projection.custom.value.history", samples));
[5234]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()) {
[8510]94 feedbackInvalid(tr("Invalid projection configuration: {0}", error));
[5234]95 } else {
96 feedbackValid(tr("Projection configuration is valid."));
97 }
98 listener.actionPerformed(null);
99 }
100
101 @Override
[9059]102 public boolean isValid() {
[5234]103 try {
104 CustomProjection test = new CustomProjection();
105 test.update(input.getText());
106 } catch (ProjectionConfigurationException ex) {
[12620]107 Logging.warn(ex);
[5234]108 error = ex.getMessage();
[8056]109 valStatus.setIcon(ImageProvider.get("data", "error"));
[5234]110 valStatus.setVisible(true);
111 errorsPanel.setText(error);
112 errorsPanel.setVisible(true);
113 return false;
114 }
115 errorsPanel.setVisible(false);
[8056]116 valStatus.setIcon(ImageProvider.get("misc", "green_check"));
[5234]117 valStatus.setVisible(true);
118 return true;
119 }
120 };
121
122 JButton btnCheck = new JButton(tr("Validate"));
[10611]123 btnCheck.addActionListener(e -> val.validate());
[5234]124 btnCheck.setLayout(new BorderLayout());
[8510]125 btnCheck.setMargin(new Insets(-1, 0, -1, 0));
[5234]126
127 JButton btnInfo = new JButton(tr("Parameter information..."));
[10611]128 btnInfo.addActionListener(e -> {
129 CustomProjectionChoice.ParameterInfoDialog dlg = new CustomProjectionChoice.ParameterInfoDialog();
130 dlg.showDialog();
131 dlg.toFront();
[5234]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();
[12841]151 Main.pref.putList("projection.custom.value.history", cbInput.getHistory());
[5234]152 }
153 }
154
155 public static class ParameterInfoDialog extends ExtendedDialog {
156
[6295]157 /**
158 * Constructs a new {@code ParameterInfoDialog}.
159 */
[5234]160 public ParameterInfoDialog() {
[8444]161 super(null, tr("Parameter information"), new String[] {tr("Close")}, false);
[5234]162 setContent(build());
163 }
164
[8870]165 private static JComponent build() {
[10242]166 StringBuilder s = new StringBuilder(1024);
[8390]167 s.append("<b>+proj=...</b> - <i>").append(tr("Projection name"))
168 .append("</i><br>&nbsp;&nbsp;&nbsp;&nbsp;").append(tr("Supported values:")).append(' ')
[8533]169 .append(Projections.listProjs())
[8390]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(' ')
[8533]176 .append(Projections.listEllipsoids())
[8390]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(' ')
[8533]180 .append(Projections.listDatums())
[8390]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(' ')
[8533]184 .append(Projections.listNadgrids())
[8390]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>");
[5234]188
[6792]189 return new HtmlPanel(s.toString());
[5234]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
[5548]206 public String getCurrentCode() {
207 // not needed - getProjection() is overridden
[6070]208 throw new UnsupportedOperationException();
[5548]209 }
210
211 @Override
212 public String getProjectionName() {
213 // not needed - getProjection() is overridden
214 throw new UnsupportedOperationException();
215 }
216
217 @Override
[5234]218 public JPanel getPreferencePanel(ActionListener listener) {
219 return new PreferencePanel(pref, listener);
220 }
221
222 @Override
223 public Collection<String> getPreferences(JPanel panel) {
[6295]224 if (!(panel instanceof PreferencePanel)) {
[7864]225 throw new IllegalArgumentException("Unsupported panel: "+panel);
[6295]226 }
[5234]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 }
[5634]247
248 @Override
249 public boolean showProjectionName() {
250 return false;
251 }
[5234]252}
Note: See TracBrowser for help on using the repository browser.