source: josm/src/org/openstreetmap/josm/actions/mapmode/AddNodeAction.java@ 98

Last change on this file since 98 was 98, checked in by imi, 18 years ago
  • added Applet version of JOSM (unfinished)
  • fixed display bug if --no-fullscreen and --geometry was specified
File size: 3.3 KB
Line 
1package org.openstreetmap.josm.actions.mapmode;
2
3import java.awt.event.KeyEvent;
4import java.awt.event.MouseEvent;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.HashMap;
8import java.util.LinkedList;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.actions.GroupAction;
14import org.openstreetmap.josm.command.AddCommand;
15import org.openstreetmap.josm.command.ChangeCommand;
16import org.openstreetmap.josm.command.Command;
17import org.openstreetmap.josm.command.SequenceCommand;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.Segment;
20import org.openstreetmap.josm.data.osm.Way;
21import org.openstreetmap.josm.gui.MapFrame;
22
23/**
24 * This mode adds a new node to the dataset. The user clicks on a place to add
25 * and there is it. Nothing more, nothing less.
26 *
27 * Newly created nodes are selected. Shift modifier does not cancel the old
28 * selection as usual.
29 *
30 * @author imi
31 *
32 */
33public class AddNodeAction extends MapMode {
34
35 enum Mode {node, nodesegment}
36 private final Mode mode;
37
38 public static class AddNodeGroup extends GroupAction {
39 public AddNodeGroup(MapFrame mf) {
40 super(KeyEvent.VK_N,0);
41 actions.add(new AddNodeAction(mf, "Add node", Mode.node, "Add a new node to the map"));
42 actions.add(new AddNodeAction(mf, "Add node into segment", Mode.nodesegment, "Add a node into an existing segment"));
43 setCurrent(0);
44 }
45 }
46
47 public AddNodeAction(MapFrame mapFrame, String name, Mode mode, String desc) {
48 super(name, "node/"+mode, desc, mapFrame);
49 this.mode = mode;
50 }
51
52 @Override public void enterMode() {
53 super.enterMode();
54 Main.map.mapView.addMouseListener(this);
55 }
56
57 @Override public void exitMode() {
58 super.exitMode();
59 Main.map.mapView.removeMouseListener(this);
60 }
61
62 /**
63 * If user clicked with the left button, add a node at the current mouse
64 * position.
65 *
66 * If in nodesegment mode, add the node to the line segment by splitting the
67 * segment. The new created segment will be inserted in every way the segment
68 * was part of.
69 */
70 @Override public void mouseClicked(MouseEvent e) {
71 if (e.getButton() != MouseEvent.BUTTON1)
72 return;
73
74 Node n = new Node(Main.map.mapView.getLatLon(e.getX(), e.getY()));
75 if (n.coor.isOutSideWorld()) {
76 JOptionPane.showMessageDialog(Main.parent, "Can not add a node outside of the world.");
77 return;
78 }
79
80 Command c = new AddCommand(n);
81 if (mode == Mode.nodesegment) {
82 Segment s = Main.map.mapView.getNearestSegment(e.getPoint());
83 if (s == null)
84 return;
85
86 Collection<Command> cmds = new LinkedList<Command>();
87 cmds.add(c);
88 Segment s1 = new Segment(s);
89 s1.to = n;
90 Segment s2 = new Segment(s.from, s.to);
91 s2.from = n;
92 if (s.keys != null)
93 s2.keys = new HashMap<String, String>(s.keys);
94
95 cmds.add(new ChangeCommand(s, s1));
96 cmds.add(new AddCommand(s2));
97
98 // Add the segment to every way
99 for (Way wold : Main.ds.ways) {
100 if (wold.segments.contains(s)) {
101 Way wnew = new Way(wold);
102 Collection<Segment> segs = new ArrayList<Segment>(wnew.segments);
103 wnew.segments.clear();
104 for (Segment waySeg : segs) {
105 wnew.segments.add(waySeg);
106 if (waySeg == s)
107 wnew.segments.add(s2);
108 }
109 cmds.add(new ChangeCommand(wold, wnew));
110 }
111 }
112
113 c = new SequenceCommand(cmds);
114 }
115 Main.main.editLayer().add(c);
116 Main.map.mapView.repaint();
117 }
118}
Note: See TracBrowser for help on using the repository browser.