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

Last change on this file since 1023 was 1023, checked in by stoecker, 16 years ago

close bug #1622. Keyboard shortcuts and specific OS handling

  • 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 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: Paste"), KeyEvent.VK_V, ShortCut.GROUP_MENU), 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 double maxEast = -1E100, minEast = 1E100, maxNorth = -1E100, minNorth = 1E100;
41 for (Node n : pasteBuffer.nodes) {
42 double east = n.eastNorth.east();
43 double north = n.eastNorth.north();
44 if (east > maxEast) { maxEast = east; }
45 if (east < minEast) { minEast = east; }
46 if (north > maxNorth) { maxNorth = north; }
47 if (north < minNorth) { minNorth = north; }
48 }
49
50 EastNorth mPosition;
51 if((e.getModifiers() & ActionEvent.CTRL_MASK) ==0){
52 mPosition = Main.map.mapView.getCenter();
53 } else {
54 mPosition = Main.map.mapView.getEastNorth(Main.map.mapView.lastMEvent.getX(), Main.map.mapView.lastMEvent.getY());
55 }
56
57 double offsetEast = mPosition.east() - (maxEast + minEast)/2.0;
58 double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0;
59
60 HashMap<OsmPrimitive,OsmPrimitive> map = new HashMap<OsmPrimitive,OsmPrimitive>();
61 /* temporarily maps old nodes to new so we can do a true deep copy */
62
63 /* do the deep copy of the paste buffer contents, leaving the pasteBuffer unchanged */
64 for (Node n : pasteBuffer.nodes) {
65 Node nnew = new Node(n);
66 nnew.id = 0;
67 /* adjust the coordinates to the middle of the visible map area */
68 nnew.eastNorth = new EastNorth(nnew.eastNorth.east() + offsetEast, nnew.eastNorth.north() + offsetNorth);
69 nnew.coor = Main.proj.eastNorth2latlon(nnew.eastNorth);
70 map.put(n, nnew);
71 }
72 for (Way w : pasteBuffer.ways) {
73 Way wnew = new Way();
74 wnew.cloneFrom(w);
75 wnew.id = 0;
76 /* make sure we reference the new nodes corresponding to the old ones */
77 List<Node> nodes = new ArrayList<Node>();
78 for (Node n : w.nodes) {
79 nodes.add((Node)map.get(n));
80 }
81 wnew.nodes.clear();
82 wnew.nodes.addAll(nodes);
83 map.put(w, wnew);
84 }
85 for (Relation r : pasteBuffer.relations) {
86 Relation rnew = new Relation(r);
87 rnew.id = 0;
88 List<RelationMember> members = new ArrayList<RelationMember>();
89 for (RelationMember m : r.members) {
90 RelationMember mnew = new RelationMember(m);
91 mnew.member = map.get(m.member);
92 members.add(mnew);
93 }
94 rnew.members.clear();
95 rnew.members.addAll(members);
96 map.put(r, rnew);
97 }
98
99 /* Now execute the commands to add the dupicated contents of the paste buffer to the map */
100 Collection<OsmPrimitive> osms = map.values();
101 Collection<Command> clist = new LinkedList<Command>();
102 for (OsmPrimitive osm : osms) {
103 clist.add(new AddCommand(osm));
104 }
105
106 Main.main.undoRedo.add(new SequenceCommand(tr("Paste"), clist));
107 Main.ds.setSelected(osms);
108 Main.map.mapView.repaint();
109 }
110}
Note: See TracBrowser for help on using the repository browser.