Changeset 2305 in josm for trunk/src/org/openstreetmap/josm/actions/PasteAction.java
- Timestamp:
- 2009-10-24T21:22:49+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/PasteAction.java
r2070 r2305 8 8 import java.awt.event.KeyEvent; 9 9 import java.util.ArrayList; 10 import java.util.Collection;11 10 import java.util.HashMap; 12 import java.util.LinkedList;13 11 import java.util.List; 12 import java.util.ListIterator; 13 import java.util.Map; 14 14 15 15 import org.openstreetmap.josm.Main; 16 import org.openstreetmap.josm.command.AddCommand; 17 import org.openstreetmap.josm.command.Command; 18 import org.openstreetmap.josm.command.SequenceCommand; 16 import org.openstreetmap.josm.command.AddPrimitivesCommand; 19 17 import org.openstreetmap.josm.data.coor.EastNorth; 20 import org.openstreetmap.josm.data.osm. DataSet;21 import org.openstreetmap.josm.data.osm. Node;22 import org.openstreetmap.josm.data.osm. OsmPrimitive;23 import org.openstreetmap.josm.data.osm.Relation ;24 import org.openstreetmap.josm.data.osm.RelationMember ;25 import org.openstreetmap.josm.data.osm.Way ;18 import org.openstreetmap.josm.data.osm.NodeData; 19 import org.openstreetmap.josm.data.osm.PrimitiveData; 20 import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy; 21 import org.openstreetmap.josm.data.osm.RelationData; 22 import org.openstreetmap.josm.data.osm.RelationMemberData; 23 import org.openstreetmap.josm.data.osm.WayData; 26 24 import org.openstreetmap.josm.gui.layer.Layer; 27 25 import org.openstreetmap.josm.tools.Shortcut; … … 40 38 } 41 39 42 public void pasteData( DataSetpasteBuffer, Layer source, ActionEvent e) {40 public void pasteData(PrimitiveDeepCopy pasteBuffer, Layer source, ActionEvent e) { 43 41 /* Find the middle of the pasteBuffer area */ 44 42 double maxEast = -1E100, minEast = 1E100, maxNorth = -1E100, minNorth = 1E100; 45 for (Node n : pasteBuffer.nodes) { 46 double east = n.getEastNorth().east(); 47 double north = n.getEastNorth().north(); 48 if (east > maxEast) { maxEast = east; } 49 if (east < minEast) { minEast = east; } 50 if (north > maxNorth) { maxNorth = north; } 51 if (north < minNorth) { minNorth = north; } 43 for (PrimitiveData data : pasteBuffer.getAll()) { 44 if (data instanceof NodeData) { 45 NodeData n = (NodeData)data; 46 double east = n.getEastNorth().east(); 47 double north = n.getEastNorth().north(); 48 if (east > maxEast) { maxEast = east; } 49 if (east < minEast) { minEast = east; } 50 if (north > maxNorth) { maxNorth = north; } 51 if (north < minNorth) { minNorth = north; } 52 } 52 53 } 53 54 … … 63 64 double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0; 64 65 65 HashMap<OsmPrimitive,OsmPrimitive> map = new HashMap<OsmPrimitive,OsmPrimitive>();66 /* temporarily maps old nodes to new so we can do a true deep copy */67 66 68 /* do the deep copy of the paste buffer contents, leaving the pasteBuffer unchanged */ 69 for (Node n : pasteBuffer.nodes) { 70 Node nnew = new Node(n); 71 nnew.clearOsmId(); 72 if (Main.map.mapView.getEditLayer() == source) { 73 nnew.setEastNorth(nnew.getEastNorth().add(offsetEast, offsetNorth)); 74 } 75 map.put(n, nnew); 67 68 // Make a copy of pasteBuffer and map from old id to copied data id 69 List<PrimitiveData> bufferCopy = new ArrayList<PrimitiveData>(); 70 Map<Long, Long> newIds = new HashMap<Long, Long>(); 71 for (PrimitiveData data:pasteBuffer.getAll()) { 72 PrimitiveData copy = data.makeCopy(); 73 copy.clearOsmId(); 74 newIds.put(data.getId(), copy.getId()); 75 bufferCopy.add(copy); 76 76 } 77 for (Way w : pasteBuffer.ways) { 78 Way wnew = new Way(); 79 wnew.cloneFrom(w); 80 wnew.clearOsmId(); 81 /* make sure we reference the new nodes corresponding to the old ones */ 82 List<Node> nodes = new ArrayList<Node>(); 83 for (Node n : w.getNodes()) { 84 nodes.add((Node)map.get(n)); 85 } 86 wnew.setNodes(nodes); 87 map.put(w, wnew); 88 } 89 for (Relation r : pasteBuffer.relations) { 90 Relation rnew = new Relation(r); 91 r.clearOsmId(); 92 List<RelationMember> members = new ArrayList<RelationMember>(); 93 for (RelationMember m : r.getMembers()) { 94 OsmPrimitive mo = map.get(m.getMember()); 95 if(mo != null) /* FIXME - This only prevents illegal data, but kills the relation */ 96 { 97 RelationMember mnew = new RelationMember(m.getRole(), map.get(m.getMember())); 98 members.add(mnew); 77 78 // Update references in copied buffer 79 for (PrimitiveData data:bufferCopy) { 80 if (data instanceof NodeData) { 81 NodeData nodeData = (NodeData)data; 82 if (Main.map.mapView.getEditLayer() == source) { 83 nodeData.setEastNorth(nodeData.getEastNorth().add(offsetEast, offsetNorth)); 84 } 85 } else if (data instanceof WayData) { 86 ListIterator<Long> it = ((WayData)data).getNodes().listIterator(); 87 while (it.hasNext()) { 88 it.set(newIds.get(it.next())); 89 } 90 } else if (data instanceof RelationData) { 91 ListIterator<RelationMemberData> it = ((RelationData)data).getMembers().listIterator(); 92 while (it.hasNext()) { 93 RelationMemberData member = it.next(); 94 it.set(new RelationMemberData(member.getRole(), member.getMemberType(), newIds.get(member.getMemberId()))); 99 95 } 100 96 } 101 rnew.setMembers(members);102 map.put(r, rnew);103 97 } 104 98 105 /* Now execute the commands to add the dupicated contents of the paste buffer to the map */ 106 Collection<OsmPrimitive> osms = map.values(); 107 Collection<Command> clist = new LinkedList<Command>(); 108 for (OsmPrimitive osm : osms) { 109 clist.add(new AddCommand(osm)); 110 } 99 /* Now execute the commands to add the duplicated contents of the paste buffer to the map */ 111 100 112 Main.main.undoRedo.add(new SequenceCommand(tr("Paste"), clist));113 getCurrentDataSet().setSelected(osms);101 Main.main.undoRedo.add(new AddPrimitivesCommand(bufferCopy)); 102 //getCurrentDataSet().setSelected(osms); 114 103 Main.map.mapView.repaint(); 115 104 } … … 121 110 return; 122 111 } 123 setEnabled( 124 !Main.pasteBuffer.nodes.isEmpty() 125 || !Main.pasteBuffer.ways.isEmpty() 126 || !Main.pasteBuffer.relations.isEmpty() 127 ); 112 setEnabled(!Main.pasteBuffer.isEmpty()); 128 113 } 129 114 }
Note:
See TracChangeset
for help on using the changeset viewer.