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