Support importing the <create> element only of <osmChange> files
[potlatch2.git] / net / systemeD / potlatch2 / utils / Importer.as
index 4bf94fc77fb0939fb8da3787fda11b2013175f31..665e627f9d374921f94fc83f6bfae6611ffaa48d 100644 (file)
@@ -2,52 +2,62 @@ package net.systemeD.potlatch2.utils {
 
        import net.systemeD.halcyon.Map;
        import net.systemeD.halcyon.ExtendedURLLoader;
+    import net.systemeD.halcyon.connection.*;
+       import flash.net.URLLoader;
        import flash.display.LoaderInfo;
        import flash.events.*;
        import flash.net.*;
 
-       import net.systemeD.halcyon.Globals;
-
        public class Importer {
 
-               protected var map:Map;
-               protected var files:Array=[];
+        protected var connection:Connection;    // destination connection for way/node/relations data
+        protected var map:Map;                  // map being used - used only in Simplify calls
+
+               public var files:Array=[];
                protected var filenames:Array;
                protected var filesloaded:uint=0;
+               protected var callback:Function;
+               protected var simplify:Boolean;
 
-               public function Importer(map:Map, filenames:Array) {
-                       Globals.vars.root.addDebug("starting importer"); 
+               public function Importer(connection:Connection, map:Map, filenames:Array, callback:Function, simplify:Boolean) {
+                       this.connection = connection;
                        this.map = map;
                        this.filenames=filenames;
-
-                       var sp:uint=0;
-                       for each (var fn:String in filenames) {
-                               Globals.vars.root.addDebug("requesting file "+fn); 
-
-                               var loader:ExtendedURLLoader = new ExtendedURLLoader();
-                               loader.info['file']=sp;
+                       this.callback=callback;
+                       this.simplify=simplify;
+
+                       // Use forEach to avoid closure problem (http://stackoverflow.com/questions/422784/how-to-fix-closure-problem-in-actionscript-3-as3#3971784)
+                       filenames.forEach(function(fn:String, index:int, array:Array):void {
+                               trace("requesting file "+index);
+                               var request:URLRequest = new URLRequest(fn);
+                               var loader:URLLoader = new URLLoader();
                                loader.dataFormat=URLLoaderDataFormat.BINARY;
-                               loader.addEventListener(Event.COMPLETE,                                         fileLoaded,                             false, 0, true);
-                               loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
-                               loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
-                               loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
-                               loader.load(new URLRequest(fn));
-                               sp++;
-                       }
+                               loader.addEventListener(Event.COMPLETE,function(e:Event):void { fileLoaded(e,index); });
+                               if (callback!=null) {
+                                       loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler);
+                                       loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler);
+                               }
+                               loader.load(request);
+                       });
                }
                
-               protected function fileLoaded(e:Event):void {
-                       Globals.vars.root.addDebug("loaded file "+e.target.info['file']); 
-                       files[e.target.info['file']]=e.target.data;
+               protected function fileLoaded(e:Event,filenum:uint):void {
+                       files[filenum]=e.target.data;
                        filesloaded++;
-                       if (filesloaded==filenames.length) { doImport(); }
+                       trace("loaded file "+filenum+" ("+filesloaded+"/"+filenames.length+")"); 
+                       if (filesloaded==filenames.length) {
+                var action:CompositeUndoableAction = new CompositeUndoableAction("Import layer "+connection.name);
+                               doImport(action.push);
+                               action.doAction(); // just do it, don't add to undo stack
+                               if (callback!=null) { callback(true); }
+                       }
                }
                
-               protected function doImport():void { }
+               protected function doImport(push:Function):void {
+               }
 
-               protected function httpStatusHandler( event:HTTPStatusEvent ):void { }
-               protected function securityErrorHandler( event:SecurityErrorEvent ):void { Globals.vars.root.addDebug("securityerrorevent"); }
-               protected function ioErrorHandler( event:IOErrorEvent ):void { Globals.vars.root.addDebug("ioerrorevent"); }
+               protected function securityErrorHandler( event:SecurityErrorEvent ):void { callback(false,"You don't have permission to open that file."); }
+               protected function ioErrorHandler( event:IOErrorEvent ):void { callback(false,"The file could not be loaded."); }
 
        }
 }