Ignore:
Timestamp:
2009-10-24T21:22:49+02:00 (15 years ago)
Author:
jttt
Message:

Use PrimitiveData for Copy, Paste and Paste tags actions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/PasteAction.java

    r2070 r2305  
    88import java.awt.event.KeyEvent;
    99import java.util.ArrayList;
    10 import java.util.Collection;
    1110import java.util.HashMap;
    12 import java.util.LinkedList;
    1311import java.util.List;
     12import java.util.ListIterator;
     13import java.util.Map;
    1414
    1515import 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;
     16import org.openstreetmap.josm.command.AddPrimitivesCommand;
    1917import 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;
     18import org.openstreetmap.josm.data.osm.NodeData;
     19import org.openstreetmap.josm.data.osm.PrimitiveData;
     20import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
     21import org.openstreetmap.josm.data.osm.RelationData;
     22import org.openstreetmap.josm.data.osm.RelationMemberData;
     23import org.openstreetmap.josm.data.osm.WayData;
    2624import org.openstreetmap.josm.gui.layer.Layer;
    2725import org.openstreetmap.josm.tools.Shortcut;
     
    4038    }
    4139
    42     public  void pasteData(DataSet pasteBuffer, Layer source, ActionEvent e) {
     40    public  void pasteData(PrimitiveDeepCopy pasteBuffer, Layer source, ActionEvent e) {
    4341        /* Find the middle of the pasteBuffer area */
    4442        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            }
    5253        }
    5354
     
    6364        double offsetNorth = mPosition.north() - (maxNorth + minNorth)/2.0;
    6465
    65         HashMap<OsmPrimitive,OsmPrimitive> map = new HashMap<OsmPrimitive,OsmPrimitive>();
    66         /* temporarily maps old nodes to new so we can do a true deep copy */
    6766
    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);
    7676        }
    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())));
    9995                }
    10096            }
    101             rnew.setMembers(members);
    102             map.put(r, rnew);
    10397        }
    10498
    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 */
    111100
    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);
    114103        Main.map.mapView.repaint();
    115104    }
     
    121110            return;
    122111        }
    123         setEnabled(
    124                 !Main.pasteBuffer.nodes.isEmpty()
    125                 || !Main.pasteBuffer.ways.isEmpty()
    126                 || !Main.pasteBuffer.relations.isEmpty()
    127         );
     112        setEnabled(!Main.pasteBuffer.isEmpty());
    128113    }
    129114}
Note: See TracChangeset for help on using the changeset viewer.