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