001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.download.overpass;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.util.ArrayList;
008import java.util.Collections;
009import java.util.List;
010import java.util.Objects;
011import java.util.Optional;
012
013import org.openstreetmap.josm.gui.download.OverpassQueryWizardDialog;
014
015/**
016 * Registers the overpass query wizards.
017 * @author Michael Zangl
018 * @since 13930
019 */
020public final class OverpassWizardRegistration {
021    /**
022     * A list of all reigstered wizards. Needs to be synchronized since plugin registration may happen outside main thread / asynchronously.
023     */
024    private static List<OverpassQueryWizard> wizards = Collections.synchronizedList(new ArrayList<>());
025
026    /**
027     * Registers a wizard to be added to the overpass download dialog
028     * <p>
029     * To be called by plugins during the JOSM boot process or at least before opening the download dialog for the first time.
030     * @param wizard The wizard to register
031     * @since 13930
032     */
033    public static void registerWizard(OverpassQueryWizard wizard) {
034        Objects.requireNonNull(wizard, "wizard");
035        wizards.add(wizard);
036    }
037
038    /**
039     * Gets all wizards that are currently registered.
040     * @return The list of wizards.
041     */
042    public static List<OverpassQueryWizard> getWizards() {
043        return Collections.unmodifiableList(wizards);
044    }
045
046    static {
047        // Register the default wizard
048        registerWizard(new OverpassQueryWizard() {
049            @Override
050            public void startWizard(OverpassWizardCallbacks callbacks) {
051                new OverpassQueryWizardDialog(callbacks).showDialog();
052            }
053
054            @Override
055            public Optional<String> getWizardTooltip() {
056                return Optional.of(tr("Build an Overpass query using the Overpass Turbo Query Wizard tool"));
057            }
058
059            @Override
060            public String getWizardName() {
061                return tr("Query Wizard");
062            }
063        });
064    }
065
066    private OverpassWizardRegistration() {
067        // hidden
068    }
069
070    /**
071     * Defines a query wizard that generates overpass queries.
072     * @author Michael Zangl
073     * @since 13930
074     */
075    public interface OverpassQueryWizard {
076        /**
077         * Get the name of the wizard
078         * @return The name
079         */
080        String getWizardName();
081
082        /**
083         * Get the tooltip text to display when hovering the wizard button.
084         * @return The tooltip text or an empty optional to display no tooltip.
085         */
086        Optional<String> getWizardTooltip();
087
088        /**
089         * Start the wizard.
090         * @param callbacks The callbacks to use to send back wizard results.
091         */
092        void startWizard(OverpassWizardCallbacks callbacks);
093    }
094
095    /**
096     * Wizard callbacks required by {@link OverpassQueryWizard#startWizard}
097     * @author Michael Zangl
098     * @since 13930
099     */
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}