665e627f9d374921f94fc83f6bfae6611ffaa48d
[potlatch2.git] / net / systemeD / potlatch2 / utils / Importer.as
1 package net.systemeD.potlatch2.utils {
2
3         import net.systemeD.halcyon.Map;
4         import net.systemeD.halcyon.ExtendedURLLoader;
5     import net.systemeD.halcyon.connection.*;
6         import flash.net.URLLoader;
7         import flash.display.LoaderInfo;
8         import flash.events.*;
9         import flash.net.*;
10
11         public class Importer {
12
13         protected var connection:Connection;    // destination connection for way/node/relations data
14         protected var map:Map;                  // map being used - used only in Simplify calls
15
16                 public var files:Array=[];
17                 protected var filenames:Array;
18                 protected var filesloaded:uint=0;
19                 protected var callback:Function;
20                 protected var simplify:Boolean;
21
22                 public function Importer(connection:Connection, map:Map, filenames:Array, callback:Function, simplify:Boolean) {
23                         this.connection = connection;
24                         this.map = map;
25                         this.filenames=filenames;
26                         this.callback=callback;
27                         this.simplify=simplify;
28
29                         // Use forEach to avoid closure problem (http://stackoverflow.com/questions/422784/how-to-fix-closure-problem-in-actionscript-3-as3#3971784)
30                         filenames.forEach(function(fn:String, index:int, array:Array):void {
31                                 trace("requesting file "+index);
32                                 var request:URLRequest = new URLRequest(fn);
33                                 var loader:URLLoader = new URLLoader();
34                                 loader.dataFormat=URLLoaderDataFormat.BINARY;
35                                 loader.addEventListener(Event.COMPLETE,function(e:Event):void { fileLoaded(e,index); });
36                                 if (callback!=null) {
37                                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler);
38                                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler);
39                                 }
40                                 loader.load(request);
41                         });
42                 }
43                 
44                 protected function fileLoaded(e:Event,filenum:uint):void {
45                         files[filenum]=e.target.data;
46                         filesloaded++;
47                         trace("loaded file "+filenum+" ("+filesloaded+"/"+filenames.length+")"); 
48                         if (filesloaded==filenames.length) {
49                 var action:CompositeUndoableAction = new CompositeUndoableAction("Import layer "+connection.name);
50                                 doImport(action.push);
51                                 action.doAction(); // just do it, don't add to undo stack
52                                 if (callback!=null) { callback(true); }
53                         }
54                 }
55                 
56                 protected function doImport(push:Function):void {
57                 }
58
59                 protected function securityErrorHandler( event:SecurityErrorEvent ):void { callback(false,"You don't have permission to open that file."); }
60                 protected function ioErrorHandler( event:IOErrorEvent ):void { callback(false,"The file could not be loaded."); }
61
62         }
63 }