754d19c4da2cc9f0415ddde01b313f106704d36f
[potlatch2.git] / net / systemeD / potlatch2 / VectorSourceDialog.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:TitleWindow
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         layout="vertical" showCloseButton="true"
5         horizontalAlign="center" title="Load vector file"
6         width="400" height="350" verticalGap="0">
7
8         <mx:DataGrid editable="true" width="100%" height="100%" id="dataGrid" 
9                 dataProvider="{vectorLayers}" itemEditEnd="dataEdited(event)">
10             <mx:columns>
11                 <mx:DataGridColumn editable="false" dataField="visible" headerText="Show" width="45">
12                                 <mx:itemRenderer>
13                                         <mx:Component>
14                                                 <mx:CheckBox selectedField="isSelected"
15                                                                          click="data.isSelected=!data.isSelected; this.parent.parent.dispatchEvent(new Event('visibility_changed'));" 
16                                                                          paddingLeft="5"/>
17                                         </mx:Component>
18                                 </mx:itemRenderer>
19                         </mx:DataGridColumn>
20                 <mx:DataGridColumn editable="true"  dataField="name"  headerText="Name"/>
21                 <mx:DataGridColumn editable="false" dataField="url"   headerText="URL"/>
22                 <mx:DataGridColumn editable="false" dataField="style" headerText="Style">
23                                 <mx:itemRenderer>
24                                         <mx:Component>
25                                         <mx:ComboBox
26                                                         selectedItem="{outerDocument.whichStyle(data)}"
27                                                 change="outerDocument.styleEdited(data.name,selectedItem.url)"
28                                                 dataProvider="{outerDocument.styles}" 
29                                                         labelField="name" />
30                                         </mx:Component>
31                                 </mx:itemRenderer>
32                         </mx:DataGridColumn>
33             </mx:columns>
34         </mx:DataGrid>
35         <mx:HBox horizontalAlign="right" width="100%">
36                 <mx:LinkButton label="Delete" click="removeLayer()" enabled="{dataGrid.selectedItem != null? true : false}"/>
37         </mx:HBox>
38
39         <mx:VBox width="100%" paddingTop="10">
40                 <mx:Label htmlText="&lt;b&gt;Add new vector layer&lt;/b&gt;" />
41                 <mx:HBox>
42                         <mx:RadioButtonGroup id="filetype" />
43                         <mx:RadioButton width="100%" groupName="filetype" value="gpx" id="gpx" label="GPX" selected="true" />
44                         <mx:RadioButton width="100%" groupName="filetype" value="kml" id="kml" label="KML" />
45                         <mx:RadioButton width="100%" groupName="filetype" value="osm" id="osm" label="OSM" />
46                         <mx:RadioButton width="100%" groupName="filetype" value="shp" id="shp" label="Shapefile" />
47                         <mx:CheckBox width="100%" label="Simplify paths" selected="true" id="simplify" />
48                 </mx:HBox>
49                 <mx:HBox>
50                         <mx:Text text="URL:"/>
51                         <mx:TextInput width="100%" id="src" text="" />
52                         <mx:Button label="Load" click="loadFiles(src.text, filetype.selectedValue.toString(), simplify.selected);" enabled="{src.text == '' ? false : true}"/>
53                 </mx:HBox>
54         </mx:VBox>
55
56   <mx:Script><![CDATA[
57
58         import net.systemeD.halcyon.Map;
59         import net.systemeD.halcyon.MapPaint;
60         import net.systemeD.halcyon.Globals;
61         import net.systemeD.halcyon.connection.Connection;
62         import net.systemeD.potlatch2.utils.Importer;
63         import net.systemeD.potlatch2.utils.GpxImporter;
64         import net.systemeD.potlatch2.utils.KmlImporter;
65         import net.systemeD.potlatch2.utils.ShpImporter;
66         import net.systemeD.potlatch2.utils.OsmImporter;
67         import net.systemeD.potlatch2.collections.Stylesheets;
68     import mx.managers.PopUpManager;
69     import mx.events.DataGridEvent;
70     import mx.events.CloseEvent;
71     import mx.core.Application;
72         import mx.collections.ArrayCollection;
73         import mx.controls.Alert;
74
75         private var map:Map;
76
77         public function styleEdited(name:String,stylesheet:String):void {
78                 map.findLayer(name).setStyle(stylesheet);
79         }
80
81
82     public function init():void {
83         PopUpManager.addPopUp(this, Application(Application.application), true);
84         PopUpManager.centerPopUp(this);
85         this.addEventListener(CloseEvent.CLOSE, vectorDialog_close);
86                 dataGrid.addEventListener("visibility_changed", toggleVisibility);
87                 map = Globals.vars.root;
88                 dispatchEvent(new Event("layers_changed"));
89     }
90
91         public function whichStyle(item:Object):Object {
92                 for each (var style:Object in styles) {
93                         if (style.url==item.style) { return style; }
94                 }
95                 return {};
96         }
97
98         [Bindable(event="bogus")]
99         public function get styles():ArrayCollection {
100                 return new ArrayCollection(Stylesheets.instance().collection);
101         }
102
103         [Bindable(event="layers_changed")]
104         private function get vectorLayers():Array {
105         var v:Array=[];
106         for each (var a:MapPaint in map.getLayers() ) {
107             if (a.isBackground) {
108                 v.push( { name:a.connection.name, visible:a.visible, url:a.connection.apiBase, style:a.style } );
109             }
110         }
111                 return v;
112         }
113         
114         private function toggleVisibility(event:Event):void {
115                 map.findLayer(dataGrid.selectedItem.name).visible = !map.findLayer(dataGrid.selectedItem.name).visible;
116         }
117         
118         private function dataEdited(event:DataGridEvent):void {
119                 if (event.dataField=='name') {
120                         // Rename field
121                         var a:String=dataGrid.selectedItem.name;
122                         var b:String=dataGrid.itemEditorInstance['text'];       // this is, unbelievably, how you get data within an itemEditEnd event
123                         if (a==b || map.findLayer(b)) {
124                                 event.preventDefault();
125                         } else {
126                                 map.findLayer(a).connection.name = b;
127                         }
128                         dispatchEvent(new Event("layers_changed"));
129                 }
130         }
131
132         private function removeLayer():void {
133                 map.removeLayerByName(dataGrid.selectedItem.name);
134                 dispatchEvent(new Event("layers_changed"));
135         }
136     
137     private function vectorDialog_close(evt:CloseEvent):void {
138         PopUpManager.removePopUp(this);
139     }
140
141         private function loadFiles(url:String,type:String,simplify:Boolean):void {
142                 var names:Array=url.split('/'); var name:String=names[names.length-1];
143                 var stylesheet:String="stylesheets/potlatch.css";
144                 if (type=='gpx') { stylesheet="stylesheets/gpx.css"; }
145
146                 var connection:Connection = new Connection(name, url, null, null);
147
148                 var filesLoaded:Function = function(success:Boolean,message:String=null):void {
149                         if (success) {
150                             var paint:MapPaint = map.addLayer(connection, stylesheet);
151                             paint.updateEntityUIs(false, false);
152                             dispatchEvent(new Event("layers_changed"));
153                         } else {
154                             Alert.show(message, 'Error', mx.controls.Alert.OK);
155                         }
156                 }
157
158                 if (type=='gpx') {
159                         var gpx:GpxImporter=new GpxImporter(connection, map, [url], filesLoaded, simplify);
160                 } else if (type=='kml') {
161                         var kml:KmlImporter=new KmlImporter(connection, map, [url], filesLoaded, simplify);
162                 } else if (type=='osm') {
163                         var osm:OsmImporter=new OsmImporter(connection, map, [url], filesLoaded, simplify);
164                 } else {
165                         var re:RegExp=/.shp$/i; url=url.replace(re,'');
166                         var shp:ShpImporter=new ShpImporter(connection,
167                                                             map,
168                                                             [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify);
169                 }
170         }
171
172               ]]>
173   </mx:Script>
174 </mx:TitleWindow>