1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.download;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.FocusEvent;
|
---|
11 | import java.awt.event.FocusListener;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.concurrent.Future;
|
---|
14 | import java.util.function.Consumer;
|
---|
15 |
|
---|
16 | import javax.swing.AbstractAction;
|
---|
17 | import javax.swing.Action;
|
---|
18 | import javax.swing.Icon;
|
---|
19 | import javax.swing.JButton;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JScrollPane;
|
---|
24 | import javax.swing.event.ListSelectionEvent;
|
---|
25 | import javax.swing.event.ListSelectionListener;
|
---|
26 | import javax.swing.plaf.basic.BasicArrowButton;
|
---|
27 |
|
---|
28 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
29 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
30 | import org.openstreetmap.josm.data.Bounds;
|
---|
31 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
32 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
33 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
34 | import org.openstreetmap.josm.gui.preferences.server.OverpassServerPreference;
|
---|
35 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
36 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
37 | import org.openstreetmap.josm.io.OverpassDownloadReader;
|
---|
38 | import org.openstreetmap.josm.tools.GBC;
|
---|
39 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Class defines the way data is fetched from Overpass API.
|
---|
43 | * @since 12652
|
---|
44 | */
|
---|
45 | public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSource.OverpassDownloadData> {
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public AbstractDownloadSourcePanel<OverpassDownloadData> createPanel() {
|
---|
49 | return new OverpassDownloadSourcePanel(this);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public void doDownload(OverpassDownloadData data, DownloadSettings settings) {
|
---|
54 | /*
|
---|
55 | * In order to support queries generated by the Overpass Turbo Query Wizard tool
|
---|
56 | * which do not require the area to be specified.
|
---|
57 | */
|
---|
58 | Bounds area = settings.getDownloadBounds().orElse(new Bounds(0, 0, 0, 0));
|
---|
59 | DownloadOsmTask task = new DownloadOsmTask();
|
---|
60 | task.setZoomAfterDownload(settings.zoomToData());
|
---|
61 | Future<?> future = task.download(
|
---|
62 | new OverpassDownloadReader(area, OverpassServerPreference.getOverpassServer(), data.getQuery()),
|
---|
63 | settings.asNewLayer(), area, null);
|
---|
64 | MainApplication.worker.submit(new PostDownloadHandler(task, future, data.getErrorReporter()));
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public String getLabel() {
|
---|
69 | return tr("Download from Overpass API");
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public boolean onlyExpert() {
|
---|
74 | return true;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * The GUI representation of the Overpass download source.
|
---|
79 | * @since 12652
|
---|
80 | */
|
---|
81 | public static class OverpassDownloadSourcePanel extends AbstractDownloadSourcePanel<OverpassDownloadData> {
|
---|
82 |
|
---|
83 | private JosmTextArea overpassQuery;
|
---|
84 | private OverpassQueryList overpassQueryList;
|
---|
85 |
|
---|
86 | private static final String SIMPLE_NAME = "overpassdownloadpanel";
|
---|
87 | private static final BooleanProperty OVERPASS_QUERY_LIST_OPENED =
|
---|
88 | new BooleanProperty("download.overpass.query-list.opened", false);
|
---|
89 | private static final String ACTION_IMG_SUBDIR = "dialogs";
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Create a new {@link OverpassDownloadSourcePanel}
|
---|
93 | * @param ds The download source to create the panel for
|
---|
94 | */
|
---|
95 | public OverpassDownloadSourcePanel(OverpassDownloadSource ds) {
|
---|
96 | super(ds);
|
---|
97 | setLayout(new BorderLayout());
|
---|
98 |
|
---|
99 | String tooltip = tr("Build an Overpass query using the Overpass Turbo Query Wizard tool");
|
---|
100 | Action queryWizardAction = new AbstractAction() {
|
---|
101 | @Override
|
---|
102 | public void actionPerformed(ActionEvent e) {
|
---|
103 | new OverpassQueryWizardDialog(OverpassDownloadSourcePanel.this).showDialog();
|
---|
104 | }
|
---|
105 | };
|
---|
106 |
|
---|
107 | JButton openQueryWizard = new JButton(tr("Query Wizard"));
|
---|
108 | openQueryWizard.setToolTipText(tooltip);
|
---|
109 | openQueryWizard.addActionListener(queryWizardAction);
|
---|
110 |
|
---|
111 | // CHECKSTYLE.OFF: LineLength
|
---|
112 | this.overpassQuery = new JosmTextArea(
|
---|
113 | "/*\n" +
|
---|
114 | tr("Place your Overpass query below or generate one using the Overpass Turbo Query Wizard")
|
---|
115 | + "\n*/",
|
---|
116 | 8, 80);
|
---|
117 | // CHECKSTYLE.ON: LineLength
|
---|
118 | this.overpassQuery.setFont(GuiHelper.getMonospacedFont(overpassQuery));
|
---|
119 | this.overpassQuery.addFocusListener(new FocusListener() {
|
---|
120 | @Override
|
---|
121 | public void focusGained(FocusEvent e) {
|
---|
122 | overpassQuery.selectAll();
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | public void focusLost(FocusEvent e) {
|
---|
127 | // ignored
|
---|
128 | }
|
---|
129 | });
|
---|
130 |
|
---|
131 |
|
---|
132 | this.overpassQueryList = new OverpassQueryList(this, this.overpassQuery);
|
---|
133 | this.overpassQueryList.setPreferredSize(new Dimension(350, 300));
|
---|
134 |
|
---|
135 | EditSnippetAction edit = new EditSnippetAction();
|
---|
136 | RemoveSnippetAction remove = new RemoveSnippetAction();
|
---|
137 | this.overpassQueryList.addSelectionListener(edit);
|
---|
138 | this.overpassQueryList.addSelectionListener(remove);
|
---|
139 |
|
---|
140 | JPanel listPanel = new JPanel(new GridBagLayout());
|
---|
141 | listPanel.add(new JLabel(tr("Your saved queries:")), GBC.eol().insets(2).anchor(GBC.CENTER));
|
---|
142 | listPanel.add(this.overpassQueryList, GBC.eol().fill(GBC.BOTH));
|
---|
143 | listPanel.add(new JButton(new AddSnippetAction()), GBC.std().fill(GBC.HORIZONTAL));
|
---|
144 | listPanel.add(new JButton(edit), GBC.std().fill(GBC.HORIZONTAL));
|
---|
145 | listPanel.add(new JButton(remove), GBC.std().fill(GBC.HORIZONTAL));
|
---|
146 | listPanel.setVisible(OVERPASS_QUERY_LIST_OPENED.get());
|
---|
147 |
|
---|
148 | JScrollPane scrollPane = new JScrollPane(overpassQuery);
|
---|
149 | BasicArrowButton arrowButton = new BasicArrowButton(listPanel.isVisible()
|
---|
150 | ? BasicArrowButton.EAST
|
---|
151 | : BasicArrowButton.WEST);
|
---|
152 | arrowButton.setToolTipText(tr("Show/hide Overpass snippet list"));
|
---|
153 | arrowButton.addActionListener(e -> {
|
---|
154 | if (listPanel.isVisible()) {
|
---|
155 | listPanel.setVisible(false);
|
---|
156 | arrowButton.setDirection(BasicArrowButton.WEST);
|
---|
157 | OVERPASS_QUERY_LIST_OPENED.put(Boolean.FALSE);
|
---|
158 | } else {
|
---|
159 | listPanel.setVisible(true);
|
---|
160 | arrowButton.setDirection(BasicArrowButton.EAST);
|
---|
161 | OVERPASS_QUERY_LIST_OPENED.put(Boolean.TRUE);
|
---|
162 | }
|
---|
163 | });
|
---|
164 |
|
---|
165 | JPanel innerPanel = new JPanel(new BorderLayout());
|
---|
166 | innerPanel.add(scrollPane, BorderLayout.CENTER);
|
---|
167 | innerPanel.add(arrowButton, BorderLayout.EAST);
|
---|
168 |
|
---|
169 | JPanel leftPanel = new JPanel(new GridBagLayout());
|
---|
170 | leftPanel.add(new JLabel(tr("Overpass query:")), GBC.eol().insets(5, 1, 5, 1).anchor(GBC.NORTHWEST));
|
---|
171 | leftPanel.add(new JLabel(), GBC.eol().fill(GBC.VERTICAL));
|
---|
172 | leftPanel.add(openQueryWizard, GBC.eol().anchor(GBC.CENTER));
|
---|
173 | leftPanel.add(new JLabel(), GBC.eol().fill(GBC.VERTICAL));
|
---|
174 |
|
---|
175 | add(leftPanel, BorderLayout.WEST);
|
---|
176 | add(innerPanel, BorderLayout.CENTER);
|
---|
177 | add(listPanel, BorderLayout.EAST);
|
---|
178 |
|
---|
179 | setMinimumSize(new Dimension(450, 240));
|
---|
180 | }
|
---|
181 |
|
---|
182 | @Override
|
---|
183 | public OverpassDownloadData getData() {
|
---|
184 | String query = overpassQuery.getText();
|
---|
185 | /*
|
---|
186 | * A callback that is passed to PostDownloadReporter that is called once the download task
|
---|
187 | * has finished. According to the number of errors happened, their type we decide whether we
|
---|
188 | * want to save the last query in OverpassQueryList.
|
---|
189 | */
|
---|
190 | Consumer<Collection<Object>> errorReporter = errors -> {
|
---|
191 |
|
---|
192 | boolean onlyNoDataError = errors.size() == 1 &&
|
---|
193 | errors.contains("No data found in this area.");
|
---|
194 |
|
---|
195 | if (errors.isEmpty() || onlyNoDataError) {
|
---|
196 | overpassQueryList.saveHistoricItem(query);
|
---|
197 | }
|
---|
198 | };
|
---|
199 |
|
---|
200 | return new OverpassDownloadData(query, errorReporter);
|
---|
201 | }
|
---|
202 |
|
---|
203 | @Override
|
---|
204 | public void rememberSettings() {
|
---|
205 | // nothing
|
---|
206 | }
|
---|
207 |
|
---|
208 | @Override
|
---|
209 | public void restoreSettings() {
|
---|
210 | // nothing
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Override
|
---|
214 | public boolean checkDownload(DownloadSettings settings) {
|
---|
215 | String query = getData().getQuery();
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * Absence of the selected area can be justified only if the overpass query
|
---|
219 | * is not restricted to bbox.
|
---|
220 | */
|
---|
221 | if (!settings.getDownloadBounds().isPresent() && query.contains("{{bbox}}")) {
|
---|
222 | JOptionPane.showMessageDialog(
|
---|
223 | this.getParent(),
|
---|
224 | tr("Please select a download area first."),
|
---|
225 | tr("Error"),
|
---|
226 | JOptionPane.ERROR_MESSAGE
|
---|
227 | );
|
---|
228 | return false;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * Check for an empty query. User might want to download everything, if so validation is passed,
|
---|
233 | * otherwise return false.
|
---|
234 | */
|
---|
235 | if (query.matches("(/\\*(\\*[^/]|[^\\*/])*\\*/|\\s)*")) {
|
---|
236 | boolean doFix = ConditionalOptionPaneUtil.showConfirmationDialog(
|
---|
237 | "download.overpass.fix.emptytoall",
|
---|
238 | this,
|
---|
239 | tr("You entered an empty query. Do you want to download all data in this area instead?"),
|
---|
240 | tr("Download all data?"),
|
---|
241 | JOptionPane.YES_NO_OPTION,
|
---|
242 | JOptionPane.QUESTION_MESSAGE,
|
---|
243 | JOptionPane.YES_OPTION);
|
---|
244 | if (doFix) {
|
---|
245 | String repairedQuery = "[out:xml]; \n"
|
---|
246 | + query + "\n"
|
---|
247 | + "(\n"
|
---|
248 | + " node({{bbox}});\n"
|
---|
249 | + "<;\n"
|
---|
250 | + ");\n"
|
---|
251 | + "(._;>;);"
|
---|
252 | + "out meta;";
|
---|
253 | this.overpassQuery.setText(repairedQuery);
|
---|
254 | } else {
|
---|
255 | return false;
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | return true;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Sets query to the query text field.
|
---|
264 | * @param query The query to set.
|
---|
265 | */
|
---|
266 | public void setOverpassQuery(String query) {
|
---|
267 | this.overpassQuery.setText(query);
|
---|
268 | }
|
---|
269 |
|
---|
270 | @Override
|
---|
271 | public Icon getIcon() {
|
---|
272 | return ImageProvider.get("download-overpass");
|
---|
273 | }
|
---|
274 |
|
---|
275 | @Override
|
---|
276 | public String getSimpleName() {
|
---|
277 | return SIMPLE_NAME;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Action that delegates snippet creation to {@link OverpassQueryList#createNewItem()}.
|
---|
282 | */
|
---|
283 | private class AddSnippetAction extends AbstractAction {
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Constructs a new {@code AddSnippetAction}.
|
---|
287 | */
|
---|
288 | AddSnippetAction() {
|
---|
289 | super();
|
---|
290 | putValue(SMALL_ICON, ImageProvider.get(ACTION_IMG_SUBDIR, "add"));
|
---|
291 | putValue(SHORT_DESCRIPTION, tr("Add new snippet"));
|
---|
292 | }
|
---|
293 |
|
---|
294 | @Override
|
---|
295 | public void actionPerformed(ActionEvent e) {
|
---|
296 | overpassQueryList.createNewItem();
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Action that delegates snippet removal to {@link OverpassQueryList#removeSelectedItem()}.
|
---|
302 | */
|
---|
303 | private class RemoveSnippetAction extends AbstractAction implements ListSelectionListener {
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Constructs a new {@code RemoveSnippetAction}.
|
---|
307 | */
|
---|
308 | RemoveSnippetAction() {
|
---|
309 | super();
|
---|
310 | putValue(SMALL_ICON, ImageProvider.get(ACTION_IMG_SUBDIR, "delete"));
|
---|
311 | putValue(SHORT_DESCRIPTION, tr("Delete selected snippet"));
|
---|
312 | checkEnabled();
|
---|
313 | }
|
---|
314 |
|
---|
315 | @Override
|
---|
316 | public void actionPerformed(ActionEvent e) {
|
---|
317 | overpassQueryList.removeSelectedItem();
|
---|
318 | }
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Disables the action if no items are selected.
|
---|
322 | */
|
---|
323 | void checkEnabled() {
|
---|
324 | setEnabled(overpassQueryList.getSelectedItem().isPresent());
|
---|
325 | }
|
---|
326 |
|
---|
327 | @Override
|
---|
328 | public void valueChanged(ListSelectionEvent e) {
|
---|
329 | checkEnabled();
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Action that delegates snippet edit to {@link OverpassQueryList#editSelectedItem()}.
|
---|
335 | */
|
---|
336 | private class EditSnippetAction extends AbstractAction implements ListSelectionListener {
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Constructs a new {@code EditSnippetAction}.
|
---|
340 | */
|
---|
341 | EditSnippetAction() {
|
---|
342 | super();
|
---|
343 | putValue(SMALL_ICON, ImageProvider.get(ACTION_IMG_SUBDIR, "edit"));
|
---|
344 | putValue(SHORT_DESCRIPTION, tr("Edit selected snippet"));
|
---|
345 | checkEnabled();
|
---|
346 | }
|
---|
347 |
|
---|
348 | @Override
|
---|
349 | public void actionPerformed(ActionEvent e) {
|
---|
350 | overpassQueryList.editSelectedItem();
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Disables the action if no items are selected.
|
---|
355 | */
|
---|
356 | void checkEnabled() {
|
---|
357 | setEnabled(overpassQueryList.getSelectedItem().isPresent());
|
---|
358 | }
|
---|
359 |
|
---|
360 | @Override
|
---|
361 | public void valueChanged(ListSelectionEvent e) {
|
---|
362 | checkEnabled();
|
---|
363 | }
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Encapsulates data that is required to preform download from Overpass API.
|
---|
369 | */
|
---|
370 | static class OverpassDownloadData {
|
---|
371 | private String query;
|
---|
372 | private Consumer<Collection<Object>> errorReporter;
|
---|
373 |
|
---|
374 | OverpassDownloadData(String query, Consumer<Collection<Object>> errorReporter) {
|
---|
375 | this.query = query;
|
---|
376 | this.errorReporter = errorReporter;
|
---|
377 | }
|
---|
378 |
|
---|
379 | String getQuery() {
|
---|
380 | return this.query;
|
---|
381 | }
|
---|
382 |
|
---|
383 | Consumer<Collection<Object>> getErrorReporter() {
|
---|
384 | return this.errorReporter;
|
---|
385 | }
|
---|
386 | }
|
---|
387 | }
|
---|