forward & backward in route roles
[potlatch2.git] / net / systemeD / halcyon / connection / Relation.as
1 package net.systemeD.halcyon.connection {
2
3     public class Relation extends Entity {
4         private var members:Array;
5                 public static var entity_type:String = 'relation';
6
7         public function Relation(id:Number, version:uint, tags:Object, loaded:Boolean, members:Array) {
8             super(id, version, tags, loaded);
9             this.members = members;
10                         for each (var member:RelationMember in members) { member.entity.addParent(this); }
11         }
12
13         public function update(version:uint, tags:Object, loaded:Boolean, members:Array):void {
14                         var member:RelationMember;
15                         for each (member in this.members) { member.entity.removeParent(this); }
16                         updateEntityProperties(version,tags,loaded); this.members=members;
17                         for each (member in members) { member.entity.addParent(this); }
18                 }
19                 
20         public function get length():uint {
21             return members.length;
22         }
23
24         public function findEntityMemberIndex(entity:Entity):int {
25             for (var index:uint = 0; index < members.length; index++) {
26                 var member:RelationMember = members[index];
27                 if ( member.entity == entity )
28                     return index;
29             }
30             return -1;
31         }
32
33         public function findEntityMemberIndexes(entity:Entity):Array {
34             var indexes:Array = [];
35             for (var index:uint = 0; index < members.length; index++) {
36                 var member:RelationMember = members[index];
37                 if ( member.entity == entity )
38                     indexes.push(index);
39             }
40             return indexes;
41         }
42         
43         public function getMember(index:uint):RelationMember {
44             return members[index];
45         }
46
47         public function setMember(index:uint, member:RelationMember):void {
48             var oldMember:RelationMember = getMember(index);
49             oldMember.entity.removeParent(this);
50             
51                         member.entity.addParent(this);
52                         members.splice(index, 1, member);
53                         markDirty();
54         }
55
56         public function insertMember(index:uint, member:RelationMember):void {
57                         member.entity.addParent(this);
58             members.splice(index, 0, member);
59                         markDirty();
60         }
61
62         public function appendMember(member:RelationMember):uint {
63                         member.entity.addParent(this);
64             members.push(member);
65                         markDirty();
66             return members.length;
67         }
68
69                 public function removeMember(entity:Entity):void {
70                         var i:int;
71                         while ((i=findEntityMemberIndex(entity))>-1) {
72                                 members.splice(i, 1);
73                         }
74                         entity.removeParent(this);
75                         markDirty();
76                 }
77
78         public function removeMemberByIndex(index:uint):void {
79             var removed:Array=members.splice(index, 1);
80                         var entity:Entity=removed[0].entity;
81                         if (findEntityMemberIndex(entity)==-1) { entity.removeParent(this); }
82                         markDirty();
83         }
84
85                 public override function remove():void {
86                         removeFromParents();
87                         for each (var member:RelationMember in members) { member.entity.removeParent(this); }
88                         members=[];
89                         deleted=true;
90             dispatchEvent(new EntityEvent(Connection.RELATION_DELETED, this));
91                 }
92
93                 internal override function isEmpty():Boolean {
94                         return (deleted || (members.length==0));
95                 }
96
97         public function getDescription():String {
98             var desc:String = "";
99             var relTags:Object = getTagsHash();
100             if ( relTags["type"] ) {
101                 desc = relTags["type"];
102                 if ( relTags[desc] )
103                     desc += " " + relTags[desc];
104             }
105             if ( relTags["ref"] )
106                 desc += " " + relTags["ref"];
107             if ( relTags["name"] )
108                 desc += " " + relTags["name"];
109             return desc;
110         }
111
112                 public override function getType():String {
113                         return 'relation';
114                 }
115                 
116                 public override function toString():String {
117             return "Relation("+id+"@"+version+"): "+members.length+" members "+getTagList();
118         }
119
120     }
121
122 }