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