first stage of undo/redo functionality -- there are still a number of actions that...
[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 insertMember(index:uint, member:RelationMember):void {
64             members.splice(index, 0, member);
65                         member.entity.addParent(this);
66                         markDirty();
67                         
68                         dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_ADDED, member.entity, this, index));
69         }
70
71         public function appendMember(member:RelationMember):uint {
72             members.push(member);
73                         member.entity.addParent(this);
74                         markDirty();
75
76                         dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_ADDED, member.entity, this, members.length-1));
77             return members.length;
78         }
79
80                 public function removeMember(entity:Entity, performAction:Function):void {
81                         performAction(new RemoveEntityFromRelationAction(this, entity, members));
82                 }
83
84         public function removeMemberByIndex(index:uint):void {
85             var removed:Array=members.splice(index, 1);
86                         var entity:Entity=removed[0].entity;
87                         
88                         // only remove as parent if this was only reference
89                         if (findEntityMemberIndex(entity)==-1)
90                             entity.removeParent(this);
91                             
92                         markDirty();
93             dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_REMOVED, entity, this, index));
94         }
95
96                 public override function remove(performAction:Function):void {
97                         performAction(new DeleteRelationAction(this, setDeletedState, members));
98                 }
99
100                 internal override function isEmpty():Boolean {
101                         return (deleted || (members.length==0));
102                 }
103
104         public function getDescription():String {
105             var desc:String = "";
106             var relTags:Object = getTagsHash();
107             if ( relTags["type"] ) {
108                 desc = relTags["type"];
109                 if ( relTags[desc] )
110                     desc += " " + relTags[desc];
111             }
112             if ( relTags["ref"] )
113                 desc += " " + relTags["ref"];
114             if ( relTags["name"] )
115                 desc += " " + relTags["name"];
116             return desc;
117         }
118
119                 public override function getType():String {
120                         return 'relation';
121                 }
122                 
123                 public override function toString():String {
124             return "Relation("+id+"@"+version+"): "+members.length+" members "+getTagList();
125         }
126
127     }
128
129 }