Refactor the connection loading of traces, to not reload every time.
[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     private var conn:Connection;
24
25     public function init():void {
26         PopUpManager.addPopUp(this, Application(Application.application), true);
27         PopUpManager.centerPopUp(this);
28         this.addEventListener(CloseEvent.CLOSE, myGpxDialog_close);
29
30         conn = Connection.getConnectionInstance();
31         map = Globals.vars.root;
32
33         SaveManager.getAccessSorted();
34         conn.addEventListener(Connection.TRACES_LOADED, onTracesLoaded);
35         fetchList();
36     }
37
38     private function myGpxDialog_close(evt:CloseEvent):void {
39         PopUpManager.removePopUp(this);
40         conn.removeEventListener(Connection.TRACES_LOADED, onTracesLoaded);
41     }
42
43     private function fetchList(refresh:Boolean=false):void {
44         results.text = "Fetching Traces...";
45         conn.fetchUserTraces(refresh);
46     }
47
48     private function onTracesLoaded(e:Event):void {
49         results.text = "Traces Loaded";
50         dispatchEvent(new Event("traces_loaded"));
51     }
52
53     [Bindable(event="traces_loaded")]
54     private function get traces():Array {
55         return conn.getTraces();
56     }
57
58     public function loadFile(t:Trace):void {
59         t.addToMap();
60     }
61
62     public function filesLoaded(success:Boolean,message:String=null):void {
63         if (success) {
64             dispatchEvent(new Event("layers_changed"));
65         } else {
66             Alert.show(message, 'Error', mx.controls.Alert.OK);
67         }
68     }
69               ]]>
70   </mx:Script>
71
72   <mx:Text text="Traces" id="results" />
73   <mx:DataGrid dataProvider="{traces}" width="100%" height="100%" id="traceGrid">
74     <mx:columns>
75             <mx:DataGridColumn editable="false" dataField="id" headerText="id" width="45"/>
76             <mx:DataGridColumn editable="false" dataField="filename" headerText="filename"/>
77             <mx:DataGridColumn editable="false" dataField="description" headerText="description"/>
78             <mx:DataGridColumn editable="false" dataField="tagsText" headerText="tags"/>
79             <mx:DataGridColumn editable="false">
80               <mx:itemRenderer>
81                 <mx:Component>
82                   <mx:VBox>
83                     <mx:Button label="Load"
84                       click="parentDocument.loadFile(data)"/>
85                   </mx:VBox>
86                 </mx:Component>
87               </mx:itemRenderer>
88             </mx:DataGridColumn>
89     </mx:columns>
90   </mx:DataGrid>
91   <mx:ControlBar>
92     <mx:Button label="Refresh List" click="fetchList(true)" />
93   </mx:ControlBar>
94 </mx:TitleWindow>