1 | //License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
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.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.HashSet;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
20 | import org.openstreetmap.josm.command.Command;
|
---|
21 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
25 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
26 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
27 |
|
---|
28 | public class JoinNodeWayAction extends JosmAction {
|
---|
29 | public JoinNodeWayAction() {
|
---|
30 | super(tr("Join Node to Way"), "joinnodeway", tr("Include a node into the nearest way segments"),
|
---|
31 | Shortcut.registerShortcut("tools:joinnodeway", tr("Tool: {0}", tr("Join Node to Way")), KeyEvent.VK_J, Shortcut.DIRECT), true);
|
---|
32 | putValue("help", ht("/Action/JoinNodeWay"));
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public void actionPerformed(ActionEvent e) {
|
---|
37 | if (!isEnabled())
|
---|
38 | return;
|
---|
39 | Collection<Node> selectedNodes = getCurrentDataSet().getSelectedNodes();
|
---|
40 | // Allow multiple selected nodes too?
|
---|
41 | if (selectedNodes.size() != 1) return;
|
---|
42 |
|
---|
43 | Node node = selectedNodes.iterator().next();
|
---|
44 |
|
---|
45 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
46 |
|
---|
47 | // If the user has selected some ways, only join the node to these.
|
---|
48 | boolean restrictToSelectedWays =
|
---|
49 | !getCurrentDataSet().getSelectedWays().isEmpty();
|
---|
50 |
|
---|
51 | List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(
|
---|
52 | Main.map.mapView.getPoint(node), OsmPrimitive.isSelectablePredicate);
|
---|
53 | HashMap<Way, List<Integer>> insertPoints = new HashMap<Way, List<Integer>>();
|
---|
54 | for (WaySegment ws : wss) {
|
---|
55 | // Maybe cleaner to pass a "isSelected" predicate to getNearestWaySegements, but this is atm. less invasive.
|
---|
56 | if(restrictToSelectedWays && !ws.way.isSelected()) {
|
---|
57 | continue;
|
---|
58 | }
|
---|
59 |
|
---|
60 | List<Integer> is;
|
---|
61 | if (insertPoints.containsKey(ws.way)) {
|
---|
62 | is = insertPoints.get(ws.way);
|
---|
63 | } else {
|
---|
64 | is = new ArrayList<Integer>();
|
---|
65 | insertPoints.put(ws.way, is);
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (ws.way.getNode(ws.lowerIndex) != node
|
---|
69 | && ws.way.getNode(ws.lowerIndex+1) != node) {
|
---|
70 | is.add(ws.lowerIndex);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
|
---|
75 | List<Integer> is = insertPoint.getValue();
|
---|
76 | if (is.isEmpty()) {
|
---|
77 | continue;
|
---|
78 | }
|
---|
79 |
|
---|
80 | Way w = insertPoint.getKey();
|
---|
81 | List<Node> nodesToAdd = w.getNodes();
|
---|
82 | pruneSuccsAndReverse(is);
|
---|
83 | for (int i : is) {
|
---|
84 | nodesToAdd.add(i+1, node);
|
---|
85 | }
|
---|
86 | Way wnew = new Way(w);
|
---|
87 | wnew.setNodes(nodesToAdd);
|
---|
88 | cmds.add(new ChangeCommand(w, wnew));
|
---|
89 | }
|
---|
90 | if (cmds.isEmpty()) return;
|
---|
91 | Main.main.undoRedo.add(new SequenceCommand(tr("Join Node and Line"), cmds));
|
---|
92 | Main.map.repaint();
|
---|
93 | }
|
---|
94 |
|
---|
95 | private static void pruneSuccsAndReverse(List<Integer> is) {
|
---|
96 | HashSet<Integer> is2 = new HashSet<Integer>();
|
---|
97 | for (int i : is) {
|
---|
98 | if (!is2.contains(i - 1) && !is2.contains(i + 1)) {
|
---|
99 | is2.add(i);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | is.clear();
|
---|
103 | is.addAll(is2);
|
---|
104 | Collections.sort(is);
|
---|
105 | Collections.reverse(is);
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | protected void updateEnabledState() {
|
---|
110 | if (getCurrentDataSet() == null) {
|
---|
111 | setEnabled(false);
|
---|
112 | } else {
|
---|
113 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
119 | setEnabled(selection != null && !selection.isEmpty());
|
---|
120 | }
|
---|
121 | }
|
---|