Merge branch 'master' of github.com:systemed/potlatch2
[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         // ** FIXME: not at all elegant having these stuffed in static variables; we should probably use events instead
11         static public var lastTagsMerged:Boolean;
12         static public var lastRelationsMerged:Boolean;
13     
14         public function MergeWaysAction(way1:Way, way2:Way, toPos:uint, fromPos:uint) {
15             super("Merge ways "+way1.id+" "+way2.id);
16             this.way1 = way1;
17             this.way2 = way2;
18             this.toPos = toPos;
19             this.fromPos = fromPos;
20             lastTagsMerged = false;
21             lastRelationsMerged = false;
22         }
23         
24         public override function doAction():uint {
25             // subactions are added to the composite list -- we then
26             // execute them all at the bottom. Doing it this way gives
27             // us an automatic undo
28             super.clearActions();
29             way1.suspend();
30
31             mergeRelations();
32             lastTagsMerged = way1.mergeTags(way2,push);
33                 mergeNodes();
34                         way2.remove(push);
35
36             super.doAction();
37                         way1.resume();
38             
39             return SUCCESS;
40         }
41
42         public override function undoAction():uint {
43             way1.suspend();
44             super.undoAction();
45             way1.resume();
46             
47             return SUCCESS;
48         }
49         
50         public function mergeRelations():void {
51                         for each (var r:Relation in way2.parentRelations) {
52                                 // ** needs to copy roles as well
53                                 if (r.findEntityMemberIndex(way1)==-1) {
54                                         r.appendMember(new RelationMember(way1, ''), push);
55                                         lastRelationsMerged=true;
56                                 }
57                         }
58         }
59         
60         
61         public function mergeNodes():void {
62             var i:int;
63                 if (fromPos==0) {
64                     for (i = 0; i < way2.length; i++)
65                         way1.addToEnd(toPos, way2.getNode(i), push);
66                 } else {
67                     for (i = way2.length-1; i >= 0; i--)
68                         way1.addToEnd(toPos, way2.getNode(i), push);
69                 }
70         }   
71     }
72 }
73