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.coor.EastNorth;
|
---|
20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
21 | import org.openstreetmap.josm.data.osm.Node;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
23 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
24 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
25 | import org.openstreetmap.josm.data.osm.Way;
|
---|
26 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
27 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
28 |
|
---|
29 | public final class PasteAction extends JosmAction {
|
---|
30 |
|
---|
31 | public PasteAction() {
|
---|
32 | super(tr("Paste"), "paste", tr("Paste contents of paste buffer."),
|
---|
33 | Shortcut.registerShortcut("system:paste", tr("Edit: {0}", tr("Paste")), KeyEvent.VK_V, Shortcut.GROUP_MENU), true);
|
---|
34 | setEnabled(false);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void actionPerformed(ActionEvent e) {
|
---|
38 | pasteData(Main.pasteBuffer, Main.pasteSource, e);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void pasteData(DataSet pasteBuffer, Layer source, ActionEvent e) {
|
---|
42 | /* Find the middle of the pasteBuffer area */
|
---|
43 | double maxEast = -1E100, minEast = 1E100, maxNorth = -1E100, minNorth = 1E100;
|
---|
44 | for (Node n : pasteBuffer.nodes) {
|
---|
45 | double east = n.getEastNorth().east();
|
---|
46 | double north = n.getEastNorth().north();
|
---|
47 | if (east > maxEast) { maxEast = east; }
|
---|
48 | if (east < minEast) { minEast = east; }
|
---|
49 | if (north > maxNorth) { maxNorth = north; }
|
---|
50 | if (north < minNorth) { minNorth = north; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | EastNorth mPosition;
|
---|
54 | if((e.getModifiers() & ActionEvent.CTRL_MASK) ==0){
|
---|
55 | /* adjust the coordinates to the middle of the visible map area */
|
---|
56 | mPosition = Main.map.mapView.getCenter();
|
---|
57 | } else {
|
---|
58 | mPosition = Main.map.mapView.getEastNorth(Main.map.mapView.lastMEvent.getX(), Main.map.mapView.lastMEvent.getY());
|
---|
59 | }
|
---|
60 |
|
---|
61 | double offsetEast = mPosition.east() - (maxEast + minEast)/2.0;
|
---|
62 | double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0;
|
---|
63 |
|
---|
64 | HashMap<OsmPrimitive,OsmPrimitive> map = new HashMap<OsmPrimitive,OsmPrimitive>();
|
---|
65 | /* temporarily maps old nodes to new so we can do a true deep copy */
|
---|
66 |
|
---|
67 | /* do the deep copy of the paste buffer contents, leaving the pasteBuffer unchanged */
|
---|
68 | for (Node n : pasteBuffer.nodes) {
|
---|
69 | Node nnew = new Node(n);
|
---|
70 | nnew.id = 0;
|
---|
71 | if (Main.map.mapView.getEditLayer() == source) {
|
---|
72 | nnew.setEastNorth(nnew.getEastNorth().add(offsetEast, offsetNorth));
|
---|
73 | }
|
---|
74 | map.put(n, nnew);
|
---|
75 | }
|
---|
76 | for (Way w : pasteBuffer.ways) {
|
---|
77 | Way wnew = new Way();
|
---|
78 | wnew.cloneFrom(w);
|
---|
79 | wnew.id = 0;
|
---|
80 | /* make sure we reference the new nodes corresponding to the old ones */
|
---|
81 | List<Node> nodes = new ArrayList<Node>();
|
---|
82 | for (Node n : w.nodes) {
|
---|
83 | nodes.add((Node)map.get(n));
|
---|
84 | }
|
---|
85 | wnew.nodes.clear();
|
---|
86 | wnew.nodes.addAll(nodes);
|
---|
87 | map.put(w, wnew);
|
---|
88 | }
|
---|
89 | for (Relation r : pasteBuffer.relations) {
|
---|
90 | Relation rnew = new Relation(r);
|
---|
91 | rnew.id = 0;
|
---|
92 | List<RelationMember> members = new ArrayList<RelationMember>();
|
---|
93 | for (RelationMember m : r.members) {
|
---|
94 | OsmPrimitive mo = map.get(m.member);
|
---|
95 | if(mo != null) /* TODO - This only prevents illegal data, but kills the relation */
|
---|
96 | {
|
---|
97 | RelationMember mnew = new RelationMember(m);
|
---|
98 | mnew.member = map.get(m.member);
|
---|
99 | members.add(mnew);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | rnew.members.clear();
|
---|
103 | rnew.members.addAll(members);
|
---|
104 | map.put(r, rnew);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /* Now execute the commands to add the dupicated contents of the paste buffer to the map */
|
---|
108 | Collection<OsmPrimitive> osms = map.values();
|
---|
109 | Collection<Command> clist = new LinkedList<Command>();
|
---|
110 | for (OsmPrimitive osm : osms) {
|
---|
111 | clist.add(new AddCommand(osm));
|
---|
112 | }
|
---|
113 |
|
---|
114 | Main.main.undoRedo.add(new SequenceCommand(tr("Paste"), clist));
|
---|
115 | getCurrentDataSet().setSelected(osms);
|
---|
116 | Main.map.mapView.repaint();
|
---|
117 | }
|
---|
118 | }
|
---|