1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | // Author: David Earl
|
---|
3 | package org.openstreetmap.josm.actions;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
7 |
|
---|
8 | import java.awt.MouseInfo;
|
---|
9 | import java.awt.Point;
|
---|
10 | import java.awt.datatransfer.FlavorEvent;
|
---|
11 | import java.awt.datatransfer.FlavorListener;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
17 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
18 | import org.openstreetmap.josm.gui.datatransfer.OsmTransferHandler;
|
---|
19 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Paste OSM primitives from clipboard to the current edit layer.
|
---|
23 | * @since 404
|
---|
24 | */
|
---|
25 | public 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 | // default to paste in center of map (pasted via menu or cursor not in MapView)
|
---|
46 | EastNorth mPosition = Main.map.mapView.getCenter();
|
---|
47 | // We previously checked for modifier to know if the action has been trigerred via shortcut or via menu
|
---|
48 | // But this does not work if the shortcut is changed to a single key (see #9055)
|
---|
49 | // Observed behaviour: getActionCommand() returns Action.NAME when triggered via menu, but shortcut text when triggered with it
|
---|
50 | if (e != null && !getValue(NAME).equals(e.getActionCommand())) {
|
---|
51 | final Point mp = MouseInfo.getPointerInfo().getLocation();
|
---|
52 | final Point tl = Main.map.mapView.getLocationOnScreen();
|
---|
53 | final Point pos = new Point(mp.x-tl.x, mp.y-tl.y);
|
---|
54 | if (Main.map.mapView.contains(pos)) {
|
---|
55 | mPosition = Main.map.mapView.getEastNorth(pos.x, pos.y);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | transferHandler.pasteOn(Main.getLayerManager().getEditLayer(), mPosition);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | protected void updateEnabledState() {
|
---|
64 | setEnabled(getLayerManager().getEditDataSet() != null && transferHandler.isDataAvailable());
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public void flavorsChanged(FlavorEvent e) {
|
---|
69 | updateEnabledState();
|
---|
70 | }
|
---|
71 | }
|
---|