width can be defined based on maximum width so far
[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_CHANGE, 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_CHANGE, 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
140                 public function removeParent(parent:Entity):void {
141                         delete parents[parent];
142                 }
143                 
144                 public function get parentWays():Array {
145                         var a:Array=[];
146                         for (var o:Object in parents) {
147                                 if (o is Way) { a.push(o); }
148                         }
149                         return a;
150                 }
151
152                 public function get hasParents():Boolean {
153                         for (var o:Object in parents) { return true; }
154                         return false;
155                 }
156                 
157                 public function get hasParentWays():Boolean {
158                         for (var o:Object in parents) {
159                                 if (o is Way) { return true; }
160                         }
161                         return false;
162                 }
163                 
164                 public function get parentRelations():Array {
165                         var a:Array=[];
166                         for (var o:Object in parents) {
167                                 if (o is Relation) { a.push(o); }
168                         }
169                         return a;
170                 }
171                 
172                 public function get parentObjects():Array {
173                         var a:Array=[];
174                         for (var o:Object in parents) { a.push(o); }
175                         return a;
176                 }
177                 
178                 public function hasParent(entity:Entity):Boolean {
179             return parents[entity] == true;
180         }
181         
182                 // To be overridden
183
184         public function getType():String {
185             return '';
186         }
187
188     }
189
190 }