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

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

See #15167: Add javadoc, improve code style.

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