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

Last change on this file since 1834 was 1834, checked in by jttt, 15 years ago

Fix #3069 - Copy & paste to different layer doesn't work

  • Property svn:eol-style set to native
File size: 5.1 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 }
35
36 public void actionPerformed(ActionEvent e) {
37 if (!isEnabled())
38 return;
39 pasteData(Main.pasteBuffer, Main.pasteSource, e);
40 }
41
42 public void pasteData(DataSet pasteBuffer, Layer source, ActionEvent e) {
43 /* Find the middle of the pasteBuffer area */
44 double maxEast = -1E100, minEast = 1E100, maxNorth = -1E100, minNorth = 1E100;
45 for (Node n : pasteBuffer.nodes) {
46 double east = n.getEastNorth().east();
47 double north = n.getEastNorth().north();
48 if (east > maxEast) { maxEast = east; }
49 if (east < minEast) { minEast = east; }
50 if (north > maxNorth) { maxNorth = north; }
51 if (north < minNorth) { minNorth = north; }
52 }
53
54 EastNorth mPosition;
55 if((e.getModifiers() & ActionEvent.CTRL_MASK) ==0){
56 /* adjust the coordinates to the middle of the visible map area */
57 mPosition = Main.map.mapView.getCenter();
58 } else {
59 mPosition = Main.map.mapView.getEastNorth(Main.map.mapView.lastMEvent.getX(), Main.map.mapView.lastMEvent.getY());
60 }
61
62 double offsetEast = mPosition.east() - (maxEast + minEast)/2.0;
63 double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0;
64
65 HashMap<OsmPrimitive,OsmPrimitive> map = new HashMap<OsmPrimitive,OsmPrimitive>();
66 /* temporarily maps old nodes to new so we can do a true deep copy */
67
68 /* do the deep copy of the paste buffer contents, leaving the pasteBuffer unchanged */
69 for (Node n : pasteBuffer.nodes) {
70 Node nnew = new Node(n);
71 nnew.id = 0;
72 if (Main.map.mapView.getEditLayer() == source) {
73 nnew.setEastNorth(nnew.getEastNorth().add(offsetEast, offsetNorth));
74 }
75 map.put(n, nnew);
76 }
77 for (Way w : pasteBuffer.ways) {
78 Way wnew = new Way();
79 wnew.cloneFrom(w);
80 wnew.id = 0;
81 /* make sure we reference the new nodes corresponding to the old ones */
82 List<Node> nodes = new ArrayList<Node>();
83 for (Node n : w.nodes) {
84 nodes.add((Node)map.get(n));
85 }
86 wnew.nodes.clear();
87 wnew.nodes.addAll(nodes);
88 map.put(w, wnew);
89 }
90 for (Relation r : pasteBuffer.relations) {
91 Relation rnew = new Relation(r);
92 rnew.id = 0;
93 List<RelationMember> members = new ArrayList<RelationMember>();
94 for (RelationMember m : r.members) {
95 OsmPrimitive mo = map.get(m.member);
96 if(mo != null) /* TODO - This only prevents illegal data, but kills the relation */
97 {
98 RelationMember mnew = new RelationMember(m);
99 mnew.member = map.get(m.member);
100 members.add(mnew);
101 }
102 }
103 rnew.members.clear();
104 rnew.members.addAll(members);
105 map.put(r, rnew);
106 }
107
108 /* Now execute the commands to add the dupicated contents of the paste buffer to the map */
109 Collection<OsmPrimitive> osms = map.values();
110 Collection<Command> clist = new LinkedList<Command>();
111 for (OsmPrimitive osm : osms) {
112 clist.add(new AddCommand(osm));
113 }
114
115 Main.main.undoRedo.add(new SequenceCommand(tr("Paste"), clist));
116 getCurrentDataSet().setSelected(osms);
117 Main.map.mapView.repaint();
118 }
119
120 @Override
121 protected void updateEnabledState() {
122 if (getCurrentDataSet() == null) {
123 setEnabled(false);
124 return;
125 }
126 if (Main.pasteBuffer == null) {
127 setEnabled(false);
128 }
129 setEnabled(
130 !Main.pasteBuffer.nodes.isEmpty()
131 || !Main.pasteBuffer.ways.isEmpty()
132 || !Main.pasteBuffer.relations.isEmpty()
133 );
134 }
135}
Note: See TracBrowser for help on using the repository browser.