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