Refactor all the Importer classes to use connections instead of vectorlayers
[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                         var sp:uint=0;
31                         for each (var fn:String in filenames) {
32                                 var thissp:uint=sp;             // scope within this block for the URLLoader 'complete' closure
33                                 trace("requesting file "+fn);
34                                 var request:DebugURLRequest = new DebugURLRequest(fn);
35                                 var loader:URLLoader = new URLLoader();
36                                 loader.dataFormat=URLLoaderDataFormat.BINARY;
37                                 loader.addEventListener(Event.COMPLETE,function(e:Event):void { fileLoaded(e,thissp); });
38                                 if (callback!=null) {
39                                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler);
40                                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler);
41                                 }
42                                 loader.load(request.request);
43                                 sp++;
44                         }
45                 }
46                 
47                 protected function fileLoaded(e:Event,filenum:uint):void {
48                         trace("loaded file "+filenum); 
49                         files[filenum]=e.target.data;
50                         filesloaded++;
51                         if (filesloaded==filenames.length) {
52                 var action:CompositeUndoableAction = new CompositeUndoableAction("Import layer "+connection.name);
53                                 doImport(action.push);
54                                 action.doAction(); // just do it, don't add to undo stack
55                                 if (callback!=null) { callback(true); }
56                         }
57                 }
58                 
59                 protected function doImport(push:Function):void {
60                 }
61
62                 protected function securityErrorHandler( event:SecurityErrorEvent ):void { callback(false,"You don't have permission to open that file."); }
63                 protected function ioErrorHandler( event:IOErrorEvent ):void { callback(false,"The file could not be loaded."); }
64
65         }
66 }