Integrating search functionality from AntTheLimey
[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.DebugURLRequest;
8     import net.systemeD.halcyon.VectorLayer;
9     import net.systemeD.potlatch2.utils.*;
10         
11     public class VectorBackgrounds extends EventDispatcher {
12
13         private static const GLOBAL_INSTANCE:VectorBackgrounds = new VectorBackgrounds();
14         public static function instance():VectorBackgrounds { return GLOBAL_INSTANCE; }
15
16         private var _map:Map;
17
18
19         public function init(map:Map):void {
20             _map = map;
21             var request:DebugURLRequest = new DebugURLRequest("vectors.xml");
22             var loader:URLLoader = new URLLoader();
23             loader.addEventListener(Event.COMPLETE, onConfigLoad);
24             loader.load(request.request);
25         }
26
27         public function onConfigLoad(e:Event):void {
28             var xml:XML = XML(e.target.data);
29             for each(var set:XML in xml.set) {
30
31               // allow layers to be defined but disabled. This lets me put examples in the
32               // config file.
33               if (set.@disabled == "true") continue;
34
35               if (!(set.policyfile == undefined)) {
36                 Security.loadPolicyFile(String(set.policyfile));
37               }
38
39               var name:String = (set.name == undefined) ? null : String(set.name);
40               var loader:String = set.loader;
41               switch (loader) {
42                 case "TrackLoader":
43                   break;
44                 case "KMLImporter":
45                   break;
46                 case "GPXImporter":
47                   if (set.url) {
48                     if (set.@loaded == "true") {
49                       name ||= 'GPX file';
50                       var layer:VectorLayer = new VectorLayer(name, _map, 'stylesheets/gpx.css');
51                       _map.addVectorLayer(layer);
52                       var gpxImporter:GpxImporter = new GpxImporter(layer, layer.paint, [String(set.url)]);
53                     } else {
54                       trace("configured but not loaded isn't supported yet");
55                     }
56                   } else {
57                     trace("AutoVectorBackground: no url for GPXImporter");
58                   }
59                   break;
60
61                 case "BugLoader":
62                   if (set.url && set.apiKey) {
63                     name ||= 'Bugs';
64                     var bugLoader:BugLoader = new BugLoader(_map, String(set.url), String(set.apikey), name, String(set.details));
65                     if (set.@loaded == "true") {
66                       bugLoader.load();
67                     }
68                   } else {
69                     trace("AutoVectorBackground: error with BugLoader");
70                   }
71                   break;
72
73                 case "BikeShopLoader":
74                   if (set.url) {
75                     name ||= 'Missing Bike Shops'
76                     var bikeShopLoader:BikeShopLoader = new BikeShopLoader(_map, String(set.url), name);
77                     if (set.@loaded == "true") {
78                       bikeShopLoader.load();
79                     }
80                   } else {
81                     trace("AutoVectorBackground: no url for BikeShopLoader");
82                   }
83                   break;
84
85                 default:
86                   trace("AutoVectorBackground: unknown loader");
87               }
88             }
89         }
90     }
91 }