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