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

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

fix #8846 - use correct key mask on Mac in PasteAction

  • Property svn:eol-style set to native
File size: 8.2 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 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 if((e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0) {
90 final Point mp = MouseInfo.getPointerInfo().getLocation();
91 final Point tl = Main.map.mapView.getLocationOnScreen();
92 final Point pos = new Point(mp.x-tl.x, mp.y-tl.y);
93 if(Main.map.mapView.contains(pos)) {
94 mPosition = Main.map.mapView.getEastNorth(pos.x, pos.y);
95 }
96 }
97
98 double offsetEast = mPosition.east() - (maxEast + minEast)/2.0;
99 double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0;
100
101 // Make a copy of pasteBuffer and map from old id to copied data id
102 List<PrimitiveData> bufferCopy = new ArrayList<PrimitiveData>();
103 List<PrimitiveData> toSelect = new ArrayList<PrimitiveData>();
104 Map<Long, Long> newNodeIds = new HashMap<Long, Long>();
105 Map<Long, Long> newWayIds = new HashMap<Long, Long>();
106 Map<Long, Long> newRelationIds = new HashMap<Long, Long>();
107 for (PrimitiveData data: pasteBuffer.getAll()) {
108 if (data.isIncomplete()) {
109 continue;
110 }
111 PrimitiveData copy = data.makeCopy();
112 copy.clearOsmId();
113 if (data instanceof NodeData) {
114 newNodeIds.put(data.getUniqueId(), copy.getUniqueId());
115 } else if (data instanceof WayData) {
116 newWayIds.put(data.getUniqueId(), copy.getUniqueId());
117 } else if (data instanceof RelationData) {
118 newRelationIds.put(data.getUniqueId(), copy.getUniqueId());
119 }
120 bufferCopy.add(copy);
121 if (pasteBuffer.getDirectlyAdded().contains(data)) {
122 toSelect.add(copy);
123 }
124 }
125
126 // Update references in copied buffer
127 for (PrimitiveData data:bufferCopy) {
128 if (data instanceof NodeData) {
129 NodeData nodeData = (NodeData)data;
130 if (Main.map.mapView.getEditLayer() == source) {
131 nodeData.setEastNorth(nodeData.getEastNorth().add(offsetEast, offsetNorth));
132 }
133 } else if (data instanceof WayData) {
134 List<Long> newNodes = new ArrayList<Long>();
135 for (Long oldNodeId: ((WayData)data).getNodes()) {
136 Long newNodeId = newNodeIds.get(oldNodeId);
137 if (newNodeId != null) {
138 newNodes.add(newNodeId);
139 }
140 }
141 ((WayData)data).setNodes(newNodes);
142 } else if (data instanceof RelationData) {
143 List<RelationMemberData> newMembers = new ArrayList<RelationMemberData>();
144 for (RelationMemberData member: ((RelationData)data).getMembers()) {
145 OsmPrimitiveType memberType = member.getMemberType();
146 Long newId = null;
147 switch (memberType) {
148 case NODE:
149 newId = newNodeIds.get(member.getMemberId());
150 break;
151 case WAY:
152 newId = newWayIds.get(member.getMemberId());
153 break;
154 case RELATION:
155 newId = newRelationIds.get(member.getMemberId());
156 break;
157 }
158 if (newId != null) {
159 newMembers.add(new RelationMemberData(member.getRole(), memberType, newId));
160 }
161 }
162 ((RelationData)data).setMembers(newMembers);
163 }
164 }
165
166 /* Now execute the commands to add the duplicated contents of the paste buffer to the map */
167
168 Main.main.undoRedo.add(new AddPrimitivesCommand(bufferCopy, toSelect));
169 Main.map.mapView.repaint();
170 }
171
172 protected boolean confirmDeleteIncomplete() {
173 ExtendedDialog ed = new ExtendedDialog(Main.parent,
174 tr("Delete incomplete members?"),
175 new String[] {tr("Paste without incomplete members"), tr("Cancel")});
176 ed.setButtonIcons(new String[] {"dialogs/relation/deletemembers.png", "cancel.png"});
177 ed.setContent(tr("The copied data contains incomplete objects. "
178 + "When pasting the incomplete objects are removed. "
179 + "Do you want to paste the data without the incomplete objects?"));
180 ed.showDialog();
181 return ed.getValue() == 1;
182 }
183
184 @Override
185 protected void updateEnabledState() {
186 if (getCurrentDataSet() == null || Main.pasteBuffer == null) {
187 setEnabled(false);
188 return;
189 }
190 setEnabled(!Main.pasteBuffer.isEmpty());
191 }
192
193 @Override
194 public void pasteBufferChanged(PrimitiveDeepCopy pasteBuffer) {
195 updateEnabledState();
196 }
197}
Note: See TracBrowser for help on using the repository browser.