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.tools.ImageProvider;
|
---|
35 |
|
---|
36 | public class OsmApiUrlInputPanel extends JPanel {
|
---|
37 | static public final String API_URL_PROP = OsmApiUrlInputPanel.class.getName() + ".apiUrl";
|
---|
38 |
|
---|
39 | static private final String defaulturl = "http://api.openstreetmap.org/api";
|
---|
40 | private JLabel lblValid;
|
---|
41 | private JLabel lblApiUrl;
|
---|
42 | private JTextField 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>", defaulturl));
|
---|
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 JTextField(), 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, defaulturl);
|
---|
124 | } else if (url.trim().equals(defaulturl)) {
|
---|
125 | cbUseDefaultServerUrl.setSelected(true);
|
---|
126 | firePropertyChange(API_URL_PROP, null, defaulturl);
|
---|
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 | if (cbUseDefaultServerUrl.isSelected()) {
|
---|
139 | Main.pref.put("osm-server.url", null);
|
---|
140 | } else if (tfOsmServerUrl.getText().trim().equals(defaulturl)) {
|
---|
141 | Main.pref.put("osm-server.url", null);
|
---|
142 | } else {
|
---|
143 | Main.pref.put("osm-server.url", tfOsmServerUrl.getText().trim());
|
---|
144 | }
|
---|
145 |
|
---|
146 | }
|
---|
147 |
|
---|
148 | class ValidateApiUrlAction extends AbstractAction implements DocumentListener {
|
---|
149 | private String lastTestedUrl = null;
|
---|
150 |
|
---|
151 | public ValidateApiUrlAction() {
|
---|
152 | putValue(NAME, tr("Validate"));
|
---|
153 | putValue(SHORT_DESCRIPTION, tr("Test the API URL"));
|
---|
154 | updateEnabledState();
|
---|
155 | }
|
---|
156 |
|
---|
157 | public void actionPerformed(ActionEvent arg0) {
|
---|
158 | final String url = tfOsmServerUrl.getText().trim();
|
---|
159 | final ApiUrlTestTask task = new ApiUrlTestTask(OsmApiUrlInputPanel.this, url);
|
---|
160 | Main.worker.submit(task);
|
---|
161 | Runnable r = new Runnable() {
|
---|
162 | public void run() {
|
---|
163 | if (task.isCanceled())
|
---|
164 | return;
|
---|
165 | Runnable r = new Runnable() {
|
---|
166 | public void run() {
|
---|
167 | if (task.isSuccess()) {
|
---|
168 | lblValid.setIcon(ImageProvider.get("dialogs/changeset", "valid"));
|
---|
169 | lblValid.setToolTipText(tr("The API URL is valid."));
|
---|
170 | lastTestedUrl = url;
|
---|
171 | updateEnabledState();
|
---|
172 | } else {
|
---|
173 | lblValid.setIcon(ImageProvider.get("warning-small"));
|
---|
174 | lblValid.setToolTipText(tr("Validation failed. The API URL seems to be invalid."));
|
---|
175 | }
|
---|
176 | }
|
---|
177 | };
|
---|
178 | SwingUtilities.invokeLater(r);
|
---|
179 | }
|
---|
180 | };
|
---|
181 | Main.worker.submit(r);
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected void updateEnabledState() {
|
---|
185 | boolean enabled =
|
---|
186 | !tfOsmServerUrl.getText().trim().equals("")
|
---|
187 | && !tfOsmServerUrl.getText().trim().equals(lastTestedUrl);
|
---|
188 | if (enabled) {
|
---|
189 | lblValid.setIcon(null);
|
---|
190 | }
|
---|
191 | setEnabled(enabled);
|
---|
192 | }
|
---|
193 |
|
---|
194 | public void changedUpdate(DocumentEvent arg0) {
|
---|
195 | updateEnabledState();
|
---|
196 | }
|
---|
197 |
|
---|
198 | public void insertUpdate(DocumentEvent arg0) {
|
---|
199 | updateEnabledState();
|
---|
200 | }
|
---|
201 |
|
---|
202 | public void removeUpdate(DocumentEvent arg0) {
|
---|
203 | updateEnabledState();
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | public void setApiUrlInputEnabled(boolean enabled) {
|
---|
208 | lblApiUrl.setEnabled(enabled);
|
---|
209 | tfOsmServerUrl.setEnabled(enabled);
|
---|
210 | lblValid.setEnabled(enabled);
|
---|
211 | btnTest.setEnabled(enabled);
|
---|
212 | }
|
---|
213 |
|
---|
214 | static private class ApiUrlValidator extends AbstractTextComponentValidator {
|
---|
215 | public ApiUrlValidator(JTextComponent tc) throws IllegalArgumentException {
|
---|
216 | super(tc);
|
---|
217 | }
|
---|
218 |
|
---|
219 | @Override
|
---|
220 | public boolean isValid() {
|
---|
221 | if (getComponent().getText().trim().equals(""))
|
---|
222 | return false;
|
---|
223 |
|
---|
224 | try {
|
---|
225 | new URL(getComponent().getText().trim());
|
---|
226 | return true;
|
---|
227 | } catch(MalformedURLException e) {
|
---|
228 | return false;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | @Override
|
---|
233 | public void validate() {
|
---|
234 | if (getComponent().getText().trim().equals("")) {
|
---|
235 | feedbackInvalid(tr("OSM API URL must not be empty. Please enter the OSM API URL."));
|
---|
236 | return;
|
---|
237 | }
|
---|
238 | if (!isValid()) {
|
---|
239 | feedbackInvalid(tr("The current value isn't a valid URL"));
|
---|
240 | } else {
|
---|
241 | feedbackValid(tr("Please enter the OSM API URL."));
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Handles changes in the default URL
|
---|
248 | */
|
---|
249 | class UseDefaultServerUrlChangeHandler implements ItemListener {
|
---|
250 | public void itemStateChanged(ItemEvent e) {
|
---|
251 | switch(e.getStateChange()) {
|
---|
252 | case ItemEvent.SELECTED:
|
---|
253 | setApiUrlInputEnabled(false);
|
---|
254 | firePropertyChange(API_URL_PROP, null, defaulturl);
|
---|
255 | break;
|
---|
256 | case ItemEvent.DESELECTED:
|
---|
257 | setApiUrlInputEnabled(true);
|
---|
258 | valOsmServerUrl.validate();
|
---|
259 | tfOsmServerUrl.requestFocusInWindow();
|
---|
260 | firePropertyChange(API_URL_PROP, null, tfOsmServerUrl.getText());
|
---|
261 | break;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | class ApiUrlPropagator extends FocusAdapter implements ActionListener {
|
---|
267 | public void propagate() {
|
---|
268 | firePropertyChange(API_URL_PROP, null, tfOsmServerUrl.getText());
|
---|
269 | }
|
---|
270 |
|
---|
271 | public void actionPerformed(ActionEvent e) {
|
---|
272 | propagate();
|
---|
273 | }
|
---|
274 |
|
---|
275 | @Override
|
---|
276 | public void focusLost(FocusEvent arg0) {
|
---|
277 | propagate();
|
---|
278 | }
|
---|
279 | }
|
---|
280 | }
|
---|