delete objects (possibly not complete)
[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 getMember(index:uint):RelationMember {
34             return members[index];
35         }
36
37         public function setMember(index:uint, member:RelationMember):void {
38                         member.entity.addParent(this);
39                         members.splice(index, 1, member);
40                         markDirty();
41         }
42
43         public function insertMember(index:uint, member:RelationMember):void {
44                         member.entity.addParent(this);
45             members.splice(index, 0, member);
46                         markDirty();
47         }
48
49         public function appendMember(member:RelationMember):uint {
50                         member.entity.addParent(this);
51             members.push(member);
52                         markDirty();
53             return members.length;
54         }
55
56                 public function removeMember(entity:Entity):void {
57                         var i:int;
58                         while ((i=findEntityMemberIndex(entity))>-1) {
59                                 members.splice(i, 1);
60                         }
61                         entity.removeParent(this);
62                         markDirty();
63                 }
64
65         public function removeMemberByIndex(index:uint):void {
66             var removed:Array=members.splice(index, 1);
67                         var entity:Entity=removed[0].entity;
68                         if (findEntityMemberIndex(entity)==-1) { entity.removeParent(this); }
69                         markDirty();
70         }
71
72                 public override function remove():void {
73                         removeFromParents();
74                         for each (var member:RelationMember in members) { member.entity.removeParent(this); }
75                         members=[];
76                         deleted=true;
77             dispatchEvent(new EntityEvent(Connection.RELATION_DELETED, this));
78                 }
79
80                 internal override function isEmpty():Boolean {
81                         return (deleted || (members.length==0));
82                 }
83
84         public function getDescription():String {
85             var desc:String = "";
86             var relTags:Object = getTagsHash();
87             if ( relTags["type"] ) {
88                 desc = relTags["type"];
89                 if ( relTags[desc] )
90                     desc += " " + relTags[desc];
91             }
92             if ( relTags["ref"] )
93                 desc += " " + relTags["ref"];
94             if ( relTags["name"] )
95                 desc += " " + relTags["name"];
96             return desc;
97         }
98
99                 public override function getType():String {
100                         return 'relation';
101                 }
102                 
103                 public override function toString():String {
104             return "Relation("+id+"@"+version+"): "+members.length+" members "+getTagList();
105         }
106
107     }
108
109 }