Ignore:
Timestamp:
2012-12-26T03:25:48+01:00 (12 years ago)
Author:
donvip
Message:

[josm_utilsplugin2] fix #josm6403 - Unglue relation does not work correctly when multiple ways are selected

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/UnGlueRelationAction.java

    r28028 r29118  
    55import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    66import static org.openstreetmap.josm.tools.I18n.tr;
    7 import static org.openstreetmap.josm.tools.I18n.trn;
    87
     8import java.awt.event.ActionEvent;
    99import java.awt.event.KeyEvent;
    10 import java.awt.event.ActionEvent;
    11 
    1210import java.util.Collection;
    13 import java.util.Collections;
    1411import java.util.LinkedList;
    1512import java.util.List;
     
    1714import org.openstreetmap.josm.Main;
    1815import org.openstreetmap.josm.actions.JosmAction;
    19 import org.openstreetmap.josm.tools.Shortcut;
    2016import org.openstreetmap.josm.command.AddCommand;
    21 import org.openstreetmap.josm.command.ChangeCommand;
    2217import org.openstreetmap.josm.command.Command;
    2318import org.openstreetmap.josm.command.SequenceCommand;
    2419import org.openstreetmap.josm.data.osm.Node;
    2520import org.openstreetmap.josm.data.osm.OsmPrimitive;
     21import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    2622import org.openstreetmap.josm.data.osm.Relation;
    2723import org.openstreetmap.josm.data.osm.RelationMember;
    2824import org.openstreetmap.josm.data.osm.Way;
     25import org.openstreetmap.josm.gui.DefaultNameFormatter;
     26import org.openstreetmap.josm.tools.Shortcut;
    2927
    3028/**
     
    7270                    newPrimitives.add(newp);
    7371                    cmds.add(new AddCommand(newp));
    74                     cmds.add(new ChangeCommand(relation, changeRelationMember(relation, p, newp)));
     72                    cmds.add(new ChangeRelationMemberCommand(relation, p, newp));
    7573                } else {
    7674                    first = false;
     
    9391    /**
    9492     * Change member in relation to another one
    95      * @param relation
    96      * @param orginalMember member to change
    97      * @param newMember
    98      * @return new relation were change is made
    9993     */
    100     private Relation changeRelationMember(Relation relation, OsmPrimitive orginalMember, OsmPrimitive newMember) {
    101         LinkedList<RelationMember> newrms = new LinkedList<RelationMember>();
    102         for (RelationMember rm : relation.getMembers()) {
    103             if (rm.getMember() == orginalMember) {
    104                 newrms.add(new RelationMember(rm.getRole(),newMember));
    105             } else {
    106                 newrms.add(rm);
     94    private class ChangeRelationMemberCommand extends Command {
     95
     96        private final Relation relation;
     97        private final OsmPrimitive oldMember;
     98        private final OsmPrimitive newMember;
     99       
     100        public ChangeRelationMemberCommand(Relation relation, OsmPrimitive oldMember, OsmPrimitive newMember) {
     101            this.relation = relation;
     102            this.oldMember = oldMember;
     103            this.newMember = newMember;
     104        }
     105
     106        private boolean replaceMembers(OsmPrimitive oldP, OsmPrimitive newP) {
     107            if (relation == null || oldMember == null || newMember == null) {
     108                return false;
    107109            }
     110            LinkedList<RelationMember> newrms = new LinkedList<RelationMember>();
     111            for (RelationMember rm : relation.getMembers()) {
     112                if (rm.getMember() == oldP) {
     113                    newrms.add(new RelationMember(rm.getRole(), newP));
     114                } else {
     115                    newrms.add(rm);
     116                }
     117            }
     118            relation.setMembers(newrms);
     119            return true;
    108120        }
    109         Relation newRelation  = new Relation(relation);
    110         newRelation.setMembers(newrms);
    111         return newRelation;
     121       
     122        @Override
     123        public boolean executeCommand() {
     124            return replaceMembers(oldMember, newMember);
     125        }
     126
     127        @Override
     128        public void fillModifiedData(Collection<OsmPrimitive> modified,
     129                Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
     130            modified.add(relation);
     131        }
     132
     133        @Override
     134        public void undoCommand() {
     135            replaceMembers(newMember, oldMember);
     136        }
     137
     138        @Override
     139        public String getDescriptionText() {
     140            return tr("Change relation member for {0} {1}",
     141                    OsmPrimitiveType.from(relation),
     142                    relation.getDisplayName(DefaultNameFormatter.getInstance()));
     143        }
    112144    }
    113145
Note: See TracChangeset for help on using the changeset viewer.