1 | package org.openstreetmap.josm.actions.mapmode;
|
---|
2 |
|
---|
3 | import java.awt.event.KeyEvent;
|
---|
4 | import java.awt.event.MouseEvent;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.LinkedList;
|
---|
9 |
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.actions.GroupAction;
|
---|
14 | import org.openstreetmap.josm.command.AddCommand;
|
---|
15 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
16 | import org.openstreetmap.josm.command.Command;
|
---|
17 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
20 | import org.openstreetmap.josm.data.osm.Way;
|
---|
21 | import 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 | */
|
---|
33 | public 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 | }
|
---|