drag ways (still needs undo though)
[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
91         protected function get nextNegative():Number {
92             return negativeID--;
93         }
94
95         protected function setNode(node:Node, queue:Boolean):void {
96             nodes[node.id] = node;
97             if (node.loaded) { sendEvent(new EntityEvent(NEW_NODE, node),queue); }
98         }
99
100         protected function setWay(way:Way, queue:Boolean):void {
101             ways[way.id] = way;
102             if (way.loaded) { sendEvent(new EntityEvent(NEW_WAY, way),queue); }
103         }
104
105         protected function setRelation(relation:Relation, queue:Boolean):void {
106             relations[relation.id] = relation;
107             if (relation.loaded) { sendEvent(new EntityEvent(NEW_RELATION, relation),queue); }
108         }
109
110         protected function renumberNode(oldID:Number, node:Node, queue:Boolean):void {
111             nodes[node.id] = node;
112             if (node.loaded) { sendEvent(new EntityRenumberedEvent(NODE_RENUMBERED, node, oldID),queue); }
113             delete nodes[oldID];
114         }
115
116         protected function renumberWay(oldID:Number, way:Way, queue:Boolean):void {
117             ways[way.id] = way;
118             if (way.loaded) { sendEvent(new EntityRenumberedEvent(WAY_RENUMBERED, way, oldID),queue); }
119             delete ways[oldID];
120         }
121
122         protected function renumberRelation(oldID:Number, relation:Relation, queue:Boolean):void {
123             relations[relation.id] = relation;
124             if (relation.loaded) { sendEvent(new EntityRenumberedEvent(RELATION_RENUMBERED, relation, oldID),queue); }
125             delete relations[oldID];
126         }
127
128
129                 public function sendEvent(e:*,queue:Boolean):void {
130                         // queue is only used for AMFConnection
131                         dispatchEvent(e);
132                 }
133
134         public function registerPOI(node:Node):void {
135             if ( pois.indexOf(node) < 0 ) {
136                 pois.push(node);
137                 sendEvent(new EntityEvent(NEW_POI, node),false);
138             }
139         }
140
141         public function unregisterPOI(node:Node):void {
142             var index:uint = pois.indexOf(node);
143             if ( index >= 0 ) {
144                 pois.splice(index,1);
145             }
146         }
147
148         protected function setActiveChangeset(changeset:Changeset):void {
149             this.changeset = changeset;
150             sendEvent(new EntityEvent(NEW_CHANGESET, changeset),false);
151         }
152         
153         public function getNode(id:Number):Node {
154             return nodes[id];
155         }
156
157         public function getWay(id:Number):Way {
158             return ways[id];
159         }
160
161         public function getRelation(id:Number):Relation {
162             return relations[id];
163         }
164
165                 public function killNode(id:Number):void {
166                         delete nodes[id];
167                 }
168                 
169                 public function killWay(id:Number):void {
170                         delete ways[id];
171                 }
172                 
173                 public function killRelation(id:Number):void {
174                         delete relations[id];
175                 }
176
177         public function createNode(tags:Object, lat:Number, lon:Number, performCreate:Function):Node {
178             var node:Node = new Node(nextNegative, 0, tags, true, lat, lon);
179             performCreate(new CreateEntityAction(node, setNode));
180             return node;
181         }
182
183         public function createWay(tags:Object, nodes:Array, performCreate:Function):Way {
184             var way:Way = new Way(nextNegative, 0, tags, true, nodes.concat());
185             performCreate(new CreateEntityAction(way, setWay));
186             return way;
187         }
188
189         public function createRelation(tags:Object, members:Array, performCreate:Function):Relation {
190             var relation:Relation = new Relation(nextNegative, 0, tags, true, members.concat());
191             performCreate(new CreateEntityAction(relation, setRelation));
192             return relation;
193         }
194
195         public function getAllNodeIDs():Array {
196             var list:Array = [];
197             for each (var node:Node in nodes)
198                 list.push(node.id);
199             return list;
200         }
201
202         public function getAllWayIDs():Array {
203             var list:Array = [];
204             for each (var way:Way in ways)
205                 list.push(way.id);
206             return list;
207         }
208
209         public function getAllRelationIDs():Array {
210             var list:Array = [];
211             for each (var relation:Relation in relations)
212                 list.push(relation.id);
213             return list;
214         }
215
216         public function getActiveChangeset():Changeset {
217             return changeset;
218         }
219         
220         // these are functions that the Connection implementation is expected to
221         // provide. This class has some generic helpers for the implementation.
222                 public function loadBbox(left:Number, right:Number,
223                                                                 top:Number, bottom:Number):void {
224             }
225             
226             public function setAppID(id:Object):void {}
227             public function setAuthToken(id:Object):void {}
228             public function createChangeset(tags:Object):void {}
229             public function uploadChanges():void {}
230     }
231
232 }
233