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

Last change on this file since 10611 was 10611, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 9.7 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.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;
32import org.openstreetmap.josm.gui.widgets.JosmTextField;
33import org.openstreetmap.josm.tools.GBC;
34import org.openstreetmap.josm.tools.ImageProvider;
35
36public 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 error = ex.getMessage();
100 valStatus.setIcon(ImageProvider.get("data", "error"));
101 valStatus.setVisible(true);
102 errorsPanel.setText(error);
103 errorsPanel.setVisible(true);
104 return false;
105 }
106 errorsPanel.setVisible(false);
107 valStatus.setIcon(ImageProvider.get("misc", "green_check"));
108 valStatus.setVisible(true);
109 return true;
110 }
111 };
112
113 JButton btnCheck = new JButton(tr("Validate"));
114 btnCheck.addActionListener(e -> val.validate());
115 btnCheck.setLayout(new BorderLayout());
116 btnCheck.setMargin(new Insets(-1, 0, -1, 0));
117
118 JButton btnInfo = new JButton(tr("Parameter information..."));
119 btnInfo.addActionListener(e -> {
120 CustomProjectionChoice.ParameterInfoDialog dlg = new CustomProjectionChoice.ParameterInfoDialog();
121 dlg.showDialog();
122 dlg.toFront();
123 });
124
125 this.setLayout(new GridBagLayout());
126 JPanel p2 = new JPanel(new GridBagLayout());
127 p2.add(cbInput, GBC.std().fill(GBC.HORIZONTAL).insets(0, 20, 5, 5));
128 p2.add(btnCheck, GBC.eol().insets(0, 20, 0, 5));
129 this.add(p2, GBC.eol().fill(GBC.HORIZONTAL));
130 p2 = new JPanel(new GridBagLayout());
131 p2.add(valStatus, GBC.std().anchor(GBC.WEST).weight(0.0001, 0));
132 p2.add(errorsPanel, GBC.eol().fill(GBC.HORIZONTAL));
133 this.add(p2, GBC.eol().fill(GBC.HORIZONTAL));
134 p2 = new JPanel(new GridBagLayout());
135 p2.add(btnInfo, GBC.std().insets(0, 20, 0, 0));
136 p2.add(GBC.glue(1, 0), GBC.eol().fill(GBC.HORIZONTAL));
137 this.add(p2, GBC.eol().fill(GBC.HORIZONTAL));
138 }
139
140 public void rememberHistory() {
141 cbInput.addCurrentItemToHistory();
142 Main.pref.putCollection("projection.custom.value.history", cbInput.getHistory());
143 }
144 }
145
146 public static class ParameterInfoDialog extends ExtendedDialog {
147
148 /**
149 * Constructs a new {@code ParameterInfoDialog}.
150 */
151 public ParameterInfoDialog() {
152 super(null, tr("Parameter information"), new String[] {tr("Close")}, false);
153 setContent(build());
154 }
155
156 private static JComponent build() {
157 StringBuilder s = new StringBuilder(1024);
158 s.append("<b>+proj=...</b> - <i>").append(tr("Projection name"))
159 .append("</i><br>&nbsp;&nbsp;&nbsp;&nbsp;").append(tr("Supported values:")).append(' ')
160 .append(Projections.listProjs())
161 .append("<br><b>+lat_0=..., +lat_1=..., +lat_2=...</b> - <i>").append(tr("Projection parameters"))
162 .append("</i><br><b>+x_0=..., +y_0=...</b> - <i>").append(tr("False easting and false northing"))
163 .append("</i><br><b>+lon_0=...</b> - <i>").append(tr("Central meridian"))
164 .append("</i><br><b>+k_0=...</b> - <i>").append(tr("Scaling factor"))
165 .append("</i><br><b>+ellps=...</b> - <i>").append(tr("Ellipsoid name"))
166 .append("</i><br>&nbsp;&nbsp;&nbsp;&nbsp;").append(tr("Supported values:")).append(' ')
167 .append(Projections.listEllipsoids())
168 .append("<br><b>+a=..., +b=..., +rf=..., +f=..., +es=...</b> - <i>").append(tr("Ellipsoid parameters"))
169 .append("</i><br><b>+datum=...</b> - <i>").append(tr("Datum name"))
170 .append("</i><br>&nbsp;&nbsp;&nbsp;&nbsp;").append(tr("Supported values:")).append(' ')
171 .append(Projections.listDatums())
172 .append("<br><b>+towgs84=...</b> - <i>").append(tr("3 or 7 term datum transform parameters"))
173 .append("</i><br><b>+nadgrids=...</b> - <i>").append(tr("NTv2 grid file"))
174 .append("</i><br>&nbsp;&nbsp;&nbsp;&nbsp;").append(tr("Built-in:")).append(' ')
175 .append(Projections.listNadgrids())
176 .append("<br><b>+bounds=</b>minlon,minlat,maxlon,maxlat - <i>").append(tr("Projection bounds (in degrees)"))
177 .append("</i><br><b>+wmssrs=</b>EPSG:123456 - <i>").append(tr("Sets the SRS=... parameter in the WMS request"))
178 .append("</i><br>");
179
180 return new HtmlPanel(s.toString());
181 }
182 }
183
184 @Override
185 public void setPreferences(Collection<String> args) {
186 if (args != null && !args.isEmpty()) {
187 pref = args.iterator().next();
188 }
189 }
190
191 @Override
192 public Projection getProjection() {
193 return new CustomProjection(pref);
194 }
195
196 @Override
197 public String getCurrentCode() {
198 // not needed - getProjection() is overridden
199 throw new UnsupportedOperationException();
200 }
201
202 @Override
203 public String getProjectionName() {
204 // not needed - getProjection() is overridden
205 throw new UnsupportedOperationException();
206 }
207
208 @Override
209 public JPanel getPreferencePanel(ActionListener listener) {
210 return new PreferencePanel(pref, listener);
211 }
212
213 @Override
214 public Collection<String> getPreferences(JPanel panel) {
215 if (!(panel instanceof PreferencePanel)) {
216 throw new IllegalArgumentException("Unsupported panel: "+panel);
217 }
218 PreferencePanel prefPanel = (PreferencePanel) panel;
219 String pref = prefPanel.input.getText();
220 prefPanel.rememberHistory();
221 return Collections.singleton(pref);
222 }
223
224 @Override
225 public String[] allCodes() {
226 return new String[0];
227 }
228
229 @Override
230 public Collection<String> getPreferencesFromCode(String code) {
231 return null;
232 }
233
234 @Override
235 public boolean showProjectionCode() {
236 return false;
237 }
238
239 @Override
240 public boolean showProjectionName() {
241 return false;
242 }
243}
Note: See TracBrowser for help on using the repository browser.