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