1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.datatransfer.importers;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.EnumMap;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import javax.swing.TransferHandler.TransferSupport;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.command.AddPrimitivesCommand;
|
---|
18 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
20 | import org.openstreetmap.josm.data.osm.NodeData;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
22 | import org.openstreetmap.josm.data.osm.PrimitiveData;
|
---|
23 | import org.openstreetmap.josm.data.osm.RelationData;
|
---|
24 | import org.openstreetmap.josm.data.osm.RelationMemberData;
|
---|
25 | import org.openstreetmap.josm.data.osm.WayData;
|
---|
26 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
27 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
28 | import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
|
---|
29 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
30 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
31 | import org.openstreetmap.josm.tools.bugreport.BugReport;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * This transfer support allows us to transfer primitives. This is the default paste action when primitives were copied.
|
---|
35 | * @author Michael Zangl
|
---|
36 | * @since 10604
|
---|
37 | */
|
---|
38 | public final class PrimitiveDataPaster extends AbstractOsmDataPaster {
|
---|
39 | /**
|
---|
40 | * Create a new {@link PrimitiveDataPaster}
|
---|
41 | */
|
---|
42 | public PrimitiveDataPaster() {
|
---|
43 | super(PrimitiveTransferData.DATA_FLAVOR);
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public boolean importData(TransferSupport support, final OsmDataLayer layer, EastNorth pasteAt)
|
---|
48 | throws UnsupportedFlavorException, IOException {
|
---|
49 | PrimitiveTransferData pasteBuffer = (PrimitiveTransferData) support.getTransferable().getTransferData(df);
|
---|
50 | // Allow to cancel paste if there are incomplete primitives
|
---|
51 | if (pasteBuffer.hasIncompleteData() && !confirmDeleteIncomplete()) {
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 |
|
---|
55 | EastNorth center = pasteBuffer.getCenter();
|
---|
56 | EastNorth offset = center == null || pasteAt == null ? new EastNorth(0, 0) : pasteAt.subtract(center);
|
---|
57 |
|
---|
58 | AddPrimitivesCommand command = createNewPrimitives(pasteBuffer, offset, layer);
|
---|
59 |
|
---|
60 | /* Now execute the commands to add the duplicated contents of the paste buffer to the map */
|
---|
61 | UndoRedoHandler.getInstance().add(command);
|
---|
62 | return true;
|
---|
63 | }
|
---|
64 |
|
---|
65 | private static AddPrimitivesCommand createNewPrimitives(PrimitiveTransferData pasteBuffer, EastNorth offset, OsmDataLayer layer) {
|
---|
66 | // Make a copy of pasteBuffer and map from old id to copied data id
|
---|
67 | List<PrimitiveData> bufferCopy = new ArrayList<>();
|
---|
68 | List<PrimitiveData> toSelect = new ArrayList<>();
|
---|
69 | EnumMap<OsmPrimitiveType, Map<Long, Long>> newIds = generateNewPrimitives(pasteBuffer, bufferCopy, toSelect);
|
---|
70 |
|
---|
71 | // Update references in copied buffer
|
---|
72 | for (PrimitiveData data : bufferCopy) {
|
---|
73 | try {
|
---|
74 | if (data instanceof NodeData) {
|
---|
75 | NodeData nodeData = (NodeData) data;
|
---|
76 | nodeData.setEastNorth(nodeData.getEastNorth(ProjectionRegistry.getProjection()).add(offset));
|
---|
77 | } else if (data instanceof WayData) {
|
---|
78 | updateNodes(newIds.get(OsmPrimitiveType.NODE), data);
|
---|
79 | } else if (data instanceof RelationData) {
|
---|
80 | updateMembers(newIds, data);
|
---|
81 | }
|
---|
82 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
|
---|
83 | throw BugReport.intercept(e).put("data", data);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | return new AddPrimitivesCommand(bufferCopy, toSelect, layer.getDataSet());
|
---|
87 | }
|
---|
88 |
|
---|
89 | private static EnumMap<OsmPrimitiveType, Map<Long, Long>> generateNewPrimitives(PrimitiveTransferData pasteBuffer,
|
---|
90 | List<PrimitiveData> bufferCopy, List<PrimitiveData> toSelect) {
|
---|
91 | EnumMap<OsmPrimitiveType, Map<Long, Long>> newIds = new EnumMap<>(OsmPrimitiveType.class);
|
---|
92 | newIds.put(OsmPrimitiveType.NODE, new HashMap<Long, Long>());
|
---|
93 | newIds.put(OsmPrimitiveType.WAY, new HashMap<Long, Long>());
|
---|
94 | newIds.put(OsmPrimitiveType.RELATION, new HashMap<Long, Long>());
|
---|
95 |
|
---|
96 | for (PrimitiveData data : pasteBuffer.getAll()) {
|
---|
97 | if (data.isIncomplete() || !data.isVisible()) {
|
---|
98 | continue;
|
---|
99 | }
|
---|
100 | PrimitiveData copy = data.makeCopy();
|
---|
101 | // don't know why this is reset, but we need it to not crash on copying incomplete nodes.
|
---|
102 | boolean wasIncomplete = copy.isIncomplete();
|
---|
103 | copy.clearOsmMetadata();
|
---|
104 | copy.setIncomplete(wasIncomplete);
|
---|
105 | newIds.get(data.getType()).put(data.getUniqueId(), copy.getUniqueId());
|
---|
106 |
|
---|
107 | bufferCopy.add(copy);
|
---|
108 | if (pasteBuffer.getDirectlyAdded().contains(data)) {
|
---|
109 | toSelect.add(copy);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | return newIds;
|
---|
113 | }
|
---|
114 |
|
---|
115 | private static void updateMembers(EnumMap<OsmPrimitiveType, Map<Long, Long>> newIds, PrimitiveData data) {
|
---|
116 | List<RelationMemberData> newMembers = new ArrayList<>();
|
---|
117 | for (RelationMemberData member : ((RelationData) data).getMembers()) {
|
---|
118 | OsmPrimitiveType memberType = member.getMemberType();
|
---|
119 | Long newId = newIds.get(memberType).get(member.getMemberId());
|
---|
120 | if (newId != null) {
|
---|
121 | newMembers.add(new RelationMemberData(member.getRole(), memberType, newId));
|
---|
122 | }
|
---|
123 | }
|
---|
124 | ((RelationData) data).setMembers(newMembers);
|
---|
125 | }
|
---|
126 |
|
---|
127 | private static void updateNodes(Map<Long, Long> newNodeIds, PrimitiveData data) {
|
---|
128 | List<Long> newNodes = new ArrayList<>();
|
---|
129 | for (Long oldNodeId : ((WayData) data).getNodeIds()) {
|
---|
130 | Long newNodeId = newNodeIds.get(oldNodeId);
|
---|
131 | if (newNodeId != null) {
|
---|
132 | newNodes.add(newNodeId);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | ((WayData) data).setNodeIds(newNodes);
|
---|
136 | }
|
---|
137 |
|
---|
138 | private static boolean confirmDeleteIncomplete() {
|
---|
139 | ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Delete incomplete members?"),
|
---|
140 | tr("Paste without incomplete members"), tr("Cancel"));
|
---|
141 | ed.setButtonIcons("dialogs/relation/deletemembers", "cancel");
|
---|
142 | ed.setContent(tr(
|
---|
143 | "The copied data contains incomplete objects. " + "When pasting the incomplete objects are removed. "
|
---|
144 | + "Do you want to paste the data without the incomplete objects?"));
|
---|
145 | ed.showDialog();
|
---|
146 | return ed.getValue() == 1;
|
---|
147 | }
|
---|
148 | }
|
---|