Refactoring importers
[potlatch2.git] / net / systemeD / potlatch2 / dialogs / VectorSourceDialog.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:TitleWindow
3         xmlns:fx="http://ns.adobe.com/mxml/2009"
4         xmlns:mx="library://ns.adobe.com/flex/mx"
5                 xmlns:s="library://ns.adobe.com/flex/spark"
6         layout="vertical" showCloseButton="true"
7         horizontalAlign="center" title="Load vector file"
8         width="630" height="450" verticalGap="0">
9
10         <mx:DataGrid editable="true" width="100%" height="100%" id="dataGrid" 
11                 dataProvider="{vectorLayers}" itemEditEnd="dataEdited(event)">
12             <mx:columns>
13                 <mx:DataGridColumn editable="false" dataField="visible" headerText="Show?" width="45">
14                                 <mx:itemRenderer>
15                                         <fx:Component>
16                                                 <mx:CheckBox selectedField="isSelected"
17                                                                          click="data.isSelected=!data.isSelected; this.parent.parent.dispatchEvent(new Event('visibility_changed'));" 
18                                                                          paddingLeft="5"/>
19                                         </fx:Component>
20                                 </mx:itemRenderer>
21                         </mx:DataGridColumn>
22                 <mx:DataGridColumn editable="false" dataField="interactive" headerText="Select?" width="45">
23                                 <mx:itemRenderer>
24                                         <!-- There should really be a way to hide this for the editable layer. See
25                                              http://stackoverflow.com/questions/6329895/how-do-i-change-the-style-of-one-individual-cell-in-a-flex-datagrid 
26                                              Leave it as a FIXME until we move to Flex 4 -->
27                                         <fx:Component>
28                                                 <mx:CheckBox selectedField="isInteractive"
29                                                                          click="data.isInteractive=!data.isInteractive; this.parent.parent.dispatchEvent(new Event('interactive_changed'));" 
30                                                              paddingLeft="5"/>
31                                         </fx:Component>
32                                 </mx:itemRenderer>
33                         </mx:DataGridColumn>
34                 <mx:DataGridColumn editable="true"  dataField="name"  headerText="Name"/>
35                 <mx:DataGridColumn editable="false" dataField="url"   headerText="URL"/>
36                 <mx:DataGridColumn editable="false" dataField="style" headerText="Style">
37                                 <mx:itemRenderer>
38                                         <fx:Component>
39                                         <mx:ComboBox
40                                                         selectedItem="{outerDocument.whichStyle(data)}"
41                                                 change="outerDocument.styleEdited(data.name,selectedItem.url)"
42                                                 dataProvider="{outerDocument.styles}" 
43                                                         labelField="name" />
44                                         </fx:Component>
45                                 </mx:itemRenderer>
46                         </mx:DataGridColumn>
47             </mx:columns>
48         </mx:DataGrid>
49         <mx:HBox horizontalAlign="right" width="100%">
50                 <mx:LinkButton label="Delete" click="removeLayer()" enabled="{dataGrid.selectedItem != null &amp;&amp; dataGrid.selectedItem.isBackground}"/>
51         </mx:HBox>
52
53         <fx:Declarations>
54                 <s:RadioButtonGroup id="filetype" />
55         </fx:Declarations>
56         <s:Form id="addlayer" width="100%">
57                 <s:FormHeading label="Add new vector layer" />
58
59                 <s:FormItem label="File type:">
60                         <s:HGroup>
61                                 <s:RadioButton groupName="filetype" value="gpx" id="gpx" label="GPX" selected="true" />
62                                 <s:RadioButton groupName="filetype" value="kml" id="kml" label="KML" />
63                                 <s:RadioButton groupName="filetype" value="osm" id="osm" label="OSM" />
64                                 <s:RadioButton groupName="filetype" value="shp" id="shp" label="Shapefile" />
65                         </s:HGroup>
66                         <s:helpContent><s:Label text="Type of data to load"/></s:helpContent>
67                 </s:FormItem>
68
69                 <s:FormItem label="Projection:">
70                         <s:DropDownList id="projection" selectedIndex="0" width="200"> 
71                                 <s:ArrayCollection>
72                                         <fx:Object label="Lat/long (EPSG:4326)" data="" />
73                                         <fx:Object label="Ordnance Survey GB" data="EPSG:27700" />
74                                         <fx:Object label="NAD83" data="EPSG:4269" />
75                                 </s:ArrayCollection>
76                         </s:DropDownList>
77                         <s:helpContent><s:Label text="Shapefiles only"/></s:helpContent>
78                 </s:FormItem>
79
80                 <s:FormItem label="Simplify:">
81                         <s:CheckBox label="Enabled" id="simplify" />
82                         <s:helpContent><s:Label text="Reduce points in paths"/></s:helpContent>
83                 </s:FormItem>
84
85         <s:FormItem label="File:">
86                         <s:HGroup>
87                                 <s:Button label="Open..." click="loadFileLocal()" />
88                                 <s:Rect width="20" height="1" />
89                     <s:TextInput id="src" text="" change="selectByExtension()" />
90                                 <s:Button label="Fetch" click="loadFiles(src.text);" enabled="{src.text == '' ? false : true}"/>
91                         </s:HGroup>
92             <s:helpContent><s:Label text="URL of file to load" /></s:helpContent>
93         </s:FormItem>
94
95         <s:FormItem label="Tag transform:">
96                         <s:HGroup>
97                                 <s:Button label="Open..." />
98                                 <s:Rect width="20" height="1" />
99                                 <s:TextInput id="transformsrc" text="{CSSTransform.getInstance().url}"/>
100                                 <s:Button label="Fetch" click="loadTransform(transformsrc.text);" enabled="{transformsrc.text == '' ? false : true}"/>
101                                 <s:Button label="Clear" click="clearTransform();"                 enabled="{transformsrc.text == '' ? false : true}"/>
102                         </s:HGroup>
103             <s:helpContent><s:Label text="MapCSS transformation file" /></s:helpContent>
104         </s:FormItem>
105
106         </s:Form>
107
108   <fx:Script><![CDATA[
109
110         import net.systemeD.halcyon.Map;
111         import net.systemeD.halcyon.MapPaint;
112         import net.systemeD.halcyon.Globals;
113         import net.systemeD.halcyon.connection.Connection;
114         import net.systemeD.halcyon.styleparser.CSSTransform;
115         import net.systemeD.potlatch2.utils.*;
116         import net.systemeD.potlatch2.collections.Stylesheets;
117     import mx.managers.PopUpManager;
118     import mx.events.DataGridEvent;
119     import mx.events.CloseEvent;
120     import mx.core.Application;
121     import mx.core.FlexGlobals;
122         import mx.collections.ArrayCollection;
123         import mx.controls.Alert;
124
125         private var map:Map;
126
127         public function styleEdited(name:String,stylesheet:String):void {
128                 map.findLayer(name).setStyle(stylesheet);
129         }
130
131
132     public function init():void {
133         PopUpManager.addPopUp(this, Application(FlexGlobals.topLevelApplication), true);
134         PopUpManager.centerPopUp(this);
135         this.addEventListener(CloseEvent.CLOSE, vectorDialog_close);
136                 dataGrid.addEventListener("visibility_changed", toggleVisibility);
137                 dataGrid.addEventListener("interactive_changed", toggleInteractive);
138                 map = Globals.vars.root;
139                 dispatchEvent(new Event("layers_changed"));
140     }
141
142         public function whichStyle(item:Object):Object {
143                 for each (var style:Object in styles) {
144                         if (style.url==item.style) { return style; }
145                 }
146                 return {};
147         }
148
149         [Bindable(event="bogus")]
150         public function get styles():ArrayCollection {
151                 return new ArrayCollection(Stylesheets.instance().collection);
152         }
153
154         [Bindable(event="layers_changed")]
155         private function get vectorLayers():Array {
156         var v:Array=[];
157         for each (var a:MapPaint in map.getLayers() ) {
158             v.push( { name:a.connection.name, visible:a.visible, interactive:a.interactive, url:a.connection.apiBase, style:a.style, isBackground:a.isBackground } );
159         }
160                 return v;
161         }
162         
163         private function get selectedType():String {
164                 return filetype.selectedValue.toString();
165         }
166         
167         private function toggleVisibility(event:Event):void {
168                 map.findLayer(dataGrid.selectedItem.name).visible = !map.findLayer(dataGrid.selectedItem.name).visible;
169         }
170
171         private function toggleInteractive(event:Event):void {
172                 map.findLayer(dataGrid.selectedItem.name).interactive = !map.findLayer(dataGrid.selectedItem.name).interactive;
173         }
174         
175         private function dataEdited(event:DataGridEvent):void {
176                 if (event.dataField=='name') {
177                         // Rename field
178                         var a:String=dataGrid.selectedItem.name;
179                         var b:String=dataGrid.itemEditorInstance['text'];       // this is, unbelievably, how you get data within an itemEditEnd event
180                         if (a==b || map.findLayer(b)) {
181                                 event.preventDefault();
182                         } else {
183                                 map.findLayer(a).connection.name = b;
184                         }
185                         dispatchEvent(new Event("layers_changed"));
186                 }
187         }
188
189         private function removeLayer():void {
190                 map.removeLayerByName(dataGrid.selectedItem.name);
191                 dispatchEvent(new Event("layers_changed"));
192         }
193     
194         private function selectByExtension():void {
195                 if      (src.text.match(/\.shp$/i)) filetype.selectedValue='shp'
196                 else if (src.text.match(/\.osm$/i)) filetype.selectedValue='osm'
197                 else if (src.text.match(/\.gpx$/i)) filetype.selectedValue='gpx'
198                 else if (src.text.match(/\.kml$/i)) filetype.selectedValue='kml'
199         }
200
201     private function vectorDialog_close(evt:CloseEvent):void {
202         PopUpManager.removePopUp(this);
203     }
204
205         /** Launch importer for remotely hosted files. */
206         private function loadFiles(url:String):void {
207                 var names:Array=url.split('/'); var name:String=names[names.length-1];
208                 var connection:Connection = new Connection(name, url, null, null, CSSTransform.getInstance());
209                 var importer:Importer = findImporter(connection);
210                 if (selectedType=='shp') {
211                         importer.importFromRemoteFiles([url]);
212                 } else {
213                         var re:RegExp=/.shp$/i; url=url.replace(re,'');
214                         importer.importFromRemoteFiles([url+".shp",url+".shx",url+".dbf"]);
215                 }
216         }
217         
218         /** Present an 'Open File' dialogue. */
219         private function loadFileLocal():void {
220                 var file:FileReference=new FileReference(); 
221                 var fileTypes:FileFilter=new FileFilter("*."+selectedType, "*."+selectedType); 
222                 file.browse([fileTypes]); 
223                 file.addEventListener(Event.SELECT, selectFileLocal);
224         }
225         
226         /** The user has chosen a file in the local 'Open File' dialogue. */
227         private function selectFileLocal(event:Event):void {
228                 var connection:Connection = new Connection(name, url, null, null, CSSTransform.getInstance());
229                 var importer:Importer = findImporter(connection);
230                 importer.importFromLocalFile();
231         }
232         
233         /** Create an Importer object of the type selected by the user. */
234         private function findImporter(connection:Connection):Importer {
235                 var simplifyPath:Boolean=simplify.selected;
236                 var reproject:String=projection.selectedItem.data;
237                 var stylesheet:String="stylesheets/potlatch.css"; if (selectedType=='gpx') { stylesheet="stylesheets/gpx.css"; }
238
239                 switch (selectedType) {
240                         case 'gpx':     return new GpxImporter(connection, map, filesLoaded, simplifyPath, { stylesheet: stylesheet });
241                         case 'kml':     return new KmlImporter(connection, map, filesLoaded, simplifyPath, { stylesheet: stylesheet });
242                         case 'osm': return new OsmImporter(connection, map, filesLoaded, simplifyPath, { stylesheet: stylesheet });
243                         case 'shp': return new ShpImporter(connection, map, filesLoaded, simplifyPath, { stylesheet: stylesheet, projection: reproject });
244                 }
245                 return null;
246         }
247         
248         /** Callback routine when import has completed. */
249         private function filesLoaded(connection:Connection,options:Object,success:Boolean,message:String=null):void {
250                 if (success) {
251                         var paint:MapPaint = map.addLayer(connection, options['stylesheet']);
252                         paint.updateEntityUIs(false, false);
253                         dispatchEvent(new Event("layers_changed"));
254                 } else {
255                         Alert.show(message, 'Error', mx.controls.Alert.OK);
256                 }
257         }
258         
259         private function loadTransform(url:String):void {
260                 CSSTransform.getInstance().loadFromUrl(url);
261         }
262         private function clearTransform():void {
263                 CSSTransform.getInstance().clear();
264         }
265
266               ]]>
267   </fx:Script>
268 </mx:TitleWindow>