fix horrid merge bug (yay!). Implement suspend/resume events for redrawing, to speed...
[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_CHANGED:String = "tag_change";
63         public static var NODE_MOVED:String = "node_moved";
64         public static var WAY_NODE_ADDED:String = "way_node_added";
65         public static var WAY_NODE_REMOVED:String = "way_node_removed";
66                 public static var NODE_DELETED:String = "node_deleted";
67                 public static var WAY_DELETED:String = "way_deleted";
68                 public static var RELATION_DELETED:String = "relation_deleted";
69                 public static var RELATION_MEMBER_ADDED:String = "relation_member_added";
70                 public static var RELATION_MEMBER_REMOVED:String = "relation_member_deleted";
71                 public static var ADDED_TO_RELATION:String = "added_to_relation";
72                 public static var REMOVED_FROM_RELATION:String = "removed_from_relation";
73                 public static var SUSPEND_REDRAW:String = "suspend_redraw";
74                 public static var RESUME_REDRAW:String = "resume_redraw";
75
76         // store the data we download
77         private var negativeID:Number = -1;
78         private var nodes:Object = {};
79         private var ways:Object = {};
80         private var relations:Object = {};
81         private var pois:Array = [];
82         private var changeset:Changeset = null;
83
84         protected function get nextNegative():Number {
85             return negativeID--;
86         }
87
88         protected function setNode(node:Node,queue:Boolean):void {
89             nodes[node.id] = node;
90             if (node.loaded) { sendEvent(new EntityEvent(NEW_NODE, node),queue); }
91         }
92
93         protected function setWay(way:Way,queue:Boolean):void {
94             ways[way.id] = way;
95             if (way.loaded) { sendEvent(new EntityEvent(NEW_WAY, way),queue); }
96         }
97
98         protected function setRelation(relation:Relation,queue:Boolean):void {
99             relations[relation.id] = relation;
100             if (relation.loaded) { sendEvent(new EntityEvent(NEW_RELATION, relation),queue); }
101         }
102
103         protected function renumberNode(oldID:Number, node:Node):void {
104             nodes[node.id] = node;
105             delete nodes[oldID];
106         }
107
108         protected function renumberWay(oldID:Number, way:Way):void {
109             ways[way.id] = way;
110             delete ways[oldID];
111         }
112
113         protected function renumberRelation(oldID:Number, relation:Relation):void {
114             relations[relation.id] = relation;
115             delete relations[oldID];
116         }
117
118
119                 public function sendEvent(e:*,queue:Boolean):void {
120                         // queue is only used for AMFConnection
121                         dispatchEvent(e);
122                 }
123
124         public function registerPOI(node:Node):void {
125             if ( pois.indexOf(node) < 0 ) {
126                 pois.push(node);
127                 sendEvent(new EntityEvent(NEW_POI, node),false);
128             }
129         }
130
131         protected function unregisterPOI(node:Node):void {
132             var index:uint = pois.indexOf(node);
133             if ( index >= 0 ) {
134                 pois.splice(index,1);
135             }
136         }
137
138         protected function setActiveChangeset(changeset:Changeset):void {
139             this.changeset = changeset;
140             sendEvent(new EntityEvent(NEW_CHANGESET, changeset),false);
141         }
142         
143         public function getNode(id:Number):Node {
144             return nodes[id];
145         }
146
147         public function getWay(id:Number):Way {
148             return ways[id];
149         }
150
151         public function getRelation(id:Number):Relation {
152             return relations[id];
153         }
154
155         public function createNode(tags:Object, lat:Number, lon:Number):Node {
156             var node:Node = new Node(nextNegative, 0, tags, true, lat, lon);
157             setNode(node,false);
158             return node;
159         }
160
161         public function createWay(tags:Object, nodes:Array):Way {
162             var way:Way = new Way(nextNegative, 0, tags, true, nodes.concat());
163             setWay(way,false);
164             return way;
165         }
166
167         public function createRelation(tags:Object, members:Array):Relation {
168             var relation:Relation = new Relation(nextNegative, 0, tags, true, members.concat());
169             setRelation(relation,false);
170             return relation;
171         }
172
173         public function getAllNodeIDs():Array {
174             var list:Array = [];
175             for each (var node:Node in nodes)
176                 list.push(node.id);
177             return list;
178         }
179
180         public function getAllWayIDs():Array {
181             var list:Array = [];
182             for each (var way:Way in ways)
183                 list.push(way.id);
184             return list;
185         }
186
187         public function getAllRelationIDs():Array {
188             var list:Array = [];
189             for each (var relation:Relation in relations)
190                 list.push(relation.id);
191             return list;
192         }
193
194         public function getActiveChangeset():Changeset {
195             return changeset;
196         }
197         
198         // these are functions that the Connection implementation is expected to
199         // provide. This class has some generic helpers for the implementation.
200                 public function loadBbox(left:Number, right:Number,
201                                                                 top:Number, bottom:Number):void {
202             }
203             
204             public function setAppID(id:Object):void {}
205             public function setAuthToken(id:Object):void {}
206             public function createChangeset(tags:Object):void {}
207             public function uploadChanges():void {}
208     }
209
210 }
211