1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.presets;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.HashSet;
|
---|
9 | import java.util.Set;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
15 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * A dialog that allows to select a preset and then selects all matching OSM objects.
|
---|
19 | * @see org.openstreetmap.josm.gui.tagging.presets.TaggingPresetSearchDialog
|
---|
20 | */
|
---|
21 | public final class TaggingPresetSearchPrimitiveDialog extends ExtendedDialog {
|
---|
22 |
|
---|
23 | private static TaggingPresetSearchPrimitiveDialog instance;
|
---|
24 |
|
---|
25 | private final TaggingPresetSelector selector;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * An action executing {@link TaggingPresetSearchPrimitiveDialog}.
|
---|
29 | */
|
---|
30 | public static class Action extends JosmAction {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new {@link TaggingPresetSearchPrimitiveDialog.Action}.
|
---|
34 | */
|
---|
35 | public Action() {
|
---|
36 | super(tr("Search for objects by preset..."), "dialogs/search", tr("Show preset search dialog"),
|
---|
37 | Shortcut.registerShortcut("preset:search-objects", tr("Search for objects by preset"), KeyEvent.VK_F3, Shortcut.SHIFT),
|
---|
38 | false);
|
---|
39 | putValue("toolbar", "presets/search-objects");
|
---|
40 | Main.toolbar.register(this);
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public void actionPerformed(ActionEvent e) {
|
---|
45 | if (Main.getLayerManager().getEditLayer() != null) {
|
---|
46 | TaggingPresetSearchPrimitiveDialog.getInstance().showDialog();
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected void updateEnabledState() {
|
---|
52 | setEnabled(getLayerManager().getEditLayer() != null);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Returns the unique instance of {@code TaggingPresetSearchPrimitiveDialog}.
|
---|
58 | * @return the unique instance of {@code TaggingPresetSearchPrimitiveDialog}.
|
---|
59 | */
|
---|
60 | public static synchronized TaggingPresetSearchPrimitiveDialog getInstance() {
|
---|
61 | if (instance == null) {
|
---|
62 | instance = new TaggingPresetSearchPrimitiveDialog();
|
---|
63 | }
|
---|
64 | return instance;
|
---|
65 | }
|
---|
66 |
|
---|
67 | TaggingPresetSearchPrimitiveDialog() {
|
---|
68 | super(Main.parent, tr("Presets"), tr("Search"), tr("Cancel"));
|
---|
69 | selector = new TaggingPresetSelector(false, false);
|
---|
70 | setContent(selector, false);
|
---|
71 | selector.setDblClickListener(e -> buttonAction(0, null));
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public ExtendedDialog showDialog() {
|
---|
76 | selector.init();
|
---|
77 | super.showDialog();
|
---|
78 | selector.clearSelection();
|
---|
79 | return this;
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | protected void buttonAction(int buttonIndex, ActionEvent evt) {
|
---|
84 | super.buttonAction(buttonIndex, evt);
|
---|
85 | if (buttonIndex == 0) {
|
---|
86 | TaggingPreset preset = selector.getSelectedPresetAndUpdateClassification();
|
---|
87 | if (preset != null) {
|
---|
88 | final Set<OsmPrimitive> matching = new HashSet<>(Main.getLayerManager().getEditDataSet().getPrimitives(preset));
|
---|
89 | Main.getLayerManager().getEditDataSet().setSelected(matching);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|