cool vector background layer stuff!
[potlatch2.git] / net / systemeD / potlatch2 / utils / Importer.as
1 package net.systemeD.potlatch2.utils {
2
3         import net.systemeD.halcyon.MapPaint;
4         import net.systemeD.halcyon.ExtendedURLLoader;
5         import flash.net.URLLoader;
6         import flash.display.LoaderInfo;
7         import flash.events.*;
8         import flash.net.*;
9
10         import net.systemeD.halcyon.Globals;
11
12         public class Importer {
13
14                 protected var container:Object;                         // destination object for way/node/relations data
15                 protected var paint:MapPaint;                           // destination sprite for WayUIs/NodeUIs
16
17                 public var files:Array=[];
18                 protected var filenames:Array;
19                 protected var filesloaded:uint=0;
20                 protected var callback:Function;
21                 protected var simplify:Boolean;
22
23                 public function Importer(container:*, paint:MapPaint, filenames:Array, callback:Function, simplify:Boolean) {
24                         Globals.vars.root.addDebug("starting importer"); 
25                         Globals.vars.root.addDebug("container is "+container);
26                         Globals.vars.root.addDebug("paint is "+paint);
27                         this.container = container;
28                         this.paint = paint;
29                         this.filenames=filenames;
30                         this.callback=callback;
31                         this.simplify=simplify;
32
33                         var sp:uint=0;
34                         for each (var fn:String in filenames) {
35                                 Globals.vars.root.addDebug("requesting file "+fn); 
36
37                                 var loader:ExtendedURLLoader = new ExtendedURLLoader();
38                                 loader.info['file']=sp;
39                                 loader.dataFormat=URLLoaderDataFormat.BINARY;
40                                 loader.addEventListener(Event.COMPLETE,fileLoaded);
41                                 if (callback!=null) {
42                                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler);
43                                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler);
44                                 }
45                                 loader.load(new URLRequest(fn));
46                                 sp++;
47                         }
48                 }
49                 
50                 protected function fileLoaded(e:Event):void {
51                         Globals.vars.root.addDebug("loaded file "+e.target.info['file']); 
52                         files[e.target.info['file']]=e.target.data;
53                         filesloaded++;
54                         if (filesloaded==filenames.length) { 
55                                 doImport();
56                                 paint.updateEntityUIs(container.getObjectsByBbox(paint.map.edge_l, paint.map.edge_r, paint.map.edge_t, paint.map.edge_b), false, false);
57                                 if (callback!=null) { callback(true); }
58                         }
59                 }
60                 
61                 protected function doImport():void {
62                 }
63
64                 protected function securityErrorHandler( event:SecurityErrorEvent ):void { callback(false,"You don't have permission to open that file."); }
65                 protected function ioErrorHandler( event:IOErrorEvent ):void { callback(false,"The file could not be loaded."); }
66
67         }
68 }