refactoring and some bugfixes
[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.data)"
28                                                 dataProvider="{outerDocument.styles}" />
29                                         </mx:Component>
30                                 </mx:itemRenderer>
31                         </mx:DataGridColumn>
32             </mx:columns>
33         </mx:DataGrid>
34         <mx:HBox horizontalAlign="right" width="100%">
35                 <mx:LinkButton label="Delete" click="removeVectorLayer()" enabled="{dataGrid.selectedItem != null? true : false}"/>
36         </mx:HBox>
37
38         <mx:VBox width="100%" paddingTop="10">
39                 <mx:Label htmlText="&lt;b&gt;Add new vector layer&lt;/b&gt;" />
40                 <mx:HBox>
41                         <mx:RadioButtonGroup id="filetype" />
42                         <mx:RadioButton width="100%" groupName="filetype" value="gpx" id="gpx" label="GPX" selected="true" />
43                         <mx:RadioButton width="100%" groupName="filetype" value="osm" id="osm" label="OSM" />
44                         <mx:RadioButton width="100%" groupName="filetype" value="shp" id="shp" label="Shapefile" />
45                         <mx:CheckBox width="100%" label="Simplify paths" selected="true" id="simplify" />
46                 </mx:HBox>
47                 <mx:HBox>
48                         <mx:Text text="URL:"/>
49                         <mx:TextInput width="100%" id="src" text="" />
50                         <mx:Button label="Load" click="loadFiles(src.text, filetype.selectedValue.toString(), simplify.selected);" enabled="{src.text == '' ? false : true}"/>
51                 </mx:HBox>
52         </mx:VBox>
53
54   <mx:Script><![CDATA[
55
56         import net.systemeD.halcyon.Map;
57         import net.systemeD.halcyon.Globals;
58         import net.systemeD.halcyon.VectorLayer;
59         import net.systemeD.potlatch2.utils.Importer;
60         import net.systemeD.potlatch2.utils.GpxImporter;
61         import net.systemeD.potlatch2.utils.ShpImporter;
62         import net.systemeD.potlatch2.utils.OsmImporter;
63     import mx.managers.PopUpManager;
64     import mx.events.DataGridEvent;
65     import mx.events.CloseEvent;
66     import mx.core.Application;
67         import mx.collections.ArrayCollection;
68         import mx.controls.Alert;
69
70         private var map:Map;
71
72         public function styleEdited(name:String,stylesheet:String):void {
73                 map.vectorlayers[name].style=stylesheet;
74                 map.vectorlayers[name].redrawFromCSS(stylesheet)
75         }
76
77
78     public function init():void {
79         PopUpManager.addPopUp(this, Application(Application.application), true);
80         PopUpManager.centerPopUp(this);
81         this.addEventListener(CloseEvent.CLOSE, vectorDialog_close);
82                 dataGrid.addEventListener("visibility_changed", toggleVisibility);
83                 map = Globals.vars.root;
84                 dispatchEvent(new Event("layers_changed"));
85     }
86
87         public function whichStyle(item:Object):Object {
88                 for each (var style:Object in styles) {
89                         if (style.data==item.style) { return style; }
90                 }
91                 return {};
92         }
93
94         [Bindable(event="bogus")]
95         public function get styles():ArrayCollection {
96                 return new ArrayCollection(Application.application.theController.stylesheets);
97         }
98
99         [Bindable(event="layers_changed")]
100         private function get vectorLayers():Array {
101                 var v:Array=[];
102                 for (var a:String in map.vectorlayers) {
103                         var vl:VectorLayer=map.vectorlayers[a];
104                         v.push( { name:a, visible:vl.paint.visible, url:vl.url, style:vl.style } );
105                 }
106                 return v;
107         }
108         
109         private function toggleVisibility(event:Event):void {
110                 map.vectorlayers[dataGrid.selectedItem.name].paint.visible = !map.vectorlayers[dataGrid.selectedItem.name].paint.visible;
111         }
112         
113         private function dataEdited(event:DataGridEvent):void {
114                 if (event.dataField=='name') {
115                         // Rename field
116                         var a:String=dataGrid.selectedItem.name;
117                         var b:String=dataGrid.itemEditorInstance['text'];       // this is, unbelievably, how you get data within an itemEditEnd event
118                         if (a==b || map.vectorlayers[b]) {
119                                 event.preventDefault();
120                         } else {
121                                 map.vectorlayers[b]=map.vectorlayers[a];
122                                 delete map.vectorlayers[a];
123                         }
124                         dispatchEvent(new Event("layers_changed"));
125                 }
126         }
127
128         private function removeVectorLayer():void {
129                 var a:String=dataGrid.selectedItem.name;
130                 map.vectorlayers[a].blank();
131                 delete map.vectorlayers[a];
132                 dispatchEvent(new Event("layers_changed"));
133         }
134     
135     private function vectorDialog_close(evt:CloseEvent):void {
136         PopUpManager.removePopUp(this);
137     }
138
139         private function loadFiles(url:String,type:String,simplify:Boolean):void {
140                 var names:Array=url.split('/'); var name:String=names[names.length-1];
141                 var stylesheet:String="potlatch.css";
142                 if (type=='gpx') { stylesheet="gpx.css"; }
143                 var vectorlayer:VectorLayer=new VectorLayer(name,map,stylesheet);
144                 vectorlayer.url=url;
145                 map.addVectorLayer(vectorlayer);
146
147                 if (type=='gpx') {
148                         var gpx:GpxImporter=new GpxImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
149                 } else if (type=='osm') {
150                         var osm:OsmImporter=new OsmImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
151                 } else {
152                         var re:RegExp=/.shp$/i; url=url.replace(re,'');
153                         var shp:ShpImporter=new ShpImporter(vectorlayer,
154                                                             vectorlayer.paint,
155                                                             [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify);
156                 }
157         }
158         
159         public function filesLoaded(success:Boolean,message:String=null):void {
160                 if (success) {
161                         dispatchEvent(new Event("layers_changed"));
162                 } else {
163                         Alert.show(message, 'Error', mx.controls.Alert.OK);
164                 }
165         }
166
167               ]]>
168   </mx:Script>
169 </mx:TitleWindow>