Make the dialog automatically fetch the traces. Still needs a guard against repeatedl...
[potlatch2.git] / net / systemeD / potlatch2 / mygpx / MyGpxDialog.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="MyGpx Files"
6         width="600" height="400"
7         verticalGap="0">
8   <mx:Script><![CDATA[
9     import mx.managers.PopUpManager;
10     import mx.events.CloseEvent;
11     import mx.core.Application;
12
13     import net.systemeD.halcyon.connection.*;
14     import net.systemeD.potlatch2.save.*;
15     import net.systemeD.halcyon.Map;
16     import net.systemeD.halcyon.Globals;
17     import net.systemeD.halcyon.VectorLayer;
18     import net.systemeD.potlatch2.utils.Importer;
19     import net.systemeD.potlatch2.utils.GpxImporter;
20     import mx.controls.Alert;
21
22     private var map:Map;
23
24     public function init():void {
25         PopUpManager.addPopUp(this, Application(Application.application), true);
26         PopUpManager.centerPopUp(this);
27         this.addEventListener(CloseEvent.CLOSE, myGpxDialog_close);
28
29         map = Globals.vars.root;
30         fetchList();
31     }
32
33     private function myGpxDialog_close(evt:CloseEvent):void {
34         PopUpManager.removePopUp(this);
35     }
36
37     private function fetchList():void {
38         SaveManager.getAccessSorted();
39         var conn:Connection = Connection.getConnectionInstance();
40         conn.addEventListener(Connection.TRACES_LOADED, onTracesLoaded);
41         conn.fetchUserTraces();
42         
43         results.text = "fetching...";
44     }
45
46     private function onTracesLoaded(e:Event):void {
47         results.text = "loaded!!";
48         // Although the connection emits a Connection.TRACES_LOADED event,
49         // I couldn't get the next binding to work properly. Emitting
50         // an event here does the trick
51         dispatchEvent(new Event("traces_loaded"));
52     }
53
54     // [Bindable(event=Connection.TRACES_LOADED)]
55     [Bindable(event="traces_loaded")]
56     private function get traces():Array {
57         var conn:Connection = Connection.getConnectionInstance();
58         return conn.getTraces();
59     }
60
61     public function loadFile(e:Event):void {
62         var o:Object = traces[traceGrid.selectedIndex];
63         var vectorlayer:VectorLayer=new VectorLayer(o.name,map,"gpx.css");
64         vectorlayer.url=o.url;
65         map.addVectorLayer(vectorlayer);
66         var gpx:GpxImporter=new GpxImporter(vectorlayer, vectorlayer.paint, [o.url], filesLoaded, false);
67     }
68
69     public function filesLoaded(success:Boolean,message:String=null):void {
70         if (success) {
71             dispatchEvent(new Event("layers_changed"));
72         } else {
73             Alert.show(message, 'Error', mx.controls.Alert.OK);
74         }
75     }
76               ]]>
77   </mx:Script>
78
79   <mx:Text text="foo" id="results" />
80   <mx:DataGrid dataProvider="{traces}" width="100%" height="100%" id="traceGrid">
81     <mx:columns>
82             <mx:DataGridColumn editable="false" dataField="id" headerText="id" width="45"/>
83             <mx:DataGridColumn editable="false" dataField="name" headerText="filename"/>
84             <mx:DataGridColumn editable="false" dataField="url" headerText="url"/>
85             <mx:DataGridColumn editable="false">
86               <mx:itemRenderer>
87                 <mx:Component>
88                   <mx:VBox>
89                     <mx:Button label="Load"
90                       click="parentDocument.loadFile(event)"/>
91                   </mx:VBox>
92                 </mx:Component>
93               </mx:itemRenderer>
94             </mx:DataGridColumn>
95     </mx:columns>
96   </mx:DataGrid>
97   <mx:ControlBar>
98     <mx:Button label="Refresh List" click="fetchList()" />
99   </mx:ControlBar>
100 </mx:TitleWindow>