1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Cursor;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.awt.event.MouseEvent;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.LinkedList;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.actions.GroupAction;
|
---|
19 | import org.openstreetmap.josm.command.AddCommand;
|
---|
20 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
21 | import org.openstreetmap.josm.command.Command;
|
---|
22 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
23 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.Way;
|
---|
27 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
28 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
29 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * This mode adds a new node to the dataset. The user clicks on a place to add
|
---|
33 | * and there is it. Nothing more, nothing less.
|
---|
34 | *
|
---|
35 | * FIXME: "nothing more, nothing less" is a bit out-of-date
|
---|
36 | *
|
---|
37 | * Newly created nodes are selected. Shift modifier does not cancel the old
|
---|
38 | * selection as usual.
|
---|
39 | *
|
---|
40 | * @author imi
|
---|
41 | *
|
---|
42 | */
|
---|
43 | public class AddNodeAction extends MapMode {
|
---|
44 |
|
---|
45 | enum Mode {node, nodeway, autonode}
|
---|
46 | private final Mode mode;
|
---|
47 |
|
---|
48 | public static class AddNodeGroup extends GroupAction {
|
---|
49 | public AddNodeGroup(MapFrame mf) {
|
---|
50 | super(KeyEvent.VK_N,0);
|
---|
51 | putValue("help", "Action/AddNode");
|
---|
52 | actions.add(new AddNodeAction(mf,tr("Add node"), Mode.node, tr("Add a new node to the map")));
|
---|
53 | actions.add(new AddNodeAction(mf, tr("Add node into way"), Mode.nodeway,tr( "Add a node into an existing way")));
|
---|
54 | actions.add(new AddNodeAction(mf, tr("Add node and connect"), Mode.autonode,tr( "Add a node and connect it to the selected node (with CTRL: add node into way; with SHIFT: re-use existing node)")));
|
---|
55 | setCurrent(0);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public AddNodeAction(MapFrame mapFrame, String name, Mode mode, String desc) {
|
---|
60 | super(name, "node/"+mode, desc, mapFrame, getCursor());
|
---|
61 | this.mode = mode;
|
---|
62 | putValue("help", "Action/AddNode/"+Character.toUpperCase(mode.toString().charAt(0))+mode.toString().substring(1));
|
---|
63 | }
|
---|
64 |
|
---|
65 | private static Cursor getCursor() {
|
---|
66 | try {
|
---|
67 | return ImageProvider.getCursor("crosshair", null);
|
---|
68 | } catch (Exception e) {
|
---|
69 | }
|
---|
70 | return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override public void enterMode() {
|
---|
74 | super.enterMode();
|
---|
75 | Main.map.mapView.addMouseListener(this);
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override public void exitMode() {
|
---|
79 | super.exitMode();
|
---|
80 | Main.map.mapView.removeMouseListener(this);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * If user clicked with the left button, add a node at the current mouse
|
---|
85 | * position.
|
---|
86 | *
|
---|
87 | * If in nodeway mode, insert the node into the way.
|
---|
88 | */
|
---|
89 | @Override public void mouseClicked(MouseEvent e) {
|
---|
90 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
91 | return;
|
---|
92 |
|
---|
93 | Node n = new Node(Main.map.mapView.getLatLon(e.getX(), e.getY()));
|
---|
94 | if (n.coor.isOutSideWorld()) {
|
---|
95 | JOptionPane.showMessageDialog(Main.parent,tr("Cannot add a node outside of the world."));
|
---|
96 | return;
|
---|
97 | }
|
---|
98 |
|
---|
99 | Command c = new AddCommand(n);
|
---|
100 | if (mode == Mode.nodeway) {
|
---|
101 | WaySegment ws = Main.map.mapView.getNearestWaySegment(e.getPoint());
|
---|
102 | if (ws == null)
|
---|
103 | return;
|
---|
104 |
|
---|
105 | // see if another segment is also near
|
---|
106 | WaySegment other = Main.map.mapView.getNearestWaySegment(e.getPoint(),
|
---|
107 | Collections.singleton(ws));
|
---|
108 |
|
---|
109 | Node n1 = ws.way.nodes.get(ws.lowerIndex),
|
---|
110 | n2 = ws.way.nodes.get(ws.lowerIndex + 1);
|
---|
111 |
|
---|
112 | if (other == null && (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) == 0) {
|
---|
113 | // moving the new point to the perpendicular point
|
---|
114 | // FIXME: when two way segments are split, should move the new point to the
|
---|
115 | // intersection point!
|
---|
116 | EastNorth A = n1.eastNorth;
|
---|
117 | EastNorth B = n2.eastNorth;
|
---|
118 | double ab = A.distance(B);
|
---|
119 | double nb = n.eastNorth.distance(B);
|
---|
120 | double na = n.eastNorth.distance(A);
|
---|
121 | double q = (nb-na+ab)/ab/2;
|
---|
122 | n.eastNorth = new EastNorth(B.east() + q*(A.east()-B.east()), B.north() + q*(A.north()-B.north()));
|
---|
123 | n.coor = Main.proj.eastNorth2latlon(n.eastNorth);
|
---|
124 | }
|
---|
125 |
|
---|
126 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
127 | cmds.add(c);
|
---|
128 |
|
---|
129 | // split the first segment
|
---|
130 | splitWaySegmentAtNode(ws, n, cmds);
|
---|
131 |
|
---|
132 | // if a second segment was found, split that as well
|
---|
133 | if (other != null) splitWaySegmentAtNode(other, n, cmds);
|
---|
134 |
|
---|
135 | c = new SequenceCommand(tr((other == null) ?
|
---|
136 | "Add node into way" : "Add common node into two ways"), cmds);
|
---|
137 | }
|
---|
138 |
|
---|
139 | // Add a node and connecting segment.
|
---|
140 | if (mode == Mode.autonode) {
|
---|
141 |
|
---|
142 | WaySegment insertInto = null;
|
---|
143 | Node reuseNode = null;
|
---|
144 |
|
---|
145 | // If CTRL is held, insert the node into a potentially existing way segment
|
---|
146 | if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0) {
|
---|
147 | insertInto = Main.map.mapView.getNearestWaySegment(e.getPoint());
|
---|
148 | if (insertInto == null) System.err.println("Couldn't find nearby way segment");
|
---|
149 | if (insertInto == null)
|
---|
150 | return;
|
---|
151 | }
|
---|
152 | // If ALT is held, instead of creating a new node, re-use an existing
|
---|
153 | // node (making this action identical to AddSegmentAction with the
|
---|
154 | // small difference that the node used will then be selected to allow
|
---|
155 | // continuation of the "add node and connect" stuff)
|
---|
156 | else if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
|
---|
157 | OsmPrimitive clicked = Main.map.mapView.getNearest(e.getPoint());
|
---|
158 | if (clicked == null || !(clicked instanceof Node))
|
---|
159 | return;
|
---|
160 | reuseNode = (Node) clicked;
|
---|
161 | }
|
---|
162 |
|
---|
163 | Collection<OsmPrimitive> selection = Main.ds.getSelected();
|
---|
164 | if (selection.size() == 1 && selection.iterator().next() instanceof Node) {
|
---|
165 | Node n1 = (Node)selection.iterator().next();
|
---|
166 |
|
---|
167 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
168 |
|
---|
169 | if (reuseNode != null) {
|
---|
170 | // in re-use node mode, n1 must not be identical to clicked node
|
---|
171 | if (n1 == reuseNode) System.err.println("n1 == reuseNode");
|
---|
172 | if (n1 == reuseNode) return;
|
---|
173 | // replace newly created node with existing node
|
---|
174 | n = reuseNode;
|
---|
175 | } else {
|
---|
176 | // only add the node creation command if we're not re-using
|
---|
177 | cmds.add(c);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /* Keep track of the way we change, it might be the same into
|
---|
181 | * which we insert the node.
|
---|
182 | */
|
---|
183 | Way newInsertInto = null;
|
---|
184 | if (insertInto != null)
|
---|
185 | newInsertInto = splitWaySegmentAtNode(insertInto, n, cmds);
|
---|
186 |
|
---|
187 | Way way = getWayForNode(n1);
|
---|
188 | if (way == null) {
|
---|
189 | way = new Way();
|
---|
190 | way.nodes.add(n1);
|
---|
191 | cmds.add(new AddCommand(way));
|
---|
192 | } else {
|
---|
193 | if (insertInto != null && way == insertInto.way) {
|
---|
194 | way = newInsertInto;
|
---|
195 | } else {
|
---|
196 | Way wnew = new Way(way);
|
---|
197 | cmds.add(new ChangeCommand(way, wnew));
|
---|
198 | way = wnew;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | if (way.nodes.get(way.nodes.size() - 1) == n1) {
|
---|
203 | way.nodes.add(n);
|
---|
204 | } else {
|
---|
205 | way.nodes.add(0, n);
|
---|
206 | }
|
---|
207 |
|
---|
208 | c = new SequenceCommand(tr((insertInto == null) ? "Add node and connect" : "Add node into way and connect"), cmds);
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | Main.main.undoRedo.add(c);
|
---|
213 | Main.ds.setSelected(n);
|
---|
214 | Main.map.mapView.repaint();
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * @return If the node is the end of exactly one way, return this.
|
---|
219 | * <code>null</code> otherwise.
|
---|
220 | */
|
---|
221 | private Way getWayForNode(Node n) {
|
---|
222 | Way way = null;
|
---|
223 | for (Way w : Main.ds.ways) {
|
---|
224 | int i = w.nodes.indexOf(n);
|
---|
225 | if (i == -1) continue;
|
---|
226 | if (i == 0 || i == w.nodes.size() - 1) {
|
---|
227 | if (way != null)
|
---|
228 | return null;
|
---|
229 | way = w;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | return way;
|
---|
233 | }
|
---|
234 |
|
---|
235 | private Way splitWaySegmentAtNode(WaySegment ws, Node n, Collection<Command> cmds) {
|
---|
236 | Way wnew = new Way(ws.way);
|
---|
237 | wnew.nodes.add(ws.lowerIndex + 1, n);
|
---|
238 | cmds.add(new ChangeCommand(ws.way, wnew));
|
---|
239 | return wnew;
|
---|
240 | }
|
---|
241 | }
|
---|