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

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

fix Checkstyle issues

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