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.KeyEvent;
008import java.util.HashSet;
009
010import org.openstreetmap.josm.actions.JosmAction;
011import org.openstreetmap.josm.data.osm.OsmData;
012import org.openstreetmap.josm.gui.ExtendedDialog;
013import org.openstreetmap.josm.gui.MainApplication;
014import org.openstreetmap.josm.tools.Shortcut;
015
016/**
017 * A dialog that allows to select a preset and then selects all matching OSM objects.
018 * @see org.openstreetmap.josm.gui.tagging.presets.TaggingPresetSearchDialog
019 */
020public final class TaggingPresetSearchPrimitiveDialog extends ExtendedDialog {
021
022    private static TaggingPresetSearchPrimitiveDialog instance;
023
024    private final TaggingPresetSelector selector;
025
026    /**
027     * An action executing {@link TaggingPresetSearchPrimitiveDialog}.
028     */
029    public static class Action extends JosmAction {
030
031        /**
032         * Constructs a new {@link TaggingPresetSearchPrimitiveDialog.Action}.
033         */
034        public Action() {
035            super(tr("Search for objects by preset..."), "dialogs/search", tr("Search for objects by their presets."),
036                    Shortcut.registerShortcut("preset:search-objects", tr("Presets: {0}", tr("Search for objects by preset...")),
037                    KeyEvent.VK_F3, Shortcut.SHIFT), false);
038            putValue("toolbar", "presets/search-objects");
039            MainApplication.getToolbar().register(this);
040        }
041
042        @Override
043        public void actionPerformed(ActionEvent e) {
044            if (MainApplication.getLayerManager().getActiveData() != null) {
045                TaggingPresetSearchPrimitiveDialog.getInstance().showDialog();
046            }
047        }
048
049        @Override
050        protected void updateEnabledState() {
051            setEnabled(getLayerManager().getActiveData() != null);
052        }
053    }
054
055    /**
056     * Returns the unique instance of {@code TaggingPresetSearchPrimitiveDialog}.
057     * @return the unique instance of {@code TaggingPresetSearchPrimitiveDialog}.
058     */
059    public static synchronized TaggingPresetSearchPrimitiveDialog getInstance() {
060        if (instance == null) {
061            instance = new TaggingPresetSearchPrimitiveDialog();
062        }
063        return instance;
064    }
065
066    TaggingPresetSearchPrimitiveDialog() {
067        super(MainApplication.getMainFrame(), tr("Search for objects by preset"), tr("Search"), tr("Cancel"));
068        setButtonIcons("dialogs/search", "cancel");
069        configureContextsensitiveHelp("/Action/TaggingPresetSearchPrimitive", true /* show help button */);
070        selector = new TaggingPresetSelector(false, false);
071        setContent(selector, false);
072        selector.setDblClickListener(e -> buttonAction(0, null));
073    }
074
075    @Override
076    public ExtendedDialog showDialog() {
077        selector.init();
078        super.showDialog();
079        selector.clearSelection();
080        return this;
081    }
082
083    @Override
084    protected void buttonAction(int buttonIndex, ActionEvent evt) {
085        super.buttonAction(buttonIndex, evt);
086        if (buttonIndex == 0) {
087            TaggingPreset preset = selector.getSelectedPresetAndUpdateClassification();
088            if (preset != null) {
089                OsmData<?, ?, ?, ?> ds = MainApplication.getLayerManager().getActiveData();
090                ds.setSelected(new HashSet<>(ds.getPrimitives(preset)));
091            }
092        }
093    }
094}