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.Main;
|
---|
14 | import org.openstreetmap.josm.actions.mapmode.DrawAction;
|
---|
15 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
16 | import org.openstreetmap.josm.command.SelectCommand;
|
---|
17 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
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;
|
---|
23 | import org.openstreetmap.josm.tools.Utils;
|
---|
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",
|
---|
41 | tr("Continues drawing a line that shares nodes with another line."),
|
---|
42 | Shortcut.registerShortcut("tools:followline", tr(
|
---|
43 | "Tool: {0}", tr("Follow")),
|
---|
44 | KeyEvent.VK_F, Shortcut.DIRECT), true);
|
---|
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
|
---|
67 |
|
---|
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;
|
---|
72 |
|
---|
73 | Node last = ((DrawAction) Main.map.mapMode).getCurrentBaseNode();
|
---|
74 | if (last == null)
|
---|
75 | return;
|
---|
76 | Way follower = selectedLines.iterator().next();
|
---|
77 | if (follower.isClosed()) /* Don't loop until OOM */
|
---|
78 | return;
|
---|
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
|
---|
87 |
|
---|
88 | Node newPoint = null;
|
---|
89 | for (final Way toFollow : Utils.filteredCollection(referrers, Way.class)) {
|
---|
90 | if (toFollow.equals(follower)) {
|
---|
91 | continue;
|
---|
92 | }
|
---|
93 | Set<Node> points = toFollow.getNeighbours(last);
|
---|
94 | points.remove(prev);
|
---|
95 | if (points.isEmpty()) // No candidate -> consider next way
|
---|
96 | continue;
|
---|
97 | if (points.size() > 1) // Ambiguous junction?
|
---|
98 | return;
|
---|
99 |
|
---|
100 | // points contains exactly one element
|
---|
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);
|
---|
112 | } else {
|
---|
113 | newFollower.addNode(newPoint);
|
---|
114 | }
|
---|
115 | Main.main.undoRedo.add(new SequenceCommand(tr("Follow line"),
|
---|
116 | new ChangeCommand(follower, newFollower),
|
---|
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 | );
|
---|
122 | // "viewport following" mode for tracing long features
|
---|
123 | // from aerial imagery or GPS tracks.
|
---|
124 | if (Main.map.mapView.viewportFollowing) {
|
---|
125 | Main.map.mapView.smoothScrollTo(newPoint.getEastNorth());
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|