source: josm/trunk/src/org/openstreetmap/josm/actions/AbstractPasteAction.java@ 11113

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

fix #13290 - pasting from other layer changes objects position (modified patch by michael2402) - gsoc-core

  • Property svn:eol-style set to native
File size: 4.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import java.awt.MouseInfo;
5import java.awt.Point;
6import java.awt.datatransfer.FlavorEvent;
7import java.awt.datatransfer.FlavorListener;
8import java.awt.datatransfer.Transferable;
9import java.awt.event.ActionEvent;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.coor.EastNorth;
13import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
14import org.openstreetmap.josm.gui.datatransfer.OsmTransferHandler;
15import 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 */
22public 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) {
87 doPaste(e, ClipboardUtils.getClipboard().getContents(null));
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}
Note: See TracBrowser for help on using the repository browser.