nice zoom buttons and a loading message
[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
8         public class Connection extends EventDispatcher {
9
10         private static var CONNECTION_TYPE:String = "XML";
11         private static var connectionInstance:Connection = null;
12
13         protected static var policyURL:String = "http://127.0.0.1:3000/api/crossdomain.xml";
14         protected static var apiBaseURL:String = "http://127.0.0.1:3000/api/0.6/";
15
16         public static function getConnection(api:String,policy:String,conn:String):Connection {
17                         
18                         if ( policy != null )
19                             policyURL=policy;
20                         if ( api != null )
21                             apiBaseURL=api;
22                         if ( conn != null )
23                             CONNECTION_TYPE=conn;
24                         
25             if ( connectionInstance == null ) {
26                 if ( CONNECTION_TYPE == "XML" )
27                     connectionInstance = new XMLConnection();
28                 else
29                     connectionInstance = new AMFConnection();
30             }
31             return connectionInstance;
32         }
33
34                 public static function getConnectionInstance():Connection {
35             return connectionInstance;
36                 }
37
38                 public function getEnvironment(responder:Responder):void {}
39
40         // connection events
41         public static var LOAD_STARTED:String = "load_started";
42         public static var LOAD_COMPLETED:String = "load_completed";
43         public static var SAVE_STARTED:String = "save_started";
44         public static var SAVE_COMPLETED:String = "save_completed";
45         public static var NEW_NODE:String = "new_node";
46         public static var NEW_WAY:String = "new_way";
47         public static var NEW_RELATION:String = "new_relation";
48         public static var NEW_POI:String = "new_poi";
49         public static var TAG_CHANGE:String = "tag_change";
50
51         // store the data we download
52         private var negativeID:Number = -1;
53         private var nodes:Object = {};
54         private var ways:Object = {};
55         private var relations:Object = {};
56         private var pois:Array = [];
57
58         protected function get nextNegative():Number {
59             return negativeID--;
60         }
61
62         protected function setNode(node:Node):void {
63             nodes[node.id] = node;
64             dispatchEvent(new EntityEvent(NEW_NODE, node));
65         }
66
67         protected function setWay(way:Way):void {
68             ways[way.id] = way;
69             dispatchEvent(new EntityEvent(NEW_WAY, way));
70         }
71
72         protected function setRelation(relation:Relation):void {
73             relations[relation.id] = relation;
74             dispatchEvent(new EntityEvent(NEW_RELATION, relation));
75         }
76
77         protected function registerPOI(node:Node):void {
78             if ( pois.indexOf(node) < 0 ) {
79                 pois.push(node);
80                 dispatchEvent(new EntityEvent(NEW_POI, node));
81             }
82         }
83
84         protected function unregisterPOI(node:Node):void {
85             var index:uint = pois.indexOf(node);
86             if ( index >= 0 ) {
87                 pois.splice(index,1);
88             }
89         }
90
91         public function getNode(id:Number):Node {
92             return nodes[id];
93         }
94
95         public function getWay(id:Number):Way {
96             return ways[id];
97         }
98
99         public function getRelation(id:Number):Relation {
100             return relations[id];
101         }
102
103         public function createNode(tags:Object, lat:Number, lon:Number):Node {
104             var node:Node = new Node(nextNegative, 0, tags, lat, lon);
105             setNode(node);
106             return node;
107         }
108
109         public function createWay(tags:Object, nodes:Array):Way {
110             var way:Way = new Way(nextNegative, 0, tags, nodes.concat());
111             setWay(way);
112             return way;
113         }
114
115         public function createRelation(tags:Object, members:Array):Relation {
116             var relation:Relation = new Relation(nextNegative, 0, tags, members.concat());
117             setRelation(relation);
118             return relation;
119         }
120
121         public function getAllNodeIDs():Array {
122             var list:Array = [];
123             for each (var node:Node in nodes)
124                 list.push(node.id);
125             return list;
126         }
127
128         public function getAllWayIDs():Array {
129             var list:Array = [];
130             for each (var way:Way in ways)
131                 list.push(way.id);
132             return list;
133         }
134
135         public function getAllRelationIDs():Array {
136             var list:Array = [];
137             for each (var relation:Relation in relations)
138                 list.push(relation.id);
139             return list;
140         }
141
142         // these are functions that the Connection implementation is expected to
143         // provide. This class has some generic helpers for the implementation.
144                 public function loadBbox(left:Number, right:Number,
145                                                                 top:Number, bottom:Number):void {
146             }
147     }
148
149 }
150