[466] | 1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[3713] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[466] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
[582] | 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.awt.event.KeyEvent;
|
---|
[466] | 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;
|
---|
[582] | 14 | import java.util.LinkedList;
|
---|
| 15 | import java.util.List;
|
---|
[466] | 16 | import java.util.Map;
|
---|
| 17 |
|
---|
| 18 | import org.openstreetmap.josm.Main;
|
---|
[582] | 19 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
[466] | 20 | import org.openstreetmap.josm.command.Command;
|
---|
| 21 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
[582] | 22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
[466] | 23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[582] | 24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 25 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
[1084] | 26 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[466] | 27 |
|
---|
| 28 | public class JoinNodeWayAction extends JosmAction {
|
---|
[1169] | 29 | public JoinNodeWayAction() {
|
---|
[4788] | 30 | super(tr("Join Node to Way"), "joinnodeway", tr("Include a node into the nearest way segments"),
|
---|
[4982] | 31 | Shortcut.registerShortcut("tools:joinnodeway", tr("Tool: {0}", tr("Join Node to Way")), KeyEvent.VK_J, Shortcut.DIRECT), true);
|
---|
[2323] | 32 | putValue("help", ht("/Action/JoinNodeWay"));
|
---|
[1169] | 33 | }
|
---|
[466] | 34 |
|
---|
[6084] | 35 | @Override
|
---|
[1169] | 36 | public void actionPerformed(ActionEvent e) {
|
---|
[1820] | 37 | if (!isEnabled())
|
---|
| 38 | return;
|
---|
[4557] | 39 | Collection<Node> selectedNodes = getCurrentDataSet().getSelectedNodes();
|
---|
| 40 | // Allow multiple selected nodes too?
|
---|
| 41 | if (selectedNodes.size() != 1) return;
|
---|
[466] | 42 |
|
---|
[4557] | 43 | Node node = selectedNodes.iterator().next();
|
---|
| 44 |
|
---|
[3643] | 45 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
[466] | 46 |
|
---|
[4557] | 47 | // If the user has selected some ways, only join the node to these.
|
---|
| 48 | boolean restrictToSelectedWays =
|
---|
[6093] | 49 | !getCurrentDataSet().getSelectedWays().isEmpty();
|
---|
[3643] | 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) {
|
---|
[4557] | 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 |
|
---|
[3643] | 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 | }
|
---|
[1169] | 72 | }
|
---|
[466] | 73 |
|
---|
[3643] | 74 | for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
|
---|
| 75 | List<Integer> is = insertPoint.getValue();
|
---|
[6093] | 76 | if (is.isEmpty()) {
|
---|
[3643] | 77 | continue;
|
---|
[3713] | 78 | }
|
---|
[2431] | 79 |
|
---|
[3643] | 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));
|
---|
[1814] | 89 | }
|
---|
[6093] | 90 | if (cmds.isEmpty()) return;
|
---|
[4557] | 91 | Main.main.undoRedo.add(new SequenceCommand(tr("Join Node and Line"), cmds));
|
---|
| 92 | Main.map.repaint();
|
---|
[1169] | 93 | }
|
---|
[466] | 94 |
|
---|
[1169] | 95 | private static void pruneSuccsAndReverse(List<Integer> is) {
|
---|
| 96 | //if (is.size() < 2) return;
|
---|
[466] | 97 |
|
---|
[1169] | 98 | HashSet<Integer> is2 = new HashSet<Integer>();
|
---|
| 99 | for (int i : is) {
|
---|
| 100 | if (!is2.contains(i - 1) && !is2.contains(i + 1)) {
|
---|
| 101 | is2.add(i);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | is.clear();
|
---|
| 105 | is.addAll(is2);
|
---|
| 106 | Collections.sort(is);
|
---|
| 107 | Collections.reverse(is);
|
---|
| 108 | }
|
---|
[1820] | 109 |
|
---|
| 110 | @Override
|
---|
| 111 | protected void updateEnabledState() {
|
---|
[2256] | 112 | if (getCurrentDataSet() == null) {
|
---|
| 113 | setEnabled(false);
|
---|
| 114 | } else {
|
---|
| 115 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
| 116 | }
|
---|
[1820] | 117 | }
|
---|
[2256] | 118 |
|
---|
| 119 | @Override
|
---|
| 120 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 121 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 122 | }
|
---|
[466] | 123 | }
|
---|