001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.download; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.GridBagLayout; 007import java.awt.event.ActionEvent; 008import java.util.ArrayList; 009import java.util.Arrays; 010import java.util.Collections; 011import java.util.List; 012import java.util.Optional; 013 014import javax.swing.JEditorPane; 015import javax.swing.JLabel; 016import javax.swing.JOptionPane; 017import javax.swing.JPanel; 018import javax.swing.JScrollPane; 019import javax.swing.event.HyperlinkEvent; 020import javax.swing.text.JTextComponent; 021 022import org.openstreetmap.josm.data.preferences.ListProperty; 023import org.openstreetmap.josm.gui.ExtendedDialog; 024import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration.OverpassWizardCallbacks; 025import org.openstreetmap.josm.gui.util.GuiHelper; 026import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 027import org.openstreetmap.josm.spi.preferences.Config; 028import org.openstreetmap.josm.tools.GBC; 029import org.openstreetmap.josm.tools.Logging; 030import org.openstreetmap.josm.tools.OpenBrowser; 031import org.openstreetmap.josm.tools.OverpassTurboQueryWizard; 032import org.openstreetmap.josm.tools.UncheckedParseException; 033import org.openstreetmap.josm.tools.Utils; 034 035/** 036 * This dialog provides an easy and fast way to create an overpass query. 037 * @since 12576 038 * @since 12652: Moved here 039 */ 040public final class OverpassQueryWizardDialog extends ExtendedDialog { 041 042 private final HistoryComboBox queryWizard; 043 private static final String HEADLINE_START = "<h3>"; 044 private static final String HEADLINE_END = "</h3>"; 045 private static final String TR_START = "<tr>"; 046 private static final String TR_END = "</tr>"; 047 private static final String TD_START = "<td>"; 048 private static final String TD_END = "</td>"; 049 private static final String SPAN_START = "<span>"; 050 private static final String SPAN_END = "</span>"; 051 private static final ListProperty OVERPASS_WIZARD_HISTORY = 052 new ListProperty("download.overpass.wizard", new ArrayList<String>()); 053 private final transient OverpassTurboQueryWizard overpassQueryBuilder; 054 055 // dialog buttons 056 private static final int BUILD_QUERY = 0; 057 private static final int BUILD_AN_EXECUTE_QUERY = 1; 058 private static final int CANCEL = 2; 059 060 private static final String DESCRIPTION_STYLE = 061 "<style type=\"text/css\">\n" 062 + "table { border-spacing: 0pt;}\n" 063 + "h3 {text-align: center; padding: 8px;}\n" 064 + "td {border: 1px solid #dddddd; text-align: left; padding: 8px;}\n" 065 + "#desc {width: 350px;}" 066 + "</style>\n"; 067 068 private final OverpassWizardCallbacks dsPanel; 069 070 /** 071 * Create a new {@link OverpassQueryWizardDialog} 072 * @param callbacks The Overpass download source panel. 073 */ 074 public OverpassQueryWizardDialog(OverpassWizardCallbacks callbacks) { 075 super(callbacks.getParent(), tr("Overpass Turbo Query Wizard"), 076 tr("Build query"), tr("Build query and execute"), tr("Cancel")); 077 this.dsPanel = callbacks; 078 079 this.queryWizard = new HistoryComboBox(); 080 this.overpassQueryBuilder = OverpassTurboQueryWizard.getInstance(); 081 082 JPanel panel = new JPanel(new GridBagLayout()); 083 084 JLabel searchLabel = new JLabel(tr("Search:")); 085 JTextComponent descPane = buildDescriptionSection(); 086 JScrollPane scroll = GuiHelper.embedInVerticalScrollPane(descPane); 087 scroll.getVerticalScrollBar().setUnitIncrement(10); // make scrolling smooth 088 089 panel.add(searchLabel, GBC.std().insets(0, 0, 0, 20).anchor(GBC.SOUTHEAST)); 090 panel.add(queryWizard, GBC.eol().insets(0, 0, 0, 15).fill(GBC.HORIZONTAL).anchor(GBC.SOUTH)); 091 panel.add(scroll, GBC.eol().fill(GBC.BOTH).anchor(GBC.CENTER)); 092 093 List<String> items = new ArrayList<>(OVERPASS_WIZARD_HISTORY.get()); 094 if (!items.isEmpty()) { 095 queryWizard.setText(items.get(0)); 096 } 097 queryWizard.setPossibleItemsTopDown(items); 098 099 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 return Optional.of(overpassQueryBuilder.constructQuery(searchTerm)); 146 } catch (UncheckedParseException | IllegalStateException ex) { 147 Logging.error(ex); 148 JOptionPane.showMessageDialog( 149 dsPanel.getParent(), 150 "<html>" + 151 tr("The Overpass wizard could not parse the following query:") + 152 Utils.joinAsHtmlUnorderedList(Collections.singleton(searchTerm)) + 153 "</html>", 154 tr("Parse error"), 155 JOptionPane.ERROR_MESSAGE 156 ); 157 return Optional.empty(); 158 } 159 } 160 161 /** 162 * Builds an Overpass query out from {@link OverpassQueryWizardDialog#queryWizard} contents. 163 * @return {@code true} if the query successfully built, {@code false} otherwise. 164 */ 165 private boolean buildQueryAction() { 166 final String wizardSearchTerm = this.queryWizard.getText(); 167 168 Optional<String> q = this.tryParseSearchTerm(wizardSearchTerm); 169 q.ifPresent(dsPanel::submitWizardResult); 170 return q.isPresent(); 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>.", Config.getUrls().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_START).append(tr("Download objects of a certain type.")).append(SPAN_END) 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_START) 218 .append(tr("Instead of <i>location</i> any valid place name can be used like address, city, etc.")) 219 .append(SPAN_END) 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_START) 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_END) 237 .append(TD_END).append(TR_END) 238 .append(TR_START).append(TD_START) 239 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList( 240 tr("<i>ref ~ \"[0-9]+\"</i>"), tr("<i>name ~ /postnord/i in sweden</i>")))) 241 .append(TD_END).append(TD_START) 242 .append(SPAN_START) 243 .append(tr("Regular expressions can be provided either as plain strings or with the regex notation. " + 244 "The modifier \"i\" makes the match case-insensitive")) 245 .append(SPAN_END) 246 .append(TD_END).append(TR_END) 247 .append("</table>") 248 .append("</body>") 249 .append("</html>") 250 .toString(); 251 } 252}