add node drawing + dragging & highlight selected way using stylesheet
[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 connectionInstance:Connection = null;
11
12         protected static var policyURL:String;
13         protected static var apiBaseURL:String;
14         protected static var params:Object;
15
16         public static function getConnection(initparams:Object=null):Connection {
17             if ( connectionInstance == null ) {
18             
19                 params = initparams == null ? new Object() : initparams;
20                 policyURL = getParam("policy", "http://127.0.0.1:3000/api/crossdomain.xml");
21                 apiBaseURL = getParam("api", "http://127.0.0.1:3000/api/0.6/");
22                 var connectType:String = getParam("connection", "XML");
23                 
24                 if ( connectType == "XML" )
25                     connectionInstance = new XMLConnection();
26                 else
27                     connectionInstance = new AMFConnection();
28             }
29             return connectionInstance;
30         }
31
32         public static function getParam(name:String, defaultValue:String):String {
33             trace("Returning param "+name+" as "+(params[name] == null ? defaultValue : params[name]));
34             return params[name] == null ? defaultValue : params[name];
35         }
36
37         public static function get apiBase():String {
38             return apiBaseURL;
39         }
40
41         public static function get serverName():String {
42             return getParam("serverName", "Localhost");
43         }
44                 
45                 public static function getConnectionInstance():Connection {
46             return connectionInstance;
47                 }
48
49                 public function getEnvironment(responder:Responder):void {}
50
51         // connection events
52         public static var LOAD_STARTED:String = "load_started";
53         public static var LOAD_COMPLETED:String = "load_completed";
54         public static var SAVE_STARTED:String = "save_started";
55         public static var SAVE_COMPLETED:String = "save_completed";
56         public static var NEW_CHANGESET:String = "new_changeset";
57         public static var NEW_CHANGESET_ERROR:String = "new_changeset_error";
58         public static var NEW_NODE:String = "new_node";
59         public static var NEW_WAY:String = "new_way";
60         public static var NEW_RELATION:String = "new_relation";
61         public static var NEW_POI:String = "new_poi";
62         public static var TAG_CHANGE:String = "tag_change";
63         public static var NODE_MOVED:String = "node_moved";
64
65         // store the data we download
66         private var negativeID:Number = -1;
67         private var nodes:Object = {};
68         private var ways:Object = {};
69         private var relations:Object = {};
70         private var pois:Array = [];
71         private var changeset:Changeset = null;
72
73         protected function get nextNegative():Number {
74             return negativeID--;
75         }
76
77         protected function setNode(node:Node):void {
78             nodes[node.id] = node;
79             dispatchEvent(new EntityEvent(NEW_NODE, node));
80         }
81
82         protected function setWay(way:Way):void {
83             ways[way.id] = way;
84             dispatchEvent(new EntityEvent(NEW_WAY, way));
85         }
86
87         protected function setRelation(relation:Relation):void {
88             relations[relation.id] = relation;
89             dispatchEvent(new EntityEvent(NEW_RELATION, relation));
90         }
91
92         protected function registerPOI(node:Node):void {
93             if ( pois.indexOf(node) < 0 ) {
94                 pois.push(node);
95                 dispatchEvent(new EntityEvent(NEW_POI, node));
96             }
97         }
98
99         protected function unregisterPOI(node:Node):void {
100             var index:uint = pois.indexOf(node);
101             if ( index >= 0 ) {
102                 pois.splice(index,1);
103             }
104         }
105
106         protected function setActiveChangeset(changeset:Changeset):void {
107             this.changeset = changeset;
108             dispatchEvent(new EntityEvent(NEW_CHANGESET, changeset));
109         }
110         
111         public function getNode(id:Number):Node {
112             return nodes[id];
113         }
114
115         public function getWay(id:Number):Way {
116             return ways[id];
117         }
118
119         public function getRelation(id:Number):Relation {
120             return relations[id];
121         }
122
123         public function createNode(tags:Object, lat:Number, lon:Number):Node {
124             var node:Node = new Node(nextNegative, 0, tags, lat, lon);
125             setNode(node);
126             return node;
127         }
128
129         public function createWay(tags:Object, nodes:Array):Way {
130             var way:Way = new Way(nextNegative, 0, tags, nodes.concat());
131             setWay(way);
132             return way;
133         }
134
135         public function createRelation(tags:Object, members:Array):Relation {
136             var relation:Relation = new Relation(nextNegative, 0, tags, members.concat());
137             setRelation(relation);
138             return relation;
139         }
140
141         public function getAllNodeIDs():Array {
142             var list:Array = [];
143             for each (var node:Node in nodes)
144                 list.push(node.id);
145             return list;
146         }
147
148         public function getAllWayIDs():Array {
149             var list:Array = [];
150             for each (var way:Way in ways)
151                 list.push(way.id);
152             return list;
153         }
154
155         public function getAllRelationIDs():Array {
156             var list:Array = [];
157             for each (var relation:Relation in relations)
158                 list.push(relation.id);
159             return list;
160         }
161
162         public function getActiveChangeset():Changeset {
163             return changeset;
164         }
165         
166         // these are functions that the Connection implementation is expected to
167         // provide. This class has some generic helpers for the implementation.
168                 public function loadBbox(left:Number, right:Number,
169                                                                 top:Number, bottom:Number):void {
170             }
171             
172             public function setAppID(id:Object):void {}
173             public function setAuthToken(id:Object):void {}
174             public function createChangeset(tags:Object):void {}
175             public function uploadChanges():void {}
176     }
177
178 }
179