generic parent object handling
[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 parents:Dictionary = new Dictionary();
12
13         public function Entity(id:Number, version:uint, tags:Object) {
14             this._id = id;
15             this._version = version;
16             this.tags = tags;
17             modified = id < 0;
18         }
19
20         public function get id():Number {
21             return _id;
22         }
23
24         public function get version():uint {
25             return _version;
26         }
27
28                 // Tag-handling methods
29
30         public function hasTags():Boolean {
31             for (var key:String in tags)
32                 return true;
33             return false;
34         }
35
36         public function getTag(key:String):String {
37             return tags[key];
38         }
39
40         public function setTag(key:String, value:String):void {
41             var old:String = tags[key];
42             if ( old != value ) {
43                 if ( value == null || value == "" )
44                     delete tags[key];
45                 else
46                     tags[key] = value;
47                 markDirty();
48                 dispatchEvent(new TagEvent(Connection.TAG_CHANGE, this, key, key, old, value));
49             }
50         }
51
52         public function renameTag(oldKey:String, newKey:String):void {
53             var value:String = tags[oldKey];
54             if ( oldKey != newKey ) {
55                 delete tags[oldKey];
56                 tags[newKey] = value;
57                 markDirty();
58                 dispatchEvent(new TagEvent(Connection.TAG_CHANGE, this, oldKey, newKey, value, value));
59             }
60         }
61
62         public function getTagList():TagList {
63             return new TagList(tags);
64         }
65
66         public function getTagsCopy():Object {
67             var copy:Object = {};
68             for (var key:String in tags )
69                 copy[key] = tags[key];
70             return copy;
71         }
72
73                 public function getTagsHash():Object {
74                         // hm, not sure we should be doing this, but for read-only purposes
75                         // it's faster than using getTagsCopy
76                         return tags;
77                 }
78
79         public function getTagArray():Array {
80             var copy:Array = [];
81             for (var key:String in tags )
82                 copy.push(new Tag(this, key, tags[key]));
83             return copy;
84         }
85
86                 // Clean/dirty methods
87
88         public function get isDirty():Boolean {
89             return modified;
90         }
91
92         public function markClean(newID:Number, newVersion:uint):void {
93             this._id = newID;
94             this._version = newVersion;
95             modified = false;
96         }
97
98         protected function markDirty():void {
99             modified = true;
100         }
101
102                 // Parent handling
103                 
104                 public function addParent(parent:Entity):void {
105                         parents[parent]=true;
106                 }
107
108                 public function removeParent(parent:Entity):void {
109                         delete parents[parent];
110                 }
111                 
112                 public function get parentWays():Array {
113                         var a:Array=[];
114                         for (var o:Object in parents) {
115                                 if (o is Way) { a.push(o); }
116                         }
117                         return a;
118                 }
119                 
120                 public function get parentRelations():Array {
121                         var a:Array=[];
122                         for (var o:Object in parents) {
123                                 if (o is Relation) { a.push(o); }
124                         }
125                         return a;
126                 }
127                 
128                 public function get parentObjects():Array {
129                         var a:Array=[];
130                         for (var o:Object in parents) { a.push(o); }
131                         return a;
132                 }
133
134                 // To be overridden
135
136         public function getType():String {
137             return '';
138         }
139
140     }
141
142 }