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