fix horrid merge bug (yay!). Implement suspend/resume events for redrawing, to speed...
[potlatch2.git] / net / systemeD / halcyon / connection / Entity.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.EventDispatcher;
4     import flash.utils.Dictionary;
5
6     public class Entity extends EventDispatcher {
7         private var _id:Number;
8         private var _version:uint;
9         private var tags:Object = {};
10         private var modified:Boolean = false;
11                 private var _loaded:Boolean = true;
12                 private var parents:Dictionary = new Dictionary();
13                 private var locked:Boolean = false;
14                 protected var deleted:Boolean = false;
15
16         public function Entity(id:Number, version:uint, tags:Object, loaded:Boolean) {
17             this._id = id;
18             this._version = version;
19             this.tags = tags;
20                         this._loaded = loaded;
21             modified = id < 0;
22         }
23
24         public function get id():Number {
25             return _id;
26         }
27
28         public function get version():uint {
29             return _version;
30         }
31
32         public function get loaded():Boolean {
33             return _loaded;
34         }
35
36                 public function updateEntityProperties(version:uint, tags:Object, loaded:Boolean):void {
37                         _version=version; this.tags=tags; _loaded=loaded;
38                 }
39
40                 // Tag-handling methods
41
42         public function hasTags():Boolean {
43             for (var key:String in tags)
44                 return true;
45             return false;
46         }
47
48                 // ** we could do with hasInterestingTags - don't bother with source, created_by, any TIGER tags, etc.
49
50         public function getTag(key:String):String {
51             return tags[key];
52         }
53
54         public function setTag(key:String, value:String):void {
55             var old:String = tags[key];
56             if ( old != value ) {
57                 if ( value == null || value == "" )
58                     delete tags[key];
59                 else
60                     tags[key] = value;
61                 markDirty();
62                 dispatchEvent(new TagEvent(Connection.TAG_CHANGED, this, key, key, old, value));
63             }
64         }
65
66         public function renameTag(oldKey:String, newKey:String):void {
67             var value:String = tags[oldKey];
68             if ( oldKey != newKey ) {
69                 delete tags[oldKey];
70                 tags[newKey] = value;
71                 markDirty();
72                 dispatchEvent(new TagEvent(Connection.TAG_CHANGED, this, oldKey, newKey, value, value));
73             }
74         }
75
76         public function getTagList():TagList {
77             return new TagList(tags);
78         }
79
80         public function getTagsCopy():Object {
81             var copy:Object = {};
82             for (var key:String in tags )
83                 copy[key] = tags[key];
84             return copy;
85         }
86
87                 public function getTagsHash():Object {
88                         // hm, not sure we should be doing this, but for read-only purposes
89                         // it's faster than using getTagsCopy
90                         return tags;
91                 }
92
93         public function getTagArray():Array {
94             var copy:Array = [];
95             for (var key:String in tags )
96                 copy.push(new Tag(this, key, tags[key]));
97             return copy;
98         }
99
100                 // Clean/dirty methods
101
102         public function get isDirty():Boolean {
103             return modified;
104         }
105
106         public function markClean(newID:Number, newVersion:uint):void {
107             this._id = newID;
108             this._version = newVersion;
109             modified = false;
110         }
111
112         protected function markDirty():void {
113             modified = true;
114         }
115
116                 // Delete entity
117                 
118                 public function remove():void {
119                         // to be overridden
120                 }
121                 
122                 internal function isEmpty():Boolean {
123                         return false;   // to be overridden
124                 }
125
126                 protected function removeFromParents():void {
127                         for (var o:Object in parents) {
128                                 if (o is Relation) { Relation(o).removeMember(this); }
129                                 else if (o is Way) { Way(o).removeNode(Node(this)); }
130                                 if (o.isEmpty()) { o.remove(); }
131                         }
132                 }
133
134                 // Parent handling
135                 
136                 public function addParent(parent:Entity):void {
137                         parents[parent]=true;
138                         
139                         if ( parent is Relation )
140                             dispatchEvent(new RelationMemberEvent(Connection.ADDED_TO_RELATION, this, parent as Relation, -1));
141                 }
142
143                 public function removeParent(parent:Entity):void {
144                         delete parents[parent];
145
146                         if ( parent is Relation )
147                             dispatchEvent(new RelationMemberEvent(Connection.REMOVED_FROM_RELATION, this, parent as Relation, -1));
148                 }
149                 
150                 public function get parentWays():Array {
151                         var a:Array=[];
152                         for (var o:Object in parents) {
153                                 if (o is Way) { a.push(o); }
154                         }
155                         return a;
156                 }
157
158                 public function get hasParents():Boolean {
159                         for (var o:Object in parents) { return true; }
160                         return false;
161                 }
162                 
163                 public function get hasParentWays():Boolean {
164                         for (var o:Object in parents) {
165                                 if (o is Way) { return true; }
166                         }
167                         return false;
168                 }
169                 
170                 public function get numParentWays():uint {
171                         var i:uint=0;
172                         for (var o:Object in parents) {
173                                 if (o is Way) { i++; }
174                         }
175                         return i;
176                 }
177                 
178                 public function get parentRelations():Array {
179                         var a:Array=[];
180                         for (var o:Object in parents) {
181                                 if (o is Relation) { a.push(o); }
182                         }
183                         return a;
184                 }
185                 
186                 public function get parentObjects():Array {
187                         var a:Array=[];
188                         for (var o:Object in parents) { a.push(o); }
189                         return a;
190                 }
191                 
192                 public function hasParent(entity:Entity):Boolean {
193             return parents[entity] == true;
194         }
195
196                 // Resume/suspend redraw
197                 
198                 public function suspend():void {
199                         dispatchEvent(new EntityEvent(Connection.SUSPEND_REDRAW, this));
200                 }
201                 
202                 public function resume():void {
203                         dispatchEvent(new EntityEvent(Connection.RESUME_REDRAW, this));
204                 }
205
206                 // To be overridden
207
208         public function getType():String {
209             return '';
210         }
211
212     }
213
214 }