support GPX 1.1 too
[potlatch2.git] / net / systemeD / potlatch2 / VectorSourceDialog.mxml
index 8cf2c7243250ca938ca6fc7d8862eeeb305327aa..ea11ab44fa142caec7eca23163a14eea3ab9e837 100644 (file)
         xmlns:mx="http://www.adobe.com/2006/mxml" 
         layout="vertical" showCloseButton="true"
         horizontalAlign="center" title="Load vector file"
-        width="350" height="250" verticalGap="0">
+        width="400" height="350" verticalGap="0">
+
+       <mx:DataGrid editable="true" width="100%" height="100%" id="dataGrid" 
+               dataProvider="{vectorLayers}" itemEditEnd="dataEdited(event)">
+           <mx:columns>
+               <mx:DataGridColumn editable="false" dataField="visible" headerText="Show" width="45">
+                               <mx:itemRenderer>
+                                       <mx:Component>
+                                               <mx:CheckBox selectedField="isSelected"
+                                                                        click="data.isSelected=!data.isSelected; this.parent.parent.dispatchEvent(new Event('visibility_changed'));" 
+                                                                        paddingLeft="5"/>
+                                       </mx:Component>
+                               </mx:itemRenderer>
+                       </mx:DataGridColumn>
+               <mx:DataGridColumn editable="true"  dataField="name"  headerText="Name"/>
+               <mx:DataGridColumn editable="false" dataField="url"   headerText="URL"/>
+               <mx:DataGridColumn editable="false" dataField="style" headerText="Style">
+                               <mx:itemRenderer>
+                                       <mx:Component>
+                                       <mx:ComboBox
+                                                       selectedItem="{outerDocument.whichStyle(data)}"
+                                               change="outerDocument.styleEdited(data.name,selectedItem.url)"
+                                               dataProvider="{outerDocument.styles}" 
+                                                       labelField="name" />
+                                       </mx:Component>
+                               </mx:itemRenderer>
+                       </mx:DataGridColumn>
+           </mx:columns>
+       </mx:DataGrid>
+       <mx:HBox horizontalAlign="right" width="100%">
+               <mx:LinkButton label="Delete" click="removeVectorLayer()" enabled="{dataGrid.selectedItem != null? true : false}"/>
+       </mx:HBox>
+
+       <mx:VBox width="100%" paddingTop="10">
+               <mx:Label htmlText="&lt;b&gt;Add new vector layer&lt;/b&gt;" />
+               <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:CheckBox width="100%" label="Simplify paths" selected="true" id="simplify" />
+               </mx:HBox>
+               <mx:HBox>
+                       <mx:Text text="URL:"/>
+                       <mx:TextInput width="100%" id="src" text="" />
+                       <mx:Button label="Load" click="loadFiles(src.text, filetype.selectedValue.toString(), simplify.selected);" enabled="{src.text == '' ? false : true}"/>
+               </mx:HBox>
+       </mx:VBox>
+
   <mx:Script><![CDATA[
+
        import net.systemeD.halcyon.Map;
        import net.systemeD.halcyon.Globals;
-       import net.systemeD.halcyon.vectorlayers.*;
+       import net.systemeD.halcyon.VectorLayer;
        import net.systemeD.potlatch2.utils.Importer;
+       import net.systemeD.potlatch2.utils.GpxImporter;
+       import net.systemeD.potlatch2.utils.KmlImporter;
        import net.systemeD.potlatch2.utils.ShpImporter;
+       import net.systemeD.potlatch2.utils.OsmImporter;
+       import net.systemeD.potlatch2.collections.Stylesheets;
     import mx.managers.PopUpManager;
+    import mx.events.DataGridEvent;
     import mx.events.CloseEvent;
     import mx.core.Application;
+       import mx.collections.ArrayCollection;
+       import mx.controls.Alert;
+
+       private var map:Map;
+
+       public function styleEdited(name:String,stylesheet:String):void {
+               map.vectorlayers[name].style=stylesheet;
+               map.vectorlayers[name].redrawFromCSS(stylesheet)
+       }
+
 
     public function init():void {
         PopUpManager.addPopUp(this, Application(Application.application), true);
         PopUpManager.centerPopUp(this);
         this.addEventListener(CloseEvent.CLOSE, vectorDialog_close);
+               dataGrid.addEventListener("visibility_changed", toggleVisibility);
+               map = Globals.vars.root;
+               dispatchEvent(new Event("layers_changed"));
     }
+
+       public function whichStyle(item:Object):Object {
+               for each (var style:Object in styles) {
+                       if (style.url==item.style) { return style; }
+               }
+               return {};
+       }
+
+       [Bindable(event="bogus")]
+       public function get styles():ArrayCollection {
+               return new ArrayCollection(Stylesheets.instance().collection);
+       }
+
+       [Bindable(event="layers_changed")]
+       private function get vectorLayers():Array {
+               var v:Array=[];
+               for (var a:String in map.vectorlayers) {
+                       var vl:VectorLayer=map.vectorlayers[a];
+                       v.push( { name:a, visible:vl.paint.visible, url:vl.url, style:vl.style } );
+               }
+               return v;
+       }
+       
+       private function toggleVisibility(event:Event):void {
+               map.vectorlayers[dataGrid.selectedItem.name].paint.visible = !map.vectorlayers[dataGrid.selectedItem.name].paint.visible;
+       }
+       
+       private function dataEdited(event:DataGridEvent):void {
+               if (event.dataField=='name') {
+                       // Rename field
+                       var a:String=dataGrid.selectedItem.name;
+                       var b:String=dataGrid.itemEditorInstance['text'];       // this is, unbelievably, how you get data within an itemEditEnd event
+                       if (a==b || map.vectorlayers[b]) {
+                               event.preventDefault();
+                       } else {
+                               map.vectorlayers[b]=map.vectorlayers[a];
+                               delete map.vectorlayers[a];
+                       }
+                       dispatchEvent(new Event("layers_changed"));
+               }
+       }
+
+       private function removeVectorLayer():void {
+               var a:String=dataGrid.selectedItem.name;
+               map.vectorlayers[a].blank();
+               delete map.vectorlayers[a];
+               dispatchEvent(new Event("layers_changed"));
+       }
     
     private function vectorDialog_close(evt:CloseEvent):void {
         PopUpManager.removePopUp(this);
     }
 
-       private function loadSHP(url:String):void {
-        PopUpManager.removePopUp(this);
-               var theMap:Map = Globals.vars.root;
-               var vectorlayer:CustomVectorLayer=new CustomVectorLayer(theMap,"potlatch.css");
-               theMap.vectorlayers.push(vectorlayer);
-               theMap.vectorbg.addChild(vectorlayer.paint);
+       private function loadFiles(url:String,type:String,simplify:Boolean):void {
+               var names:Array=url.split('/'); var name:String=names[names.length-1];
+               var stylesheet:String="potlatch.css";
+               if (type=='gpx') { stylesheet="gpx.css"; }
+               var vectorlayer:VectorLayer=new VectorLayer(name,map,stylesheet);
+               vectorlayer.url=url;
 
-               var re:RegExp=/.shp$/i;
-               url=url.replace(re,'');
+                var filesLoaded:Function = function(success:Boolean,message:String=null):void {
+                        if (success) {
+                                map.addVectorLayer(vectorlayer);
+                                dispatchEvent(new Event("layers_changed"));
+                        } else {
+                                Alert.show(message, 'Error', mx.controls.Alert.OK);
+                        }
+                }
 
-               var shp:ShpImporter=new ShpImporter(vectorlayer,
-                                                   vectorlayer.paint,
-                                                   [url+".shp",url+".shx",url+".dbf"]);
+               if (type=='gpx') {
+                       var gpx:GpxImporter=new GpxImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
+               } else if (type=='kml') {
+                       var kml:KmlImporter=new KmlImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
+               } else if (type=='osm') {
+                       var osm:OsmImporter=new OsmImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
+               } else {
+                       var re:RegExp=/.shp$/i; url=url.replace(re,'');
+                       var shp:ShpImporter=new ShpImporter(vectorlayer,
+                                                           vectorlayer.paint,
+                                                           [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify);
+               }
        }
 
               ]]>
   </mx:Script>
-  <mx:Text>
-    <mx:text>
-      Input the URL of a shapefile to import.
-    </mx:text>
-  </mx:Text>
-  <mx:TextInput id="src" text="" />
-  <mx:ControlBar>
-    <mx:Spacer width="100%"/>
-    <mx:Button label="Ok" click="loadSHP(src.text);"/>
-  </mx:ControlBar>
-
-</mx:TitleWindow>
\ No newline at end of file
+</mx:TitleWindow>