1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | // Author: David Earl
|
---|
3 | package org.openstreetmap.josm.actions;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
7 |
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.ListIterator;
|
---|
14 | import java.util.Map;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.command.AddPrimitivesCommand;
|
---|
18 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
19 | import org.openstreetmap.josm.data.osm.NodeData;
|
---|
20 | import org.openstreetmap.josm.data.osm.PrimitiveData;
|
---|
21 | import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
|
---|
22 | import org.openstreetmap.josm.data.osm.RelationData;
|
---|
23 | import org.openstreetmap.josm.data.osm.RelationMemberData;
|
---|
24 | import org.openstreetmap.josm.data.osm.WayData;
|
---|
25 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
26 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
27 |
|
---|
28 | public 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 | }
|
---|