source: josm/trunk/src/org/openstreetmap/josm/actions/PasteAction.java@ 1351

Last change on this file since 1351 was 1351, checked in by stoecker, 15 years ago

close #1669. Patch by xeen

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