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