Merge branch 'master' into history
[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         private var node:Node;
9         private 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
23             for each (var way:Way in node.parentWays) {
24               for (var x:uint=0; x<way.length; x++) {
25                 if (way.getNode(x) == node) {
26                   way.removeNodeByIndex(x, push);
27                   way.insertNode(x, replacement, push);
28                 }
29               }
30             }
31
32             for each (var relation:Relation in node.parentRelations) {
33               for (var y:uint=0; y<relation.length; y++) {
34                 var member:RelationMember = relation.getMember(y);
35                 if (member.entity == node) {
36                   relation.removeMemberByIndex(y, push);
37                   relation.insertMember(y, new RelationMember(replacement, member.role), push);
38                 }
39               }
40             }
41
42             node.remove(push);
43
44             return super.doAction();
45         }
46
47         public override function undoAction():uint {
48             return super.undoAction();
49         }
50     }
51 }
52