1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.changeset.query;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
10 | import java.awt.event.FocusAdapter;
|
---|
11 | import java.awt.event.FocusEvent;
|
---|
12 | import java.net.MalformedURLException;
|
---|
13 | import java.net.URL;
|
---|
14 |
|
---|
15 | import javax.swing.BorderFactory;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.event.DocumentEvent;
|
---|
19 | import javax.swing.event.DocumentListener;
|
---|
20 | import javax.swing.event.HyperlinkEvent;
|
---|
21 | import javax.swing.event.HyperlinkListener;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
24 | import org.openstreetmap.josm.io.ChangesetQuery;
|
---|
25 | import org.openstreetmap.josm.io.OsmApi;
|
---|
26 | import org.openstreetmap.josm.io.ChangesetQuery.ChangesetQueryUrlException;
|
---|
27 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
28 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
29 |
|
---|
30 |
|
---|
31 | public class UrlBasedQueryPanel extends JPanel {
|
---|
32 |
|
---|
33 | private JosmTextField tfUrl;
|
---|
34 | private JLabel lblValid;
|
---|
35 |
|
---|
36 | protected JPanel buildURLPanel() {
|
---|
37 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
38 | GridBagConstraints gc = new GridBagConstraints();
|
---|
39 | gc.weightx = 0.0;
|
---|
40 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
41 | gc.insets = new Insets(0,0,0,5);
|
---|
42 | pnl.add(new JLabel(tr("URL: ")), gc);
|
---|
43 |
|
---|
44 | gc.gridx = 1;
|
---|
45 | gc.weightx = 1.0;
|
---|
46 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
47 | pnl.add(tfUrl = new JosmTextField(), gc);
|
---|
48 | tfUrl.getDocument().addDocumentListener(new ChangetQueryUrlValidator());
|
---|
49 | tfUrl.addFocusListener(
|
---|
50 | new FocusAdapter() {
|
---|
51 | @Override
|
---|
52 | public void focusGained(FocusEvent e) {
|
---|
53 | tfUrl.selectAll();
|
---|
54 | }
|
---|
55 | }
|
---|
56 | );
|
---|
57 |
|
---|
58 | gc.gridx = 2;
|
---|
59 | gc.weightx = 0.0;
|
---|
60 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
61 | pnl.add(lblValid = new JLabel(), gc);
|
---|
62 | lblValid.setPreferredSize(new Dimension(20,20));
|
---|
63 | return pnl;
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected JPanel buildHelpPanel() {
|
---|
67 | HtmlPanel pnl = new HtmlPanel();
|
---|
68 | pnl.setText(
|
---|
69 | "<html><body>"
|
---|
70 | + tr("Please enter or paste an URL to retrieve changesets from the OSM API.")
|
---|
71 | + "<p><strong>" + tr("Examples") + "</strong></p>"
|
---|
72 | + "<ul>"
|
---|
73 | + "<li><a href=\"http://www.openstreetmap.org/browse/changesets?open=true\">http://www.openstreetmap.org/browse/changesets?open=true</a></li>"
|
---|
74 | + "<li><a href=\"http://api.openstreetmap.org/api/0.6/changesets?open=true\">http://api.openstreetmap.org/api/0.6/changesets?open=true</a></li>"
|
---|
75 | + "</ul>"
|
---|
76 | + tr("Note that changeset queries are currently always submitted to ''{0}'', regardless of the "
|
---|
77 | + "host, port and path of the URL entered below.", OsmApi.getOsmApi().getBaseUrl())
|
---|
78 | + "</body></html>"
|
---|
79 | );
|
---|
80 | pnl.getEditorPane().addHyperlinkListener(
|
---|
81 | new HyperlinkListener() {
|
---|
82 | @Override
|
---|
83 | public void hyperlinkUpdate(HyperlinkEvent e) {
|
---|
84 | if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
|
---|
85 | tfUrl.setText(e.getDescription());
|
---|
86 | tfUrl.requestFocusInWindow();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | );
|
---|
91 | return pnl;
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected void build() {
|
---|
95 | setLayout(new GridBagLayout());
|
---|
96 | setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
97 |
|
---|
98 | GridBagConstraints gc = new GridBagConstraints();
|
---|
99 | gc.weightx = 1.0;
|
---|
100 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
101 | gc.insets = new Insets(0,0,10,0);
|
---|
102 | add(buildHelpPanel(),gc);
|
---|
103 |
|
---|
104 | gc.gridy = 1;
|
---|
105 | gc.weightx = 1.0;
|
---|
106 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
107 | add(buildURLPanel(),gc);
|
---|
108 |
|
---|
109 | gc.gridy = 2;
|
---|
110 | gc.weightx = 1.0;
|
---|
111 | gc.weighty = 1.0;
|
---|
112 | gc.fill = GridBagConstraints.BOTH;
|
---|
113 | add(new JPanel(),gc);
|
---|
114 |
|
---|
115 | }
|
---|
116 | public UrlBasedQueryPanel() {
|
---|
117 | build();
|
---|
118 | }
|
---|
119 |
|
---|
120 | protected boolean isValidChangesetQueryUrl(String text) {
|
---|
121 | return buildChangesetQuery(text) != null;
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected ChangesetQuery buildChangesetQuery(String text) {
|
---|
125 | URL url = null;
|
---|
126 | try {
|
---|
127 | url = new URL(text);
|
---|
128 | } catch(MalformedURLException e) {
|
---|
129 | return null;
|
---|
130 | }
|
---|
131 | String path = url.getPath();
|
---|
132 | String query = url.getQuery();
|
---|
133 | if (path == null || ! path.endsWith("/changesets")) return null;
|
---|
134 |
|
---|
135 | try {
|
---|
136 | return ChangesetQuery.buildFromUrlQuery(query);
|
---|
137 | } catch(ChangesetQueryUrlException e) {
|
---|
138 | return null;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Replies the {@link ChangesetQuery} specified in this panel. null, if no valid changeset query
|
---|
144 | * is specified.
|
---|
145 | *
|
---|
146 | * @return the changeset query
|
---|
147 | */
|
---|
148 | public ChangesetQuery buildChangesetQuery() {
|
---|
149 | String value = tfUrl.getText().trim();
|
---|
150 | return buildChangesetQuery(value);
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void startUserInput() {
|
---|
154 | tfUrl.requestFocusInWindow();
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Validates text entered in the changeset query URL field on the fly
|
---|
159 | */
|
---|
160 | class ChangetQueryUrlValidator implements DocumentListener {
|
---|
161 | protected String getCurrentFeedback() {
|
---|
162 | String fb = (String)lblValid.getClientProperty("valid");
|
---|
163 | return fb == null ? "none" : fb;
|
---|
164 | }
|
---|
165 | protected void feedbackValid() {
|
---|
166 | if (getCurrentFeedback().equals("valid")) return;
|
---|
167 | lblValid.setIcon(ImageProvider.get("dialogs/changeset", "valid"));
|
---|
168 | lblValid.setToolTipText("");
|
---|
169 | lblValid.putClientProperty("valid", "valid");
|
---|
170 | }
|
---|
171 |
|
---|
172 | protected void feedbackInvalid() {
|
---|
173 | if (getCurrentFeedback().equals("invalid")) return;
|
---|
174 | lblValid.setIcon(ImageProvider.get("warning-small"));
|
---|
175 | lblValid.setToolTipText(tr("This changeset query URL is invalid"));
|
---|
176 | lblValid.putClientProperty("valid", "invalid");
|
---|
177 | }
|
---|
178 |
|
---|
179 | protected void feedbackNone() {
|
---|
180 | lblValid.setIcon(null);
|
---|
181 | lblValid.putClientProperty("valid", "none");
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected void validate() {
|
---|
185 | String value = tfUrl.getText();
|
---|
186 | if (value.trim().isEmpty()) {
|
---|
187 | feedbackNone();
|
---|
188 | return;
|
---|
189 | }
|
---|
190 | value = value.trim();
|
---|
191 | if (isValidChangesetQueryUrl(value)) {
|
---|
192 | feedbackValid();
|
---|
193 | } else {
|
---|
194 | feedbackInvalid();
|
---|
195 | }
|
---|
196 | }
|
---|
197 | @Override
|
---|
198 | public void changedUpdate(DocumentEvent e) {
|
---|
199 | validate();
|
---|
200 | }
|
---|
201 |
|
---|
202 | @Override
|
---|
203 | public void insertUpdate(DocumentEvent e) {
|
---|
204 | validate();
|
---|
205 | }
|
---|
206 |
|
---|
207 | @Override
|
---|
208 | public void removeUpdate(DocumentEvent e) {
|
---|
209 | validate();
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|