update toolbox icons depending on what's selected (and add the useful W keypress)
[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         import net.systemeD.halcyon.Globals;
8         import net.systemeD.halcyon.connection.actions.*;
9
10         public class Connection extends EventDispatcher {
11
12         private static var connectionInstance:Connection = null;
13
14         protected static var policyURL:String;
15         protected static var apiBaseURL:String;
16         protected static var params:Object;
17
18         public static function getConnection(initparams:Object=null):Connection {
19             if ( connectionInstance == null ) {
20             
21                 params = initparams == null ? new Object() : initparams;
22                 policyURL = getParam("policy", "http://127.0.0.1:3000/api/crossdomain.xml");
23                 apiBaseURL = getParam("api", "http://127.0.0.1:3000/api/0.6/");
24                 var connectType:String = getParam("connection", "XML");
25                 
26                 if ( connectType == "XML" )
27                     connectionInstance = new XMLConnection();
28                 else
29                     connectionInstance = new AMFConnection();
30             }
31             return connectionInstance;
32         }
33
34         public static function getParam(name:String, defaultValue:String):String {
35             trace("Returning param "+name+" as "+(params[name] == null ? defaultValue : params[name]));
36             return params[name] == null ? defaultValue : params[name];
37         }
38
39         public static function get apiBase():String {
40             return apiBaseURL;
41         }
42
43         public static function get serverName():String {
44             return getParam("serverName", "Localhost");
45         }
46                 
47                 public static function getConnectionInstance():Connection {
48             return connectionInstance;
49                 }
50
51                 public function getEnvironment(responder:Responder):void {}
52
53         // connection events
54         public static var LOAD_STARTED:String = "load_started";
55         public static var LOAD_COMPLETED:String = "load_completed";
56         public static var SAVE_STARTED:String = "save_started";
57         public static var SAVE_COMPLETED:String = "save_completed";
58         public static var NEW_CHANGESET:String = "new_changeset";
59         public static var NEW_CHANGESET_ERROR:String = "new_changeset_error";
60         public static var NEW_NODE:String = "new_node";
61         public static var NEW_WAY:String = "new_way";
62         public static var NEW_RELATION:String = "new_relation";
63         public static var NEW_POI:String = "new_poi";
64         public static var NODE_RENUMBERED:String = "node_renumbered";
65         public static var WAY_RENUMBERED:String = "way_renumbered";
66         public static var RELATION_RENUMBERED:String = "relation_renumbered";
67         public static var TAG_CHANGED:String = "tag_change";
68         public static var NODE_MOVED:String = "node_moved";
69         public static var WAY_NODE_ADDED:String = "way_node_added";
70         public static var WAY_NODE_REMOVED:String = "way_node_removed";
71         public static var WAY_REORDERED:String = "way_reordered";
72         public static var WAY_DRAGGED:String = "way_dragged";
73                 public static var NODE_DELETED:String = "node_deleted";
74                 public static var WAY_DELETED:String = "way_deleted";
75                 public static var RELATION_DELETED:String = "relation_deleted";
76                 public static var RELATION_MEMBER_ADDED:String = "relation_member_added";
77                 public static var RELATION_MEMBER_REMOVED:String = "relation_member_deleted";
78                 public static var ADDED_TO_RELATION:String = "added_to_relation";
79                 public static var REMOVED_FROM_RELATION:String = "removed_from_relation";
80                 public static var SUSPEND_REDRAW:String = "suspend_redraw";
81                 public static var RESUME_REDRAW:String = "resume_redraw";
82
83         // store the data we download
84         private var negativeID:Number = -1;
85         private var nodes:Object = {};
86         private var ways:Object = {};
87         private var relations:Object = {};
88         private var pois:Array = [];
89         private var changeset:Changeset = null;
90                 public var nodecount:int=0;
91                 public var waycount:int=0;
92                 public var relationcount:int=0;
93
94         protected function get nextNegative():Number {
95             return negativeID--;
96         }
97
98         protected function setNode(node:Node, queue:Boolean):void {
99                         if (!nodes[node.id]) { nodecount++; }
100             nodes[node.id] = node;
101             if (node.loaded) { sendEvent(new EntityEvent(NEW_NODE, node),queue); }
102         }
103
104         protected function setWay(way:Way, queue:Boolean):void {
105                         if (!ways[way.id]) { waycount++; }
106             ways[way.id] = way;
107             if (way.loaded) { sendEvent(new EntityEvent(NEW_WAY, way),queue); }
108         }
109
110         protected function setRelation(relation:Relation, queue:Boolean):void {
111                         if (!relations[relation.id]) { relationcount++; }
112             relations[relation.id] = relation;
113             if (relation.loaded) { sendEvent(new EntityEvent(NEW_RELATION, relation),queue); }
114         }
115
116         protected function renumberNode(oldID:Number, node:Node, queue:Boolean):void {
117             nodes[node.id] = node;
118             if (node.loaded) { sendEvent(new EntityRenumberedEvent(NODE_RENUMBERED, node, oldID),queue); }
119             delete nodes[oldID];
120         }
121
122         protected function renumberWay(oldID:Number, way:Way, queue:Boolean):void {
123             ways[way.id] = way;
124             if (way.loaded) { sendEvent(new EntityRenumberedEvent(WAY_RENUMBERED, way, oldID),queue); }
125             delete ways[oldID];
126         }
127
128         protected function renumberRelation(oldID:Number, relation:Relation, queue:Boolean):void {
129             relations[relation.id] = relation;
130             if (relation.loaded) { sendEvent(new EntityRenumberedEvent(RELATION_RENUMBERED, relation, oldID),queue); }
131             delete relations[oldID];
132         }
133
134
135                 public function sendEvent(e:*,queue:Boolean):void {
136                         // queue is only used for AMFConnection
137                         dispatchEvent(e);
138                 }
139
140         public function registerPOI(node:Node):void {
141             if ( pois.indexOf(node) < 0 ) {
142                 pois.push(node);
143                 sendEvent(new EntityEvent(NEW_POI, node),false);
144             }
145         }
146
147         public function unregisterPOI(node:Node):void {
148             var index:uint = pois.indexOf(node);
149             if ( index >= 0 ) {
150                 pois.splice(index,1);
151             }
152         }
153
154         protected function setActiveChangeset(changeset:Changeset):void {
155             this.changeset = changeset;
156             sendEvent(new EntityEvent(NEW_CHANGESET, changeset),false);
157         }
158         
159         public function getNode(id:Number):Node {
160             return nodes[id];
161         }
162
163         public function getWay(id:Number):Way {
164             return ways[id];
165         }
166
167         public function getRelation(id:Number):Relation {
168             return relations[id];
169         }
170
171                 // Remove data from Connection
172                 // These functions are used only internally to stop redundant data hanging around
173                 // (either because it's been deleted on the server, or because we have panned away
174                 //  and need to reduce memory usage)
175
176                 protected function killNode(id:Number):void {
177                         if (!nodes[id]) return;
178             nodes[id].dispatchEvent(new EntityEvent(Connection.NODE_DELETED, nodes[id]));
179                         if (nodes[id].parentRelations.length>0) {
180                                 nodes[id]=new Node(id,0,{},false,0,0);
181                         } else {
182                                 delete nodes[id];
183                         }
184                         nodecount--;
185                 }
186
187                 protected function killWay(id:Number):void {
188                         if (!ways[id]) return;
189             ways[id].dispatchEvent(new EntityEvent(Connection.WAY_DELETED, ways[id]));
190                         if (ways[id].parentRelations.length>0) {
191                                 ways[id]=new Way(id,0,{},false,[]);
192                         } else {
193                                 delete ways[id];
194                         }
195                         waycount--;
196                 }
197
198                 protected function killRelation(id:Number):void {
199                         if (!relations[id]) return;
200             relations[id].dispatchEvent(new EntityEvent(Connection.RELATION_DELETED, relations[id]));
201                         if (relations[id].parentRelations.length>0) {
202                                 relations[id]=new Relation(id,0,{},false,[]);
203                         } else {
204                                 delete relations[id];
205                         }
206                         relationcount--;
207                 }
208
209                 protected function killWayWithNodes(id:Number):void {
210                         var way:Way=ways[id];
211                         var node:Node;
212                         for (var i:uint=0; i<way.length; i++) {
213                                 node=way.getNode(i);
214                                 if (node.isDirty) { continue; }
215                                 if (node.parentWays.length>1) {
216                                         node.removeParent(way);
217                                 } else {
218                                         killNode(node.id);
219                                 }
220                         }
221                         killWay(id);
222                 }
223                 
224
225
226         public function createNode(tags:Object, lat:Number, lon:Number, performCreate:Function):Node {
227             var node:Node = new Node(nextNegative, 0, tags, true, lat, lon);
228             performCreate(new CreateEntityAction(node, setNode));
229             return node;
230         }
231
232         public function createWay(tags:Object, nodes:Array, performCreate:Function):Way {
233             var way:Way = new Way(nextNegative, 0, tags, true, nodes.concat());
234             performCreate(new CreateEntityAction(way, setWay));
235             return way;
236         }
237
238         public function createRelation(tags:Object, members:Array, performCreate:Function):Relation {
239             var relation:Relation = new Relation(nextNegative, 0, tags, true, members.concat());
240             performCreate(new CreateEntityAction(relation, setRelation));
241             return relation;
242         }
243
244         public function getAllNodeIDs():Array {
245             var list:Array = [];
246             for each (var node:Node in nodes)
247                 list.push(node.id);
248             return list;
249         }
250
251         public function getAllWayIDs():Array {
252             var list:Array = [];
253             for each (var way:Way in ways)
254                 list.push(way.id);
255             return list;
256         }
257
258         public function getAllRelationIDs():Array {
259             var list:Array = [];
260             for each (var relation:Relation in relations)
261                 list.push(relation.id);
262             return list;
263         }
264
265                 public function getObjectsByBbox(left:Number, right:Number, top:Number, bottom:Number):Object {
266                         var o:Object = { poisInside: [], poisOutside: [], waysInside: [], waysOutside: [] };
267                         for each (var way:Way in ways) {
268                                 if (way.within(left,right,top,bottom)) { o.waysInside.push(way); }
269                                                                   else { o.waysOutside.push(way); }
270                         }
271                         for each (var node:Node in nodes) {
272                                 if (!node.hasParentWays) {
273                                         if (node.within(left,right,top,bottom)) { o.poisInside.push(node); }
274                                                                            else { o.poisOutside.push(node); }
275                                 }
276                         }
277                         return o;
278                 }
279
280         public function getActiveChangeset():Changeset {
281             return changeset;
282         }
283         
284                 public function purgeOutside(left:Number, right:Number, top:Number, bottom:Number):void {
285                         for each (var way:Way in ways) {
286                                 if (!way.within(left,right,top,bottom) && !way.isDirty) {
287                                         killWayWithNodes(way.id);
288                                 }
289                         }
290                         // ** should purge POIs and relations too
291                 }
292
293         // these are functions that the Connection implementation is expected to
294         // provide. This class has some generic helpers for the implementation.
295                 public function loadBbox(left:Number, right:Number,
296                                                                 top:Number, bottom:Number):void {
297             }
298             
299             public function setAppID(id:Object):void {}
300             public function setAuthToken(id:Object):void {}
301             public function createChangeset(tags:Object):void {}
302             public function uploadChanges():void {}
303     }
304
305 }
306