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