add undo for creating stuff
[potlatch2.git] / net / systemeD / halcyon / connection / actions / MergeWaysAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4     
5     public class MergeWaysAction extends CompositeUndoableAction {
6         private var way1:Way;
7         private var way2:Way;
8         private var toPos:uint;
9         private var fromPos:uint;
10     
11         public function MergeWaysAction(way1:Way, way2:Way, toPos:uint, fromPos:uint) {
12             super("Merge ways "+way1.id+" "+way2.id);
13             this.way1 = way1;
14             this.way2 = way2;
15             this.toPos = toPos;
16             this.fromPos = fromPos;
17         }
18         
19         public override function doAction():uint {
20             // subactions are added to the composite list -- we then
21             // execute them all at the bottom. Doing it this way gives
22             // us an automatic undo
23             
24                         way1.suspend();
25
26             mergeRelations();
27                 mergeTags();
28                 mergeNodes();
29                         way2.remove(push);
30
31             super.doAction();
32                         way1.resume();
33             
34             return SUCCESS;
35         }
36
37         public override function undoAction():uint {
38             way1.suspend();
39             super.undoAction();
40             way1.resume();
41             
42             return SUCCESS;
43         }
44         
45         public function mergeRelations():void {
46                         for each (var r:Relation in way2.parentRelations) {
47                                 // ** needs to copy roles as well
48                                 if (r.findEntityMemberIndex(way1)==-1) {
49                                         r.appendMember(new RelationMember(way1, ''));
50                                 }
51                         }
52         }
53         
54         public function mergeTags():void {
55                 var way1Tags:Object = way1.getTagsHash();
56                         var way2Tags:Object = way2.getTagsHash();
57                         for (var k:String in way2Tags) {
58                             var v1:String = way1Tags[k];
59                             var v2:String = way2Tags[k];
60                                 if ( v1 && v1 != v2) {
61                                         way1.setTag(k, v1+"; "+v2, push);
62                                         // ** send a warning about tags not matching
63                                 } else {
64                                         way1.setTag(k, v2, push);
65                                 }
66                         }
67         }
68         
69         public function mergeNodes():void {
70             var i:int;
71                 if (fromPos==0) {
72                     for (i = 0; i < way2.length; i++)
73                         way1.addToEnd(toPos, way2.getNode(i), push);
74                 } else {
75                     for (i = way2.length-1; i >= 0; i--)
76                         way1.addToEnd(toPos, way2.getNode(i), push);
77                 }
78         }   
79     }
80 }
81