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

Last change on this file since 13334 was 13151, checked in by Don-vip, 6 years ago

fix #15585 - NPE

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