fix some bugs in feature selector, and awesome stuff
[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 NEW_NODE:String = "new_node";
42         public static var NEW_WAY:String = "new_way";
43         public static var NEW_RELATION:String = "new_relation";
44         public static var NEW_POI:String = "new_poi";
45         public static var TAG_CHANGE:String = "tag_change";
46
47         // store the data we download
48         private var negativeID:Number = -1;
49         private var nodes:Object = {};
50         private var ways:Object = {};
51         private var relations:Object = {};
52         private var pois:Array = [];
53
54         protected function get nextNegative():Number {
55             return negativeID--;
56         }
57
58         protected function setNode(node:Node):void {
59             nodes[node.id] = node;
60             dispatchEvent(new EntityEvent(NEW_NODE, node));
61         }
62
63         protected function setWay(way:Way):void {
64             ways[way.id] = way;
65             dispatchEvent(new EntityEvent(NEW_WAY, way));
66         }
67
68         protected function setRelation(relation:Relation):void {
69             relations[relation.id] = relation;
70             dispatchEvent(new EntityEvent(NEW_RELATION, relation));
71         }
72
73         protected function registerPOI(node:Node):void {
74             if ( pois.indexOf(node) < 0 ) {
75                 pois.push(node);
76                 dispatchEvent(new EntityEvent(NEW_POI, node));
77             }
78         }
79
80         protected function unregisterPOI(node:Node):void {
81             var index:uint = pois.indexOf(node);
82             if ( index >= 0 ) {
83                 pois.splice(index,1);
84             }
85         }
86
87         public function getNode(id:Number):Node {
88             return nodes[id];
89         }
90
91         public function getWay(id:Number):Way {
92             return ways[id];
93         }
94
95         public function getRelation(id:Number):Relation {
96             return relations[id];
97         }
98
99         public function createNode(tags:Object, lat:Number, lon:Number):Node {
100             var node:Node = new Node(nextNegative, 0, tags, lat, lon);
101             setNode(node);
102             return node;
103         }
104
105         public function createWay(tags:Object, nodes:Array):Way {
106             var way:Way = new Way(nextNegative, 0, tags, nodes.concat());
107             setWay(way);
108             return way;
109         }
110
111         public function createRelation(tags:Object, members:Array):Relation {
112             var relation:Relation = new Relation(nextNegative, 0, tags, members.concat());
113             setRelation(relation);
114             return relation;
115         }
116
117         public function getAllNodeIDs():Array {
118             var list:Array = [];
119             for each (var node:Node in nodes)
120                 list.push(node.id);
121             return list;
122         }
123
124         public function getAllWayIDs():Array {
125             var list:Array = [];
126             for each (var way:Way in ways)
127                 list.push(way.id);
128             return list;
129         }
130
131         public function getAllRelationIDs():Array {
132             var list:Array = [];
133             for each (var relation:Relation in relations)
134                 list.push(relation.id);
135             return list;
136         }
137
138         // these are functions that the Connection implementation is expected to
139         // provide. This class has some generic helpers for the implementation.
140                 public function loadBbox(left:Number, right:Number,
141                                                                 top:Number, bottom:Number):void {
142             }
143     }
144
145 }
146