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