[10766] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import java.awt.MouseInfo;
|
---|
| 5 | import java.awt.Point;
|
---|
| 6 | import java.awt.datatransfer.FlavorEvent;
|
---|
| 7 | import java.awt.datatransfer.FlavorListener;
|
---|
| 8 | import java.awt.datatransfer.Transferable;
|
---|
| 9 | import java.awt.event.ActionEvent;
|
---|
| 10 |
|
---|
| 11 | import org.openstreetmap.josm.Main;
|
---|
| 12 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
| 13 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
| 14 | import org.openstreetmap.josm.gui.datatransfer.OsmTransferHandler;
|
---|
| 15 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 16 |
|
---|
| 17 | /**
|
---|
| 18 | * This is the base class for all actions that paste objects.
|
---|
| 19 | * @author Michael Zangl
|
---|
| 20 | * @since 10765
|
---|
| 21 | */
|
---|
| 22 | public abstract class AbstractPasteAction extends JosmAction implements FlavorListener {
|
---|
| 23 |
|
---|
| 24 | protected final OsmTransferHandler transferHandler;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * Constructs a new {@link AbstractPasteAction}.
|
---|
| 28 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
| 29 | * @param iconName the filename of the icon to use
|
---|
| 30 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
---|
| 31 | * that html is not supported for menu actions on some platforms.
|
---|
| 32 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
---|
| 33 | * do want a shortcut, remember you can always register it with group=none, so you
|
---|
| 34 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
---|
| 35 | * the user CANNOT configure a shortcut for your action.
|
---|
| 36 | * @param registerInToolbar register this action for the toolbar preferences?
|
---|
| 37 | */
|
---|
| 38 | public AbstractPasteAction(String name, String iconName, String tooltip, Shortcut shortcut,
|
---|
| 39 | boolean registerInToolbar) {
|
---|
| 40 | this(name, iconName, tooltip, shortcut, registerInToolbar, null);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Constructs a new {@link AbstractPasteAction}.
|
---|
| 45 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
| 46 | * @param iconName the filename of the icon to use
|
---|
| 47 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
---|
| 48 | * that html is not supported for menu actions on some platforms.
|
---|
| 49 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
---|
| 50 | * do want a shortcut, remember you can always register it with group=none, so you
|
---|
| 51 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
---|
| 52 | * the user CANNOT configure a shortcut for your action.
|
---|
| 53 | * @param registerInToolbar register this action for the toolbar preferences?
|
---|
| 54 | * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
|
---|
| 55 | */
|
---|
| 56 | public AbstractPasteAction(String name, String iconName, String tooltip, Shortcut shortcut,
|
---|
| 57 | boolean registerInToolbar, String toolbarId) {
|
---|
| 58 | super(name, iconName, tooltip, shortcut, registerInToolbar, toolbarId, true);
|
---|
| 59 | transferHandler = new OsmTransferHandler();
|
---|
| 60 | ClipboardUtils.getClipboard().addFlavorListener(this);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Compute the location the objects should be pasted at.
|
---|
| 65 | * @param e The action event that triggered the paste
|
---|
| 66 | * @return The paste position.
|
---|
| 67 | */
|
---|
| 68 | protected EastNorth computePastePosition(ActionEvent e) {
|
---|
| 69 | // default to paste in center of map (pasted via menu or cursor not in MapView)
|
---|
| 70 | EastNorth mPosition = Main.map.mapView.getCenter();
|
---|
| 71 | // We previously checked for modifier to know if the action has been trigerred via shortcut or via menu
|
---|
| 72 | // But this does not work if the shortcut is changed to a single key (see #9055)
|
---|
| 73 | // Observed behaviour: getActionCommand() returns Action.NAME when triggered via menu, but shortcut text when triggered with it
|
---|
| 74 | if (e != null && !getValue(NAME).equals(e.getActionCommand())) {
|
---|
| 75 | final Point mp = MouseInfo.getPointerInfo().getLocation();
|
---|
| 76 | final Point tl = Main.map.mapView.getLocationOnScreen();
|
---|
| 77 | final Point pos = new Point(mp.x-tl.x, mp.y-tl.y);
|
---|
| 78 | if (Main.map.mapView.contains(pos)) {
|
---|
| 79 | mPosition = Main.map.mapView.getEastNorth(pos.x, pos.y);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | return mPosition;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | @Override
|
---|
| 86 | public void actionPerformed(ActionEvent e) {
|
---|
[11554] | 87 | doPaste(e, ClipboardUtils.getClipboardContent());
|
---|
[10766] | 88 | }
|
---|
| 89 |
|
---|
| 90 | protected void doPaste(ActionEvent e, Transferable contents) {
|
---|
| 91 | transferHandler.pasteOn(Main.getLayerManager().getEditLayer(), computePastePosition(e), contents);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | @Override
|
---|
| 95 | protected void updateEnabledState() {
|
---|
| 96 | setEnabled(getLayerManager().getEditDataSet() != null && transferHandler.isDataAvailable());
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | @Override
|
---|
| 100 | public void flavorsChanged(FlavorEvent e) {
|
---|
| 101 | updateEnabledState();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|