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.GridBagLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Optional;
|
---|
13 |
|
---|
14 | import javax.swing.JEditorPane;
|
---|
15 | import javax.swing.JLabel;
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JScrollPane;
|
---|
19 | import javax.swing.event.HyperlinkEvent;
|
---|
20 | import javax.swing.text.JTextComponent;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.data.preferences.ListProperty;
|
---|
24 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
25 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
26 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
27 | import org.openstreetmap.josm.tools.GBC;
|
---|
28 | import org.openstreetmap.josm.tools.Logging;
|
---|
29 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
30 | import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
|
---|
31 | import org.openstreetmap.josm.tools.UncheckedParseException;
|
---|
32 | import org.openstreetmap.josm.tools.Utils;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * This dialog provides an easy and fast way to create an overpass query.
|
---|
36 | * @since 12576
|
---|
37 | * @since 12652: Moved here
|
---|
38 | */
|
---|
39 | public final class OverpassQueryWizardDialog extends ExtendedDialog {
|
---|
40 |
|
---|
41 | private final HistoryComboBox queryWizard;
|
---|
42 | private static final String HEADLINE_START = "<h3>";
|
---|
43 | private static final String HEADLINE_END = "</h3>";
|
---|
44 | private static final String TR_START = "<tr>";
|
---|
45 | private static final String TR_END = "</tr>";
|
---|
46 | private static final String TD_START = "<td>";
|
---|
47 | private static final String TD_END = "</td>";
|
---|
48 | private static final String SPAN_START = "<span>";
|
---|
49 | private static final String SPAN_END = "</span>";
|
---|
50 | private static final ListProperty OVERPASS_WIZARD_HISTORY =
|
---|
51 | new ListProperty("download.overpass.wizard", new ArrayList<String>());
|
---|
52 | private final transient OverpassTurboQueryWizard overpassQueryBuilder;
|
---|
53 |
|
---|
54 | // dialog buttons
|
---|
55 | private static final int BUILD_QUERY = 0;
|
---|
56 | private static final int BUILD_AN_EXECUTE_QUERY = 1;
|
---|
57 | private static final int CANCEL = 2;
|
---|
58 |
|
---|
59 | private static final String DESCRIPTION_STYLE =
|
---|
60 | "<style type=\"text/css\">\n"
|
---|
61 | + "table { border-spacing: 0pt;}\n"
|
---|
62 | + "h3 {text-align: center; padding: 8px;}\n"
|
---|
63 | + "td {border: 1px solid #dddddd; text-align: left; padding: 8px;}\n"
|
---|
64 | + "#desc {width: 350px;}"
|
---|
65 | + "</style>\n";
|
---|
66 |
|
---|
67 | private final OverpassDownloadSource.OverpassDownloadSourcePanel dsPanel;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Create a new {@link OverpassQueryWizardDialog}
|
---|
71 | * @param dsPanel The Overpass download source panel.
|
---|
72 | */
|
---|
73 | public OverpassQueryWizardDialog(OverpassDownloadSource.OverpassDownloadSourcePanel dsPanel) {
|
---|
74 | super(dsPanel.getParent(), tr("Overpass Turbo Query Wizard"),
|
---|
75 | tr("Build query"), tr("Build query and execute"), tr("Cancel"));
|
---|
76 | this.dsPanel = dsPanel;
|
---|
77 |
|
---|
78 | this.queryWizard = new HistoryComboBox();
|
---|
79 | this.overpassQueryBuilder = OverpassTurboQueryWizard.getInstance();
|
---|
80 |
|
---|
81 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
82 |
|
---|
83 | JLabel searchLabel = new JLabel(tr("Search :"));
|
---|
84 | JTextComponent descPane = buildDescriptionSection();
|
---|
85 | JScrollPane scroll = GuiHelper.embedInVerticalScrollPane(descPane);
|
---|
86 | scroll.getVerticalScrollBar().setUnitIncrement(10); // make scrolling smooth
|
---|
87 |
|
---|
88 | panel.add(searchLabel, GBC.std().insets(0, 0, 0, 20).anchor(GBC.SOUTHEAST));
|
---|
89 | panel.add(queryWizard, GBC.eol().insets(0, 0, 0, 15).fill(GBC.HORIZONTAL).anchor(GBC.SOUTH));
|
---|
90 | panel.add(scroll, GBC.eol().fill(GBC.BOTH).anchor(GBC.CENTER));
|
---|
91 |
|
---|
92 | List<String> items = new ArrayList<>(OVERPASS_WIZARD_HISTORY.get());
|
---|
93 | if (!items.isEmpty()) {
|
---|
94 | queryWizard.setText(items.get(0));
|
---|
95 | }
|
---|
96 | // HistoryComboBox needs the reversed list
|
---|
97 | Collections.reverse(items);
|
---|
98 | queryWizard.setPossibleItems(items);
|
---|
99 |
|
---|
100 | setCancelButton(CANCEL + 1);
|
---|
101 | setDefaultButton(BUILD_AN_EXECUTE_QUERY + 1);
|
---|
102 | setContent(panel, false);
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public void buttonAction(int buttonIndex, ActionEvent evt) {
|
---|
107 | switch (buttonIndex) {
|
---|
108 | case BUILD_QUERY:
|
---|
109 | if (this.buildQueryAction()) {
|
---|
110 | this.saveHistory();
|
---|
111 | super.buttonAction(BUILD_QUERY, evt);
|
---|
112 | }
|
---|
113 | break;
|
---|
114 | case BUILD_AN_EXECUTE_QUERY:
|
---|
115 | if (this.buildQueryAction()) {
|
---|
116 | this.saveHistory();
|
---|
117 | super.buttonAction(BUILD_AN_EXECUTE_QUERY, evt);
|
---|
118 |
|
---|
119 | DownloadDialog.getInstance().startDownload();
|
---|
120 | }
|
---|
121 | break;
|
---|
122 | default:
|
---|
123 | super.buttonAction(buttonIndex, evt);
|
---|
124 |
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Saves the latest, successfully parsed search term.
|
---|
130 | */
|
---|
131 | private void saveHistory() {
|
---|
132 | queryWizard.addCurrentItemToHistory();
|
---|
133 | OVERPASS_WIZARD_HISTORY.put(queryWizard.getHistory());
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Tries to process a search term using {@link OverpassTurboQueryWizard}. If the term cannot
|
---|
138 | * be parsed, the the corresponding dialog is shown.
|
---|
139 | * @param searchTerm The search term to parse.
|
---|
140 | * @return {@link Optional#empty()} if an exception was thrown when parsing, meaning
|
---|
141 | * that the term cannot be processed, or non-empty {@link Optional} containing the result
|
---|
142 | * of parsing.
|
---|
143 | */
|
---|
144 | private Optional<String> tryParseSearchTerm(String searchTerm) {
|
---|
145 | try {
|
---|
146 | String query = this.overpassQueryBuilder.constructQuery(searchTerm);
|
---|
147 |
|
---|
148 | return Optional.of(query);
|
---|
149 | } catch (UncheckedParseException ex) {
|
---|
150 | Logging.error(ex);
|
---|
151 | JOptionPane.showMessageDialog(
|
---|
152 | dsPanel.getParent(),
|
---|
153 | "<html>" +
|
---|
154 | tr("The Overpass wizard could not parse the following query:") +
|
---|
155 | Utils.joinAsHtmlUnorderedList(Collections.singleton(searchTerm)) +
|
---|
156 | "</html>",
|
---|
157 | tr("Parse error"),
|
---|
158 | JOptionPane.ERROR_MESSAGE
|
---|
159 | );
|
---|
160 |
|
---|
161 | return Optional.empty();
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Builds an Overpass query out from {@link OverpassQueryWizardDialog#queryWizard} contents.
|
---|
167 | * @return {@code true} if the query successfully built, {@code false} otherwise.
|
---|
168 | */
|
---|
169 | private boolean buildQueryAction() {
|
---|
170 | final String wizardSearchTerm = this.queryWizard.getText();
|
---|
171 |
|
---|
172 | Optional<String> q = this.tryParseSearchTerm(wizardSearchTerm);
|
---|
173 | if (q.isPresent()) {
|
---|
174 | String query = q.get();
|
---|
175 | dsPanel.setOverpassQuery(query);
|
---|
176 |
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 |
|
---|
180 | return false;
|
---|
181 | }
|
---|
182 |
|
---|
183 | private static JTextComponent buildDescriptionSection() {
|
---|
184 | JEditorPane descriptionSection = new JEditorPane("text/html", getDescriptionContent());
|
---|
185 | descriptionSection.setEditable(false);
|
---|
186 | descriptionSection.addHyperlinkListener(e -> {
|
---|
187 | if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
|
---|
188 | OpenBrowser.displayUrl(e.getURL().toString());
|
---|
189 | }
|
---|
190 | });
|
---|
191 |
|
---|
192 | return descriptionSection;
|
---|
193 | }
|
---|
194 |
|
---|
195 | private static String getDescriptionContent() {
|
---|
196 | return new StringBuilder("<html>")
|
---|
197 | .append(DESCRIPTION_STYLE)
|
---|
198 | .append("<body>")
|
---|
199 | .append(HEADLINE_START)
|
---|
200 | .append(tr("Query Wizard"))
|
---|
201 | .append(HEADLINE_END)
|
---|
202 | .append("<p>")
|
---|
203 | .append(tr("Allows you to interact with <i>Overpass API</i> by writing declarative, human-readable terms."))
|
---|
204 | .append(tr("The <i>Query Wizard</i> tool will transform those to a valid overpass query."))
|
---|
205 | .append(tr("For more detailed description see "))
|
---|
206 | .append(tr("<a href=\"{0}\">OSM Wiki</a>.", Main.getOSMWebsite() + "/wiki/Overpass_turbo/Wizard"))
|
---|
207 | .append("</p>")
|
---|
208 | .append(HEADLINE_START).append(tr("Hints")).append(HEADLINE_END)
|
---|
209 | .append("<table>").append(TR_START).append(TD_START)
|
---|
210 | .append(Utils.joinAsHtmlUnorderedList(Arrays.asList("<i>type:node</i>", "<i>type:relation</i>", "<i>type:way</i>")))
|
---|
211 | .append(TD_END).append(TD_START)
|
---|
212 | .append(SPAN_START).append(tr("Download objects of a certain type.")).append(SPAN_END)
|
---|
213 | .append(TD_END).append(TR_END)
|
---|
214 | .append(TR_START).append(TD_START)
|
---|
215 | .append(Utils.joinAsHtmlUnorderedList(
|
---|
216 | Arrays.asList("<i>key=value in <u>location</u></i>",
|
---|
217 | "<i>key=value around <u>location</u></i>",
|
---|
218 | "<i>key=value in bbox</i>")))
|
---|
219 | .append(TD_END).append(TD_START)
|
---|
220 | .append(tr("Download object by specifying a specific location. For example,"))
|
---|
221 | .append(Utils.joinAsHtmlUnorderedList(Arrays.asList(
|
---|
222 | tr("{0} all objects having {1} as attribute are downloaded.", "<i>tourism=hotel in Berlin</i> -", "'tourism=hotel'"),
|
---|
223 | tr("{0} all object with the corresponding key/value pair located around Berlin. Note, the default value for radius "+
|
---|
224 | "is set to 1000m, but it can be changed in the generated query.", "<i>tourism=hotel around Berlin</i> -"),
|
---|
225 | tr("{0} all objects within the current selection that have {1} as attribute.", "<i>tourism=hotel in bbox</i> -",
|
---|
226 | "'tourism=hotel'"))))
|
---|
227 | .append(SPAN_START)
|
---|
228 | .append(tr("Instead of <i>location</i> any valid place name can be used like address, city, etc."))
|
---|
229 | .append(SPAN_END)
|
---|
230 | .append(TD_END).append(TR_END)
|
---|
231 | .append(TR_START).append(TD_START)
|
---|
232 | .append(Utils.joinAsHtmlUnorderedList(Arrays.asList("<i>key=value</i>", "<i>key=*</i>", "<i>key~regex</i>",
|
---|
233 | "<i>key!=value</i>", "<i>key!~regex</i>", "<i>key=\"combined value\"</i>")))
|
---|
234 | .append(TD_END).append(TD_START)
|
---|
235 | .append(tr("<span>Download objects that have some concrete key/value pair, only the key with any contents for the value, " +
|
---|
236 | "the value matching some regular expression. \"Not equal\" operators are supported as well.</span>"))
|
---|
237 | .append(TD_END).append(TR_END)
|
---|
238 | .append(TR_START).append(TD_START)
|
---|
239 | .append(Utils.joinAsHtmlUnorderedList(Arrays.asList(
|
---|
240 | tr("<i>expression1 {0} expression2</i>", "or"),
|
---|
241 | tr("<i>expression1 {0} expression2</i>", "and"))))
|
---|
242 | .append(TD_END).append(TD_START)
|
---|
243 | .append(SPAN_START)
|
---|
244 | .append(tr("Basic logical operators can be used to create more sophisticated queries. Instead of \"or\" - \"|\", \"||\" " +
|
---|
245 | "can be used, and instead of \"and\" - \"&\", \"&&\"."))
|
---|
246 | .append(SPAN_END)
|
---|
247 | .append(TD_END).append(TR_END).append("</table>")
|
---|
248 | .append("</body>")
|
---|
249 | .append("</html>")
|
---|
250 | .toString();
|
---|
251 | }
|
---|
252 | }
|
---|