Ignore:
Timestamp:
2010-01-11T21:25:29+01:00 (14 years ago)
Author:
mjulius
Message:

don't paste incomplete members
When relations with incomplete members have been copied offer the user the choice between cancel and pasting without the incomplete members.

File:
1 edited

Legend:

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

    r2512 r2818  
    33package org.openstreetmap.josm.actions;
    44
     5import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    56import static org.openstreetmap.josm.tools.I18n.tr;
    6 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    77
    88import java.awt.event.ActionEvent;
     
    1111import java.util.HashMap;
    1212import java.util.List;
    13 import java.util.ListIterator;
    1413import java.util.Map;
    1514
     
    1817import org.openstreetmap.josm.data.coor.EastNorth;
    1918import org.openstreetmap.josm.data.osm.NodeData;
     19import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    2020import org.openstreetmap.josm.data.osm.PrimitiveData;
    2121import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
     
    2323import org.openstreetmap.josm.data.osm.RelationMemberData;
    2424import org.openstreetmap.josm.data.osm.WayData;
     25import org.openstreetmap.josm.gui.ExtendedDialog;
    2526import org.openstreetmap.josm.gui.layer.Layer;
    2627import org.openstreetmap.josm.tools.Shortcut;
     
    4344        /* Find the middle of the pasteBuffer area */
    4445        double maxEast = -1E100, minEast = 1E100, maxNorth = -1E100, minNorth = 1E100;
     46        boolean incomplete = false;
    4547        for (PrimitiveData data : pasteBuffer.getAll()) {
    4648            if (data instanceof NodeData) {
     
    5355                if (north < minNorth) { minNorth = north; }
    5456            }
     57            if (data.isIncomplete()) {
     58                incomplete = true;
     59            }
     60        }
     61
     62        // Allow to cancel paste if there are incomplete primitives
     63        if (incomplete) {
     64            if (!confirmDeleteIncomplete()) return;
    5565        }
    5666
     
    7282        // Make a copy of pasteBuffer and map from old id to copied data id
    7383        List<PrimitiveData> bufferCopy = new ArrayList<PrimitiveData>();
    74         Map<Long, Long> newIds = new HashMap<Long, Long>();
    75         for (PrimitiveData data:pasteBuffer.getAll()) {
     84        Map<Long, Long> newNodeIds = new HashMap<Long, Long>();
     85        Map<Long, Long> newWayIds = new HashMap<Long, Long>();
     86        Map<Long, Long> newRelationIds = new HashMap<Long, Long>();
     87        for (PrimitiveData data: pasteBuffer.getAll()) {
     88            if (data.isIncomplete()) {
     89                continue;
     90            }
    7691            PrimitiveData copy = data.makeCopy();
    7792            copy.clearOsmId();
    78             newIds.put(data.getId(), copy.getId());
     93            if (data instanceof NodeData) {
     94                newNodeIds.put(data.getId(), copy.getId());
     95            } else if (data instanceof WayData) {
     96                newWayIds.put(data.getId(), copy.getId());
     97            } else if (data instanceof RelationData) {
     98                newRelationIds.put(data.getId(), copy.getId());
     99            }
    79100            bufferCopy.add(copy);
    80101        }
     
    88109                }
    89110            } else if (data instanceof WayData) {
    90                 ListIterator<Long> it = ((WayData)data).getNodes().listIterator();
    91                 while (it.hasNext()) {
    92                     it.set(newIds.get(it.next()));
     111                List<Long> newNodes = new ArrayList<Long>();
     112                for (Long oldNodeId: ((WayData)data).getNodes()) {
     113                    Long newNodeId = newNodeIds.get(oldNodeId);
     114                    if (newNodeId != null) {
     115                        newNodes.add(newNodeId);
     116                    }
    93117                }
     118                ((WayData)data).setNodes(newNodes);
    94119            } else if (data instanceof RelationData) {
    95                 ListIterator<RelationMemberData> it = ((RelationData)data).getMembers().listIterator();
    96                 while (it.hasNext()) {
    97                     RelationMemberData member = it.next();
    98                     it.set(new RelationMemberData(member.getRole(), member.getMemberType(), newIds.get(member.getMemberId())));
     120                List<RelationMemberData> newMembers = new ArrayList<RelationMemberData>();
     121                for (RelationMemberData member: ((RelationData)data).getMembers()) {
     122                    OsmPrimitiveType memberType = member.getMemberType();
     123                    Long newId = null;
     124                    switch (memberType) {
     125                    case NODE:
     126                        newId = newNodeIds.get(member.getMemberId());
     127                        break;
     128                    case WAY:
     129                        newId = newWayIds.get(member.getMemberId());
     130                        break;
     131                    case RELATION:
     132                        newId = newRelationIds.get(member.getMemberId());
     133                        break;
     134                    }
     135                    if (newId != null) {
     136                        newMembers.add(new RelationMemberData(member.getRole(), memberType, newId));
     137                    }
    99138                }
     139                ((RelationData)data).setMembers(newMembers);
    100140            }
    101141        }
     
    105145        Main.main.undoRedo.add(new AddPrimitivesCommand(bufferCopy));
    106146        Main.map.mapView.repaint();
     147    }
     148
     149    protected boolean confirmDeleteIncomplete() {
     150        ExtendedDialog ed = new ExtendedDialog(Main.parent,
     151                tr("Delete incomplete members?"),
     152                new String[] {tr("Paste without incomplete members"), tr("Cancel")});
     153        ed.setButtonIcons(new String[] {"dialogs/relation/deletemembers.png", "cancel.png"});
     154        ed.setContent(tr("The copied data contains incomplete primitives.  "
     155                + "When pasting the incomplete primitives are removed.  "
     156                + "Do you want to paste the data without the incomplete primitives?"));
     157        ed.showDialog();
     158        return ed.getValue() == 1;
    107159    }
    108160
Note: See TracChangeset for help on using the changeset viewer.