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