1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.server;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Font;
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.awt.event.ActionListener;
|
---|
12 | import java.awt.event.FocusAdapter;
|
---|
13 | import java.awt.event.FocusEvent;
|
---|
14 | import java.awt.event.ItemEvent;
|
---|
15 | import java.awt.event.ItemListener;
|
---|
16 | import java.net.MalformedURLException;
|
---|
17 | import java.net.URL;
|
---|
18 |
|
---|
19 | import javax.swing.AbstractAction;
|
---|
20 | import javax.swing.JCheckBox;
|
---|
21 | import javax.swing.JLabel;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.SwingUtilities;
|
---|
24 | import javax.swing.event.DocumentEvent;
|
---|
25 | import javax.swing.event.DocumentListener;
|
---|
26 | import javax.swing.text.JTextComponent;
|
---|
27 |
|
---|
28 | import org.openstreetmap.josm.Main;
|
---|
29 | import org.openstreetmap.josm.gui.SideButton;
|
---|
30 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
31 | import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
|
---|
32 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
33 | import org.openstreetmap.josm.io.OsmApi;
|
---|
34 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
35 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
36 |
|
---|
37 | public class OsmApiUrlInputPanel extends JPanel {
|
---|
38 | static public final String API_URL_PROP = OsmApiUrlInputPanel.class.getName() + ".apiUrl";
|
---|
39 |
|
---|
40 | private JLabel lblValid;
|
---|
41 | private JLabel lblApiUrl;
|
---|
42 | private JosmTextField tfOsmServerUrl;
|
---|
43 | private ApiUrlValidator valOsmServerUrl;
|
---|
44 | private SideButton btnTest;
|
---|
45 | /** indicates whether to use the default OSM URL or not */
|
---|
46 | private JCheckBox cbUseDefaultServerUrl;
|
---|
47 |
|
---|
48 | protected JPanel buildDefultServerUrlPanel() {
|
---|
49 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
50 | GridBagConstraints gc = new GridBagConstraints();
|
---|
51 |
|
---|
52 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
53 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
54 | gc.weightx = 0.0;
|
---|
55 | gc.insets = new Insets(0,0,0,3);
|
---|
56 | gc.gridwidth = 1;
|
---|
57 | pnl.add(cbUseDefaultServerUrl = new JCheckBox(), gc);
|
---|
58 | cbUseDefaultServerUrl.addItemListener(new UseDefaultServerUrlChangeHandler());
|
---|
59 |
|
---|
60 | gc.gridx = 1;
|
---|
61 | gc.weightx = 1.0;
|
---|
62 | JLabel lbl = new JLabel(tr("<html>Use the default OSM server URL (<strong>{0}</strong>)</html>", OsmApi.DEFAULT_API_URL));
|
---|
63 | lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
|
---|
64 | pnl.add(lbl, gc);
|
---|
65 |
|
---|
66 | return pnl;
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected void build() {
|
---|
70 | setLayout(new GridBagLayout());
|
---|
71 | GridBagConstraints gc = new GridBagConstraints();
|
---|
72 |
|
---|
73 | // the checkbox for the default UL
|
---|
74 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
75 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
76 | gc.weightx = 1.0;
|
---|
77 | gc.insets = new Insets(0,0,0,0);
|
---|
78 | gc.gridwidth = 4;
|
---|
79 | add(buildDefultServerUrlPanel(), gc);
|
---|
80 |
|
---|
81 |
|
---|
82 | // the input field for the URL
|
---|
83 | gc.gridx = 0;
|
---|
84 | gc.gridy = 1;
|
---|
85 | gc.gridwidth = 1;
|
---|
86 | gc.weightx = 0.0;
|
---|
87 | gc.insets = new Insets(0,0,0,3);
|
---|
88 | add(lblApiUrl = new JLabel(tr("OSM Server URL:")), gc);
|
---|
89 |
|
---|
90 | gc.gridx = 1;
|
---|
91 | gc.weightx = 1.0;
|
---|
92 | add(tfOsmServerUrl = new JosmTextField(), gc);
|
---|
93 | SelectAllOnFocusGainedDecorator.decorate(tfOsmServerUrl);
|
---|
94 | valOsmServerUrl = new ApiUrlValidator(tfOsmServerUrl);
|
---|
95 | valOsmServerUrl.validate();
|
---|
96 | ApiUrlPropagator propagator = new ApiUrlPropagator();
|
---|
97 | tfOsmServerUrl.addActionListener(propagator);
|
---|
98 | tfOsmServerUrl.addFocusListener(propagator);
|
---|
99 |
|
---|
100 | gc.gridx = 2;
|
---|
101 | gc.weightx = 0.0;
|
---|
102 | add(lblValid = new JLabel(), gc);
|
---|
103 |
|
---|
104 | gc.gridx = 3;
|
---|
105 | gc.weightx = 0.0;
|
---|
106 | ValidateApiUrlAction actTest = new ValidateApiUrlAction();
|
---|
107 | tfOsmServerUrl.getDocument().addDocumentListener(actTest);
|
---|
108 | add(btnTest = new SideButton(actTest), gc);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public OsmApiUrlInputPanel() {
|
---|
112 | build();
|
---|
113 | HelpUtil.setHelpContext(this, HelpUtil.ht("/Preferences/Connection#ApiUrl"));
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Initializes the configuration panel with values from the preferences
|
---|
118 | */
|
---|
119 | public void initFromPreferences() {
|
---|
120 | String url = Main.pref.get("osm-server.url", null);
|
---|
121 | if (url == null) {
|
---|
122 | cbUseDefaultServerUrl.setSelected(true);
|
---|
123 | firePropertyChange(API_URL_PROP, null, OsmApi.DEFAULT_API_URL);
|
---|
124 | } else if (url.trim().equals(OsmApi.DEFAULT_API_URL)) {
|
---|
125 | cbUseDefaultServerUrl.setSelected(true);
|
---|
126 | firePropertyChange(API_URL_PROP, null, OsmApi.DEFAULT_API_URL);
|
---|
127 | } else {
|
---|
128 | cbUseDefaultServerUrl.setSelected(false);
|
---|
129 | tfOsmServerUrl.setText(url);
|
---|
130 | firePropertyChange(API_URL_PROP, null, url);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Saves the values to the preferences
|
---|
136 | */
|
---|
137 | public void saveToPreferences() {
|
---|
138 | String old_url = Main.pref.get("osm-server.url", null);
|
---|
139 | if (cbUseDefaultServerUrl.isSelected()) {
|
---|
140 | Main.pref.put("osm-server.url", null);
|
---|
141 | } else if (tfOsmServerUrl.getText().trim().equals(OsmApi.DEFAULT_API_URL)) {
|
---|
142 | Main.pref.put("osm-server.url", null);
|
---|
143 | } else {
|
---|
144 | Main.pref.put("osm-server.url", tfOsmServerUrl.getText().trim());
|
---|
145 | }
|
---|
146 | String new_url = Main.pref.get("osm-server.url", null);
|
---|
147 |
|
---|
148 | // When API URL changes, re-initialize API connection so we may adjust
|
---|
149 | // server-dependent settings.
|
---|
150 | if ((old_url == null && new_url != null) || (old_url != null && !old_url.equals(new_url))) {
|
---|
151 | try {
|
---|
152 | OsmApi.getOsmApi().initialize(null);
|
---|
153 | } catch (Exception x) {
|
---|
154 | // ignore;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | class ValidateApiUrlAction extends AbstractAction implements DocumentListener {
|
---|
160 | private String lastTestedUrl = null;
|
---|
161 |
|
---|
162 | public ValidateApiUrlAction() {
|
---|
163 | putValue(NAME, tr("Validate"));
|
---|
164 | putValue(SHORT_DESCRIPTION, tr("Test the API URL"));
|
---|
165 | updateEnabledState();
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public void actionPerformed(ActionEvent arg0) {
|
---|
170 | final String url = tfOsmServerUrl.getText().trim();
|
---|
171 | final ApiUrlTestTask task = new ApiUrlTestTask(OsmApiUrlInputPanel.this, url);
|
---|
172 | Main.worker.submit(task);
|
---|
173 | Runnable r = new Runnable() {
|
---|
174 | @Override
|
---|
175 | public void run() {
|
---|
176 | if (task.isCanceled())
|
---|
177 | return;
|
---|
178 | Runnable r = new Runnable() {
|
---|
179 | @Override
|
---|
180 | public void run() {
|
---|
181 | if (task.isSuccess()) {
|
---|
182 | lblValid.setIcon(ImageProvider.get("dialogs/changeset", "valid"));
|
---|
183 | lblValid.setToolTipText(tr("The API URL is valid."));
|
---|
184 | lastTestedUrl = url;
|
---|
185 | updateEnabledState();
|
---|
186 | } else {
|
---|
187 | lblValid.setIcon(ImageProvider.get("warning-small"));
|
---|
188 | lblValid.setToolTipText(tr("Validation failed. The API URL seems to be invalid."));
|
---|
189 | }
|
---|
190 | }
|
---|
191 | };
|
---|
192 | SwingUtilities.invokeLater(r);
|
---|
193 | }
|
---|
194 | };
|
---|
195 | Main.worker.submit(r);
|
---|
196 | }
|
---|
197 |
|
---|
198 | protected void updateEnabledState() {
|
---|
199 | boolean enabled =
|
---|
200 | !tfOsmServerUrl.getText().trim().isEmpty()
|
---|
201 | && !tfOsmServerUrl.getText().trim().equals(lastTestedUrl);
|
---|
202 | if (enabled) {
|
---|
203 | lblValid.setIcon(null);
|
---|
204 | }
|
---|
205 | setEnabled(enabled);
|
---|
206 | }
|
---|
207 |
|
---|
208 | @Override
|
---|
209 | public void changedUpdate(DocumentEvent arg0) {
|
---|
210 | updateEnabledState();
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Override
|
---|
214 | public void insertUpdate(DocumentEvent arg0) {
|
---|
215 | updateEnabledState();
|
---|
216 | }
|
---|
217 |
|
---|
218 | @Override
|
---|
219 | public void removeUpdate(DocumentEvent arg0) {
|
---|
220 | updateEnabledState();
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | public void setApiUrlInputEnabled(boolean enabled) {
|
---|
225 | lblApiUrl.setEnabled(enabled);
|
---|
226 | tfOsmServerUrl.setEnabled(enabled);
|
---|
227 | lblValid.setEnabled(enabled);
|
---|
228 | btnTest.setEnabled(enabled);
|
---|
229 | }
|
---|
230 |
|
---|
231 | static private class ApiUrlValidator extends AbstractTextComponentValidator {
|
---|
232 | public ApiUrlValidator(JTextComponent tc) throws IllegalArgumentException {
|
---|
233 | super(tc);
|
---|
234 | }
|
---|
235 |
|
---|
236 | @Override
|
---|
237 | public boolean isValid() {
|
---|
238 | if (getComponent().getText().trim().isEmpty())
|
---|
239 | return false;
|
---|
240 |
|
---|
241 | try {
|
---|
242 | new URL(getComponent().getText().trim());
|
---|
243 | return true;
|
---|
244 | } catch(MalformedURLException e) {
|
---|
245 | return false;
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | @Override
|
---|
250 | public void validate() {
|
---|
251 | if (getComponent().getText().trim().isEmpty()) {
|
---|
252 | feedbackInvalid(tr("OSM API URL must not be empty. Please enter the OSM API URL."));
|
---|
253 | return;
|
---|
254 | }
|
---|
255 | if (!isValid()) {
|
---|
256 | feedbackInvalid(tr("The current value is not a valid URL"));
|
---|
257 | } else {
|
---|
258 | feedbackValid(tr("Please enter the OSM API URL."));
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Handles changes in the default URL
|
---|
265 | */
|
---|
266 | class UseDefaultServerUrlChangeHandler implements ItemListener {
|
---|
267 | @Override
|
---|
268 | public void itemStateChanged(ItemEvent e) {
|
---|
269 | switch(e.getStateChange()) {
|
---|
270 | case ItemEvent.SELECTED:
|
---|
271 | setApiUrlInputEnabled(false);
|
---|
272 | firePropertyChange(API_URL_PROP, null, OsmApi.DEFAULT_API_URL);
|
---|
273 | break;
|
---|
274 | case ItemEvent.DESELECTED:
|
---|
275 | setApiUrlInputEnabled(true);
|
---|
276 | valOsmServerUrl.validate();
|
---|
277 | tfOsmServerUrl.requestFocusInWindow();
|
---|
278 | firePropertyChange(API_URL_PROP, null, tfOsmServerUrl.getText());
|
---|
279 | break;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | class ApiUrlPropagator extends FocusAdapter implements ActionListener {
|
---|
285 | public void propagate() {
|
---|
286 | firePropertyChange(API_URL_PROP, null, tfOsmServerUrl.getText());
|
---|
287 | }
|
---|
288 |
|
---|
289 | @Override
|
---|
290 | public void actionPerformed(ActionEvent e) {
|
---|
291 | propagate();
|
---|
292 | }
|
---|
293 |
|
---|
294 | @Override
|
---|
295 | public void focusLost(FocusEvent arg0) {
|
---|
296 | propagate();
|
---|
297 | }
|
---|
298 | }
|
---|
299 | }
|
---|