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.event.ActionEvent; 007import java.util.ArrayList; 008import java.util.Collections; 009import java.util.Optional; 010 011import javax.swing.JOptionPane; 012 013import org.openstreetmap.josm.data.osm.search.SearchSetting; 014import org.openstreetmap.josm.data.preferences.ListProperty; 015import org.openstreetmap.josm.gui.dialogs.SearchDialog; 016import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration.OverpassWizardCallbacks; 017import org.openstreetmap.josm.gui.tagging.ac.AutoCompComboBoxModel; 018import org.openstreetmap.josm.gui.widgets.JosmComboBoxModel; 019import org.openstreetmap.josm.tools.Logging; 020import org.openstreetmap.josm.tools.SearchCompilerQueryWizard; 021import org.openstreetmap.josm.tools.UncheckedParseException; 022import org.openstreetmap.josm.tools.Utils; 023 024/** 025 * This dialog provides an easy and fast way to create an overpass query. 026 * @since 12576 027 * @since 12652: Moved here 028 */ 029public final class OverpassQueryWizardDialog extends SearchDialog { 030 031 private static final ListProperty OVERPASS_WIZARD_HISTORY = 032 new ListProperty("download.overpass.wizard", new ArrayList<>()); 033 private final OverpassWizardCallbacks callbacks; 034 035 // dialog buttons 036 private static final int BUILD_QUERY = 0; 037 private static final int BUILD_AN_EXECUTE_QUERY = 1; 038 private static final int CANCEL = 2; 039 040 private final AutoCompComboBoxModel<SearchSetting> model; 041 042 /** preferences reader/writer with automatic transmogrification to and from String */ 043 private final JosmComboBoxModel<SearchSetting>.Preferences prefs; 044 045 /** 046 * Create a new {@link OverpassQueryWizardDialog} 047 * @param callbacks The Overpass download source panel. 048 */ 049 public OverpassQueryWizardDialog(OverpassWizardCallbacks callbacks) { 050 super(new SearchSetting(), new AutoCompComboBoxModel<>(), new PanelOptions(false, true), callbacks.getParent(), 051 tr("Overpass Query Wizard"), 052 tr("Build query"), tr("Build query and execute"), tr("Cancel")); 053 this.callbacks = callbacks; 054 model = hcbSearchString.getModel(); 055 setButtonIcons("dialogs/magic-wand", "download-overpass", "cancel"); 056 setCancelButton(CANCEL + 1); 057 setDefaultButton(BUILD_AN_EXECUTE_QUERY + 1); 058 prefs = model.prefs(SearchSetting::fromString, SearchSetting::toString); 059 prefs.load(OVERPASS_WIZARD_HISTORY); 060 } 061 062 @Override 063 public void buttonAction(int buttonIndex, ActionEvent evt) { 064 switch (buttonIndex) { 065 case BUILD_QUERY: 066 if (this.buildQueryAction()) { 067 this.saveHistory(); 068 super.buttonAction(BUILD_QUERY, evt); 069 } 070 break; 071 case BUILD_AN_EXECUTE_QUERY: 072 if (this.buildQueryAction()) { 073 this.saveHistory(); 074 super.buttonAction(BUILD_AN_EXECUTE_QUERY, evt); 075 076 DownloadDialog.getInstance().startDownload(); 077 } 078 break; 079 default: 080 super.buttonAction(buttonIndex, evt); 081 } 082 } 083 084 /** 085 * Saves the latest, successfully parsed search term. 086 */ 087 private void saveHistory() { 088 Optional.ofNullable(SearchSetting.fromString(hcbSearchString.getText())) 089 .ifPresent(model::addTopElement); 090 prefs.save(OVERPASS_WIZARD_HISTORY); 091 } 092 093 /** 094 * Tries to process a search term using {@link SearchCompilerQueryWizard}. If the term cannot 095 * be parsed, the the corresponding dialog is shown. 096 * @param searchTerm The search term to parse. 097 * @return {@link Optional#empty()} if an exception was thrown when parsing, meaning 098 * that the term cannot be processed, or non-empty {@link Optional} containing the result 099 * of parsing. 100 */ 101 private Optional<String> tryParseSearchTerm(String searchTerm) { 102 try { 103 return Optional.of(SearchCompilerQueryWizard.constructQuery(searchTerm)); 104 } catch (UncheckedParseException | IllegalStateException ex) { 105 Logging.error(ex); 106 JOptionPane.showMessageDialog( 107 callbacks.getParent(), 108 "<html>" + 109 tr("The Overpass wizard could not parse the following query:") + 110 Utils.joinAsHtmlUnorderedList(Collections.singleton(Utils.escapeReservedCharactersHTML(searchTerm))) + 111 "</html>", 112 tr("Parse error"), 113 JOptionPane.ERROR_MESSAGE 114 ); 115 return Optional.empty(); 116 } 117 } 118 119 /** 120 * Builds an Overpass query out from {@link SearchSetting} contents. 121 * @return {@code true} if the query successfully built, {@code false} otherwise. 122 */ 123 private boolean buildQueryAction() { 124 Optional<String> q = tryParseSearchTerm(getSearchSettings().text); 125 q.ifPresent(callbacks::submitWizardResult); 126 return q.isPresent(); 127 } 128}