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.event.ActionEvent;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.Optional;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.search.SearchSetting;
|
---|
14 | import org.openstreetmap.josm.data.preferences.ListProperty;
|
---|
15 | import org.openstreetmap.josm.gui.dialogs.SearchDialog;
|
---|
16 | import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration.OverpassWizardCallbacks;
|
---|
17 | import org.openstreetmap.josm.tools.Logging;
|
---|
18 | import org.openstreetmap.josm.tools.SearchCompilerQueryWizard;
|
---|
19 | import org.openstreetmap.josm.tools.UncheckedParseException;
|
---|
20 | import org.openstreetmap.josm.tools.Utils;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * This dialog provides an easy and fast way to create an overpass query.
|
---|
24 | * @since 12576
|
---|
25 | * @since 12652: Moved here
|
---|
26 | */
|
---|
27 | public final class OverpassQueryWizardDialog extends SearchDialog {
|
---|
28 |
|
---|
29 | private static final ListProperty OVERPASS_WIZARD_HISTORY =
|
---|
30 | new ListProperty("download.overpass.wizard", new ArrayList<String>());
|
---|
31 | private final OverpassWizardCallbacks callbacks;
|
---|
32 |
|
---|
33 | // dialog buttons
|
---|
34 | private static final int BUILD_QUERY = 0;
|
---|
35 | private static final int BUILD_AN_EXECUTE_QUERY = 1;
|
---|
36 | private static final int CANCEL = 2;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Create a new {@link OverpassQueryWizardDialog}
|
---|
40 | * @param callbacks The Overpass download source panel.
|
---|
41 | */
|
---|
42 | public OverpassQueryWizardDialog(OverpassWizardCallbacks callbacks) {
|
---|
43 | super(new SearchSetting(), OVERPASS_WIZARD_HISTORY.get(), new PanelOptions(false, true), callbacks.getParent(),
|
---|
44 | tr("Overpass Query Wizard"),
|
---|
45 | tr("Build query"), tr("Build query and execute"), tr("Cancel"));
|
---|
46 | this.callbacks = callbacks;
|
---|
47 | setButtonIcons("dialogs/magic-wand", "download-overpass", "cancel");
|
---|
48 | setCancelButton(CANCEL + 1);
|
---|
49 | setDefaultButton(BUILD_AN_EXECUTE_QUERY + 1);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public void buttonAction(int buttonIndex, ActionEvent evt) {
|
---|
54 | switch (buttonIndex) {
|
---|
55 | case BUILD_QUERY:
|
---|
56 | if (this.buildQueryAction()) {
|
---|
57 | this.saveHistory();
|
---|
58 | super.buttonAction(BUILD_QUERY, evt);
|
---|
59 | }
|
---|
60 | break;
|
---|
61 | case BUILD_AN_EXECUTE_QUERY:
|
---|
62 | if (this.buildQueryAction()) {
|
---|
63 | this.saveHistory();
|
---|
64 | super.buttonAction(BUILD_AN_EXECUTE_QUERY, evt);
|
---|
65 |
|
---|
66 | DownloadDialog.getInstance().startDownload();
|
---|
67 | }
|
---|
68 | break;
|
---|
69 | default:
|
---|
70 | super.buttonAction(buttonIndex, evt);
|
---|
71 |
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Saves the latest, successfully parsed search term.
|
---|
77 | */
|
---|
78 | private void saveHistory() {
|
---|
79 | hcbSearchString.addCurrentItemToHistory();
|
---|
80 | OVERPASS_WIZARD_HISTORY.put(hcbSearchString.getHistory());
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Tries to process a search term using {@link SearchCompilerQueryWizard}. If the term cannot
|
---|
85 | * be parsed, the the corresponding dialog is shown.
|
---|
86 | * @param searchTerm The search term to parse.
|
---|
87 | * @return {@link Optional#empty()} if an exception was thrown when parsing, meaning
|
---|
88 | * that the term cannot be processed, or non-empty {@link Optional} containing the result
|
---|
89 | * of parsing.
|
---|
90 | */
|
---|
91 | private Optional<String> tryParseSearchTerm(String searchTerm) {
|
---|
92 | try {
|
---|
93 | return Optional.of(SearchCompilerQueryWizard.getInstance().constructQuery(searchTerm));
|
---|
94 | } catch (UncheckedParseException | IllegalStateException ex) {
|
---|
95 | Logging.error(ex);
|
---|
96 | JOptionPane.showMessageDialog(
|
---|
97 | callbacks.getParent(),
|
---|
98 | "<html>" +
|
---|
99 | tr("The Overpass wizard could not parse the following query:") +
|
---|
100 | Utils.joinAsHtmlUnorderedList(Collections.singleton(Utils.escapeReservedCharactersHTML(searchTerm))) +
|
---|
101 | "</html>",
|
---|
102 | tr("Parse error"),
|
---|
103 | JOptionPane.ERROR_MESSAGE
|
---|
104 | );
|
---|
105 | return Optional.empty();
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Builds an Overpass query out from {@link SearchSetting} contents.
|
---|
111 | * @return {@code true} if the query successfully built, {@code false} otherwise.
|
---|
112 | */
|
---|
113 | private boolean buildQueryAction() {
|
---|
114 | final String wizardSearchTerm = getSearchSettings().text;
|
---|
115 |
|
---|
116 | Optional<String> q = this.tryParseSearchTerm(wizardSearchTerm);
|
---|
117 | q.ifPresent(callbacks::submitWizardResult);
|
---|
118 | return q.isPresent();
|
---|
119 | }
|
---|
120 | }
|
---|