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 final 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.getLayerManager().getEditLayer() != null) { 048 TaggingPresetSearchPrimitiveDialog.getInstance().showDialog(); 049 } 050 } 051 052 @Override 053 protected void updateEnabledState() { 054 setEnabled(getLayerManager().getEditLayer() != null); 055 } 056 } 057 058 /** 059 * Returns the unique instance of {@code TaggingPresetSearchPrimitiveDialog}. 060 * @return the unique instance of {@code TaggingPresetSearchPrimitiveDialog}. 061 */ 062 public static synchronized TaggingPresetSearchPrimitiveDialog getInstance() { 063 if (instance == null) { 064 instance = new TaggingPresetSearchPrimitiveDialog(); 065 } 066 return instance; 067 } 068 069 TaggingPresetSearchPrimitiveDialog() { 070 super(Main.parent, tr("Presets"), new String[] {tr("Search"), tr("Cancel")}); 071 selector = new TaggingPresetSelector(false, false); 072 setContent(selector, false); 073 selector.setDblClickListener(new ActionListener() { 074 @Override 075 public void actionPerformed(ActionEvent e) { 076 buttonAction(0, null); 077 } 078 }); 079 } 080 081 @Override 082 public ExtendedDialog showDialog() { 083 selector.init(); 084 super.showDialog(); 085 selector.clearSelection(); 086 return this; 087 } 088 089 @Override 090 protected void buttonAction(int buttonIndex, ActionEvent evt) { 091 super.buttonAction(buttonIndex, evt); 092 if (buttonIndex == 0) { 093 TaggingPreset preset = selector.getSelectedPresetAndUpdateClassification(); 094 if (preset != null) { 095 final Set<OsmPrimitive> matching = new HashSet<>(Utils.filter(Main.getLayerManager().getEditDataSet().allPrimitives(), preset)); 096 Main.getLayerManager().getEditDataSet().setSelected(matching); 097 } 098 } 099 } 100}