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

Last change on this file since 12891 was 12636, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

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