001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging.presets;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007
008import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
009import org.openstreetmap.josm.gui.ExtendedDialog;
010import org.openstreetmap.josm.gui.MainApplication;
011
012/**
013 * The tagging presets search dialog (F3).
014 * @since 3388
015 */
016public final class TaggingPresetSearchDialog extends ExtendedDialog {
017
018    private static TaggingPresetSearchDialog instance;
019
020    private final TaggingPresetSelector selector;
021
022    /**
023     * Returns the unique instance of {@code TaggingPresetSearchDialog}.
024     * @return the unique instance of {@code TaggingPresetSearchDialog}.
025     */
026    public static synchronized TaggingPresetSearchDialog getInstance() {
027        if (instance == null) {
028            instance = new TaggingPresetSearchDialog();
029        }
030        return instance;
031    }
032
033    private TaggingPresetSearchDialog() {
034        super(MainApplication.getMainFrame(), tr("Search presets"), tr("Select"), tr("Cancel"));
035        setButtonIcons("dialogs/search", "cancel");
036        configureContextsensitiveHelp("/Action/TaggingPresetSearch", true /* show help button */);
037        selector = new TaggingPresetSelector(true, true);
038        setContent(selector, false);
039        SelectionEventManager.getInstance().addSelectionListener(selector);
040        selector.setDblClickListener(e -> buttonAction(0, null));
041    }
042
043    @Override
044    public ExtendedDialog showDialog() {
045        selector.init();
046        super.showDialog();
047        selector.clearSelection();
048        return this;
049    }
050
051    @Override
052    protected void buttonAction(int buttonIndex, ActionEvent evt) {
053        super.buttonAction(buttonIndex, evt);
054        if (buttonIndex == 0) {
055            TaggingPreset preset = selector.getSelectedPresetAndUpdateClassification();
056            if (preset != null) {
057                preset.actionPerformed(null);
058            }
059        }
060        selector.savePreferences();
061    }
062}