1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.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.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 |
|
---|
17 | import javax.swing.JButton;
|
---|
18 | import javax.swing.JComponent;
|
---|
19 | import javax.swing.JLabel;
|
---|
20 | import javax.swing.JPanel;
|
---|
21 | import javax.swing.JTextField;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.projection.SubPrefsOptions;
|
---|
25 | import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
|
---|
26 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
27 | import org.openstreetmap.josm.tools.GBC;
|
---|
28 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
29 | import org.openstreetmap.josm.tools.Utils;
|
---|
30 |
|
---|
31 | public class CustomProjectionPrefGui extends CustomProjection implements ProjectionSubPrefs, SubPrefsOptions {
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public void setupPreferencePanel(JPanel p, ActionListener listener) {
|
---|
35 | JPanel inner = new PreferencePanel();
|
---|
36 | p.setLayout(new GridBagLayout());
|
---|
37 | p.add(inner, GBC.std().fill(GBC.HORIZONTAL));
|
---|
38 | }
|
---|
39 |
|
---|
40 | private class PreferencePanel extends JPanel {
|
---|
41 |
|
---|
42 | public final JTextField input;
|
---|
43 |
|
---|
44 | public PreferencePanel() {
|
---|
45 | input = new JTextField(pref == null ? "" : pref, 30);
|
---|
46 | build();
|
---|
47 | }
|
---|
48 |
|
---|
49 | private void build() {
|
---|
50 | final HtmlPanel errorsPanel = new HtmlPanel();
|
---|
51 | errorsPanel.setVisible(false);
|
---|
52 | final JLabel valStatus = new JLabel();
|
---|
53 | valStatus.setVisible(false);
|
---|
54 |
|
---|
55 | final AbstractTextComponentValidator val = new AbstractTextComponentValidator(input, false, false, false) {
|
---|
56 |
|
---|
57 | private String error;
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void validate() {
|
---|
61 | if (!isValid()) {
|
---|
62 | feedbackInvalid(tr("Invalid projection configuration: {0}",error));
|
---|
63 | } else {
|
---|
64 | feedbackValid(tr("Projection configuration is valid."));
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public boolean isValid() {
|
---|
70 | try {
|
---|
71 | CustomProjection test = new CustomProjection();
|
---|
72 | test.update(input.getText());
|
---|
73 | } catch (ProjectionConfigurationException ex) {
|
---|
74 | error = ex.getMessage();
|
---|
75 | valStatus.setIcon(ImageProvider.get("data", "error.png"));
|
---|
76 | valStatus.setVisible(true);
|
---|
77 | errorsPanel.setText(error);
|
---|
78 | errorsPanel.setVisible(true);
|
---|
79 | return false;
|
---|
80 | }
|
---|
81 | errorsPanel.setVisible(false);
|
---|
82 | valStatus.setIcon(ImageProvider.get("misc", "green_check.png"));
|
---|
83 | valStatus.setVisible(true);
|
---|
84 | return true;
|
---|
85 | }
|
---|
86 |
|
---|
87 | };
|
---|
88 |
|
---|
89 | JButton btnCheck = new JButton(tr("Validate"));
|
---|
90 | btnCheck.addActionListener(new ActionListener() {
|
---|
91 | @Override
|
---|
92 | public void actionPerformed(ActionEvent e) {
|
---|
93 | val.validate();
|
---|
94 | }
|
---|
95 | });
|
---|
96 | btnCheck.setLayout(new BorderLayout());
|
---|
97 | btnCheck.setMargin(new Insets(-1,0,-1,0));
|
---|
98 |
|
---|
99 | JButton btnInfo = new JButton(tr("Parameter information..."));
|
---|
100 | btnInfo.addActionListener(new ActionListener() {
|
---|
101 | @Override
|
---|
102 | public void actionPerformed(ActionEvent e) {
|
---|
103 | ParameterInfoDialog dlg = new ParameterInfoDialog();
|
---|
104 | dlg.showDialog();
|
---|
105 | dlg.toFront();
|
---|
106 | }
|
---|
107 | });
|
---|
108 |
|
---|
109 | this.setLayout(new GridBagLayout());
|
---|
110 | JPanel p2 = new JPanel(new GridBagLayout());
|
---|
111 | p2.add(input, GBC.std().fill(GBC.HORIZONTAL).insets(0, 20, 5, 5));
|
---|
112 | p2.add(btnCheck, GBC.eol().insets(0, 20, 0, 5));
|
---|
113 | this.add(p2, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
114 | p2 = new JPanel(new GridBagLayout());
|
---|
115 | p2.add(valStatus, GBC.std().anchor(GBC.WEST).weight(0.0001, 0));
|
---|
116 | p2.add(errorsPanel, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
117 | this.add(p2, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
118 | p2 = new JPanel(new GridBagLayout());
|
---|
119 | p2.add(btnInfo, GBC.std().insets(0, 20, 0, 0));
|
---|
120 | p2.add(GBC.glue(1, 0), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
121 | this.add(p2, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | public class ParameterInfoDialog extends ExtendedDialog {
|
---|
126 |
|
---|
127 | public ParameterInfoDialog() {
|
---|
128 | super(null, tr("Parameter information"), new String[] { tr("Close") }, false);
|
---|
129 | setContent(build());
|
---|
130 | }
|
---|
131 |
|
---|
132 | private JComponent build() {
|
---|
133 | StringBuilder s = new StringBuilder();
|
---|
134 | s.append("<b>+proj=...</b> - <i>"+tr("Projection name")+"</i><br>");
|
---|
135 | s.append(" "+tr("Supported values:")+" ");
|
---|
136 | s.append(listKeys(Projections.projs)+"<br>");
|
---|
137 | s.append("<b>+lat_0=..., +lat_1=..., +lat_2=...</b> - <i>"+tr("Projection parameters")+"</i><br>");
|
---|
138 | s.append("<b>+x_0=..., +y_0=...</b> - <i>"+tr("False easting and false northing")+"</i><br>");
|
---|
139 | s.append("<b>+lon_0=...</b> - <i>"+tr("Central meridian")+"</i><br>");
|
---|
140 | s.append("<b>+k_0=...</b> - <i>"+tr("Scaling factor")+"</i><br>");
|
---|
141 | s.append("<b>+ellps=...</b> - <i>"+tr("Ellipsoid name")+"</i><br>");
|
---|
142 | s.append(" "+tr("Supported values:")+" ");
|
---|
143 | s.append(listKeys(Projections.ellipsoids)+"<br>");
|
---|
144 | s.append("<b>+a=..., +b=..., +rf=..., +f=..., +es=...</b> - <i>"+tr("Ellipsoid parameters")+"</i><br>");
|
---|
145 | s.append("<b>+datum=...</b> - <i>"+tr("Datum name")+"</i><br>");
|
---|
146 | s.append(" "+tr("Supported values:")+" ");
|
---|
147 | s.append(listKeys(Projections.datums)+"<br>");
|
---|
148 | s.append("<b>+towgs84=...</b> - <i>"+tr("3 or 7 term datum transform parameters")+"</i><br>");
|
---|
149 | s.append("<b>+nadgrids=...</b> - <i>"+tr("NTv2 grid file")+"</i><br>");
|
---|
150 | s.append(" "+tr("Build-in:")+" ");
|
---|
151 | s.append(listKeys(Projections.nadgrids)+"<br>");
|
---|
152 |
|
---|
153 | HtmlPanel info = new HtmlPanel(s.toString());
|
---|
154 | return info;
|
---|
155 | }
|
---|
156 |
|
---|
157 | private String listKeys(Map<String, ?> map) {
|
---|
158 | List<String> keys = new ArrayList<String>(map.keySet());
|
---|
159 | Collections.sort(keys);
|
---|
160 | return Utils.join(", ", keys);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | public Collection<String> getPreferences(JPanel p) {
|
---|
166 | PreferencePanel prefPanel = (PreferencePanel) p.getComponent(0);
|
---|
167 | String pref = prefPanel.input.getText();
|
---|
168 | return Collections.singleton(pref);
|
---|
169 | }
|
---|
170 |
|
---|
171 | @Override
|
---|
172 | public void setPreferences(Collection<String> args) {
|
---|
173 | try {
|
---|
174 | if (args == null || args.isEmpty()) throw new ProjectionConfigurationException();
|
---|
175 | update(args.iterator().next());
|
---|
176 | } catch (ProjectionConfigurationException ex) {
|
---|
177 | System.err.println("Error: Parsing of custom projection failed, falling back to Mercator. Error message is: "+ex.getMessage());
|
---|
178 | try {
|
---|
179 | update(null);
|
---|
180 | } catch (ProjectionConfigurationException ex1) {
|
---|
181 | throw new RuntimeException(ex1);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override
|
---|
187 | public String[] allCodes() {
|
---|
188 | return new String[0];
|
---|
189 | }
|
---|
190 |
|
---|
191 | @Override
|
---|
192 | public Collection<String> getPreferencesFromCode(String code) {
|
---|
193 | return null;
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | public boolean showProjectionCode() {
|
---|
198 | return false;
|
---|
199 | }
|
---|
200 |
|
---|
201 | }
|
---|