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

Last change on this file since 12938 was 12904, checked in by Don-vip, 7 years ago

see #15267 - fix error in previous commit

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 Collections.reverse(items);
94 queryWizard.setPossibleItems(items);
95 if (!items.isEmpty()) {
96 queryWizard.setText(items.get(0));
97 }
98
99 setCancelButton(CANCEL + 1);
100 setDefaultButton(BUILD_AN_EXECUTE_QUERY + 1);
101 setContent(panel, false);
102 }
103
104 @Override
105 public void buttonAction(int buttonIndex, ActionEvent evt) {
106 switch (buttonIndex) {
107 case BUILD_QUERY:
108 if (this.buildQueryAction()) {
109 this.saveHistory();
110 super.buttonAction(BUILD_QUERY, evt);
111 }
112 break;
113 case BUILD_AN_EXECUTE_QUERY:
114 if (this.buildQueryAction()) {
115 this.saveHistory();
116 super.buttonAction(BUILD_AN_EXECUTE_QUERY, evt);
117
118 DownloadDialog.getInstance().startDownload();
119 }
120 break;
121 default:
122 super.buttonAction(buttonIndex, evt);
123
124 }
125 }
126
127 /**
128 * Saves the latest, successfully parsed search term.
129 */
130 private void saveHistory() {
131 queryWizard.addCurrentItemToHistory();
132 OVERPASS_WIZARD_HISTORY.put(queryWizard.getHistory());
133 }
134
135 /**
136 * Tries to process a search term using {@link OverpassTurboQueryWizard}. If the term cannot
137 * be parsed, the the corresponding dialog is shown.
138 * @param searchTerm The search term to parse.
139 * @return {@link Optional#empty()} if an exception was thrown when parsing, meaning
140 * that the term cannot be processed, or non-empty {@link Optional} containing the result
141 * of parsing.
142 */
143 private Optional<String> tryParseSearchTerm(String searchTerm) {
144 try {
145 String query = this.overpassQueryBuilder.constructQuery(searchTerm);
146
147 return Optional.of(query);
148 } catch (UncheckedParseException ex) {
149 Logging.error(ex);
150 JOptionPane.showMessageDialog(
151 dsPanel.getParent(),
152 "<html>" +
153 tr("The Overpass wizard could not parse the following query:") +
154 Utils.joinAsHtmlUnorderedList(Collections.singleton(searchTerm)) +
155 "</html>",
156 tr("Parse error"),
157 JOptionPane.ERROR_MESSAGE
158 );
159
160 return Optional.empty();
161 }
162 }
163
164 /**
165 * Builds an Overpass query out from {@link OverpassQueryWizardDialog#queryWizard} contents.
166 * @return {@code true} if the query successfully built, {@code false} otherwise.
167 */
168 private boolean buildQueryAction() {
169 final String wizardSearchTerm = this.queryWizard.getText();
170
171 Optional<String> q = this.tryParseSearchTerm(wizardSearchTerm);
172 if (q.isPresent()) {
173 String query = q.get();
174 dsPanel.setOverpassQuery(query);
175
176 return true;
177 }
178
179 return false;
180 }
181
182 private static JTextComponent buildDescriptionSection() {
183 JEditorPane descriptionSection = new JEditorPane("text/html", getDescriptionContent());
184 descriptionSection.setEditable(false);
185 descriptionSection.addHyperlinkListener(e -> {
186 if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
187 OpenBrowser.displayUrl(e.getURL().toString());
188 }
189 });
190
191 return descriptionSection;
192 }
193
194 private static String getDescriptionContent() {
195 return new StringBuilder("<html>")
196 .append(DESCRIPTION_STYLE)
197 .append("<body>")
198 .append(HEADLINE_START)
199 .append(tr("Query Wizard"))
200 .append(HEADLINE_END)
201 .append("<p>")
202 .append(tr("Allows you to interact with <i>Overpass API</i> by writing declarative, human-readable terms."))
203 .append(tr("The <i>Query Wizard</i> tool will transform those to a valid overpass query."))
204 .append(tr("For more detailed description see "))
205 .append(tr("<a href=\"{0}\">OSM Wiki</a>.", Main.getOSMWebsite() + "/wiki/Overpass_turbo/Wizard"))
206 .append("</p>")
207 .append(HEADLINE_START).append(tr("Hints")).append(HEADLINE_END)
208 .append("<table>").append(TR_START).append(TD_START)
209 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList("<i>type:node</i>", "<i>type:relation</i>", "<i>type:way</i>")))
210 .append(TD_END).append(TD_START)
211 .append(SPAN_START).append(tr("Download objects of a certain type.")).append(SPAN_END)
212 .append(TD_END).append(TR_END)
213 .append(TR_START).append(TD_START)
214 .append(Utils.joinAsHtmlUnorderedList(
215 Arrays.asList("<i>key=value in <u>location</u></i>",
216 "<i>key=value around <u>location</u></i>",
217 "<i>key=value in bbox</i>")))
218 .append(TD_END).append(TD_START)
219 .append(tr("Download object by specifying a specific location. For example,"))
220 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList(
221 tr("{0} all objects having {1} as attribute are downloaded.", "<i>tourism=hotel in Berlin</i> -", "'tourism=hotel'"),
222 tr("{0} all object with the corresponding key/value pair located around Berlin. Note, the default value for radius "+
223 "is set to 1000m, but it can be changed in the generated query.", "<i>tourism=hotel around Berlin</i> -"),
224 tr("{0} all objects within the current selection that have {1} as attribute.", "<i>tourism=hotel in bbox</i> -",
225 "'tourism=hotel'"))))
226 .append(SPAN_START)
227 .append(tr("Instead of <i>location</i> any valid place name can be used like address, city, etc."))
228 .append(SPAN_END)
229 .append(TD_END).append(TR_END)
230 .append(TR_START).append(TD_START)
231 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList("<i>key=value</i>", "<i>key=*</i>", "<i>key~regex</i>",
232 "<i>key!=value</i>", "<i>key!~regex</i>", "<i>key=\"combined value\"</i>")))
233 .append(TD_END).append(TD_START)
234 .append(tr("<span>Download objects that have some concrete key/value pair, only the key with any contents for the value, " +
235 "the value matching some regular expression. \"Not equal\" operators are supported as well.</span>"))
236 .append(TD_END).append(TR_END)
237 .append(TR_START).append(TD_START)
238 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList(
239 tr("<i>expression1 {0} expression2</i>", "or"),
240 tr("<i>expression1 {0} expression2</i>", "and"))))
241 .append(TD_END).append(TD_START)
242 .append(SPAN_START)
243 .append(tr("Basic logical operators can be used to create more sophisticated queries. Instead of \"or\" - \"|\", \"||\" " +
244 "can be used, and instead of \"and\" - \"&\", \"&&\"."))
245 .append(SPAN_END)
246 .append(TD_END).append(TR_END).append("</table>")
247 .append("</body>")
248 .append("</html>")
249 .toString();
250 }
251}
Note: See TracBrowser for help on using the repository browser.