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.Main; 023import org.openstreetmap.josm.data.preferences.ListProperty; 024import org.openstreetmap.josm.gui.ExtendedDialog; 025import org.openstreetmap.josm.gui.util.GuiHelper; 026import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 027import org.openstreetmap.josm.tools.GBC; 028import org.openstreetmap.josm.tools.Logging; 029import org.openstreetmap.josm.tools.OpenBrowser; 030import org.openstreetmap.josm.tools.OverpassTurboQueryWizard; 031import org.openstreetmap.josm.tools.UncheckedParseException; 032import org.openstreetmap.josm.tools.Utils; 033 034/** 035 * This dialog provides an easy and fast way to create an overpass query. 036 * @since 12576 037 * @since 12652: Moved here 038 */ 039public final class OverpassQueryWizardDialog extends ExtendedDialog { 040 041 private final HistoryComboBox queryWizard; 042 private static final String HEADLINE_START = "<h3>"; 043 private static final String HEADLINE_END = "</h3>"; 044 private static final String TR_START = "<tr>"; 045 private static final String TR_END = "</tr>"; 046 private static final String TD_START = "<td>"; 047 private static final String TD_END = "</td>"; 048 private static final String SPAN_START = "<span>"; 049 private static final String SPAN_END = "</span>"; 050 private static final ListProperty OVERPASS_WIZARD_HISTORY = 051 new ListProperty("download.overpass.wizard", new ArrayList<String>()); 052 private final transient OverpassTurboQueryWizard overpassQueryBuilder; 053 054 // dialog buttons 055 private static final int BUILD_QUERY = 0; 056 private static final int BUILD_AN_EXECUTE_QUERY = 1; 057 private static final int CANCEL = 2; 058 059 private static final String DESCRIPTION_STYLE = 060 "<style type=\"text/css\">\n" 061 + "table { border-spacing: 0pt;}\n" 062 + "h3 {text-align: center; padding: 8px;}\n" 063 + "td {border: 1px solid #dddddd; text-align: left; padding: 8px;}\n" 064 + "#desc {width: 350px;}" 065 + "</style>\n"; 066 067 private final OverpassDownloadSource.OverpassDownloadSourcePanel dsPanel; 068 069 /** 070 * Create a new {@link OverpassQueryWizardDialog} 071 * @param dsPanel The Overpass download source panel. 072 */ 073 public OverpassQueryWizardDialog(OverpassDownloadSource.OverpassDownloadSourcePanel dsPanel) { 074 super(dsPanel.getParent(), tr("Overpass Turbo Query Wizard"), 075 tr("Build query"), tr("Build query and execute"), tr("Cancel")); 076 this.dsPanel = dsPanel; 077 078 this.queryWizard = new HistoryComboBox(); 079 this.overpassQueryBuilder = OverpassTurboQueryWizard.getInstance(); 080 081 JPanel panel = new JPanel(new GridBagLayout()); 082 083 JLabel searchLabel = new JLabel(tr("Search :")); 084 JTextComponent descPane = buildDescriptionSection(); 085 JScrollPane scroll = GuiHelper.embedInVerticalScrollPane(descPane); 086 scroll.getVerticalScrollBar().setUnitIncrement(10); // make scrolling smooth 087 088 panel.add(searchLabel, GBC.std().insets(0, 0, 0, 20).anchor(GBC.SOUTHEAST)); 089 panel.add(queryWizard, GBC.eol().insets(0, 0, 0, 15).fill(GBC.HORIZONTAL).anchor(GBC.SOUTH)); 090 panel.add(scroll, GBC.eol().fill(GBC.BOTH).anchor(GBC.CENTER)); 091 092 List<String> items = new ArrayList<>(OVERPASS_WIZARD_HISTORY.get()); 093 if (!items.isEmpty()) { 094 queryWizard.setText(items.get(0)); 095 } 096 // HistoryComboBox needs the reversed list 097 Collections.reverse(items); 098 queryWizard.setPossibleItems(items); 099 100 setCancelButton(CANCEL + 1); 101 setDefaultButton(BUILD_AN_EXECUTE_QUERY + 1); 102 setContent(panel, false); 103 } 104 105 @Override 106 public void buttonAction(int buttonIndex, ActionEvent evt) { 107 switch (buttonIndex) { 108 case BUILD_QUERY: 109 if (this.buildQueryAction()) { 110 this.saveHistory(); 111 super.buttonAction(BUILD_QUERY, evt); 112 } 113 break; 114 case BUILD_AN_EXECUTE_QUERY: 115 if (this.buildQueryAction()) { 116 this.saveHistory(); 117 super.buttonAction(BUILD_AN_EXECUTE_QUERY, evt); 118 119 DownloadDialog.getInstance().startDownload(); 120 } 121 break; 122 default: 123 super.buttonAction(buttonIndex, evt); 124 125 } 126 } 127 128 /** 129 * Saves the latest, successfully parsed search term. 130 */ 131 private void saveHistory() { 132 queryWizard.addCurrentItemToHistory(); 133 OVERPASS_WIZARD_HISTORY.put(queryWizard.getHistory()); 134 } 135 136 /** 137 * Tries to process a search term using {@link OverpassTurboQueryWizard}. If the term cannot 138 * be parsed, the the corresponding dialog is shown. 139 * @param searchTerm The search term to parse. 140 * @return {@link Optional#empty()} if an exception was thrown when parsing, meaning 141 * that the term cannot be processed, or non-empty {@link Optional} containing the result 142 * of parsing. 143 */ 144 private Optional<String> tryParseSearchTerm(String searchTerm) { 145 try { 146 return Optional.of(overpassQueryBuilder.constructQuery(searchTerm)); 147 } catch (UncheckedParseException | IllegalStateException ex) { 148 Logging.error(ex); 149 JOptionPane.showMessageDialog( 150 dsPanel.getParent(), 151 "<html>" + 152 tr("The Overpass wizard could not parse the following query:") + 153 Utils.joinAsHtmlUnorderedList(Collections.singleton(searchTerm)) + 154 "</html>", 155 tr("Parse error"), 156 JOptionPane.ERROR_MESSAGE 157 ); 158 return Optional.empty(); 159 } 160 } 161 162 /** 163 * Builds an Overpass query out from {@link OverpassQueryWizardDialog#queryWizard} contents. 164 * @return {@code true} if the query successfully built, {@code false} otherwise. 165 */ 166 private boolean buildQueryAction() { 167 final String wizardSearchTerm = this.queryWizard.getText(); 168 169 Optional<String> q = this.tryParseSearchTerm(wizardSearchTerm); 170 if (q.isPresent()) { 171 String query = q.get(); 172 dsPanel.setOverpassQuery(query); 173 174 return true; 175 } 176 177 return false; 178 } 179 180 private static JTextComponent buildDescriptionSection() { 181 JEditorPane descriptionSection = new JEditorPane("text/html", getDescriptionContent()); 182 descriptionSection.setEditable(false); 183 descriptionSection.addHyperlinkListener(e -> { 184 if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) { 185 OpenBrowser.displayUrl(e.getURL().toString()); 186 } 187 }); 188 189 return descriptionSection; 190 } 191 192 private static String getDescriptionContent() { 193 return new StringBuilder("<html>") 194 .append(DESCRIPTION_STYLE) 195 .append("<body>") 196 .append(HEADLINE_START) 197 .append(tr("Query Wizard")) 198 .append(HEADLINE_END) 199 .append("<p>") 200 .append(tr("Allows you to interact with <i>Overpass API</i> by writing declarative, human-readable terms.")) 201 .append(tr("The <i>Query Wizard</i> tool will transform those to a valid overpass query.")) 202 .append(tr("For more detailed description see ")) 203 .append(tr("<a href=\"{0}\">OSM Wiki</a>.", Main.getOSMWebsite() + "/wiki/Overpass_turbo/Wizard")) 204 .append("</p>") 205 .append(HEADLINE_START).append(tr("Hints")).append(HEADLINE_END) 206 .append("<table>").append(TR_START).append(TD_START) 207 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList("<i>type:node</i>", "<i>type:relation</i>", "<i>type:way</i>"))) 208 .append(TD_END).append(TD_START) 209 .append(SPAN_START).append(tr("Download objects of a certain type.")).append(SPAN_END) 210 .append(TD_END).append(TR_END) 211 .append(TR_START).append(TD_START) 212 .append(Utils.joinAsHtmlUnorderedList( 213 Arrays.asList("<i>key=value in <u>location</u></i>", 214 "<i>key=value around <u>location</u></i>", 215 "<i>key=value in bbox</i>"))) 216 .append(TD_END).append(TD_START) 217 .append(tr("Download object by specifying a specific location. For example,")) 218 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList( 219 tr("{0} all objects having {1} as attribute are downloaded.", "<i>tourism=hotel in Berlin</i> -", "'tourism=hotel'"), 220 tr("{0} all object with the corresponding key/value pair located around Berlin. Note, the default value for radius "+ 221 "is set to 1000m, but it can be changed in the generated query.", "<i>tourism=hotel around Berlin</i> -"), 222 tr("{0} all objects within the current selection that have {1} as attribute.", "<i>tourism=hotel in bbox</i> -", 223 "'tourism=hotel'")))) 224 .append(SPAN_START) 225 .append(tr("Instead of <i>location</i> any valid place name can be used like address, city, etc.")) 226 .append(SPAN_END) 227 .append(TD_END).append(TR_END) 228 .append(TR_START).append(TD_START) 229 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList("<i>key=value</i>", "<i>key=*</i>", "<i>key~regex</i>", 230 "<i>key!=value</i>", "<i>key!~regex</i>", "<i>key=\"combined value\"</i>"))) 231 .append(TD_END).append(TD_START) 232 .append(tr("<span>Download objects that have some concrete key/value pair, only the key with any contents for the value, " + 233 "the value matching some regular expression. \"Not equal\" operators are supported as well.</span>")) 234 .append(TD_END).append(TR_END) 235 .append(TR_START).append(TD_START) 236 .append(Utils.joinAsHtmlUnorderedList(Arrays.asList( 237 tr("<i>expression1 {0} expression2</i>", "or"), 238 tr("<i>expression1 {0} expression2</i>", "and")))) 239 .append(TD_END).append(TD_START) 240 .append(SPAN_START) 241 .append(tr("Basic logical operators can be used to create more sophisticated queries. Instead of \"or\" - \"|\", \"||\" " + 242 "can be used, and instead of \"and\" - \"&\", \"&&\".")) 243 .append(SPAN_END) 244 .append(TD_END).append(TR_END).append("</table>") 245 .append("</body>") 246 .append("</html>") 247 .toString(); 248 } 249}