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

Last change on this file since 811 was 811, checked in by david, 16 years ago

paste/duplicate at mouse position: patch on behalf of Petr Dlouhý, petr.dlouhyat at email dot cz

  • Property svn:eol-style set to native
File size: 3.8 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 java.awt.event.MouseMotionListener;
16import javax.swing.JOptionPane;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.command.AddCommand;
20import org.openstreetmap.josm.command.Command;
21import org.openstreetmap.josm.command.SequenceCommand;
22import org.openstreetmap.josm.data.osm.DataSet;
23import org.openstreetmap.josm.data.osm.Relation;
24import org.openstreetmap.josm.data.osm.Node;
25import org.openstreetmap.josm.data.osm.OsmPrimitive;
26import org.openstreetmap.josm.data.osm.RelationMember;
27import org.openstreetmap.josm.data.osm.Way;
28import org.openstreetmap.josm.data.coor.EastNorth;
29
30public final class PasteAction extends JosmAction {
31
32 public PasteAction() {
33 super(tr("Paste"), "paste",
34 tr("Paste contents of paste buffer."),
35 KeyEvent.VK_V, KeyEvent.CTRL_MASK, true);
36 setEnabled(false);
37 }
38
39 public void actionPerformed(ActionEvent e) {
40 DataSet pasteBuffer = Main.pasteBuffer;
41
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 mPosition = Main.map.mapView.getCenter();
56 } else {
57 mPosition = Main.map.mapView.getEastNorth(Main.map.mapView.lastMEvent.getX(), Main.map.mapView.lastMEvent.getY());
58 };
59
60 double offsetEast = mPosition.east() - (maxEast + minEast)/2.0;
61 double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0;
62
63 HashMap<OsmPrimitive,OsmPrimitive> map = new HashMap<OsmPrimitive,OsmPrimitive>();
64 /* temporarily maps old nodes to new so we can do a true deep copy */
65
66 /* do the deep copy of the paste buffer contents, leaving the pasteBuffer unchanged */
67 for (Node n : pasteBuffer.nodes) {
68 Node nnew = new Node(n);
69 nnew.id = 0;
70 /* adjust the coordinates to the middle of the visible map area */
71 nnew.eastNorth = new EastNorth(nnew.eastNorth.east() + offsetEast, nnew.eastNorth.north() + offsetNorth);
72 nnew.coor = Main.proj.eastNorth2latlon(nnew.eastNorth);
73 map.put(n, nnew);
74 }
75 for (Way w : pasteBuffer.ways) {
76 Way wnew = new Way();
77 wnew.cloneFrom(w);
78 wnew.id = 0;
79 /* make sure we reference the new nodes corresponding to the old ones */
80 List<Node> nodes = new ArrayList<Node>();
81 for (Node n : w.nodes) {
82 nodes.add((Node)map.get(n));
83 }
84 wnew.nodes.clear();
85 wnew.nodes.addAll(nodes);
86 map.put(w, wnew);
87 }
88 for (Relation r : pasteBuffer.relations) {
89 Relation rnew = new Relation(r);
90 rnew.id = 0;
91 List<RelationMember> members = new ArrayList<RelationMember>();
92 for (RelationMember m : r.members) {
93 RelationMember mnew = new RelationMember(m);
94 mnew.member = map.get(m.member);
95 members.add(mnew);
96 }
97 rnew.members.clear();
98 rnew.members.addAll(members);
99 map.put(r, rnew);
100 }
101
102 /* Now execute the commands to add the dupicated contents of the paste buffer to the map */
103 Collection<OsmPrimitive> osms = map.values();
104 Collection<Command> clist = new LinkedList<Command>();
105 for (OsmPrimitive osm : osms) {
106 clist.add(new AddCommand(osm));
107 }
108
109 Main.main.undoRedo.add(new SequenceCommand(tr("Paste"), clist));
110 Main.ds.setSelected(osms);
111 Main.map.mapView.repaint();
112 }
113}
Note: See TracBrowser for help on using the repository browser.