Local file vector import (not .shp yet)
[potlatch2.git] / net / systemeD / potlatch2 / dialogs / VectorSourceDialog.mxml
index 5f49e1042bf228653404cb851e4605f5549ad78c..d8a275a4aebee827edb4d67ddb03a11ee2517b68 100644 (file)
@@ -2,9 +2,10 @@
 <mx:TitleWindow
         xmlns:fx="http://ns.adobe.com/mxml/2009"
         xmlns:mx="library://ns.adobe.com/flex/mx"
+               xmlns:s="library://ns.adobe.com/flex/spark"
         layout="vertical" showCloseButton="true"
         horizontalAlign="center" title="Load vector file"
-        width="500" height="450" verticalGap="0">
+        width="630" height="450" verticalGap="0">
 
        <mx:DataGrid editable="true" width="100%" height="100%" id="dataGrid" 
                dataProvider="{vectorLayers}" itemEditEnd="dataEdited(event)">
                <mx:LinkButton label="Delete" click="removeLayer()" enabled="{dataGrid.selectedItem != null &amp;&amp; dataGrid.selectedItem.isBackground}"/>
        </mx:HBox>
 
-       <mx:VBox width="100%" paddingTop="10">
-               <mx:HRule width="100%" />
-               <!-- ** FIXME: this looks horrid - make into a nicely laid out form -->
-               <mx:Label text="Add new vector layer" fontSize="12" fontWeight="bold" />
-               <mx:HBox>
-                       <mx:RadioButtonGroup id="filetype" />
-                       <mx:RadioButton width="100%" groupName="filetype" value="gpx" id="gpx" label="GPX" selected="true" />
-                       <mx:RadioButton width="100%" groupName="filetype" value="kml" id="kml" label="KML" />
-                       <mx:RadioButton width="100%" groupName="filetype" value="osm" id="osm" label="OSM" />
-                       <mx:RadioButton width="100%" groupName="filetype" value="shp" id="shp" label="Shapefile" />
-               </mx:HBox>
-               <mx:HBox>
-                       <mx:Label text="Shapefile projection:" />
-                       <mx:ComboBox id="projection"> 
-                               <mx:ArrayCollection>
-                                       <fx:Object label="Lat/long" data="" />
+       <fx:Declarations>
+               <s:RadioButtonGroup id="filetype" />
+       </fx:Declarations>
+       <s:Form id="addlayer" width="100%">
+               <s:FormHeading label="Add new vector layer" />
+
+               <s:FormItem label="File type:">
+                       <s:HGroup>
+                               <s:RadioButton groupName="filetype" value="gpx" id="gpx" label="GPX" selected="true" />
+                               <s:RadioButton groupName="filetype" value="kml" id="kml" label="KML" />
+                               <s:RadioButton groupName="filetype" value="osm" id="osm" label="OSM" />
+                               <s:RadioButton groupName="filetype" value="shp" id="shp" label="Shapefile" />
+                       </s:HGroup>
+                       <s:helpContent><s:Label text="Type of data to load"/></s:helpContent>
+               </s:FormItem>
+
+               <s:FormItem label="Projection:">
+                       <s:DropDownList id="projection" selectedIndex="0" width="200"> 
+                               <s:ArrayCollection>
+                                       <fx:Object label="Lat/long (EPSG:4326)" data="" />
                                        <fx:Object label="Ordnance Survey GB" data="EPSG:27700" />
                                        <fx:Object label="NAD83" data="EPSG:4269" />
-                               </mx:ArrayCollection>
-                       </mx:ComboBox>
-                       <mx:CheckBox width="100%" label="Simplify paths" id="simplify" />
-               </mx:HBox>
-               <mx:HBox>
-                       <mx:Text text="URL:"/>
-                       <mx:TextInput width="100%" id="src" text="" change="selectByExtension()" />
-                       <mx:Button label="Load" click="loadFiles(src.text, filetype.selectedValue.toString(), simplify.selected, projection.selectedItem.data);" enabled="{src.text == '' ? false : true}"/>
-               </mx:HBox>
-               <mx:HBox>
-                       <mx:Text text="Tag transform:"/>
-                       <mx:TextInput width="100%" id="transformsrc" text="{CSSTransform.getInstance().url}"/>
-                       <mx:Button label="Load" click="loadTransform(transformsrc.text);" enabled="{transformsrc.text == '' ? false : true}"/>
-                       <mx:Button label="Clear" click="clearTransform();" enabled="{transformsrc.text == '' ? false : true}"/>
-               </mx:HBox>
-       </mx:VBox>
+                               </s:ArrayCollection>
+                       </s:DropDownList>
+                       <s:helpContent><s:Label text="Shapefiles only"/></s:helpContent>
+               </s:FormItem>
+
+               <s:FormItem label="Simplify:">
+                       <s:CheckBox label="Enabled" id="simplify" />
+                       <s:helpContent><s:Label text="Reduce points in paths"/></s:helpContent>
+               </s:FormItem>
+
+        <s:FormItem label="File:">
+                       <s:HGroup>
+                               <s:Button label="Open..." click="loadFileLocal()" />
+                               <s:Rect width="20" height="1" />
+                   <s:TextInput id="src" text="" change="selectByExtension()" />
+                               <s:Button label="Fetch" click="loadFiles(src.text);" enabled="{src.text == '' ? false : true}"/>
+                       </s:HGroup>
+            <s:helpContent><s:Label text="URL of file to load" /></s:helpContent>
+        </s:FormItem>
+
+        <s:FormItem label="Tag transform:">
+                       <s:HGroup>
+                               <s:Button label="Open..." />
+                               <s:Rect width="20" height="1" />
+                               <s:TextInput id="transformsrc" text="{CSSTransform.getInstance().url}"/>
+                               <s:Button label="Fetch" click="loadTransform(transformsrc.text);" enabled="{transformsrc.text == '' ? false : true}"/>
+                               <s:Button label="Clear" click="clearTransform();"                 enabled="{transformsrc.text == '' ? false : true}"/>
+                       </s:HGroup>
+            <s:helpContent><s:Label text="MapCSS transformation file" /></s:helpContent>
+        </s:FormItem>
+
+       </s:Form>
 
   <fx:Script><![CDATA[
 
        import mx.controls.Alert;
 
        private var map:Map;
+       private var localFileReference:FileReference;
 
        public function styleEdited(name:String,stylesheet:String):void {
                map.findLayer(name).setStyle(stylesheet);
                return v;
        }
        
+       private function get selectedType():String {
+               return filetype.selectedValue.toString();
+       }
+       
        private function toggleVisibility(event:Event):void {
                map.findLayer(dataGrid.selectedItem.name).visible = !map.findLayer(dataGrid.selectedItem.name).visible;
        }
         PopUpManager.removePopUp(this);
     }
 
-       private function loadFiles(url:String,type:String,simplify:Boolean,projection:String=""):void {
+       /** Launch importer for remotely hosted files. */
+       private function loadFiles(url:String):void {
                var names:Array=url.split('/'); var name:String=names[names.length-1];
-               var stylesheet:String="stylesheets/potlatch.css";
-               if (type=='gpx') { stylesheet="stylesheets/gpx.css"; }
-
                var connection:Connection = new Connection(name, url, null, null, CSSTransform.getInstance());
-
-               var filesLoaded:Function = function(success:Boolean,message:String=null):void {
-                       if (success) {
-                               var paint:MapPaint = map.addLayer(connection, stylesheet);
-                               paint.updateEntityUIs(false, false);
-                               dispatchEvent(new Event("layers_changed"));
-                       } else {
-                               Alert.show(message, 'Error', mx.controls.Alert.OK);
-                       }
+               var importer:Importer = findImporter(connection);
+               if (selectedType=='shp') {
+                       importer.importFromRemoteFiles([url]);
+               } else {
+                       var re:RegExp=/.shp$/i; url=url.replace(re,'');
+                       importer.importFromRemoteFiles([url+".shp",url+".shx",url+".dbf"]);
                }
+       }
+       
+       /** Present an 'Open File' dialogue. */
+       private function loadFileLocal():void {
+               // note scoping issues - http://blog.wrench.com.au/2010/06/16/filereferenceload-keep-it-in-scope/
+               localFileReference=new FileReference(); 
+               var fileTypes:FileFilter=new FileFilter("*."+selectedType, "*."+selectedType); 
+               localFileReference.browse([fileTypes]); 
+               localFileReference.addEventListener(Event.SELECT, selectFileLocal);
+       }
+       
+       /** The user has chosen a file in the local 'Open File' dialogue. */
+       private function selectFileLocal(event:Event):void {
+               var connection:Connection = new Connection(localFileReference.name, '', null, null, CSSTransform.getInstance());
+               var importer:Importer = findImporter(connection);
+               importer.importFromLocalFile(localFileReference);
+       }
+       
+       /** Create an Importer object of the type selected by the user. */
+       private function findImporter(connection:Connection):Importer {
+               var simplifyPath:Boolean=simplify.selected;
+               var reproject:String=projection.selectedItem.data;
+               var stylesheet:String="stylesheets/potlatch.css"; if (selectedType=='gpx') { stylesheet="stylesheets/gpx.css"; }
 
-               if (type=='gpx') {
-                       var gpx:GpxImporter=new GpxImporter(connection, map, [url], filesLoaded, simplify);
-               } else if (type=='kml') {
-                       var kml:KmlImporter=new KmlImporter(connection, map, [url], filesLoaded, simplify);
-               } else if (type=='osm') {
-                       var osm:OsmImporter=new OsmImporter(connection, map, [url], filesLoaded, simplify);
+               switch (selectedType) {
+                       case 'gpx':     return new GpxImporter(connection, map, filesLoaded, simplifyPath, { stylesheet: stylesheet });
+                       case 'kml':     return new KmlImporter(connection, map, filesLoaded, simplifyPath, { stylesheet: stylesheet });
+                       case 'osm': return new OsmImporter(connection, map, filesLoaded, simplifyPath, { stylesheet: stylesheet });
+                       case 'shp': return new ShpImporter(connection, map, filesLoaded, simplifyPath, { stylesheet: stylesheet, projection: reproject });
+               }
+               return null;
+       }
+       
+       /** Callback routine when import has completed. */
+       private function filesLoaded(connection:Connection,options:Object,success:Boolean,message:String=null):void {
+               if (success) {
+                       var paint:MapPaint = map.addLayer(connection, options['stylesheet']);
+                       paint.updateEntityUIs(false, false);
+                       dispatchEvent(new Event("layers_changed"));
                } else {
-                       /* FIXME: 
-                          we're currently hardcoding the projection values. We could populate this directly from 
-                          proj4as, or better still, parse the WKT in the .PRJ file:
-                                       http://trac.osgeo.org/proj4js/ticket/47
-                                       http://trac.osgeo.org/proj4js/changeset/1873
-                       */
-                       var re:RegExp=/.shp$/i; url=url.replace(re,'');
-                       var shp:ShpImporter=new ShpImporter(connection,
-                                                           map,
-                                                           [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify, projection);
+                       Alert.show(message, 'Error', mx.controls.Alert.OK);
                }
        }