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