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

Last change on this file since 12576 was 12576, checked in by michael2402, 7 years ago

See #15057: Move the overpass query wizard dialog to a new file.

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