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

Last change on this file since 6140 was 6140, checked in by Don-vip, 11 years ago

fix #8951 - fix clearing of primitive metadata

  • Property svn:eol-style set to native
File size: 8.3 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.gui.help.HelpUtil.ht;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.MouseInfo;
9import java.awt.Point;
10import java.awt.Toolkit;
11import java.awt.event.ActionEvent;
12import java.awt.event.KeyEvent;
13import java.util.ArrayList;
14import java.util.HashMap;
15import java.util.List;
16import java.util.Map;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.command.AddPrimitivesCommand;
20import org.openstreetmap.josm.data.coor.EastNorth;
21import org.openstreetmap.josm.data.osm.NodeData;
22import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
23import org.openstreetmap.josm.data.osm.PrimitiveData;
24import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
25import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy.PasteBufferChangedListener;
26import org.openstreetmap.josm.data.osm.RelationData;
27import org.openstreetmap.josm.data.osm.RelationMemberData;
28import org.openstreetmap.josm.data.osm.WayData;
29import org.openstreetmap.josm.gui.ExtendedDialog;
30import org.openstreetmap.josm.gui.layer.Layer;
31import org.openstreetmap.josm.tools.Shortcut;
32
33/**
34 * Paste OSM primitives from clipboard to the current edit layer.
35 * @since 404
36 */
37public final class PasteAction extends JosmAction implements PasteBufferChangedListener {
38
39 /**
40 * Constructs a new {@code PasteAction}.
41 */
42 public PasteAction() {
43 super(tr("Paste"), "paste", tr("Paste contents of paste buffer."),
44 Shortcut.registerShortcut("system:paste", tr("Edit: {0}", tr("Paste")), KeyEvent.VK_V, Shortcut.CTRL), true);
45 putValue("help", ht("/Action/Paste"));
46 Main.pasteBuffer.addPasteBufferChangedListener(this);
47 }
48
49 @Override
50 public void actionPerformed(ActionEvent e) {
51 if (!isEnabled())
52 return;
53 pasteData(Main.pasteBuffer, Main.pasteSource, e);
54 }
55
56 /**
57 * Paste OSM primitives from the given paste buffer and OSM data layer source to the current edit layer.
58 * @param pasteBuffer The paste buffer containing primitive ids to copy
59 * @param source The OSM data layer used to look for primitive ids
60 * @param e The ActionEvent that triggered this operation
61 */
62 public void pasteData(PrimitiveDeepCopy pasteBuffer, Layer source, ActionEvent e) {
63 /* Find the middle of the pasteBuffer area */
64 double maxEast = -1E100, minEast = 1E100, maxNorth = -1E100, minNorth = 1E100;
65 boolean incomplete = false;
66 for (PrimitiveData data : pasteBuffer.getAll()) {
67 if (data instanceof NodeData) {
68 NodeData n = (NodeData)data;
69 if (n.getEastNorth() != null) {
70 double east = n.getEastNorth().east();
71 double north = n.getEastNorth().north();
72 if (east > maxEast) { maxEast = east; }
73 if (east < minEast) { minEast = east; }
74 if (north > maxNorth) { maxNorth = north; }
75 if (north < minNorth) { minNorth = north; }
76 }
77 }
78 if (data.isIncomplete()) {
79 incomplete = true;
80 }
81 }
82
83 // Allow to cancel paste if there are incomplete primitives
84 if (incomplete) {
85 if (!confirmDeleteIncomplete()) return;
86 }
87
88 // default to paste in center of map (pasted via menu or cursor not in MapView)
89 EastNorth mPosition = Main.map.mapView.getCenter();
90 if((e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0) {
91 final Point mp = MouseInfo.getPointerInfo().getLocation();
92 final Point tl = Main.map.mapView.getLocationOnScreen();
93 final Point pos = new Point(mp.x-tl.x, mp.y-tl.y);
94 if(Main.map.mapView.contains(pos)) {
95 mPosition = Main.map.mapView.getEastNorth(pos.x, pos.y);
96 }
97 }
98
99 double offsetEast = mPosition.east() - (maxEast + minEast)/2.0;
100 double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0;
101
102 // Make a copy of pasteBuffer and map from old id to copied data id
103 List<PrimitiveData> bufferCopy = new ArrayList<PrimitiveData>();
104 List<PrimitiveData> toSelect = new ArrayList<PrimitiveData>();
105 Map<Long, Long> newNodeIds = new HashMap<Long, Long>();
106 Map<Long, Long> newWayIds = new HashMap<Long, Long>();
107 Map<Long, Long> newRelationIds = new HashMap<Long, Long>();
108 for (PrimitiveData data: pasteBuffer.getAll()) {
109 if (data.isIncomplete()) {
110 continue;
111 }
112 PrimitiveData copy = data.makeCopy();
113 copy.clearOsmMetadata();
114 if (data instanceof NodeData) {
115 newNodeIds.put(data.getUniqueId(), copy.getUniqueId());
116 } else if (data instanceof WayData) {
117 newWayIds.put(data.getUniqueId(), copy.getUniqueId());
118 } else if (data instanceof RelationData) {
119 newRelationIds.put(data.getUniqueId(), copy.getUniqueId());
120 }
121 bufferCopy.add(copy);
122 if (pasteBuffer.getDirectlyAdded().contains(data)) {
123 toSelect.add(copy);
124 }
125 }
126
127 // Update references in copied buffer
128 for (PrimitiveData data:bufferCopy) {
129 if (data instanceof NodeData) {
130 NodeData nodeData = (NodeData)data;
131 if (Main.map.mapView.getEditLayer() == source) {
132 nodeData.setEastNorth(nodeData.getEastNorth().add(offsetEast, offsetNorth));
133 }
134 } else if (data instanceof WayData) {
135 List<Long> newNodes = new ArrayList<Long>();
136 for (Long oldNodeId: ((WayData)data).getNodes()) {
137 Long newNodeId = newNodeIds.get(oldNodeId);
138 if (newNodeId != null) {
139 newNodes.add(newNodeId);
140 }
141 }
142 ((WayData)data).setNodes(newNodes);
143 } else if (data instanceof RelationData) {
144 List<RelationMemberData> newMembers = new ArrayList<RelationMemberData>();
145 for (RelationMemberData member: ((RelationData)data).getMembers()) {
146 OsmPrimitiveType memberType = member.getMemberType();
147 Long newId = null;
148 switch (memberType) {
149 case NODE:
150 newId = newNodeIds.get(member.getMemberId());
151 break;
152 case WAY:
153 newId = newWayIds.get(member.getMemberId());
154 break;
155 case RELATION:
156 newId = newRelationIds.get(member.getMemberId());
157 break;
158 }
159 if (newId != null) {
160 newMembers.add(new RelationMemberData(member.getRole(), memberType, newId));
161 }
162 }
163 ((RelationData)data).setMembers(newMembers);
164 }
165 }
166
167 /* Now execute the commands to add the duplicated contents of the paste buffer to the map */
168
169 Main.main.undoRedo.add(new AddPrimitivesCommand(bufferCopy, toSelect));
170 Main.map.mapView.repaint();
171 }
172
173 protected boolean confirmDeleteIncomplete() {
174 ExtendedDialog ed = new ExtendedDialog(Main.parent,
175 tr("Delete incomplete members?"),
176 new String[] {tr("Paste without incomplete members"), tr("Cancel")});
177 ed.setButtonIcons(new String[] {"dialogs/relation/deletemembers.png", "cancel.png"});
178 ed.setContent(tr("The copied data contains incomplete objects. "
179 + "When pasting the incomplete objects are removed. "
180 + "Do you want to paste the data without the incomplete objects?"));
181 ed.showDialog();
182 return ed.getValue() == 1;
183 }
184
185 @Override
186 protected void updateEnabledState() {
187 if (getCurrentDataSet() == null || Main.pasteBuffer == null) {
188 setEnabled(false);
189 return;
190 }
191 setEnabled(!Main.pasteBuffer.isEmpty());
192 }
193
194 @Override
195 public void pasteBufferChanged(PrimitiveDeepCopy pasteBuffer) {
196 updateEnabledState();
197 }
198}
Note: See TracBrowser for help on using the repository browser.