Ticket #16343: overpass-wizard-buttons.patch

File overpass-wizard-buttons.patch, 12.0 KB (added by michael2402, 6 years ago)
  • src/org/openstreetmap/josm/gui/download/OverpassDownloadSource.java

    diff --git a/src/org/openstreetmap/josm/gui/download/OverpassDownloadSource.java b/src/org/openstreetmap/josm/gui/download/OverpassDownloadSource.java
    index c43902a..bac3715 100644
    a b import java.awt.event.ActionEvent;  
    1010import java.awt.event.FocusAdapter;
    1111import java.awt.event.FocusEvent;
    1212import java.util.Collection;
     13import java.util.Objects;
    1314import java.util.concurrent.Future;
    1415import java.util.function.Consumer;
    1516
    import org.openstreetmap.josm.data.preferences.StringProperty;  
    3435import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    3536import org.openstreetmap.josm.gui.MainApplication;
    3637import org.openstreetmap.josm.gui.download.DownloadSourceSizingPolicy.AdjustableDownloadSizePolicy;
     38import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration;
     39import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration.OverpassQueryWizard;
     40import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration.OverpassWizardCallbacks;
    3741import org.openstreetmap.josm.gui.util.GuiHelper;
    3842import org.openstreetmap.josm.gui.widgets.JosmTextArea;
    3943import org.openstreetmap.josm.io.OverpassDownloadReader;
    public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo  
    8084     * The GUI representation of the Overpass download source.
    8185     * @since 12652
    8286     */
    83     public static class OverpassDownloadSourcePanel extends AbstractDownloadSourcePanel<OverpassDownloadData> {
     87    public static class OverpassDownloadSourcePanel extends AbstractDownloadSourcePanel<OverpassDownloadData>
     88            implements OverpassWizardCallbacks {
    8489
    8590        private static final String SIMPLE_NAME = "overpassdownloadpanel";
    8691        private static final AbstractProperty<Integer> PANEL_SIZE_PROPERTY =
    public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo  
    103108            super(ds);
    104109            setLayout(new BorderLayout());
    105110
    106             String tooltip = tr("Build an Overpass query using the Overpass Turbo Query Wizard tool");
    107 
    108             JButton openQueryWizard = new JButton(tr("Query Wizard"));
    109             openQueryWizard.setToolTipText(tooltip);
    110             openQueryWizard.addActionListener(new AbstractAction() {
    111                 @Override
    112                 public void actionPerformed(ActionEvent e) {
    113                     new OverpassQueryWizardDialog(OverpassDownloadSourcePanel.this).showDialog();
    114                 }
    115             });
    116 
    117111            this.overpassQuery = new JosmTextArea(DOWNLOAD_QUERY.get(), 8, 80);
    118112            this.overpassQuery.setFont(GuiHelper.getMonospacedFont(overpassQuery));
    119113            this.overpassQuery.addFocusListener(new FocusAdapter() {
    public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo  
    163157            JPanel leftPanel = new JPanel(new GridBagLayout());
    164158            leftPanel.add(new JLabel(tr("Overpass query:")), GBC.eol().insets(5, 1, 5, 1).anchor(GBC.NORTHWEST));
    165159            leftPanel.add(new JLabel(), GBC.eol().fill(GBC.VERTICAL));
    166             leftPanel.add(openQueryWizard, GBC.eol().anchor(GBC.CENTER));
     160            OverpassWizardRegistration.getWizards()
     161                .stream()
     162                .map(this::generateWizardButton)
     163                .forEach(button -> leftPanel.add(button, GBC.eol().anchor(GBC.CENTER)));
    167164            leftPanel.add(new JLabel(), GBC.eol().fill(GBC.VERTICAL));
    168165
    169166            add(leftPanel, BorderLayout.WEST);
    public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo  
    173170            setMinimumSize(new Dimension(450, 240));
    174171        }
    175172
     173        private JButton generateWizardButton(OverpassQueryWizard wizard) {
     174            JButton openQueryWizard = new JButton(wizard.getWizardName());
     175            openQueryWizard.setToolTipText(wizard.getWizardTooltip().orElse(null));
     176            openQueryWizard.addActionListener(new AbstractAction() {
     177                @Override
     178                public void actionPerformed(ActionEvent e) {
     179                    wizard.startWizard(OverpassDownloadSourcePanel.this);
     180                }
     181            });
     182            return openQueryWizard;
     183        }
     184
    176185        @Override
    177186        public OverpassDownloadData getData() {
    178187            String query = overpassQuery.getText();
    public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo  
    258267         * @param query The query to set.
    259268         */
    260269        public void setOverpassQuery(String query) {
     270            Objects.requireNonNull(query, "query");
    261271            this.overpassQuery.setText(query);
    262272        }
    263273
    public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo  
    359369                checkEnabled();
    360370            }
    361371        }
     372
     373        @Override
     374        public void submitWizardResult(String resultingQuery) {
     375            setOverpassQuery(resultingQuery);
     376        }
    362377    }
    363378
    364379    /**
    public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo  
    381396            return this.errorReporter;
    382397        }
    383398    }
     399
    384400}
  • src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java

    diff --git a/src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java b/src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java
    index 8682501..5432e15 100644
    a b import javax.swing.text.JTextComponent;  
    2222import org.openstreetmap.josm.Main;
    2323import org.openstreetmap.josm.data.preferences.ListProperty;
    2424import org.openstreetmap.josm.gui.ExtendedDialog;
     25import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration.OverpassWizardCallbacks;
    2526import org.openstreetmap.josm.gui.util.GuiHelper;
    2627import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
    2728import org.openstreetmap.josm.tools.GBC;
    public final class OverpassQueryWizardDialog extends ExtendedDialog {  
    6465            + "#desc {width: 350px;}"
    6566            + "</style>\n";
    6667
    67     private final OverpassDownloadSource.OverpassDownloadSourcePanel dsPanel;
     68    private final OverpassWizardCallbacks dsPanel;
    6869
    6970    /**
    7071     * Create a new {@link OverpassQueryWizardDialog}
    71      * @param dsPanel The Overpass download source panel.
     72     * @param callbacks The Overpass download source panel.
    7273     */
    73     public OverpassQueryWizardDialog(OverpassDownloadSource.OverpassDownloadSourcePanel dsPanel) {
    74         super(dsPanel.getParent(), tr("Overpass Turbo Query Wizard"),
     74    public OverpassQueryWizardDialog(OverpassWizardCallbacks callbacks) {
     75        super(callbacks.getParent(), tr("Overpass Turbo Query Wizard"),
    7576                tr("Build query"), tr("Build query and execute"), tr("Cancel"));
    76         this.dsPanel = dsPanel;
     77        this.dsPanel = callbacks;
    7778
    7879        this.queryWizard = new HistoryComboBox();
    7980        this.overpassQueryBuilder = OverpassTurboQueryWizard.getInstance();
    public final class OverpassQueryWizardDialog extends ExtendedDialog {  
    167168        final String wizardSearchTerm = this.queryWizard.getText();
    168169
    169170        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;
     171        q.ifPresent(dsPanel::submitWizardResult);
     172        return q.isPresent();
    178173    }
    179174
    180175    private static JTextComponent buildDescriptionSection() {
  • new file src/org/openstreetmap/josm/gui/download/overpass/OverpassWizardRegistration.java

    diff --git a/src/org/openstreetmap/josm/gui/download/overpass/OverpassWizardRegistration.java b/src/org/openstreetmap/josm/gui/download/overpass/OverpassWizardRegistration.java
    new file mode 100644
    index 0000000..bf8b99a
    - +  
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.gui.download.overpass;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import java.awt.Component;
     7import java.util.ArrayList;
     8import java.util.Collections;
     9import java.util.List;
     10import java.util.Objects;
     11import java.util.Optional;
     12
     13import org.openstreetmap.josm.gui.download.OverpassQueryWizardDialog;
     14
     15/**
     16 * Registers the overpass query wizards.
     17 * @author Michael Zangl
     18 * @since xxx
     19 */
     20public final class OverpassWizardRegistration {
     21    /**
     22     * A list of all reigstered wizards. Needs to be synchronized since plugin registration may happen outside main thread / asynchronously.
     23     */
     24    private static List<OverpassQueryWizard> wizards = Collections.synchronizedList(new ArrayList<>());
     25
     26    /**
     27     * Registers a wizard to be added to the overpass download dialog
     28     * <p>
     29     * To be called by plugins during the JOSM boot process or at least before opening the download dialog for the first time.
     30     * @param wizard The wizard to register
     31     * @since xxx
     32     */
     33    public static void registerWizard(OverpassQueryWizard wizard) {
     34        Objects.requireNonNull(wizard, "wizard");
     35        wizards.add(wizard);
     36    }
     37
     38    /**
     39     * Gets all wizards that are currently registered.
     40     * @return The list of wizards.
     41     */
     42    public static List<OverpassQueryWizard> getWizards() {
     43        return Collections.unmodifiableList(wizards);
     44    }
     45
     46    static {
     47        // Register the default wizard
     48        registerWizard(new OverpassQueryWizard() {
     49            @Override
     50            public void startWizard(OverpassWizardCallbacks callbacks) {
     51                new OverpassQueryWizardDialog(callbacks).showDialog();
     52            }
     53
     54            @Override
     55            public Optional<String> getWizardTooltip() {
     56                return Optional.of(tr("Build an Overpass query using the Overpass Turbo Query Wizard tool"));
     57            }
     58
     59            @Override
     60            public String getWizardName() {
     61                return tr("Query Wizard");
     62            }
     63        });
     64    }
     65
     66    private OverpassWizardRegistration() {
     67        // hidden
     68    }
     69
     70    /**
     71     * Defines a query wizard that generates overpass queries.
     72     * @author Michael Zangl
     73     * @since xxx
     74     */
     75    public interface OverpassQueryWizard {
     76        /**
     77         * Get the name of the wizard
     78         * @return The name
     79         */
     80        String getWizardName();
     81
     82        /**
     83         * Get the tooltip text to display when hovering the wizard button.
     84         * @return The tooltip text or an empty optional to display no tooltip.
     85         */
     86        Optional<String> getWizardTooltip();
     87
     88        /**
     89         * Start the wizard.
     90         * @param callbacks The callbacks to use to send back wizard results.
     91         */
     92        void startWizard(OverpassWizardCallbacks callbacks);
     93    }
     94
     95    /**
     96     * Wizard callbacks required by {@link OverpassQueryWizard#startWizard(OverpassWizardCallbacks)}
     97     * @author Michael Zangl
     98     * @since xxx
     99     */
     100    public interface OverpassWizardCallbacks {
     101        /**
     102         * Send the resulting query
     103         * @param resultingQuery The query that is used by the wizard
     104         */
     105        void submitWizardResult(String resultingQuery);
     106
     107        /**
     108         * Get the parent component to use when opening the wizard dialog.
     109         * @return The component.
     110         */
     111        Component getParent();
     112    }
     113}
  • new file src/org/openstreetmap/josm/gui/download/overpass/package-info.java

    diff --git a/src/org/openstreetmap/josm/gui/download/overpass/package-info.java b/src/org/openstreetmap/josm/gui/download/overpass/package-info.java
    new file mode 100644
    index 0000000..ad059b9
    - +  
     1// License: GPL. For details, see LICENSE file.
     2/**
     3 * This package contains all overpass query related classes.
     4 * <p>
     5 * Traditionally (and still now for compatibility), some of them are in the download package.
     6 */
     7package org.openstreetmap.josm.gui.download.overpass;