Changeset 329 in josm for branch/0.5/src/org/openstreetmap/josm/actions/mapmode/AddNodeAction.java
- Timestamp:
- 2007-09-24T01:36:24+02:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branch/0.5/src/org/openstreetmap/josm/actions/mapmode/AddNodeAction.java
r314 r329 24 24 import org.openstreetmap.josm.data.osm.Node; 25 25 import org.openstreetmap.josm.data.osm.OsmPrimitive; 26 import org.openstreetmap.josm.data.osm.Segment;27 26 import org.openstreetmap.josm.data.osm.Way; 27 import org.openstreetmap.josm.data.osm.WaySegment; 28 28 import org.openstreetmap.josm.gui.MapFrame; 29 29 import org.openstreetmap.josm.tools.ImageProvider; … … 33 33 * and there is it. Nothing more, nothing less. 34 34 * 35 * FIXME: "nothing more, nothing less" is a bit out-of-date 36 * 35 37 * Newly created nodes are selected. Shift modifier does not cancel the old 36 38 * selection as usual. … … 41 43 public class AddNodeAction extends MapMode { 42 44 43 enum Mode {node, node segment, autonode}45 enum Mode {node, nodeway, autonode} 44 46 private final Mode mode; 45 47 … … 49 51 putValue("help", "Action/AddNode"); 50 52 actions.add(new AddNodeAction(mf,tr("Add node"), Mode.node, tr("Add a new node to the map"))); 51 actions.add(new AddNodeAction(mf, tr("Add node into segment"), Mode.nodesegment,tr( "Add a node into an existing segment")));52 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 segment; with SHIFT: re-use existing node)")));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)"))); 53 55 setCurrent(0); 54 56 } … … 83 85 * position. 84 86 * 85 * If in nodesegment mode, add the node to the line segment by splitting the 86 * segment. The new created segment will be inserted in every way the segment 87 * was part of. 87 * If in nodeway mode, insert the node into the way. 88 88 */ 89 89 @Override public void mouseClicked(MouseEvent e) { … … 98 98 99 99 Command c = new AddCommand(n); 100 if (mode == Mode.node segment) {101 Segment s = Main.map.mapView.getNearestSegment(e.getPoint());102 if ( s == null)100 if (mode == Mode.nodeway) { 101 WaySegment ws = Main.map.mapView.getNearestWaySegment(e.getPoint()); 102 if (ws == null) 103 103 return; 104 104 105 105 // see if another segment is also near 106 Segment other = Main.map.mapView.getNearestSegment(e.getPoint(), Collections.singleton(s)); 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); 107 111 108 112 if (other == null && (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) == 0) { 109 113 // moving the new point to the perpendicular point 110 // FIXME: when two segments are split, should move the new point to the114 // FIXME: when two way segments are split, should move the new point to the 111 115 // intersection point! 112 EastNorth A = s.from.eastNorth;113 EastNorth B = s.to.eastNorth;116 EastNorth A = n1.eastNorth; 117 EastNorth B = n2.eastNorth; 114 118 double ab = A.distance(B); 115 119 double nb = n.eastNorth.distance(B); … … 124 128 125 129 // split the first segment 126 split SegmentAtNode(s, n, cmds);130 splitWaySegmentAtNode(ws, n, cmds); 127 131 128 132 // if a second segment was found, split that as well 129 if (other != null) split SegmentAtNode(other, n, cmds);133 if (other != null) splitWaySegmentAtNode(other, n, cmds); 130 134 131 135 c = new SequenceCommand(tr((other == null) ? 132 "Add node into segment" : "Add common node into two segments"), cmds);136 "Add node into way" : "Add common node into two ways"), cmds); 133 137 } 134 138 … … 136 140 if (mode == Mode.autonode) { 137 141 138 Segment insertInto = null;142 WaySegment insertInto = null; 139 143 Node reuseNode = null; 140 144 141 // If CTRL is held, insert the node into a potentially existing segment145 // If CTRL is held, insert the node into a potentially existing way segment 142 146 if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0) { 143 insertInto = Main.map.mapView.getNearestSegment(e.getPoint()); 147 insertInto = Main.map.mapView.getNearestWaySegment(e.getPoint()); 148 if (insertInto == null) System.err.println("Couldn't find nearby way segment"); 144 149 if (insertInto == null) 145 150 return; … … 150 155 // continuation of the "add node and connect" stuff) 151 156 else if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) { 152 OsmPrimitive clicked = Main.map.mapView.getNearest(e.getPoint() , false);157 OsmPrimitive clicked = Main.map.mapView.getNearest(e.getPoint()); 153 158 if (clicked == null || !(clicked instanceof Node)) 154 159 return; … … 159 164 if (selection.size() == 1 && selection.iterator().next() instanceof Node) { 160 165 Node n1 = (Node)selection.iterator().next(); 166 161 167 Collection<Command> cmds = new LinkedList<Command>(); 162 168 163 169 if (reuseNode != null) { 164 170 // in re-use node mode, n1 must not be identical to clicked node 171 if (n1 == reuseNode) System.err.println("n1 == reuseNode"); 165 172 if (n1 == reuseNode) return; 166 173 // replace newly created node with existing node … … 171 178 } 172 179 173 Segment s = new Segment(n1, n); 174 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; 175 184 if (insertInto != null) 176 splitSegmentAtNode(insertInto, n, cmds); 177 178 cmds.add(new AddCommand(s)); 185 newInsertInto = splitWaySegmentAtNode(insertInto, n, cmds); 179 186 180 187 Way way = getWayForNode(n1); 181 if (way != null) { 182 Way newWay = new Way(way); 183 if (way.segments.get(0).from == n1) { 184 Node tmp = s.from; 185 s.from = s.to; 186 s.to = tmp; 187 newWay.segments.add(0, s); 188 } else 189 newWay.segments.add(s); 190 cmds.add(new ChangeCommand(way, newWay)); 191 } 192 193 c = new SequenceCommand(tr((insertInto == null) ? "Add node and connect" : "Add node into segment and connect"), cmds); 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); 194 209 } 195 210 } … … 201 216 202 217 /** 203 * @return If the node is partof exactly one way, return this.218 * @return If the node is the end of exactly one way, return this. 204 219 * <code>null</code> otherwise. 205 220 */ … … 207 222 Way way = null; 208 223 for (Way w : Main.ds.ways) { 209 for (Segment s : w.segments) { 210 if (s.from == n || s.to == n) { 224 int i = w.nodes.indexOf(n); 225 if (i == -1) continue; 226 if (i == 0 || i == w.nodes.size() - 1) { 211 227 if (way != null) 212 228 return null; 213 if (s.from == s.to)214 return null;215 229 way = w; 216 230 } 217 231 } 218 }219 232 return way; 220 233 } 221 234 222 private void splitSegmentAtNode(Segment s, Node n, Collection<Command> cmds) { 223 Segment s1 = new Segment(s); 224 s1.to = n; 225 Segment s2 = new Segment(s.from, s.to); 226 s2.from = n; 227 if (s.keys != null) 228 s2.keys = new HashMap<String, String>(s.keys); 229 230 cmds.add(new ChangeCommand(s, s1)); 231 cmds.add(new AddCommand(s2)); 232 233 // Add the segment to every way 234 for (Way wold : Main.ds.ways) { 235 if (wold.segments.contains(s)) { 236 Way wnew = new Way(wold); 237 Collection<Segment> segs = new ArrayList<Segment>(wnew.segments); 238 wnew.segments.clear(); 239 for (Segment waySeg : segs) { 240 wnew.segments.add(waySeg); 241 if (waySeg == s) 242 wnew.segments.add(s2); 243 } 244 cmds.add(new ChangeCommand(wold, wnew)); 245 } 246 } 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; 247 240 } 248 241 }
Note:
See TracChangeset
for help on using the changeset viewer.