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

Last change on this file since 2484 was 2349, checked in by Gubaer, 14 years ago

applied #3787: patch by hansendc: Select duplicated objects

  • Property svn:eol-style set to native
File size: 4.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;
6import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
7
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.ArrayList;
11import java.util.HashMap;
12import java.util.List;
13import java.util.ListIterator;
14import java.util.Map;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.command.AddPrimitivesCommand;
18import org.openstreetmap.josm.data.coor.EastNorth;
19import org.openstreetmap.josm.data.osm.NodeData;
20import org.openstreetmap.josm.data.osm.PrimitiveData;
21import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
22import org.openstreetmap.josm.data.osm.RelationData;
23import org.openstreetmap.josm.data.osm.RelationMemberData;
24import org.openstreetmap.josm.data.osm.WayData;
25import org.openstreetmap.josm.gui.layer.Layer;
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 putValue("help", ht("/Action/Paste"));
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(PrimitiveDeepCopy 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 (PrimitiveData data : pasteBuffer.getAll()) {
46 if (data instanceof NodeData) {
47 NodeData n = (NodeData)data;
48 double east = n.getEastNorth().east();
49 double north = n.getEastNorth().north();
50 if (east > maxEast) { maxEast = east; }
51 if (east < minEast) { minEast = east; }
52 if (north > maxNorth) { maxNorth = north; }
53 if (north < minNorth) { minNorth = north; }
54 }
55 }
56
57 EastNorth mPosition;
58 if((e.getModifiers() & ActionEvent.CTRL_MASK) ==0){
59 /* adjust the coordinates to the middle of the visible map area */
60 mPosition = Main.map.mapView.getCenter();
61 } else {
62 if (Main.map.mapView.lastMEvent != null) {
63 mPosition = Main.map.mapView.getEastNorth(Main.map.mapView.lastMEvent.getX(), Main.map.mapView.lastMEvent.getY());
64 } else {
65 mPosition = Main.map.mapView.getCenter();
66 }
67 }
68
69 double offsetEast = mPosition.east() - (maxEast + minEast)/2.0;
70 double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0;
71
72
73
74 // Make a copy of pasteBuffer and map from old id to copied data id
75 List<PrimitiveData> bufferCopy = new ArrayList<PrimitiveData>();
76 Map<Long, Long> newIds = new HashMap<Long, Long>();
77 for (PrimitiveData data:pasteBuffer.getAll()) {
78 PrimitiveData copy = data.makeCopy();
79 copy.clearOsmId();
80 newIds.put(data.getId(), copy.getId());
81 bufferCopy.add(copy);
82 }
83
84 // Update references in copied buffer
85 for (PrimitiveData data:bufferCopy) {
86 if (data instanceof NodeData) {
87 NodeData nodeData = (NodeData)data;
88 if (Main.map.mapView.getEditLayer() == source) {
89 nodeData.setEastNorth(nodeData.getEastNorth().add(offsetEast, offsetNorth));
90 }
91 } else if (data instanceof WayData) {
92 ListIterator<Long> it = ((WayData)data).getNodes().listIterator();
93 while (it.hasNext()) {
94 it.set(newIds.get(it.next()));
95 }
96 } else if (data instanceof RelationData) {
97 ListIterator<RelationMemberData> it = ((RelationData)data).getMembers().listIterator();
98 while (it.hasNext()) {
99 RelationMemberData member = it.next();
100 it.set(new RelationMemberData(member.getRole(), member.getMemberType(), newIds.get(member.getMemberId())));
101 }
102 }
103 }
104
105 /* Now execute the commands to add the duplicated contents of the paste buffer to the map */
106
107 Main.main.undoRedo.add(new AddPrimitivesCommand(bufferCopy));
108 Main.map.mapView.repaint();
109 }
110
111 @Override
112 protected void updateEnabledState() {
113 if (getCurrentDataSet() == null || Main.pasteBuffer == null) {
114 setEnabled(false);
115 return;
116 }
117 setEnabled(!Main.pasteBuffer.isEmpty());
118 }
119}
Note: See TracBrowser for help on using the repository browser.