Changeset 16354 in josm


Ignore:
Timestamp:
2020-04-19T12:44:38+02:00 (4 years ago)
Author:
simon04
Message:

see #18164 - OverpassQueryWizardDialog: reuse SearchDialog

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/SearchDialog.java

    r16306 r16354  
    55import static org.openstreetmap.josm.tools.I18n.trc;
    66
     7import java.awt.Component;
    78import java.awt.Cursor;
    89import java.awt.Dimension;
     
    5455    private final SearchSetting searchSettings;
    5556
    56     private final HistoryComboBox hcbSearchString = new HistoryComboBox();
     57    protected final HistoryComboBox hcbSearchString = new HistoryComboBox();
    5758
    5859    private JCheckBox addOnToolbar;
     
    7677     */
    7778    public SearchDialog(SearchSetting initialValues, List<String> searchExpressionHistory, boolean expertMode) {
    78         super(MainApplication.getMainFrame(),
     79        this(initialValues, searchExpressionHistory, new PanelOptions(expertMode, false), MainApplication.getMainFrame(),
    7980                initialValues instanceof Filter ? tr("Filter") : tr("Search"),
    8081                initialValues instanceof Filter ? tr("Submit filter") : tr("Search"),
    8182                tr("Cancel"));
    82         this.searchSettings = new SearchSetting(initialValues);
    8383        setButtonIcons("dialogs/search", "cancel");
    8484        configureContextsensitiveHelp("/Action/Search", true /* show help button */);
    85         setContent(buildPanel(searchExpressionHistory, expertMode));
    86     }
    87 
    88     private JPanel buildPanel(List<String> searchExpressionHistory, boolean expertMode) {
     85    }
     86
     87    protected SearchDialog(SearchSetting initialValues, List<String> searchExpressionHistory, PanelOptions options,
     88                           Component mainFrame, String title, String... buttonTexts) {
     89        super(mainFrame, title, buttonTexts);
     90        this.searchSettings = new SearchSetting(initialValues);
     91        setContent(buildPanel(searchExpressionHistory, options));
     92    }
     93
     94    /**
     95     * Determines which parts of the search dialog will be shown
     96     */
     97    protected static class PanelOptions {
     98        private final boolean expertMode;
     99        private final boolean overpassQuery;
     100
     101        /**
     102         * Constructs new options which determine which parts of the search dialog will be shown
     103         * @param expertMode whether export mode is enabled
     104         * @param overpassQuery whether the panel shall be adapted for Overpass query
     105         */
     106        public PanelOptions(boolean expertMode, boolean overpassQuery) {
     107            this.expertMode = expertMode;
     108            this.overpassQuery = overpassQuery;
     109        }
     110    }
     111
     112    private JPanel buildPanel(List<String> searchExpressionHistory, PanelOptions options) {
    89113
    90114        // prepare the combo box with the search expressions
     
    140164        left.add(additionalSettings, GBC.eol().fill(GBC.BOTH));
    141165
    142         if (expertMode) {
     166        if (options.expertMode) {
    143167            additionalSettings.add(allElements, GBC.eol());
    144168            additionalSettings.add(addOnToolbar, GBC.eop());
     
    153177        }
    154178
    155         JPanel right = buildHintsSection(hcbSearchString, expertMode);
     179        JPanel right = buildHintsSection(hcbSearchString, options);
    156180        JPanel top = new JPanel(new GridBagLayout());
    157181        top.add(label, GBC.std().insets(0, 0, 5, 0));
     
    214238        JPanel p = new JPanel(new GridBagLayout());
    215239        p.add(top, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 5, 0));
    216         p.add(left, GBC.std().anchor(GBC.NORTH).insets(5, 10, 10, 0).fill(GBC.VERTICAL));
     240        if (!options.overpassQuery) {
     241            p.add(left, GBC.std().anchor(GBC.NORTH).insets(5, 10, 10, 0).fill(GBC.VERTICAL));
     242        }
    217243        p.add(right, GBC.std().fill(GBC.BOTH).insets(0, 10, 0, 0));
    218         p.add(selector, GBC.eol().fill(GBC.BOTH).insets(0, 10, 0, 0));
     244        if (!options.overpassQuery) {
     245            p.add(selector, GBC.eol().fill(GBC.BOTH).insets(0, 10, 0, 0));
     246        }
    219247
    220248        return p;
     
    279307    }
    280308
    281     private static JPanel buildHintsSection(HistoryComboBox hcbSearchString, boolean expertMode) {
     309    private static JPanel buildHintsSection(HistoryComboBox hcbSearchString, PanelOptions options) {
    282310        JPanel hintPanel = new JPanel(new GridBagLayout());
    283311        hintPanel.setBorder(BorderFactory.createTitledBorder(tr("Hints")));
     
    317345                GBC.eol());
    318346
    319         if (expertMode) {
    320             hintPanel.add(new SearchKeywordRow(hcbSearchString)
     347        SearchKeywordRow objectHints = new SearchKeywordRow(hcbSearchString)
    321348                .addTitle(tr("objects"))
    322349                .addKeyword("type:node", "type:node ", tr("all nodes"))
    323350                .addKeyword("type:way", "type:way ", tr("all ways"))
    324                 .addKeyword("type:relation", "type:relation ", tr("all relations"))
     351                .addKeyword("type:relation", "type:relation ", tr("all relations"));
     352        if (options.expertMode) {
     353            objectHints
    325354                .addKeyword("closed", "closed ", tr("all closed ways"))
    326                 .addKeyword("untagged", "untagged ", tr("object without useful tags")),
    327                 GBC.eol());
     355                .addKeyword("untagged", "untagged ", tr("object without useful tags"));
     356        }
     357        hintPanel.add(objectHints, GBC.eol());
     358
     359        if (options.expertMode) {
    328360            hintPanel.add(new SearchKeywordRow(hcbSearchString)
    329361                    .addKeyword("preset:\"Annotation/Address\"", "preset:\"Annotation/Address\"",
     
    381413                .addKeyword("allindownloadedarea", "allindownloadedarea ",
    382414                        tr("objects (and all its way nodes / relation members) in downloaded area")),
     415                GBC.eol());
     416        }
     417        if (options.overpassQuery) {
     418            hintPanel.add(new SearchKeywordRow(hcbSearchString)
     419                .addTitle(tr("location"))
     420                .addKeyword("<i>key=value in <u>location</u></i>", null,
     421                        tr("{0} all objects having {1} as attribute are downloaded.", "<i>tourism=hotel in Berlin</i> -", "'tourism=hotel'"))
     422                .addKeyword("<i>key=value around <u>location</u></i>", null,
     423                        tr("{0} all object with the corresponding key/value pair located around Berlin. Note, the default value for radius " +
     424                                "is set to 1000m, but it can be changed in the generated query.", "<i>tourism=hotel around Berlin</i> -"))
     425                .addKeyword("<i>key=value in bbox</i>", null,
     426                        tr("{0} all objects within the current selection that have {1} as attribute.", "<i>tourism=hotel in bbox</i> -",
     427                                "'tourism=hotel'")),
    383428                GBC.eol());
    384429        }
  • trunk/src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java

    r16262 r16354  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.GridBagLayout;
    76import java.awt.event.ActionEvent;
    87import java.util.ArrayList;
    9 import java.util.Arrays;
    108import java.util.Collections;
    11 import java.util.List;
    129import java.util.Optional;
    1310
    14 import javax.swing.JEditorPane;
    15 import javax.swing.JLabel;
    1611import javax.swing.JOptionPane;
    17 import javax.swing.JPanel;
    18 import javax.swing.JScrollPane;
    19 import javax.swing.event.HyperlinkEvent;
    20 import javax.swing.text.JTextComponent;
    2112
     13import org.openstreetmap.josm.data.osm.search.SearchSetting;
    2214import org.openstreetmap.josm.data.preferences.ListProperty;
    23 import org.openstreetmap.josm.gui.ExtendedDialog;
     15import org.openstreetmap.josm.gui.dialogs.SearchDialog;
    2416import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration.OverpassWizardCallbacks;
    25 import org.openstreetmap.josm.gui.util.GuiHelper;
    26 import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
    27 import org.openstreetmap.josm.spi.preferences.Config;
    28 import org.openstreetmap.josm.tools.GBC;
    2917import org.openstreetmap.josm.tools.Logging;
    30 import org.openstreetmap.josm.tools.OpenBrowser;
    3118import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
    3219import org.openstreetmap.josm.tools.UncheckedParseException;
     
    3825 * @since 12652: Moved here
    3926 */
    40 public final class OverpassQueryWizardDialog extends ExtendedDialog {
     27public final class OverpassQueryWizardDialog extends SearchDialog {
    4128
    42     private final HistoryComboBox queryWizard;
    43     private static final String HEADLINE_START = "<h3>";
    44     private static final String HEADLINE_END = "</h3>";
    45     private static final String TR_START = "<tr>";
    46     private static final String TR_END = "</tr>";
    47     private static final String TD_START = "<td>";
    48     private static final String TD_END = "</td>";
    49     private static final String SPAN_START = "<span>";
    50     private static final String SPAN_END = "</span>";
    5129    private static final ListProperty OVERPASS_WIZARD_HISTORY =
    5230            new ListProperty("download.overpass.wizard", new ArrayList<String>());
    53     private final transient OverpassTurboQueryWizard overpassQueryBuilder;
     31    private final OverpassWizardCallbacks callbacks;
    5432
    5533    // dialog buttons
     
    5836    private static final int CANCEL = 2;
    5937
    60     private static final String DESCRIPTION_STYLE =
    61             "<style type=\"text/css\">\n"
    62             + "table { border-spacing: 0pt;}\n"
    63             + "h3 {text-align: center; padding: 8px;}\n"
    64             + "td {border: 1px solid #dddddd; text-align: left; padding: 8px;}\n"
    65             + "#desc {width: 350px;}"
    66             + "</style>\n";
    67 
    68     private final OverpassWizardCallbacks dsPanel;
    69 
    7038    /**
    7139     * Create a new {@link OverpassQueryWizardDialog}
     
    7341     */
    7442    public OverpassQueryWizardDialog(OverpassWizardCallbacks callbacks) {
    75         super(callbacks.getParent(), tr("Overpass Turbo Query Wizard"),
     43        super(new SearchSetting(), OVERPASS_WIZARD_HISTORY.get(), new PanelOptions(false, true), callbacks.getParent(),
     44                tr("Overpass Turbo Query Wizard"),
    7645                tr("Build query"), tr("Build query and execute"), tr("Cancel"));
    77         this.dsPanel = callbacks;
    78 
    79         this.queryWizard = new HistoryComboBox();
    80         this.overpassQueryBuilder = OverpassTurboQueryWizard.getInstance();
    81 
    82         JPanel panel = new JPanel(new GridBagLayout());
    83 
    84         JLabel searchLabel = new JLabel(tr("Search:"));
    85         JTextComponent descPane = buildDescriptionSection();
    86         JScrollPane scroll = GuiHelper.embedInVerticalScrollPane(descPane);
    87         scroll.getVerticalScrollBar().setUnitIncrement(10); // make scrolling smooth
    88 
    89         panel.add(searchLabel, GBC.std().insets(0, 0, 0, 20).anchor(GBC.SOUTHEAST));
    90         panel.add(queryWizard, GBC.eol().insets(0, 0, 0, 15).fill(GBC.HORIZONTAL).anchor(GBC.SOUTH));
    91         panel.add(scroll, GBC.eol().fill(GBC.BOTH).anchor(GBC.CENTER));
    92 
    93         List<String> items = new ArrayList<>(OVERPASS_WIZARD_HISTORY.get());
    94         if (!items.isEmpty()) {
    95             queryWizard.setText(items.get(0));
    96         }
    97         queryWizard.setPossibleItemsTopDown(items);
    98 
     46        this.callbacks = callbacks;
     47        setButtonIcons("ok", "download-overpass", "cancel");
    9948        setCancelButton(CANCEL + 1);
    10049        setDefaultButton(BUILD_AN_EXECUTE_QUERY + 1);
    101         setContent(panel, false);
    10250    }
    10351
     
    12977     */
    13078    private void saveHistory() {
    131         queryWizard.addCurrentItemToHistory();
    132         OVERPASS_WIZARD_HISTORY.put(queryWizard.getHistory());
     79        hcbSearchString.addCurrentItemToHistory();
     80        OVERPASS_WIZARD_HISTORY.put(hcbSearchString.getHistory());
    13381    }
    13482
     
    14391    private Optional<String> tryParseSearchTerm(String searchTerm) {
    14492        try {
    145             return Optional.of(overpassQueryBuilder.constructQuery(searchTerm));
     93            return Optional.of(OverpassTurboQueryWizard.getInstance().constructQuery(searchTerm));
    14694        } catch (UncheckedParseException | IllegalStateException ex) {
    14795            Logging.error(ex);
    14896            JOptionPane.showMessageDialog(
    149                     dsPanel.getParent(),
     97                    callbacks.getParent(),
    15098                    "<html>" +
    15199                     tr("The Overpass wizard could not parse the following query:") +
     
    160108
    161109    /**
    162      * Builds an Overpass query out from {@link OverpassQueryWizardDialog#queryWizard} contents.
     110     * Builds an Overpass query out from {@link SearchSetting} contents.
    163111     * @return {@code true} if the query successfully built, {@code false} otherwise.
    164112     */
    165113    private boolean buildQueryAction() {
    166         final String wizardSearchTerm = this.queryWizard.getText();
     114        final String wizardSearchTerm = getSearchSettings().text;
    167115
    168116        Optional<String> q = this.tryParseSearchTerm(wizardSearchTerm);
    169         q.ifPresent(dsPanel::submitWizardResult);
     117        q.ifPresent(callbacks::submitWizardResult);
    170118        return q.isPresent();
    171119    }
    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     }
    252120}
Note: See TracChangeset for help on using the changeset viewer.