Only add a new vector layer to the map once we know we have loaded
[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="removeVectorLayer()" 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.Globals;
60         import net.systemeD.halcyon.VectorLayer;
61         import net.systemeD.potlatch2.utils.Importer;
62         import net.systemeD.potlatch2.utils.GpxImporter;
63         import net.systemeD.potlatch2.utils.KmlImporter;
64         import net.systemeD.potlatch2.utils.ShpImporter;
65         import net.systemeD.potlatch2.utils.OsmImporter;
66     import mx.managers.PopUpManager;
67     import mx.events.DataGridEvent;
68     import mx.events.CloseEvent;
69     import mx.core.Application;
70         import mx.collections.ArrayCollection;
71         import mx.controls.Alert;
72
73         private var map:Map;
74
75         public function styleEdited(name:String,stylesheet:String):void {
76                 map.vectorlayers[name].style=stylesheet;
77                 map.vectorlayers[name].redrawFromCSS(stylesheet)
78         }
79
80
81     public function init():void {
82         PopUpManager.addPopUp(this, Application(Application.application), true);
83         PopUpManager.centerPopUp(this);
84         this.addEventListener(CloseEvent.CLOSE, vectorDialog_close);
85                 dataGrid.addEventListener("visibility_changed", toggleVisibility);
86                 map = Globals.vars.root;
87                 dispatchEvent(new Event("layers_changed"));
88     }
89
90         public function whichStyle(item:Object):Object {
91                 for each (var style:Object in styles) {
92                         if (style.url==item.style) { return style; }
93                 }
94                 return {};
95         }
96
97         [Bindable(event="bogus")]
98         public function get styles():ArrayCollection {
99                 return new ArrayCollection(Application.application.theController.stylesheets);
100         }
101
102         [Bindable(event="layers_changed")]
103         private function get vectorLayers():Array {
104                 var v:Array=[];
105                 for (var a:String in map.vectorlayers) {
106                         var vl:VectorLayer=map.vectorlayers[a];
107                         v.push( { name:a, visible:vl.paint.visible, url:vl.url, style:vl.style } );
108                 }
109                 return v;
110         }
111         
112         private function toggleVisibility(event:Event):void {
113                 map.vectorlayers[dataGrid.selectedItem.name].paint.visible = !map.vectorlayers[dataGrid.selectedItem.name].paint.visible;
114         }
115         
116         private function dataEdited(event:DataGridEvent):void {
117                 if (event.dataField=='name') {
118                         // Rename field
119                         var a:String=dataGrid.selectedItem.name;
120                         var b:String=dataGrid.itemEditorInstance['text'];       // this is, unbelievably, how you get data within an itemEditEnd event
121                         if (a==b || map.vectorlayers[b]) {
122                                 event.preventDefault();
123                         } else {
124                                 map.vectorlayers[b]=map.vectorlayers[a];
125                                 delete map.vectorlayers[a];
126                         }
127                         dispatchEvent(new Event("layers_changed"));
128                 }
129         }
130
131         private function removeVectorLayer():void {
132                 var a:String=dataGrid.selectedItem.name;
133                 map.vectorlayers[a].blank();
134                 delete map.vectorlayers[a];
135                 dispatchEvent(new Event("layers_changed"));
136         }
137     
138     private function vectorDialog_close(evt:CloseEvent):void {
139         PopUpManager.removePopUp(this);
140     }
141
142         private function loadFiles(url:String,type:String,simplify:Boolean):void {
143                 var names:Array=url.split('/'); var name:String=names[names.length-1];
144                 var stylesheet:String="potlatch.css";
145                 if (type=='gpx') { stylesheet="gpx.css"; }
146                 var vectorlayer:VectorLayer=new VectorLayer(name,map,stylesheet);
147                 vectorlayer.url=url;
148
149                 var filesLoaded:Function = function(success:Boolean,message:String=null):void {
150                         if (success) {
151                                 map.addVectorLayer(vectorlayer);
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(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
160                 } else if (type=='kml') {
161                         var kml:KmlImporter=new KmlImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
162                 } else if (type=='osm') {
163                         var osm:OsmImporter=new OsmImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
164                 } else {
165                         var re:RegExp=/.shp$/i; url=url.replace(re,'');
166                         var shp:ShpImporter=new ShpImporter(vectorlayer,
167                                                             vectorlayer.paint,
168                                                             [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify);
169                 }
170         }
171
172               ]]>
173   </mx:Script>
174 </mx:TitleWindow>