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