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("Search for objects by preset"), KeyEvent.VK_F3, Shortcut.SHIFT), 037 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 selector = new TaggingPresetSelector(false, false); 069 setContent(selector, false); 070 selector.setDblClickListener(e -> buttonAction(0, null)); 071 } 072 073 @Override 074 public ExtendedDialog showDialog() { 075 selector.init(); 076 super.showDialog(); 077 selector.clearSelection(); 078 return this; 079 } 080 081 @Override 082 protected void buttonAction(int buttonIndex, ActionEvent evt) { 083 super.buttonAction(buttonIndex, evt); 084 if (buttonIndex == 0) { 085 TaggingPreset preset = selector.getSelectedPresetAndUpdateClassification(); 086 if (preset != null) { 087 OsmData<?, ?, ?, ?> ds = MainApplication.getLayerManager().getActiveData(); 088 ds.setSelected(new HashSet<>(ds.getPrimitives(preset))); 089 } 090 } 091 } 092}