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;
007import java.awt.event.ActionListener;
008
009import org.openstreetmap.josm.Main;
010import org.openstreetmap.josm.data.osm.DataSet;
011import org.openstreetmap.josm.gui.ExtendedDialog;
012
013/**
014 * The tagging presets search dialog (F3).
015 * @since 3388
016 */
017public final class TaggingPresetSearchDialog extends ExtendedDialog {
018
019    private static TaggingPresetSearchDialog instance;
020
021    private TaggingPresetSelector selector;
022
023    /**
024     * Returns the unique instance of {@code TaggingPresetSearchDialog}.
025     * @return the unique instance of {@code TaggingPresetSearchDialog}.
026     */
027    public static synchronized TaggingPresetSearchDialog getInstance() {
028        if (instance == null) {
029            instance = new TaggingPresetSearchDialog();
030        }
031        return instance;
032    }
033
034    private TaggingPresetSearchDialog() {
035        super(Main.parent, tr("Presets"), new String[] {tr("Select"), tr("Cancel")});
036        selector = new TaggingPresetSelector(true, true);
037        setContent(selector);
038        DataSet.addSelectionListener(selector);
039        selector.setDblClickListener(new ActionListener() {
040            @Override
041            public void actionPerformed(ActionEvent e) {
042                buttonAction(0, null);
043            }
044        });
045    }
046
047    @Override
048    public ExtendedDialog showDialog() {
049        selector.init();
050        super.showDialog();
051        selector.clearSelection();
052        return this;
053    }
054
055    @Override
056    protected void buttonAction(int buttonIndex, ActionEvent evt) {
057        super.buttonAction(buttonIndex, evt);
058        if (buttonIndex == 0) {
059            TaggingPreset preset = selector.getSelectedPreset();
060            if (preset != null) {
061                preset.actionPerformed(null);
062            }
063        }
064        selector.savePreferences();
065    }
066}