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