Take <style> parameter from config file for snapshot background layers
[potlatch2.git] / net / systemeD / potlatch2 / collections / VectorBackgrounds.as
1 package net.systemeD.potlatch2.collections {
2
3     import flash.events.*
4     import flash.net.*
5     import flash.system.Security;
6     import net.systemeD.halcyon.Map;
7     import net.systemeD.halcyon.MapPaint;
8     import net.systemeD.halcyon.connection.Connection;
9     import net.systemeD.halcyon.DebugURLRequest;
10     import net.systemeD.potlatch2.utils.*;
11         
12     public class VectorBackgrounds extends EventDispatcher {
13
14         private static const GLOBAL_INSTANCE:VectorBackgrounds = new VectorBackgrounds();
15         public static function instance():VectorBackgrounds { return GLOBAL_INSTANCE; }
16
17         private var _map:Map;
18
19
20         public function init(map:Map):void {
21             _map = map;
22             var request:DebugURLRequest = new DebugURLRequest("vectors.xml");
23             var loader:URLLoader = new URLLoader();
24             loader.addEventListener(Event.COMPLETE, onConfigLoad);
25             loader.load(request.request);
26         }
27
28         public function onConfigLoad(e:Event):void {
29             var xml:XML = XML(e.target.data);
30             for each(var set:XML in xml.set) {
31
32               // allow layers to be defined but disabled. This lets me put examples in the
33               // config file.
34               if (set.@disabled == "true") continue;
35
36               if (!(set.policyfile == undefined)) {
37                 Security.loadPolicyFile(String(set.policyfile));
38               }
39
40               var name:String = (set.name == undefined) ? null : String(set.name);
41               var loader:String = set.loader;
42               switch (loader) {
43                 case "TrackLoader":
44                   break;
45                 case "KMLImporter":
46                   break;
47                 case "GPXImporter":
48                    if (set.url) {
49                      if (set.@loaded == "true") {
50                         name ||= 'GPX file';
51                         var gpx_url:String = String(set.url);
52
53                         var connection:Connection = new Connection(name, gpx_url, null, null);
54                         var gpx:GpxImporter=new GpxImporter(connection, _map, [gpx_url],
55                                                 function(success:Boolean,message:String=null):void {
56                                                     if (!success) return;
57                                                     var paint:MapPaint = _map.addLayer(connection, "stylesheets/gpx.css");
58                                                     paint.updateEntityUIs(false, false);
59                                                     dispatchEvent(new Event("layers_changed"));
60                                                 }, false);
61                      } else {
62                        trace("configured but not loaded isn't supported yet");
63                      }
64                    } else {
65                      trace("AutoVectorBackground: no url for GPXImporter");
66                    }
67                   break;
68
69                 case "BugLoader":
70                   if (set.url && set.apiKey) {
71                     name ||= 'Bugs';
72                     var bugLoader:BugLoader = new BugLoader(_map, String(set.url), String(set.apikey), name, String(set.details));
73                     if (set.@loaded == "true") {
74                       bugLoader.load();
75                     }
76                   } else {
77                     trace("AutoVectorBackground: error with BugLoader");
78                   }
79                   break;
80
81                 case "BikeShopLoader":
82                   if (set.url) {
83                     name ||= 'Missing Bike Shops'
84                     var bikeShopLoader:BikeShopLoader = new BikeShopLoader(_map, String(set.url), name);
85                     if (set.@loaded == "true") {
86                       bikeShopLoader.load();
87                     }
88                   } else {
89                     trace("AutoVectorBackground: no url for BikeShopLoader");
90                   }
91                   break;
92
93                 case "SnapshotLoader":
94                   if (set.url) {
95                     name ||= 'Snapshot Server'
96                     var snapshotLoader:SnapshotLoader = new SnapshotLoader(_map, String(set.url), name, String(set.style));
97                     if (set.@loaded == "true") {
98                       snapshotLoader.load();
99                     }
100                   } else {
101                     trace("VectorBackground: no url for SnapshotLoader");
102                   }
103                   break;
104
105                 default:
106                   trace("AutoVectorBackground: unknown loader");
107               }
108             }
109         }
110     }
111 }