Add subcategories for map features.
[potlatch2.git] / net / systemeD / potlatch2 / VectorSourceDialog.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:TitleWindow
3         xmlns:fx="http://ns.adobe.com/mxml/2009"
4         xmlns:mx="library://ns.adobe.com/flex/mx"
5         layout="vertical" showCloseButton="true"
6         horizontalAlign="center" title="Load vector file"
7         width="500" height="350" verticalGap="0">
8
9         <mx:DataGrid editable="true" width="100%" height="100%" id="dataGrid" 
10                 dataProvider="{vectorLayers}" itemEditEnd="dataEdited(event)">
11             <mx:columns>
12                 <mx:DataGridColumn editable="false" dataField="visible" headerText="Show?" width="45">
13                                 <mx:itemRenderer>
14                                         <fx:Component>
15                                                 <mx:CheckBox selectedField="isSelected"
16                                                                          click="data.isSelected=!data.isSelected; this.parent.parent.dispatchEvent(new Event('visibility_changed'));" 
17                                                                          paddingLeft="5"/>
18                                         </fx:Component>
19                                 </mx:itemRenderer>
20                         </mx:DataGridColumn>
21                 <mx:DataGridColumn editable="false" dataField="interactive" headerText="Select?" width="45">
22                                 <mx:itemRenderer>
23                                         <!-- There should really be a way to hide this for the editable layer. See
24                                              http://stackoverflow.com/questions/6329895/how-do-i-change-the-style-of-one-individual-cell-in-a-flex-datagrid 
25                                              Leave it as a FIXME until we move to Flex 4 -->
26                                         <fx:Component>
27                                                 <mx:CheckBox selectedField="isInteractive"
28                                                                          click="data.isInteractive=!data.isInteractive; this.parent.parent.dispatchEvent(new Event('interactive_changed'));" 
29                                                              paddingLeft="5"/>
30                                         </fx:Component>
31                                 </mx:itemRenderer>
32                         </mx:DataGridColumn>
33                 <mx:DataGridColumn editable="true"  dataField="name"  headerText="Name"/>
34                 <mx:DataGridColumn editable="false" dataField="url"   headerText="URL"/>
35                 <mx:DataGridColumn editable="false" dataField="style" headerText="Style">
36                                 <mx:itemRenderer>
37                                         <fx:Component>
38                                         <mx:ComboBox
39                                                         selectedItem="{outerDocument.whichStyle(data)}"
40                                                 change="outerDocument.styleEdited(data.name,selectedItem.url)"
41                                                 dataProvider="{outerDocument.styles}" 
42                                                         labelField="name" />
43                                         </fx:Component>
44                                 </mx:itemRenderer>
45                         </mx:DataGridColumn>
46             </mx:columns>
47         </mx:DataGrid>
48         <mx:HBox horizontalAlign="right" width="100%">
49                 <mx:LinkButton label="Delete" click="removeLayer()" enabled="{dataGrid.selectedItem != null &amp;&amp; dataGrid.selectedItem.isBackground}"/>
50         </mx:HBox>
51
52         <mx:VBox width="100%" paddingTop="10">
53                 <mx:Label htmlText="&lt;b&gt;Add new vector layer&lt;/b&gt;" />
54                 <mx:HBox>
55                         <mx:RadioButtonGroup id="filetype" />
56                         <mx:RadioButton width="100%" groupName="filetype" value="gpx" id="gpx" label="GPX" selected="true" />
57                         <mx:RadioButton width="100%" groupName="filetype" value="kml" id="kml" label="KML" />
58                         <mx:RadioButton width="100%" groupName="filetype" value="osm" id="osm" label="OSM" />
59                         <mx:RadioButton width="100%" groupName="filetype" value="shp" id="shp" label="Shapefile" />
60                 </mx:HBox>
61                 <mx:HBox>
62                         <mx:Label text="Shapefile projection:" />
63                         <mx:ComboBox id="projection"> 
64                                 <mx:ArrayCollection>
65                                         <fx:Object label="Lat/long" data="" />
66                                         <fx:Object label="Ordnance Survey GB" data="EPSG:27700" />
67                                         <fx:Object label="NAD83" data="EPSG:4269" />
68                                 </mx:ArrayCollection>
69                         </mx:ComboBox>
70                         <mx:CheckBox width="100%" label="Simplify paths" selected="true" id="simplify" />
71                 </mx:HBox>
72                 <mx:HBox>
73                         <mx:Text text="URL:"/>
74                         <mx:TextInput width="100%" id="src" text="" change="selectByExtension()" />
75                         <mx:Button label="Load" click="loadFiles(src.text, filetype.selectedValue.toString(), simplify.selected, projection.selectedItem.data);" enabled="{src.text == '' ? false : true}"/>
76                 </mx:HBox>
77         </mx:VBox>
78
79   <fx:Script><![CDATA[
80
81         import net.systemeD.halcyon.Map;
82         import net.systemeD.halcyon.MapPaint;
83         import net.systemeD.halcyon.Globals;
84         import net.systemeD.halcyon.connection.Connection;
85         import net.systemeD.potlatch2.utils.Importer;
86         import net.systemeD.potlatch2.utils.GpxImporter;
87         import net.systemeD.potlatch2.utils.KmlImporter;
88         import net.systemeD.potlatch2.utils.ShpImporter;
89         import net.systemeD.potlatch2.utils.OsmImporter;
90         import net.systemeD.potlatch2.collections.Stylesheets;
91     import mx.managers.PopUpManager;
92     import mx.events.DataGridEvent;
93     import mx.events.CloseEvent;
94     import mx.core.Application;
95     import mx.core.FlexGlobals;
96         import mx.collections.ArrayCollection;
97         import mx.controls.Alert;
98
99         private var map:Map;
100
101         public function styleEdited(name:String,stylesheet:String):void {
102                 map.findLayer(name).setStyle(stylesheet);
103         }
104
105
106     public function init():void {
107         PopUpManager.addPopUp(this, Application(FlexGlobals.topLevelApplication), true);
108         PopUpManager.centerPopUp(this);
109         this.addEventListener(CloseEvent.CLOSE, vectorDialog_close);
110                 dataGrid.addEventListener("visibility_changed", toggleVisibility);
111                 dataGrid.addEventListener("interactive_changed", toggleInteractive);
112                 map = Globals.vars.root;
113                 dispatchEvent(new Event("layers_changed"));
114     }
115
116         public function whichStyle(item:Object):Object {
117                 for each (var style:Object in styles) {
118                         if (style.url==item.style) { return style; }
119                 }
120                 return {};
121         }
122
123         [Bindable(event="bogus")]
124         public function get styles():ArrayCollection {
125                 return new ArrayCollection(Stylesheets.instance().collection);
126         }
127
128         [Bindable(event="layers_changed")]
129         private function get vectorLayers():Array {
130         var v:Array=[];
131         for each (var a:MapPaint in map.getLayers() ) {
132             v.push( { name:a.connection.name, visible:a.visible, interactive:a.interactive, url:a.connection.apiBase, style:a.style, isBackground:a.isBackground } );
133         }
134                 return v;
135         }
136         
137         private function toggleVisibility(event:Event):void {
138                 map.findLayer(dataGrid.selectedItem.name).visible = !map.findLayer(dataGrid.selectedItem.name).visible;
139         }
140
141         private function toggleInteractive(event:Event):void {
142                 map.findLayer(dataGrid.selectedItem.name).interactive = !map.findLayer(dataGrid.selectedItem.name).interactive;
143         }
144         
145         private function dataEdited(event:DataGridEvent):void {
146                 if (event.dataField=='name') {
147                         // Rename field
148                         var a:String=dataGrid.selectedItem.name;
149                         var b:String=dataGrid.itemEditorInstance['text'];       // this is, unbelievably, how you get data within an itemEditEnd event
150                         if (a==b || map.findLayer(b)) {
151                                 event.preventDefault();
152                         } else {
153                                 map.findLayer(a).connection.name = b;
154                         }
155                         dispatchEvent(new Event("layers_changed"));
156                 }
157         }
158
159         private function removeLayer():void {
160                 map.removeLayerByName(dataGrid.selectedItem.name);
161                 dispatchEvent(new Event("layers_changed"));
162         }
163     
164         private function selectByExtension():void {
165                 if      (src.text.match(/\.shp$/i)) filetype.selectedValue='shp'
166                 else if (src.text.match(/\.osm$/i)) filetype.selectedValue='osm'
167                 else if (src.text.match(/\.gpx$/i)) filetype.selectedValue='gpx'
168                 else if (src.text.match(/\.kml$/i)) filetype.selectedValue='kml'
169         }
170
171     private function vectorDialog_close(evt:CloseEvent):void {
172         PopUpManager.removePopUp(this);
173     }
174
175         private function loadFiles(url:String,type:String,simplify:Boolean,projection:String=""):void {
176                 var names:Array=url.split('/'); var name:String=names[names.length-1];
177                 var stylesheet:String="stylesheets/potlatch.css";
178                 if (type=='gpx') { stylesheet="stylesheets/gpx.css"; }
179
180                 var connection:Connection = new Connection(name, url, null, null);
181
182                 var filesLoaded:Function = function(success:Boolean,message:String=null):void {
183                         if (success) {
184                             var paint:MapPaint = map.addLayer(connection, stylesheet);
185                             paint.updateEntityUIs(false, false);
186                             dispatchEvent(new Event("layers_changed"));
187                         } else {
188                             Alert.show(message, 'Error', mx.controls.Alert.OK);
189                         }
190                 }
191
192                 if (type=='gpx') {
193                         var gpx:GpxImporter=new GpxImporter(connection, map, [url], filesLoaded, simplify);
194                 } else if (type=='kml') {
195                         var kml:KmlImporter=new KmlImporter(connection, map, [url], filesLoaded, simplify);
196                 } else if (type=='osm') {
197                         var osm:OsmImporter=new OsmImporter(connection, map, [url], filesLoaded, simplify);
198                 } else {
199                         /* FIXME: 
200                            we're currently hardcoding the projection values. We could populate this directly from 
201                            proj4as, or better still, parse the WKT in the .PRJ file:
202                                         http://trac.osgeo.org/proj4js/ticket/47
203                                         http://trac.osgeo.org/proj4js/changeset/1873
204                         */
205                         var re:RegExp=/.shp$/i; url=url.replace(re,'');
206                         var shp:ShpImporter=new ShpImporter(connection,
207                                                             map,
208                                                             [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify, projection);
209                 }
210         }
211
212               ]]>
213   </fx:Script>
214 </mx:TitleWindow>