source: josm/trunk/src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java@ 13724

Last change on this file since 13724 was 13331, checked in by Don-vip, 6 years ago

see #14097, see #15783 - robustness to situations where JavaScript engine cannot be retrieved, for whatever reason

File size: 11.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.download;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.awt.event.ActionEvent;
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collections;
11import java.util.List;
12import java.util.Optional;
13
14import javax.swing.JEditorPane;
15import javax.swing.JLabel;
16import javax.swing.JOptionPane;
17import javax.swing.JPanel;
18import javax.swing.JScrollPane;
19import javax.swing.event.HyperlinkEvent;
20import javax.swing.text.JTextComponent;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.data.preferences.ListProperty;
24import org.openstreetmap.josm.gui.ExtendedDialog;
25import org.openstreetmap.josm.gui.util.GuiHelper;
26import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
27import org.openstreetmap.josm.tools.GBC;
28import org.openstreetmap.josm.tools.Logging;
29import org.openstreetmap.josm.tools.OpenBrowser;
30import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
31import org.openstreetmap.josm.tools.UncheckedParseException;
32import 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 */
39public 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 return Optional.of(overpassQueryBuilder.constructQuery(searchTerm));
147 } catch (UncheckedParseException | IllegalStateException ex) {
148 Logging.error(ex);
149 JOptionPane.showMessageDialog(
150 dsPanel.getParent(),
151 "<html>" +
152 tr("The Overpass wizard could not parse the following query:") +
153 Utils.joinAsHtmlUnorderedList(Collections.singleton(searchTerm)) +
154 "</html>",
155 tr("Parse error"),
156 JOptionPane.ERROR_MESSAGE
157 );
158 return Optional.empty();
159 }
160 }
161
162 /**
163 * Builds an Overpass query out from {@link OverpassQueryWizardDialog#queryWizard} contents.
164 * @return {@code true} if the query successfully built, {@code false} otherwise.
165 */
166 private boolean buildQueryAction() {
167 final String wizardSearchTerm = this.queryWizard.getText();
168
169 Optional<String> q = this.tryParseSearchTerm(wizardSearchTerm);
170 if (q.isPresent()) {
171 String query = q.get();
172 dsPanel.setOverpassQuery(query);
173
174 return true;
175 }
176
177 return false;
178 }
179
180 private static JTextComponent buildDescriptionSection() {
181 JEditorPane descriptionSection = new JEditorPane("text/html", getDescriptionContent());
182 descriptionSection.setEditable(false);
183 descriptionSection.addHyperlinkListener(e -> {
184 if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
185 OpenBrowser.displayUrl(e.getURL().toString());
186 }
187 });
188
189 return descriptionSection;
190 }
191
192 private static String getDescriptionContent() {
193 return new StringBuilder("<html>")
194 .append(DESCRIPTION_STYLE)
195 .append("<body>")
196 .append(HEADLINE_START)
197 .append(tr("Query Wizard"))
198 .append(HEADLINE_END)
199 .append("<p>")
200 .append(tr("Allows you to interact with <i>Overpass API</i> by writing declarative, human-readable terms."))
201 .append(tr("The <i>Query Wizard</i> tool will transform those to a valid overpass query."))
202 .append(tr("For more detailed description see "))
203 .append(tr("<a href=\"{0}\">OSM Wiki</a>.", Main.getOSMWebsite() + "/wiki/Overpass_turbo/Wizard"))
204 .append("</p>")
205 .append(HEADLINE_START).append(tr("Hints")).append(HEADLINE_END)
206 .append("<table>").append(TR_START).append(TD_START)
207 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList("<i>type:node</i>", "<i>type:relation</i>", "<i>type:way</i>")))
208 .append(TD_END).append(TD_START)
209 .append(SPAN_START).append(tr("Download objects of a certain type.")).append(SPAN_END)
210 .append(TD_END).append(TR_END)
211 .append(TR_START).append(TD_START)
212 .append(Utils.joinAsHtmlUnorderedList(
213 Arrays.asList("<i>key=value in <u>location</u></i>",
214 "<i>key=value around <u>location</u></i>",
215 "<i>key=value in bbox</i>")))
216 .append(TD_END).append(TD_START)
217 .append(tr("Download object by specifying a specific location. For example,"))
218 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList(
219 tr("{0} all objects having {1} as attribute are downloaded.", "<i>tourism=hotel in Berlin</i> -", "'tourism=hotel'"),
220 tr("{0} all object with the corresponding key/value pair located around Berlin. Note, the default value for radius "+
221 "is set to 1000m, but it can be changed in the generated query.", "<i>tourism=hotel around Berlin</i> -"),
222 tr("{0} all objects within the current selection that have {1} as attribute.", "<i>tourism=hotel in bbox</i> -",
223 "'tourism=hotel'"))))
224 .append(SPAN_START)
225 .append(tr("Instead of <i>location</i> any valid place name can be used like address, city, etc."))
226 .append(SPAN_END)
227 .append(TD_END).append(TR_END)
228 .append(TR_START).append(TD_START)
229 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList("<i>key=value</i>", "<i>key=*</i>", "<i>key~regex</i>",
230 "<i>key!=value</i>", "<i>key!~regex</i>", "<i>key=\"combined value\"</i>")))
231 .append(TD_END).append(TD_START)
232 .append(tr("<span>Download objects that have some concrete key/value pair, only the key with any contents for the value, " +
233 "the value matching some regular expression. \"Not equal\" operators are supported as well.</span>"))
234 .append(TD_END).append(TR_END)
235 .append(TR_START).append(TD_START)
236 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList(
237 tr("<i>expression1 {0} expression2</i>", "or"),
238 tr("<i>expression1 {0} expression2</i>", "and"))))
239 .append(TD_END).append(TD_START)
240 .append(SPAN_START)
241 .append(tr("Basic logical operators can be used to create more sophisticated queries. Instead of \"or\" - \"|\", \"||\" " +
242 "can be used, and instead of \"and\" - \"&\", \"&&\"."))
243 .append(SPAN_END)
244 .append(TD_END).append(TR_END).append("</table>")
245 .append("</body>")
246 .append("</html>")
247 .toString();
248 }
249}
Note: See TracBrowser for help on using the repository browser.