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

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

fix #2302 - patch by jttt - some code cleanup for better encapsulation

  • Property svn:eol-style set to native
File size: 4.7 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.coor.EastNorth;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.Relation;
24import org.openstreetmap.josm.data.osm.RelationMember;
25import org.openstreetmap.josm.data.osm.Way;
26import org.openstreetmap.josm.gui.layer.Layer;
27import org.openstreetmap.josm.tools.Shortcut;
28
29public 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 static 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.eastNorth.east();
46 double north = n.eastNorth.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.main.editLayer() == source) {
72 nnew.setEastNorth(nnew.eastNorth.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 Main.ds.setSelected(osms);
116 Main.map.mapView.repaint();
117 }
118}
Note: See TracBrowser for help on using the repository browser.