basic AutoComplete component and sample support. Needs much more work on the P2 logic...
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / MapFeatures.as
1 package net.systemeD.potlatch2.mapfeatures {
2
3     import flash.events.EventDispatcher;
4     import flash.events.Event;
5     import flash.net.URLLoader;
6
7         import flash.system.Security;
8         import flash.net.*;
9
10         import mx.core.UIComponent;
11         import mx.controls.DataGrid;
12
13     import net.systemeD.halcyon.connection.*;
14         import net.systemeD.halcyon.DebugURLRequest;
15
16         public class MapFeatures extends EventDispatcher {
17         private static var instance:MapFeatures;
18
19         public static function getInstance():MapFeatures {
20             if ( instance == null ) {
21                 instance = new MapFeatures();
22                 instance.loadFeatures();
23             }
24             return instance;
25         }
26
27         private var xml:XML = null;
28         private var _features:Array = null;
29         private var _categories:Array = null;
30                 private var _keys:Array = null;
31                 private var _tags:Object = null;
32
33         protected function loadFeatures():void {
34             var request:DebugURLRequest = new DebugURLRequest("map_features.xml");
35             var loader:URLLoader = new URLLoader();
36             loader.addEventListener(Event.COMPLETE, onFeatureLoad);
37             loader.load(request.request);
38         }
39
40         internal function get definition():XML {
41             return xml;
42         }
43         
44         private function onFeatureLoad(event:Event):void {
45                         var f:Feature;
46
47             xml = new XML(URLLoader(event.target).data);
48             _features = [];
49                         _keys = [];
50                         _tags = {};
51
52             for each(var feature:XML in xml.feature) {
53                 f=new Feature(this,feature);
54                                 _features.push(f);
55                                 for each (var tag:Object in f.tags) {
56                                         if (!_tags[tag.k]) { _tags[tag.k]=new Object; _keys.push(tag.k); }
57                                         _tags[tag.k][tag.v]=true;
58                                 }
59             }            
60                         _keys.sort();
61
62             _categories = new Array();
63             for each(var catXML:XML in xml.category) {
64                 if ( catXML.child("category").length() == 0 )
65                   _categories.push(new Category(this, catXML.@name, catXML.@id, _categories.length));
66             }
67             dispatchEvent(new Event("featuresLoaded"));
68         }
69
70         public function hasLoaded():Boolean {
71             return xml != null;
72         }
73
74         public function findMatchingFeature(entity:Entity):Feature {
75             if ( xml == null )
76                 return null;
77
78             for each(var feature:Feature in features) {
79                 var match:Boolean = true;
80
81                 // check for matching tags
82                 for each(var tag:Object in feature.tags) {
83                     var entityTag:String = entity.getTag(tag.k);
84                     match = entityTag == tag.v || (entityTag != null && tag.v == "*");
85                     if ( !match ) break;
86                 }
87
88                                 // check for matching withins
89                                 if (match) {
90                                         for each (var within:Object in feature.withins) {
91                                                 match = entity.countParentObjects(within) >= (within.minimum ? within.minimum : 1);
92                                                 if (!match) { break; }
93                                         }
94                                 }
95
96                 if (match) {
97                     return feature;
98                                 }
99             }
100             return null;
101         }
102         
103         [Bindable(event="featuresLoaded")]
104         public function get categories():Array {
105             if ( xml == null )
106                 return null;        
107             return _categories;
108         }
109
110         [Bindable(event="featuresLoaded")]
111         public function getCategoriesForType(type:String):Array {
112             if ( xml == null )
113                 return null;
114             if ( type == null || type == "" )  
115                 return []; //_categories;
116                 
117             var filteredCategories:Array = new Array();
118             for each( var cat:Category in _categories ) {
119                 if ( cat.getFeaturesForType(type).length > 0 )
120                     filteredCategories.push(cat);
121             }
122             return filteredCategories;
123         }
124
125         [Bindable(event="featuresLoaded")]
126         public function get features():Array {
127             if ( xml == null )
128                 return null;            
129             return _features;
130         }
131
132         [Bindable(event="featuresLoaded")]
133         public function get pois():Array {
134             if (xml == null )
135                 return null;
136             var pois:Array = [];
137             var counter:int = 0;
138             for each ( var feature:Feature in _features ) {
139               if (feature.isType("point")) {
140                 pois.push(feature);
141               }
142             }
143             return pois;
144         }
145
146                 [Bindable(event="featuresLoaded")]
147                 public function getAutoCompleteKeys():Array {
148                         return _keys;
149                 }
150                 
151                 public function getAutoCompleteValues(key:String):Array {
152                         var list:Array=[];
153                         for (var v:String in _tags[key]) { list.push(v); }
154                         list.sort();
155                         return list;
156                 }
157                 
158     }
159
160 }
161
162