source: subversion/applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/selection/AdjacentNodesAction.java

Last change on this file was 34812, checked in by gerdp, 3 months ago

see #17187:

  • Replace deprecated methods
  • Remove dead code
  • Make Shift+Z (Select last modified nodes) work again + Repated Shift+Z cycles through the commands available in the undo tree + Ignore changes in other layers
  • Make Alt+Shift+Z (Select last modified Ways) work again (same logic as with Shift+Z)
  • fix some javadoc errors


File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins.utilsplugin2.selection;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10import java.util.HashSet;
11import java.util.LinkedHashSet;
12import java.util.Set;
13
14import org.openstreetmap.josm.actions.JosmAction;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.data.osm.Node;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.Way;
19import org.openstreetmap.josm.tools.Shortcut;
20
21/**
22 * Extends current selection
23 */
24public 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("Tool: {0}", "Adjacent nodes"),
31                        KeyEvent.VK_E, Shortcut.DIRECT), true);
32        putValue("help", ht("/Action/AdjacentNodes"));
33    }
34
35    private Set<Way> activeWays = new HashSet<>();
36
37    @Override
38    public void actionPerformed(ActionEvent e) {
39        DataSet ds = getLayerManager().getEditDataSet();
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            HashSet<Node> newNodes = new HashSet<>();
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 HashSet<>();
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}
Note: See TracBrowser for help on using the repository browser.