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.ChangeCommand;
|
---|
15 | import org.openstreetmap.josm.command.SelectCommand;
|
---|
16 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
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.MainApplication;
|
---|
22 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
23 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
24 | import org.openstreetmap.josm.tools.Utils;
|
---|
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 |
|
---|
38 | /**
|
---|
39 | * Constructs a new {@code FollowLineAction}.
|
---|
40 | */
|
---|
41 | public FollowLineAction() {
|
---|
42 | super(
|
---|
43 | tr("Follow line"),
|
---|
44 | "followline",
|
---|
45 | tr("Continues drawing a line that shares nodes with another line."),
|
---|
46 | Shortcut.registerShortcut("tools:followline", tr(
|
---|
47 | "Tool: {0}", tr("Follow")),
|
---|
48 | KeyEvent.VK_F, Shortcut.DIRECT), true);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | protected void updateEnabledState() {
|
---|
53 | updateEnabledStateOnCurrentSelection();
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
58 | updateEnabledStateOnModifiableSelection(selection);
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void actionPerformed(ActionEvent evt) {
|
---|
63 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
64 | if (ds == null)
|
---|
65 | return;
|
---|
66 | MapFrame map = MainApplication.getMap();
|
---|
67 | if (!(map.mapMode instanceof DrawAction)) return; // We are not on draw mode
|
---|
68 |
|
---|
69 | Collection<Node> selectedPoints = ds.getSelectedNodes();
|
---|
70 | Collection<Way> selectedLines = ds.getSelectedWays();
|
---|
71 | if ((selectedPoints.size() > 1) || (selectedLines.size() != 1)) // Unsuitable selection
|
---|
72 | return;
|
---|
73 |
|
---|
74 | Node last = ((DrawAction) map.mapMode).getCurrentBaseNode();
|
---|
75 | if (last == null)
|
---|
76 | return;
|
---|
77 | Way follower = selectedLines.iterator().next();
|
---|
78 | if (follower.isClosed()) /* Don't loop until OOM */
|
---|
79 | return;
|
---|
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
|
---|
88 |
|
---|
89 | Node newPoint = null;
|
---|
90 | for (final Way toFollow : Utils.filteredCollection(referrers, Way.class)) {
|
---|
91 | if (toFollow.equals(follower)) {
|
---|
92 | continue;
|
---|
93 | }
|
---|
94 | Set<Node> points = toFollow.getNeighbours(last);
|
---|
95 | points.remove(prev);
|
---|
96 | if (points.isEmpty()) // No candidate -> consider next way
|
---|
97 | continue;
|
---|
98 | if (points.size() > 1) // Ambiguous junction?
|
---|
99 | return;
|
---|
100 |
|
---|
101 | // points contains exactly one element
|
---|
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);
|
---|
113 | } else {
|
---|
114 | newFollower.addNode(newPoint);
|
---|
115 | }
|
---|
116 | MainApplication.undoRedo.add(new SequenceCommand(tr("Follow line"),
|
---|
117 | new ChangeCommand(ds, follower, newFollower),
|
---|
118 | new SelectCommand(ds, newFollower.isClosed() // see #10028 - unselect last node when closing a way
|
---|
119 | ? Arrays.<OsmPrimitive>asList(follower)
|
---|
120 | : Arrays.<OsmPrimitive>asList(follower, newPoint)
|
---|
121 | ))
|
---|
122 | );
|
---|
123 | // "viewport following" mode for tracing long features
|
---|
124 | // from aerial imagery or GPS tracks.
|
---|
125 | if (DrawAction.VIEWPORT_FOLLOWING.get()) {
|
---|
126 | map.mapView.smoothScrollTo(newPoint.getEastNorth());
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|