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