move stylesheets into dedicated directory
[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 net.systemeD.potlatch2.collections.Stylesheets;
67     import mx.managers.PopUpManager;
68     import mx.events.DataGridEvent;
69     import mx.events.CloseEvent;
70     import mx.core.Application;
71         import mx.collections.ArrayCollection;
72         import mx.controls.Alert;
73
74         private var map:Map;
75
76         public function styleEdited(name:String,stylesheet:String):void {
77                 map.vectorlayers[name].style=stylesheet;
78                 map.vectorlayers[name].redrawFromCSS(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 a:String in map.vectorlayers) {
107                         var vl:VectorLayer=map.vectorlayers[a];
108                         v.push( { name:a, visible:vl.paint.visible, url:vl.url, style:vl.style } );
109                 }
110                 return v;
111         }
112         
113         private function toggleVisibility(event:Event):void {
114                 map.vectorlayers[dataGrid.selectedItem.name].paint.visible = !map.vectorlayers[dataGrid.selectedItem.name].paint.visible;
115         }
116         
117         private function dataEdited(event:DataGridEvent):void {
118                 if (event.dataField=='name') {
119                         // Rename field
120                         var a:String=dataGrid.selectedItem.name;
121                         var b:String=dataGrid.itemEditorInstance['text'];       // this is, unbelievably, how you get data within an itemEditEnd event
122                         if (a==b || map.vectorlayers[b]) {
123                                 event.preventDefault();
124                         } else {
125                                 map.vectorlayers[b]=map.vectorlayers[a];
126                                 delete map.vectorlayers[a];
127                         }
128                         dispatchEvent(new Event("layers_changed"));
129                 }
130         }
131
132         private function removeVectorLayer():void {
133                 var a:String=dataGrid.selectedItem.name;
134                 map.vectorlayers[a].blank();
135                 delete map.vectorlayers[a];
136                 dispatchEvent(new Event("layers_changed"));
137         }
138     
139     private function vectorDialog_close(evt:CloseEvent):void {
140         PopUpManager.removePopUp(this);
141     }
142
143         private function loadFiles(url:String,type:String,simplify:Boolean):void {
144                 var names:Array=url.split('/'); var name:String=names[names.length-1];
145                 var stylesheet:String="stylesheets/potlatch.css";
146                 if (type=='gpx') { stylesheet="stylesheets/gpx.css"; }
147                 var vectorlayer:VectorLayer=new VectorLayer(name,map,stylesheet);
148                 vectorlayer.url=url;
149
150                 var filesLoaded:Function = function(success:Boolean,message:String=null):void {
151                         if (success) {
152                                 map.addVectorLayer(vectorlayer);
153                                 dispatchEvent(new Event("layers_changed"));
154                         } else {
155                                 Alert.show(message, 'Error', mx.controls.Alert.OK);
156                         }
157                 }
158
159                 if (type=='gpx') {
160                         var gpx:GpxImporter=new GpxImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
161                 } else if (type=='kml') {
162                         var kml:KmlImporter=new KmlImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
163                 } else if (type=='osm') {
164                         var osm:OsmImporter=new OsmImporter(vectorlayer, vectorlayer.paint, [url], filesLoaded, simplify);
165                 } else {
166                         var re:RegExp=/.shp$/i; url=url.replace(re,'');
167                         var shp:ShpImporter=new ShpImporter(vectorlayer,
168                                                             vectorlayer.paint,
169                                                             [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify);
170                 }
171         }
172
173               ]]>
174   </mx:Script>
175 </mx:TitleWindow>