1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | // Author: David Earl
|
---|
3 | package org.openstreetmap.josm.actions;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.LinkedList;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.command.AddCommand;
|
---|
17 | import org.openstreetmap.josm.command.Command;
|
---|
18 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
19 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
20 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
23 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
25 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
26 |
|
---|
27 | public final class PasteAction extends JosmAction {
|
---|
28 |
|
---|
29 | public PasteAction() {
|
---|
30 | super(tr("Paste"), "paste",
|
---|
31 | tr("Paste contents of paste buffer."),
|
---|
32 | KeyEvent.VK_V, KeyEvent.CTRL_MASK, true);
|
---|
33 | setEnabled(false);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void actionPerformed(ActionEvent e) {
|
---|
37 | DataSet pasteBuffer = Main.pasteBuffer;
|
---|
38 |
|
---|
39 | /* Find the middle of the pasteBuffer area */
|
---|
40 | EastNorth centre = Main.map.mapView.getCenter();
|
---|
41 | double maxEast = -1E100, minEast = 1E100, maxNorth = -1E100, minNorth = 1E100;
|
---|
42 | for (Node n : pasteBuffer.nodes) {
|
---|
43 | double east = n.eastNorth.east();
|
---|
44 | double north = n.eastNorth.north();
|
---|
45 | if (east > maxEast) { maxEast = east; }
|
---|
46 | if (east < minEast) { minEast = east; }
|
---|
47 | if (north > maxNorth) { maxNorth = north; }
|
---|
48 | if (north < minNorth) { minNorth = north; }
|
---|
49 | }
|
---|
50 | double offsetEast = centre.east() - (maxEast + minEast)/2.0;
|
---|
51 | double offsetNorth = centre.north() - (maxNorth + minNorth)/2.0;
|
---|
52 |
|
---|
53 | HashMap<OsmPrimitive,OsmPrimitive> map = new HashMap<OsmPrimitive,OsmPrimitive>();
|
---|
54 | /* temporarily maps old nodes to new so we can do a true deep copy */
|
---|
55 |
|
---|
56 | /* do the deep copy of the paste buffer contents, leaving the pasteBuffer unchanged */
|
---|
57 | for (Node n : pasteBuffer.nodes) {
|
---|
58 | Node nnew = new Node(n);
|
---|
59 | nnew.id = 0;
|
---|
60 | /* adjust the coordinates to the middle of the visible map area */
|
---|
61 | nnew.eastNorth = new EastNorth(nnew.eastNorth.east() + offsetEast, nnew.eastNorth.north() + offsetNorth);
|
---|
62 | nnew.coor = Main.proj.eastNorth2latlon(nnew.eastNorth);
|
---|
63 | map.put(n, nnew);
|
---|
64 | }
|
---|
65 | for (Way w : pasteBuffer.ways) {
|
---|
66 | Way wnew = new Way();
|
---|
67 | wnew.cloneFrom(w);
|
---|
68 | wnew.id = 0;
|
---|
69 | /* make sure we reference the new nodes corresponding to the old ones */
|
---|
70 | List<Node> nodes = new ArrayList<Node>();
|
---|
71 | for (Node n : w.nodes) {
|
---|
72 | nodes.add((Node)map.get(n));
|
---|
73 | }
|
---|
74 | wnew.nodes.clear();
|
---|
75 | wnew.nodes.addAll(nodes);
|
---|
76 | map.put(w, wnew);
|
---|
77 | }
|
---|
78 | for (Relation r : pasteBuffer.relations) {
|
---|
79 | Relation rnew = new Relation(r);
|
---|
80 | rnew.id = 0;
|
---|
81 | List<RelationMember> members = new ArrayList<RelationMember>();
|
---|
82 | for (RelationMember m : r.members) {
|
---|
83 | RelationMember mnew = new RelationMember(m);
|
---|
84 | mnew.member = map.get(m.member);
|
---|
85 | members.add(mnew);
|
---|
86 | }
|
---|
87 | rnew.members.clear();
|
---|
88 | rnew.members.addAll(members);
|
---|
89 | map.put(r, rnew);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* Now execute the commands to add the dupicated contents of the paste buffer to the map */
|
---|
93 | Collection<OsmPrimitive> osms = map.values();
|
---|
94 | Collection<Command> clist = new LinkedList<Command>();
|
---|
95 | for (OsmPrimitive osm : osms) {
|
---|
96 | clist.add(new AddCommand(osm));
|
---|
97 | }
|
---|
98 |
|
---|
99 | Main.main.undoRedo.add(new SequenceCommand(tr("Paste"), clist));
|
---|
100 | Main.ds.setSelected(osms);
|
---|
101 | Main.map.mapView.repaint();
|
---|
102 | }
|
---|
103 | }
|
---|