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

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

fix #9055 - Changing Paste shortcut to a single key pasted in the center of screen, not at mouse location

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