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.Main;
009import org.openstreetmap.josm.data.osm.DataSet;
010import org.openstreetmap.josm.gui.ExtendedDialog;
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(Main.parent, tr("Presets"), new String[] {tr("Select"), tr("Cancel")});
035        selector = new TaggingPresetSelector(true, true);
036        setContent(selector, false);
037        DataSet.addSelectionListener(selector);
038        selector.setDblClickListener(e -> buttonAction(0, null));
039    }
040
041    @Override
042    public ExtendedDialog showDialog() {
043        selector.init();
044        super.showDialog();
045        selector.clearSelection();
046        return this;
047    }
048
049    @Override
050    protected void buttonAction(int buttonIndex, ActionEvent evt) {
051        super.buttonAction(buttonIndex, evt);
052        if (buttonIndex == 0) {
053            TaggingPreset preset = selector.getSelectedPresetAndUpdateClassification();
054            if (preset != null) {
055                preset.actionPerformed(null);
056            }
057        }
058        selector.savePreferences();
059    }
060}