]> git.openstreetmap.org Git - potlatch2.git/blob - net/systemeD/potlatch2/VectorSourceDialog.mxml
5f6b85b5d62db76211f3b86bd7b2a77093692b00
[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.MapPaint;
60         import net.systemeD.halcyon.Globals;
61         import net.systemeD.halcyon.connection.Connection;
62         import net.systemeD.potlatch2.utils.Importer;
63         import net.systemeD.potlatch2.utils.GpxImporter;
64         import net.systemeD.potlatch2.utils.KmlImporter;
65         import net.systemeD.potlatch2.utils.ShpImporter;
66         import net.systemeD.potlatch2.utils.OsmImporter;
67         import net.systemeD.potlatch2.collections.Stylesheets;
68     import mx.managers.PopUpManager;
69     import mx.events.DataGridEvent;
70     import mx.events.CloseEvent;
71     import mx.core.Application;
72         import mx.collections.ArrayCollection;
73         import mx.controls.Alert;
74
75         private var map:Map;
76
77         public function styleEdited(name:String,stylesheet:String):void {
78                 map.findLayer(name).setStyle(stylesheet);
79         }
80
81
82     public function init():void {
83         PopUpManager.addPopUp(this, Application(Application.application), true);
84         PopUpManager.centerPopUp(this);
85         this.addEventListener(CloseEvent.CLOSE, vectorDialog_close);
86                 dataGrid.addEventListener("visibility_changed", toggleVisibility);
87                 map = Globals.vars.root;
88                 dispatchEvent(new Event("layers_changed"));
89     }
90
91         public function whichStyle(item:Object):Object {
92                 for each (var style:Object in styles) {
93                         if (style.url==item.style) { return style; }
94                 }
95                 return {};
96         }
97
98         [Bindable(event="bogus")]
99         public function get styles():ArrayCollection {
100                 return new ArrayCollection(Stylesheets.instance().collection);
101         }
102
103         [Bindable(event="layers_changed")]
104         private function get vectorLayers():Array {
105         var v:Array=[];
106         for (var i:uint = 0; i < map.paintContainer.numChildren; i++) { //nasty
107             var a:MapPaint = MapPaint(map.paintContainer.getChildAt(i));
108             if (a.isBackground) {
109                 v.push( { name:a.connection.name, visible:a.visible, url:a.connection.apiBase, style:a.style } );
110             }
111                 }
112                 return v;
113         }
114         
115         private function toggleVisibility(event:Event):void {
116                 map.findLayer[dataGrid.selectedItem.name].visible = !map.findLayer[dataGrid.selectedItem.name].visible;
117         }
118         
119         private function dataEdited(event:DataGridEvent):void {
120                 if (event.dataField=='name') {
121                         // Rename field
122                         var a:String=dataGrid.selectedItem.name;
123                         var b:String=dataGrid.itemEditorInstance['text'];       // this is, unbelievably, how you get data within an itemEditEnd event
124                         if (a==b || map.findLayer(b)) {
125                                 event.preventDefault();
126                         } else {
127                                 map.findLayer(a).connection.name = b;
128                         }
129                         dispatchEvent(new Event("layers_changed"));
130                 }
131         }
132
133         private function removeVectorLayer():void {
134                 map.removeLayerByName(dataGrid.selectedItem.name);
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="stylesheets/potlatch.css";
145                 if (type=='gpx') { stylesheet="stylesheets/gpx.css"; }
146
147                 var connection:Connection = new Connection(name, url, null, null);
148                 var mp:MapPaint = map.addLayer(connection, stylesheet);
149
150 // REFACTOR this. commented out to let docs compile.
151         trace("loadFiles not implemented");
152 //                var filesLoaded:Function = function(success:Boolean,message:String=null):void {
153 //                        if (success) {
154 //                                map.addVectorLayer(vectorlayer);
155 //                                dispatchEvent(new Event("layers_changed"));
156 //                        } else {
157 //                                Alert.show(message, 'Error', mx.controls.Alert.OK);
158 //                        }
159 //                }
160
161 //              if (type=='gpx') {
162 //                      var gpx:GpxImporter=new GpxImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
163 //              } else if (type=='kml') {
164 //                      var kml:KmlImporter=new KmlImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
165 //              } else if (type=='osm') {
166 //                      var osm:OsmImporter=new OsmImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
167 //              } else {
168 //                      var re:RegExp=/.shp$/i; url=url.replace(re,'');
169 //                      var shp:ShpImporter=new ShpImporter(vectorlayer,
170 //                                                          vectorlayer.paint,
171 //                                                          [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify);
172 //              }
173         }
174
175               ]]>
176   </mx:Script>
177 </mx:TitleWindow>