| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 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.Arrays;
|
|---|
| 9 | import java.util.Collection;
|
|---|
| 10 | import java.util.List;
|
|---|
| 11 | import java.util.Set;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.actions.mapmode.DrawAction;
|
|---|
| 14 | import org.openstreetmap.josm.command.ChangeNodesCommand;
|
|---|
| 15 | import org.openstreetmap.josm.command.SelectCommand;
|
|---|
| 16 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 17 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 19 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 21 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 22 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 23 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 24 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 25 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Follow line action - Makes easier to draw a line that shares points with another line
|
|---|
| 29 | *
|
|---|
| 30 | * Aimed at those who want to draw two or more lines related with
|
|---|
| 31 | * each other, but carry different information (i.e. a river acts as boundary at
|
|---|
| 32 | * some part of its course. It preferable to have a separated boundary line than to
|
|---|
| 33 | * mix totally different kind of features in one single way).
|
|---|
| 34 | *
|
|---|
| 35 | * @author Germán Márquez Mejía
|
|---|
| 36 | */
|
|---|
| 37 | public class FollowLineAction extends JosmAction {
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Constructs a new {@code FollowLineAction}.
|
|---|
| 41 | */
|
|---|
| 42 | public FollowLineAction() {
|
|---|
| 43 | super(
|
|---|
| 44 | tr("Follow line"),
|
|---|
| 45 | "followline",
|
|---|
| 46 | tr("Continues drawing a line that shares nodes with another line."),
|
|---|
| 47 | Shortcut.registerShortcut("tools:followline", tr(
|
|---|
| 48 | "Tools: {0}", tr("Follow")),
|
|---|
| 49 | KeyEvent.VK_F, Shortcut.DIRECT), true);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | @Override
|
|---|
| 53 | protected void updateEnabledState() {
|
|---|
| 54 | updateEnabledStateOnCurrentSelection();
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | @Override
|
|---|
| 58 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 59 | updateEnabledStateOnModifiableSelection(selection);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | @Override
|
|---|
| 63 | public void actionPerformed(ActionEvent evt) {
|
|---|
| 64 | DataSet ds = getLayerManager().getEditDataSet();
|
|---|
| 65 | if (ds == null)
|
|---|
| 66 | return;
|
|---|
| 67 | MapFrame map = MainApplication.getMap();
|
|---|
| 68 | if (!(map.mapMode instanceof DrawAction)) return; // We are not on draw mode
|
|---|
| 69 |
|
|---|
| 70 | Collection<Node> selectedPoints = ds.getSelectedNodes();
|
|---|
| 71 | Collection<Way> selectedLines = ds.getSelectedWays();
|
|---|
| 72 | if ((selectedPoints.size() > 1) || (selectedLines.size() != 1)) // Unsuitable selection
|
|---|
| 73 | return;
|
|---|
| 74 |
|
|---|
| 75 | Node last = ((DrawAction) map.mapMode).getCurrentBaseNode();
|
|---|
| 76 | if (last == null)
|
|---|
| 77 | return;
|
|---|
| 78 | Way follower = selectedLines.iterator().next();
|
|---|
| 79 | if (follower.isClosed()) /* Don't loop until OOM */
|
|---|
| 80 | return;
|
|---|
| 81 | Node prev = follower.getNode(1);
|
|---|
| 82 | boolean reversed = true;
|
|---|
| 83 | if (follower.lastNode().equals(last)) {
|
|---|
| 84 | prev = follower.getNode(follower.getNodesCount() - 2);
|
|---|
| 85 | reversed = false;
|
|---|
| 86 | } else if (!follower.firstNode().equals(last)) {
|
|---|
| 87 | return; // see #23442
|
|---|
| 88 | }
|
|---|
| 89 | List<OsmPrimitive> referrers = last.getReferrers();
|
|---|
| 90 | if (referrers.size() < 2) return; // There's nothing to follow
|
|---|
| 91 |
|
|---|
| 92 | Node newPoint = null;
|
|---|
| 93 | for (final Way toFollow : Utils.filteredCollection(referrers, Way.class)) {
|
|---|
| 94 | if (toFollow.equals(follower)) {
|
|---|
| 95 | continue;
|
|---|
| 96 | }
|
|---|
| 97 | Set<Node> points = toFollow.getNeighbours(last);
|
|---|
| 98 | points.remove(prev);
|
|---|
| 99 | if (points.isEmpty()) // No candidate -> consider next way
|
|---|
| 100 | continue;
|
|---|
| 101 | if (points.size() > 1) // Ambiguous junction?
|
|---|
| 102 | return;
|
|---|
| 103 |
|
|---|
| 104 | // points contains exactly one element
|
|---|
| 105 | Node newPointCandidate = points.iterator().next();
|
|---|
| 106 |
|
|---|
| 107 | if ((newPoint != null) && (newPoint != newPointCandidate))
|
|---|
| 108 | return; // Ambiguous junction, force to select next
|
|---|
| 109 |
|
|---|
| 110 | newPoint = newPointCandidate;
|
|---|
| 111 | }
|
|---|
| 112 | if (newPoint != null) {
|
|---|
| 113 | List<Node> modNodes = follower.getNodes();
|
|---|
| 114 | if (reversed) {
|
|---|
| 115 | modNodes.add(0, newPoint);
|
|---|
| 116 | } else {
|
|---|
| 117 | modNodes.add(newPoint);
|
|---|
| 118 | }
|
|---|
| 119 | boolean isClosed = modNodes.size() >= 3 && modNodes.get(modNodes.size()-1) == modNodes.get(0);
|
|---|
| 120 | UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Follow line"),
|
|---|
| 121 | new ChangeNodesCommand(ds, follower, modNodes),
|
|---|
| 122 | new SelectCommand(ds, isClosed // see #10028 - unselect last node when closing a way
|
|---|
| 123 | ? Arrays.<OsmPrimitive>asList(follower)
|
|---|
| 124 | : Arrays.<OsmPrimitive>asList(follower, newPoint)
|
|---|
| 125 | ))
|
|---|
| 126 | );
|
|---|
| 127 | // "viewport following" mode for tracing long features
|
|---|
| 128 | // from aerial imagery or GPS tracks.
|
|---|
| 129 | if (DrawAction.VIEWPORT_FOLLOWING.get()) {
|
|---|
| 130 | map.mapView.smoothScrollTo(newPoint.getEastNorth());
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|