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

Last change on this file since 10734 was 10682, checked in by Don-vip, 8 years ago

fix #13203 - Ctrl-D (Duplicate) places new object on top of source object (patch by michael2402) - gsoc-core

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
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.datatransfer.FlavorEvent;
11import java.awt.datatransfer.FlavorListener;
12import java.awt.event.ActionEvent;
13import java.awt.event.KeyEvent;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.coor.EastNorth;
17import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
18import org.openstreetmap.josm.gui.datatransfer.OsmTransferHandler;
19import org.openstreetmap.josm.tools.Shortcut;
20
21/**
22 * Paste OSM primitives from clipboard to the current edit layer.
23 * @since 404
24 */
25public final class PasteAction extends JosmAction implements FlavorListener {
26
27 private final OsmTransferHandler transferHandler;
28
29 /**
30 * Constructs a new {@code PasteAction}.
31 */
32 public PasteAction() {
33 super(tr("Paste"), "paste", tr("Paste contents of paste buffer."),
34 Shortcut.registerShortcut("system:paste", tr("Edit: {0}", tr("Paste")), KeyEvent.VK_V, Shortcut.CTRL), true);
35 putValue("help", ht("/Action/Paste"));
36 // CUA shortcut for paste (https://en.wikipedia.org/wiki/IBM_Common_User_Access#Description)
37 Main.registerActionShortcut(this,
38 Shortcut.registerShortcut("system:paste:cua", tr("Edit: {0}", tr("Paste")), KeyEvent.VK_INSERT, Shortcut.SHIFT));
39 transferHandler = new OsmTransferHandler();
40 ClipboardUtils.getClipboard().addFlavorListener(this);
41 }
42
43 @Override
44 public void actionPerformed(ActionEvent e) {
45 transferHandler.pasteOn(Main.getLayerManager().getEditLayer(), computePastePosition(e, getValue(NAME)));
46 }
47
48 static EastNorth computePastePosition(ActionEvent e, Object name) {
49 // default to paste in center of map (pasted via menu or cursor not in MapView)
50 EastNorth mPosition = Main.map.mapView.getCenter();
51 // We previously checked for modifier to know if the action has been trigerred via shortcut or via menu
52 // But this does not work if the shortcut is changed to a single key (see #9055)
53 // Observed behaviour: getActionCommand() returns Action.NAME when triggered via menu, but shortcut text when triggered with it
54 if (e != null && !name.equals(e.getActionCommand())) {
55 final Point mp = MouseInfo.getPointerInfo().getLocation();
56 final Point tl = Main.map.mapView.getLocationOnScreen();
57 final Point pos = new Point(mp.x-tl.x, mp.y-tl.y);
58 if (Main.map.mapView.contains(pos)) {
59 mPosition = Main.map.mapView.getEastNorth(pos.x, pos.y);
60 }
61 }
62 return mPosition;
63 }
64
65 @Override
66 protected void updateEnabledState() {
67 setEnabled(getLayerManager().getEditDataSet() != null && transferHandler.isDataAvailable());
68 }
69
70 @Override
71 public void flavorsChanged(FlavorEvent e) {
72 updateEnabledState();
73 }
74}
Note: See TracBrowser for help on using the repository browser.