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