source: josm/trunk/src/org/openstreetmap/josm/actions/PasteAction.java @ 5241

Revision 5096, 7.5 KB checked in by simon04, 2 months ago (diff)

fix #7517 - NPE when pasting relation with incomplete node members to new layer

  • Property svn:eol-style set to native
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2// Author: David Earl
3package org.openstreetmap.josm.actions;
4
5import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.MouseInfo;
9import java.awt.Point;
10import java.awt.event.ActionEvent;
11import java.awt.event.KeyEvent;
12import java.util.ArrayList;
13import java.util.HashMap;
14import java.util.List;
15import java.util.Map;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.command.AddPrimitivesCommand;
19import org.openstreetmap.josm.data.coor.EastNorth;
20import org.openstreetmap.josm.data.osm.NodeData;
21import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
22import org.openstreetmap.josm.data.osm.PrimitiveData;
23import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
24import org.openstreetmap.josm.data.osm.RelationData;
25import org.openstreetmap.josm.data.osm.RelationMemberData;
26import org.openstreetmap.josm.data.osm.WayData;
27import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy.PasteBufferChangedListener;
28import org.openstreetmap.josm.gui.ExtendedDialog;
29import org.openstreetmap.josm.gui.layer.Layer;
30import org.openstreetmap.josm.tools.Shortcut;
31
32public final class PasteAction extends JosmAction implements PasteBufferChangedListener {
33
34    public PasteAction() {
35        super(tr("Paste"), "paste", tr("Paste contents of paste buffer."),
36                Shortcut.registerShortcut("system:paste", tr("Edit: {0}", tr("Paste")), KeyEvent.VK_V, Shortcut.CTRL), true);
37        putValue("help", ht("/Action/Paste"));
38        Main.pasteBuffer.addPasteBufferChangedListener(this);
39    }
40
41    public void actionPerformed(ActionEvent e) {
42        if (!isEnabled())
43            return;
44        pasteData(Main.pasteBuffer, Main.pasteSource, e);
45    }
46
47    public  void pasteData(PrimitiveDeepCopy pasteBuffer, Layer source, ActionEvent e) {
48        /* Find the middle of the pasteBuffer area */
49        double maxEast = -1E100, minEast = 1E100, maxNorth = -1E100, minNorth = 1E100;
50        boolean incomplete = false;
51        for (PrimitiveData data : pasteBuffer.getAll()) {
52            if (data instanceof NodeData) {
53                NodeData n = (NodeData)data;
54                if (n.getEastNorth() != null) {
55                    double east = n.getEastNorth().east();
56                    double north = n.getEastNorth().north();
57                    if (east > maxEast) { maxEast = east; }
58                    if (east < minEast) { minEast = east; }
59                    if (north > maxNorth) { maxNorth = north; }
60                    if (north < minNorth) { minNorth = north; }
61                }
62            }
63            if (data.isIncomplete()) {
64                incomplete = true;
65            }
66        }
67
68        // Allow to cancel paste if there are incomplete primitives
69        if (incomplete) {
70            if (!confirmDeleteIncomplete()) return;
71        }
72
73        // default to paste in center of map (pasted via menu or cursor not in MapView)
74        EastNorth mPosition = Main.map.mapView.getCenter();
75        if((e.getModifiers() & ActionEvent.CTRL_MASK) != 0) {
76            final Point mp = MouseInfo.getPointerInfo().getLocation();
77            final Point tl = Main.map.mapView.getLocationOnScreen();
78            final Point pos = new Point(mp.x-tl.x, mp.y-tl.y);
79            if(Main.map.mapView.contains(pos)) {
80                mPosition = Main.map.mapView.getEastNorth(pos.x, pos.y);
81            }
82        }
83
84        double offsetEast  = mPosition.east() - (maxEast + minEast)/2.0;
85        double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0;
86
87        // Make a copy of pasteBuffer and map from old id to copied data id
88        List<PrimitiveData> bufferCopy = new ArrayList<PrimitiveData>();
89        Map<Long, Long> newNodeIds = new HashMap<Long, Long>();
90        Map<Long, Long> newWayIds = new HashMap<Long, Long>();
91        Map<Long, Long> newRelationIds = new HashMap<Long, Long>();
92        for (PrimitiveData data: pasteBuffer.getAll()) {
93            if (data.isIncomplete()) {
94                continue;
95            }
96            PrimitiveData copy = data.makeCopy();
97            copy.clearOsmId();
98            if (data instanceof NodeData) {
99                newNodeIds.put(data.getUniqueId(), copy.getUniqueId());
100            } else if (data instanceof WayData) {
101                newWayIds.put(data.getUniqueId(), copy.getUniqueId());
102            } else if (data instanceof RelationData) {
103                newRelationIds.put(data.getUniqueId(), copy.getUniqueId());
104            }
105            bufferCopy.add(copy);
106        }
107
108        // Update references in copied buffer
109        for (PrimitiveData data:bufferCopy) {
110            if (data instanceof NodeData) {
111                NodeData nodeData = (NodeData)data;
112                if (Main.map.mapView.getEditLayer() == source) {
113                    nodeData.setEastNorth(nodeData.getEastNorth().add(offsetEast, offsetNorth));
114                }
115            } else if (data instanceof WayData) {
116                List<Long> newNodes = new ArrayList<Long>();
117                for (Long oldNodeId: ((WayData)data).getNodes()) {
118                    Long newNodeId = newNodeIds.get(oldNodeId);
119                    if (newNodeId != null) {
120                        newNodes.add(newNodeId);
121                    }
122                }
123                ((WayData)data).setNodes(newNodes);
124            } else if (data instanceof RelationData) {
125                List<RelationMemberData> newMembers = new ArrayList<RelationMemberData>();
126                for (RelationMemberData member: ((RelationData)data).getMembers()) {
127                    OsmPrimitiveType memberType = member.getMemberType();
128                    Long newId = null;
129                    switch (memberType) {
130                    case NODE:
131                        newId = newNodeIds.get(member.getMemberId());
132                        break;
133                    case WAY:
134                        newId = newWayIds.get(member.getMemberId());
135                        break;
136                    case RELATION:
137                        newId = newRelationIds.get(member.getMemberId());
138                        break;
139                    }
140                    if (newId != null) {
141                        newMembers.add(new RelationMemberData(member.getRole(), memberType, newId));
142                    }
143                }
144                ((RelationData)data).setMembers(newMembers);
145            }
146        }
147
148        /* Now execute the commands to add the duplicated contents of the paste buffer to the map */
149
150        Main.main.undoRedo.add(new AddPrimitivesCommand(bufferCopy));
151        Main.map.mapView.repaint();
152    }
153
154    protected boolean confirmDeleteIncomplete() {
155        ExtendedDialog ed = new ExtendedDialog(Main.parent,
156                tr("Delete incomplete members?"),
157                new String[] {tr("Paste without incomplete members"), tr("Cancel")});
158        ed.setButtonIcons(new String[] {"dialogs/relation/deletemembers.png", "cancel.png"});
159        ed.setContent(tr("The copied data contains incomplete objects.  "
160                + "When pasting the incomplete objects are removed.  "
161                + "Do you want to paste the data without the incomplete objects?"));
162        ed.showDialog();
163        return ed.getValue() == 1;
164    }
165
166    @Override
167    protected void updateEnabledState() {
168        if (getCurrentDataSet() == null || Main.pasteBuffer == null) {
169            setEnabled(false);
170            return;
171        }
172        setEnabled(!Main.pasteBuffer.isEmpty());
173    }
174
175    @Override
176    public void pasteBufferChanged(PrimitiveDeepCopy pasteBuffer) {
177        updateEnabledState();
178    }
179}
Note: See TracBrowser for help on using the repository browser.