1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
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.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
19 | import org.openstreetmap.josm.command.Command;
|
---|
20 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
25 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
26 |
|
---|
27 | public class JoinNodeWayAction extends JosmAction {
|
---|
28 | public JoinNodeWayAction() {
|
---|
29 | super(tr("Join Node to Way"), "joinnodeway", tr("Join a node into the nearest way segments"),
|
---|
30 | Shortcut.registerShortcut("tools:joinnodeway", tr("Tool: {0}", tr("Join Node to Way")), KeyEvent.VK_J, Shortcut.GROUP_EDIT), true);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void actionPerformed(ActionEvent e) {
|
---|
34 | if (!isEnabled())
|
---|
35 | return;
|
---|
36 | Collection<OsmPrimitive> sel = getCurrentDataSet().getSelected();
|
---|
37 | if (sel.size() != 1 || !(sel.iterator().next() instanceof Node)) return;
|
---|
38 | Node node = (Node) sel.iterator().next();
|
---|
39 |
|
---|
40 | List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(
|
---|
41 | Main.map.mapView.getPoint(node));
|
---|
42 | HashMap<Way, List<Integer>> insertPoints = new HashMap<Way, List<Integer>>();
|
---|
43 | for (WaySegment ws : wss) {
|
---|
44 | List<Integer> is;
|
---|
45 | if (insertPoints.containsKey(ws.way)) {
|
---|
46 | is = insertPoints.get(ws.way);
|
---|
47 | } else {
|
---|
48 | is = new ArrayList<Integer>();
|
---|
49 | insertPoints.put(ws.way, is);
|
---|
50 | }
|
---|
51 |
|
---|
52 | if (ws.way.getNode(ws.lowerIndex) != node
|
---|
53 | && ws.way.getNode(ws.lowerIndex+1) != node) {
|
---|
54 | is.add(ws.lowerIndex);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
59 | for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
|
---|
60 | Way w = insertPoint.getKey();
|
---|
61 | List<Node> nodesToAdd = new ArrayList<Node>(w.getNodes());
|
---|
62 | List<Integer> is = insertPoint.getValue();
|
---|
63 | pruneSuccsAndReverse(is);
|
---|
64 | for (int i : is) {
|
---|
65 | nodesToAdd.add(i+1, node);
|
---|
66 | }
|
---|
67 | Way wnew = new Way(w);
|
---|
68 | wnew.setNodes(nodesToAdd);
|
---|
69 | cmds.add(new ChangeCommand(w, wnew));
|
---|
70 | }
|
---|
71 |
|
---|
72 | Main.main.undoRedo.add(new SequenceCommand(tr("Join Node and Line"), cmds));
|
---|
73 | Main.map.repaint();
|
---|
74 | }
|
---|
75 |
|
---|
76 | private static void pruneSuccsAndReverse(List<Integer> is) {
|
---|
77 | //if (is.size() < 2) return;
|
---|
78 |
|
---|
79 | HashSet<Integer> is2 = new HashSet<Integer>();
|
---|
80 | for (int i : is) {
|
---|
81 | if (!is2.contains(i - 1) && !is2.contains(i + 1)) {
|
---|
82 | is2.add(i);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | is.clear();
|
---|
86 | is.addAll(is2);
|
---|
87 | Collections.sort(is);
|
---|
88 | Collections.reverse(is);
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | protected void updateEnabledState() {
|
---|
93 | setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
|
---|
94 | }
|
---|
95 | }
|
---|