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