1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.utilsplugin2.selection;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.LinkedHashSet;
|
---|
12 | import java.util.Set;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.Way;
|
---|
19 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Extends current selection
|
---|
23 | */
|
---|
24 | public class AdjacentNodesAction extends JosmAction {
|
---|
25 |
|
---|
26 | public static final boolean treeMode = false;
|
---|
27 |
|
---|
28 | public AdjacentNodesAction() {
|
---|
29 | super(tr("Adjacent nodes"), "adjnodes", tr("Select adjacent nodes"),
|
---|
30 | Shortcut.registerShortcut("tools:adjnodes", tr("Selection: {0}", tr("Adjacent nodes")),
|
---|
31 | KeyEvent.VK_E, Shortcut.DIRECT), true);
|
---|
32 | putValue("help", ht("/Action/AdjacentNodes"));
|
---|
33 | }
|
---|
34 |
|
---|
35 | private transient Set<Way> activeWays = new HashSet<>();
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void actionPerformed(ActionEvent e) {
|
---|
39 | DataSet ds = getLayerManager().getActiveDataSet();
|
---|
40 | Collection<Node> selectedNodes = ds.getSelectedNodes();
|
---|
41 | Set<Way> selectedWays = new LinkedHashSet<>(ds.getSelectedWays());
|
---|
42 |
|
---|
43 | // if no nodes and no ways are selected, do nothing
|
---|
44 | if (selectedNodes.isEmpty() && selectedWays.isEmpty()) return;
|
---|
45 |
|
---|
46 | if (selectedWays.isEmpty()) {
|
---|
47 | // if one node is selected, use ways connected to it to extend selection
|
---|
48 | // activeWays are remembered for next extend action (!!!)
|
---|
49 |
|
---|
50 | // FIXME: some strange behaviour is possible if user deletes some of these ways
|
---|
51 | // how to clear activeWays during such user actions? Do not know
|
---|
52 | if (selectedNodes.size() == 1) {
|
---|
53 | activeWays.clear();
|
---|
54 | }
|
---|
55 | } else {
|
---|
56 | // use only ways that were selected for adding nodes
|
---|
57 | activeWays = selectedWays;
|
---|
58 | }
|
---|
59 |
|
---|
60 | // selecting nodes of selected ways
|
---|
61 | if (selectedNodes.isEmpty()) {
|
---|
62 | Set<Node> newNodes = new LinkedHashSet<>();
|
---|
63 | NodeWayUtils.addNodesConnectedToWays(selectedWays, newNodes);
|
---|
64 | activeWays.clear();
|
---|
65 | ds.setSelected(newNodes);
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (activeWays.isEmpty()) {
|
---|
70 | NodeWayUtils.addWaysConnectedToNodes(selectedNodes, activeWays);
|
---|
71 | }
|
---|
72 |
|
---|
73 | Set<Node> newNodes = new LinkedHashSet<>();
|
---|
74 | for (Node node: selectedNodes) {
|
---|
75 | for (Way w: activeWays) {
|
---|
76 | NodeWayUtils.addNeighbours(w, node, newNodes);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | // select only newly found nodes
|
---|
81 | newNodes.removeAll(selectedNodes);
|
---|
82 |
|
---|
83 | // enable branching on next call of this function
|
---|
84 | // if no new nodes were found, next search will include all touched ways
|
---|
85 | if (newNodes.isEmpty()) {
|
---|
86 | activeWays.clear();
|
---|
87 | }
|
---|
88 |
|
---|
89 | ds.addSelected(newNodes);
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | protected void updateEnabledState() {
|
---|
94 | updateEnabledStateOnCurrentSelection();
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
99 | if (selection == null) {
|
---|
100 | setEnabled(false);
|
---|
101 | return;
|
---|
102 | }
|
---|
103 | setEnabled(!selection.isEmpty());
|
---|
104 | }
|
---|
105 | }
|
---|