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.Iterator;
|
---|
10 | import java.util.List;
|
---|
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.GROUP_EDIT), 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 | Node prev = follower.getNode(1);
|
---|
75 | boolean reversed = true;
|
---|
76 | if (follower.lastNode().equals(last)) {
|
---|
77 | prev = follower.getNode(follower.getNodesCount() - 2);
|
---|
78 | reversed = false;
|
---|
79 | }
|
---|
80 | List<OsmPrimitive> referrers = last.getReferrers();
|
---|
81 | if (referrers.size() < 2) return; // There's nothing to follow
|
---|
82 |
|
---|
83 | Iterator<OsmPrimitive> i = referrers.iterator();
|
---|
84 | while (i.hasNext()) {
|
---|
85 | OsmPrimitive referrer = i.next();
|
---|
86 | if (!referrer.getType().equals(OsmPrimitiveType.WAY)) { // Can't follow points or relations
|
---|
87 | continue;
|
---|
88 | }
|
---|
89 | Way toFollow = (Way) referrer;
|
---|
90 | if (toFollow.equals(follower)) {
|
---|
91 | continue;
|
---|
92 | }
|
---|
93 | List<Node> points = toFollow.getNodes();
|
---|
94 | int indexOfLast = points.indexOf(last);
|
---|
95 | int indexOfPrev = points.indexOf(prev);
|
---|
96 | if ((indexOfLast == -1) || (indexOfPrev == -1)) { // Both points must belong to both lines
|
---|
97 | continue;
|
---|
98 | }
|
---|
99 | if (Math.abs(indexOfPrev - indexOfLast) != 1) { // ...and be consecutive
|
---|
100 | continue;
|
---|
101 | }
|
---|
102 | Node newPoint = null;
|
---|
103 | if (indexOfPrev == (indexOfLast - 1)) {
|
---|
104 | if ((indexOfLast + 1) < points.size()) {
|
---|
105 | newPoint = points.get(indexOfLast + 1);
|
---|
106 | }
|
---|
107 | } else {
|
---|
108 | if ((indexOfLast - 1) >= 0) {
|
---|
109 | newPoint = points.get(indexOfLast - 1);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | if (newPoint != null) {
|
---|
113 | Way newFollower = new Way(follower);
|
---|
114 | if (reversed) {
|
---|
115 | newFollower.addNode(0, newPoint);
|
---|
116 | } else {
|
---|
117 | newFollower.addNode(newPoint);
|
---|
118 | }
|
---|
119 | Main.main.undoRedo.add(new ChangeCommand(follower, newFollower));
|
---|
120 | osmLayer.data.clearSelection();
|
---|
121 | osmLayer.data.addSelected(newFollower);
|
---|
122 | osmLayer.data.addSelected(newPoint);
|
---|
123 | // "viewport following" mode for tracing long features
|
---|
124 | // from aerial imagery or GPS tracks.
|
---|
125 | if (Main.map.mapView.viewportFollowing) {
|
---|
126 | Main.map.mapView.smoothScrollTo(newPoint.getEastNorth());
|
---|
127 | };
|
---|
128 | return;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|