f2ee64c566a54207485d2da4ff26c3a91c0c2122
[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 &amp;&amp; dataGrid.selectedItem.isBackground}"/>
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:HBox>
48                 <mx:HBox>
49                         <mx:Label text="Shapefile projection:" />
50                         <mx:ComboBox id="projection"> 
51                                 <mx:ArrayCollection>
52                                         <mx:Object label="Lat/long" data="" />
53                                         <mx:Object label="Ordnance Survey GB" data="EPSG:27700" />
54                                         <mx:Object label="NAD83" data="EPSG:4269" />
55                                 </mx:ArrayCollection>
56                         </mx:ComboBox>
57                         <mx:CheckBox width="100%" label="Simplify paths" selected="true" id="simplify" />
58                 </mx:HBox>
59                 <mx:HBox>
60                         <mx:Text text="URL:"/>
61                         <mx:TextInput width="100%" id="src" text="" />
62                         <mx:Button label="Load" click="loadFiles(src.text, filetype.selectedValue.toString(), simplify.selected, projection.selectedItem.data);" enabled="{src.text == '' ? false : true}"/>
63                 </mx:HBox>
64         </mx:VBox>
65
66   <mx:Script><![CDATA[
67
68         import net.systemeD.halcyon.Map;
69         import net.systemeD.halcyon.MapPaint;
70         import net.systemeD.halcyon.Globals;
71         import net.systemeD.halcyon.connection.Connection;
72         import net.systemeD.potlatch2.utils.Importer;
73         import net.systemeD.potlatch2.utils.GpxImporter;
74         import net.systemeD.potlatch2.utils.KmlImporter;
75         import net.systemeD.potlatch2.utils.ShpImporter;
76         import net.systemeD.potlatch2.utils.OsmImporter;
77         import net.systemeD.potlatch2.collections.Stylesheets;
78     import mx.managers.PopUpManager;
79     import mx.events.DataGridEvent;
80     import mx.events.CloseEvent;
81     import mx.core.Application;
82         import mx.collections.ArrayCollection;
83         import mx.controls.Alert;
84
85         private var map:Map;
86
87         public function styleEdited(name:String,stylesheet:String):void {
88                 map.findLayer(name).setStyle(stylesheet);
89         }
90
91
92     public function init():void {
93         PopUpManager.addPopUp(this, Application(Application.application), true);
94         PopUpManager.centerPopUp(this);
95         this.addEventListener(CloseEvent.CLOSE, vectorDialog_close);
96                 dataGrid.addEventListener("visibility_changed", toggleVisibility);
97                 map = Globals.vars.root;
98                 dispatchEvent(new Event("layers_changed"));
99     }
100
101         public function whichStyle(item:Object):Object {
102                 for each (var style:Object in styles) {
103                         if (style.url==item.style) { return style; }
104                 }
105                 return {};
106         }
107
108         [Bindable(event="bogus")]
109         public function get styles():ArrayCollection {
110                 return new ArrayCollection(Stylesheets.instance().collection);
111         }
112
113         [Bindable(event="layers_changed")]
114         private function get vectorLayers():Array {
115         var v:Array=[];
116         for each (var a:MapPaint in map.getLayers() ) {
117             v.push( { name:a.connection.name, visible:a.visible, url:a.connection.apiBase, style:a.style, isBackground:a.isBackground } );
118         }
119                 return v;
120         }
121         
122         private function toggleVisibility(event:Event):void {
123                 map.findLayer(dataGrid.selectedItem.name).visible = !map.findLayer(dataGrid.selectedItem.name).visible;
124         }
125         
126         private function dataEdited(event:DataGridEvent):void {
127                 if (event.dataField=='name') {
128                         // Rename field
129                         var a:String=dataGrid.selectedItem.name;
130                         var b:String=dataGrid.itemEditorInstance['text'];       // this is, unbelievably, how you get data within an itemEditEnd event
131                         if (a==b || map.findLayer(b)) {
132                                 event.preventDefault();
133                         } else {
134                                 map.findLayer(a).connection.name = b;
135                         }
136                         dispatchEvent(new Event("layers_changed"));
137                 }
138         }
139
140         private function removeLayer():void {
141                 map.removeLayerByName(dataGrid.selectedItem.name);
142                 dispatchEvent(new Event("layers_changed"));
143         }
144     
145     private function vectorDialog_close(evt:CloseEvent):void {
146         PopUpManager.removePopUp(this);
147     }
148
149         private function loadFiles(url:String,type:String,simplify:Boolean,projection:String=""):void {
150                 var names:Array=url.split('/'); var name:String=names[names.length-1];
151                 var stylesheet:String="stylesheets/potlatch.css";
152                 if (type=='gpx') { stylesheet="stylesheets/gpx.css"; }
153
154                 var connection:Connection = new Connection(name, url, null, null);
155
156                 var filesLoaded:Function = function(success:Boolean,message:String=null):void {
157                         if (success) {
158                             var paint:MapPaint = map.addLayer(connection, stylesheet);
159                             paint.updateEntityUIs(false, false);
160                             dispatchEvent(new Event("layers_changed"));
161                         } else {
162                             Alert.show(message, 'Error', mx.controls.Alert.OK);
163                         }
164                 }
165
166                 if (type=='gpx') {
167                         var gpx:GpxImporter=new GpxImporter(connection, map, [url], filesLoaded, simplify);
168                 } else if (type=='kml') {
169                         var kml:KmlImporter=new KmlImporter(connection, map, [url], filesLoaded, simplify);
170                 } else if (type=='osm') {
171                         var osm:OsmImporter=new OsmImporter(connection, map, [url], filesLoaded, simplify);
172                 } else {
173                         /* FIXME: 
174                            we're currently hardcoding the projection values. We could populate this directly from 
175                            proj4as, or better still, parse the WKT in the .PRJ file:
176                                         http://trac.osgeo.org/proj4js/ticket/47
177                                         http://trac.osgeo.org/proj4js/changeset/1873
178                         */
179                         var re:RegExp=/.shp$/i; url=url.replace(re,'');
180                         var shp:ShpImporter=new ShpImporter(connection,
181                                                             map,
182                                                             [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify, projection);
183                 }
184         }
185
186               ]]>
187   </mx:Script>
188 </mx:TitleWindow>