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