beginning of vector background layers. And a whole load of other stuff too
[potlatch2.git] / net / systemeD / halcyon / connection / Connection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.net.*;
4
5     import flash.events.EventDispatcher;
6     import flash.events.Event;
7         import net.systemeD.halcyon.Globals;
8
9         public class Connection extends EventDispatcher {
10
11         private static var connectionInstance:Connection = null;
12
13         protected static var policyURL:String;
14         protected static var apiBaseURL:String;
15         protected static var params:Object;
16
17         public static function getConnection(initparams:Object=null):Connection {
18             if ( connectionInstance == null ) {
19             
20                 params = initparams == null ? new Object() : initparams;
21                 policyURL = getParam("policy", "http://127.0.0.1:3000/api/crossdomain.xml");
22                 apiBaseURL = getParam("api", "http://127.0.0.1:3000/api/0.6/");
23                 var connectType:String = getParam("connection", "XML");
24                 
25                 if ( connectType == "XML" )
26                     connectionInstance = new XMLConnection();
27                 else
28                     connectionInstance = new AMFConnection();
29             }
30             return connectionInstance;
31         }
32
33         public static function getParam(name:String, defaultValue:String):String {
34             trace("Returning param "+name+" as "+(params[name] == null ? defaultValue : params[name]));
35             return params[name] == null ? defaultValue : params[name];
36         }
37
38         public static function get apiBase():String {
39             return apiBaseURL;
40         }
41
42         public static function get serverName():String {
43             return getParam("serverName", "Localhost");
44         }
45                 
46                 public static function getConnectionInstance():Connection {
47             return connectionInstance;
48                 }
49
50                 public function getEnvironment(responder:Responder):void {}
51
52         // connection events
53         public static var LOAD_STARTED:String = "load_started";
54         public static var LOAD_COMPLETED:String = "load_completed";
55         public static var SAVE_STARTED:String = "save_started";
56         public static var SAVE_COMPLETED:String = "save_completed";
57         public static var NEW_CHANGESET:String = "new_changeset";
58         public static var NEW_CHANGESET_ERROR:String = "new_changeset_error";
59         public static var NEW_NODE:String = "new_node";
60         public static var NEW_WAY:String = "new_way";
61         public static var NEW_RELATION:String = "new_relation";
62         public static var NEW_POI:String = "new_poi";
63         public static var TAG_CHANGED:String = "tag_change";
64         public static var NODE_MOVED:String = "node_moved";
65         public static var WAY_NODE_ADDED:String = "way_node_added";
66         public static var WAY_NODE_REMOVED:String = "way_node_removed";
67                 public static var NODE_DELETED:String = "node_deleted";
68                 public static var WAY_DELETED:String = "way_deleted";
69                 public static var RELATION_DELETED:String = "relation_deleted";
70                 public static var RELATION_MEMBER_ADDED:String = "relation_member_added";
71                 public static var RELATION_MEMBER_REMOVED:String = "relation_member_deleted";
72                 public static var ADDED_TO_RELATION:String = "added_to_relation";
73                 public static var REMOVED_FROM_RELATION:String = "removed_from_relation";
74                 public static var SUSPEND_REDRAW:String = "suspend_redraw";
75                 public static var RESUME_REDRAW:String = "resume_redraw";
76
77         // store the data we download
78         private var negativeID:Number = -1;
79         private var nodes:Object = {};
80         private var ways:Object = {};
81         private var relations:Object = {};
82         private var pois:Array = [];
83         private var changeset:Changeset = null;
84
85         protected function get nextNegative():Number {
86             return negativeID--;
87         }
88
89         protected function setNode(node:Node,queue:Boolean):void {
90             nodes[node.id] = node;
91             if (node.loaded) { sendEvent(new EntityEvent(NEW_NODE, node),queue); }
92         }
93
94         protected function setWay(way:Way,queue:Boolean):void {
95             ways[way.id] = way;
96             if (way.loaded) { sendEvent(new EntityEvent(NEW_WAY, way),queue); }
97         }
98
99         protected function setRelation(relation:Relation,queue:Boolean):void {
100             relations[relation.id] = relation;
101             if (relation.loaded) { sendEvent(new EntityEvent(NEW_RELATION, relation),queue); }
102         }
103
104         protected function renumberNode(oldID:Number, node:Node):void {
105             nodes[node.id] = node;
106             delete nodes[oldID];
107         }
108
109         protected function renumberWay(oldID:Number, way:Way):void {
110             ways[way.id] = way;
111             delete ways[oldID];
112         }
113
114         protected function renumberRelation(oldID:Number, relation:Relation):void {
115             relations[relation.id] = relation;
116             delete relations[oldID];
117         }
118
119
120                 public function sendEvent(e:*,queue:Boolean):void {
121                         // queue is only used for AMFConnection
122                         dispatchEvent(e);
123                 }
124
125         public function registerPOI(node:Node):void {
126             if ( pois.indexOf(node) < 0 ) {
127                 pois.push(node);
128                 sendEvent(new EntityEvent(NEW_POI, node),false);
129             }
130         }
131
132         public function unregisterPOI(node:Node):void {
133             var index:uint = pois.indexOf(node);
134             if ( index >= 0 ) {
135                 pois.splice(index,1);
136             }
137         }
138
139         protected function setActiveChangeset(changeset:Changeset):void {
140             this.changeset = changeset;
141             sendEvent(new EntityEvent(NEW_CHANGESET, changeset),false);
142         }
143         
144         public function getNode(id:Number):Node {
145             return nodes[id];
146         }
147
148         public function getWay(id:Number):Way {
149             return ways[id];
150         }
151
152         public function getRelation(id:Number):Relation {
153             return relations[id];
154         }
155
156         public function createNode(tags:Object, lat:Number, lon:Number):Node {
157             var node:Node = new Node(nextNegative, 0, tags, true, lat, lon);
158             setNode(node,false);
159             return node;
160         }
161
162         public function createWay(tags:Object, nodes:Array):Way {
163             var way:Way = new Way(nextNegative, 0, tags, true, nodes.concat());
164             setWay(way,false);
165             return way;
166         }
167
168         public function createRelation(tags:Object, members:Array):Relation {
169             var relation:Relation = new Relation(nextNegative, 0, tags, true, members.concat());
170             setRelation(relation,false);
171             return relation;
172         }
173
174         public function getAllNodeIDs():Array {
175             var list:Array = [];
176             for each (var node:Node in nodes)
177                 list.push(node.id);
178             return list;
179         }
180
181         public function getAllWayIDs():Array {
182             var list:Array = [];
183             for each (var way:Way in ways)
184                 list.push(way.id);
185             return list;
186         }
187
188         public function getAllRelationIDs():Array {
189             var list:Array = [];
190             for each (var relation:Relation in relations)
191                 list.push(relation.id);
192             return list;
193         }
194
195         public function getActiveChangeset():Changeset {
196             return changeset;
197         }
198         
199         // these are functions that the Connection implementation is expected to
200         // provide. This class has some generic helpers for the implementation.
201                 public function loadBbox(left:Number, right:Number,
202                                                                 top:Number, bottom:Number):void {
203             }
204             
205             public function setAppID(id:Object):void {}
206             public function setAuthToken(id:Object):void {}
207             public function createChangeset(tags:Object):void {}
208             public function uploadChanges():void {}
209     }
210
211 }
212