| 1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 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 | public void actionPerformed(ActionEvent e) { |
|---|
| 36 | if (!isEnabled()) |
|---|
| 37 | return; |
|---|
| 38 | Collection<Node> selectedNodes = getCurrentDataSet().getSelectedNodes(); |
|---|
| 39 | // Allow multiple selected nodes too? |
|---|
| 40 | if (selectedNodes.size() != 1) return; |
|---|
| 41 | |
|---|
| 42 | Node node = selectedNodes.iterator().next(); |
|---|
| 43 | |
|---|
| 44 | Collection<Command> cmds = new LinkedList<Command>(); |
|---|
| 45 | |
|---|
| 46 | // If the user has selected some ways, only join the node to these. |
|---|
| 47 | boolean restrictToSelectedWays = |
|---|
| 48 | getCurrentDataSet().getSelectedWays().size() > 0; |
|---|
| 49 | |
|---|
| 50 | List<WaySegment> wss = Main.map.mapView.getNearestWaySegments( |
|---|
| 51 | Main.map.mapView.getPoint(node), OsmPrimitive.isSelectablePredicate); |
|---|
| 52 | HashMap<Way, List<Integer>> insertPoints = new HashMap<Way, List<Integer>>(); |
|---|
| 53 | for (WaySegment ws : wss) { |
|---|
| 54 | // Maybe cleaner to pass a "isSelected" predicate to getNearestWaySegements, but this is atm. less invasive. |
|---|
| 55 | if(restrictToSelectedWays && !ws.way.isSelected()) { |
|---|
| 56 | continue; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | List<Integer> is; |
|---|
| 60 | if (insertPoints.containsKey(ws.way)) { |
|---|
| 61 | is = insertPoints.get(ws.way); |
|---|
| 62 | } else { |
|---|
| 63 | is = new ArrayList<Integer>(); |
|---|
| 64 | insertPoints.put(ws.way, is); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | if (ws.way.getNode(ws.lowerIndex) != node |
|---|
| 68 | && ws.way.getNode(ws.lowerIndex+1) != node) { |
|---|
| 69 | is.add(ws.lowerIndex); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) { |
|---|
| 74 | List<Integer> is = insertPoint.getValue(); |
|---|
| 75 | if (is.size() == 0) { |
|---|
| 76 | continue; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | Way w = insertPoint.getKey(); |
|---|
| 80 | List<Node> nodesToAdd = w.getNodes(); |
|---|
| 81 | pruneSuccsAndReverse(is); |
|---|
| 82 | for (int i : is) { |
|---|
| 83 | nodesToAdd.add(i+1, node); |
|---|
| 84 | } |
|---|
| 85 | Way wnew = new Way(w); |
|---|
| 86 | wnew.setNodes(nodesToAdd); |
|---|
| 87 | cmds.add(new ChangeCommand(w, wnew)); |
|---|
| 88 | } |
|---|
| 89 | if (cmds.size() == 0) return; |
|---|
| 90 | Main.main.undoRedo.add(new SequenceCommand(tr("Join Node and Line"), cmds)); |
|---|
| 91 | Main.map.repaint(); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | private static void pruneSuccsAndReverse(List<Integer> is) { |
|---|
| 95 | //if (is.size() < 2) return; |
|---|
| 96 | |
|---|
| 97 | HashSet<Integer> is2 = new HashSet<Integer>(); |
|---|
| 98 | for (int i : is) { |
|---|
| 99 | if (!is2.contains(i - 1) && !is2.contains(i + 1)) { |
|---|
| 100 | is2.add(i); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | is.clear(); |
|---|
| 104 | is.addAll(is2); |
|---|
| 105 | Collections.sort(is); |
|---|
| 106 | Collections.reverse(is); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | @Override |
|---|
| 110 | protected void updateEnabledState() { |
|---|
| 111 | if (getCurrentDataSet() == null) { |
|---|
| 112 | setEnabled(false); |
|---|
| 113 | } else { |
|---|
| 114 | updateEnabledState(getCurrentDataSet().getSelected()); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | @Override |
|---|
| 119 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { |
|---|
| 120 | setEnabled(selection != null && !selection.isEmpty()); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|