Merge branch 'master' of github.com:systemed/potlatch2
[potlatch2.git] / net / systemeD / halcyon / connection / actions / ReplaceNodeAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4
5     /** Action that substitutes one node instead of another, in all the ways and relations that that node is part of. */
6     public class ReplaceNodeAction extends CompositeUndoableAction {
7
8         protected var node:Node;
9         public var replacement:Node;
10
11         /**
12         * @param node The node we're getting rid of
13         * @param replacement The node we want to end up with
14         */
15         public function ReplaceNodeAction(node:Node, replacement:Node) {
16             super("Replace node "+node+" with "+replacement);
17             this.node = node;
18             this.replacement = replacement;
19         }
20
21         public override function doAction():uint {
22             for each (var way:Way in node.parentWays) {
23               for (var x:uint=0; x<way.length; x++) {
24                 if (way.getNode(x) == node) {
25                   way.removeNodeByIndex(x, push);
26                   way.insertNode(x, replacement, push);
27                 }
28               }
29             }
30
31             for each (var relation:Relation in node.parentRelations) {
32               for (var y:uint=0; y<relation.length; y++) {
33                 var member:RelationMember = relation.getMember(y);
34                 if (member.entity == node) {
35                   relation.removeMemberByIndex(y, push);
36                   relation.insertMember(y, new RelationMember(replacement, member.role), push);
37                 }
38               }
39             }
40
41             node.remove(push);
42
43             return super.doAction();
44         }
45
46         public override function undoAction():uint {
47             return super.undoAction();
48         }
49     }
50 }
51