Merge remote-tracking branch 'gravitystorm/choices'
[potlatch2.git] / net / systemeD / potlatch2 / mygpx / MyGpxDialog.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         layout="vertical" showCloseButton="true"
6         horizontalAlign="center" title="My tracks"
7         width="600" height="400"
8         verticalGap="0">
9   <fx:Script><![CDATA[
10     import mx.managers.PopUpManager;
11     import mx.events.CloseEvent;
12     import mx.core.Application;
13     import mx.core.FlexGlobals;
14
15     import net.systemeD.halcyon.connection.*;
16     import net.systemeD.potlatch2.save.*;
17     import net.systemeD.halcyon.Map;
18     import net.systemeD.halcyon.Globals;
19     import net.systemeD.potlatch2.utils.Importer;
20     import net.systemeD.potlatch2.utils.GpxImporter;
21     import mx.controls.Alert;
22     import com.bealearts.collection.VectorCollection;
23
24     private var map:Map;
25     private var conn:Connection;
26
27     public function init():void {
28         PopUpManager.addPopUp(this, Application(FlexGlobals.topLevelApplication), true);
29         PopUpManager.centerPopUp(this);
30         this.addEventListener(CloseEvent.CLOSE, myGpxDialog_close);
31
32                 // >>>> REFACTOR: really horrible way of getting both map and connection
33         map = Globals.vars.root;
34                 conn = map.editableLayer.connection;
35
36         conn.addEventListener(Connection.TRACES_LOADED, onTracesLoaded);
37         SaveManager.ensureAccess(fetchList, conn);
38     }
39
40     private function myGpxDialog_close(evt:CloseEvent):void {
41         PopUpManager.removePopUp(this);
42         conn.removeEventListener(Connection.TRACES_LOADED, onTracesLoaded);
43     }
44
45     private function fetchList(refresh:Boolean=false):void {
46         results.text = "Fetching Traces...";
47         conn.fetchUserTraces(refresh);
48     }
49
50     private function onTracesLoaded(e:Event):void {
51         results.text = "Traces Loaded";
52         dispatchEvent(new Event("traces_loaded"));
53     }
54
55     [Bindable(event="traces_loaded")]
56     private function get traces():VectorCollection {
57                 return new VectorCollection(conn.getTraces());
58     }
59
60     public function loadFile(t:Trace):void {
61         t.addToMap();
62     }
63
64     public function filesLoaded(success:Boolean,message:String=null):void {
65         if (success) {
66             dispatchEvent(new Event("layers_changed"));
67         } else {
68             Alert.show(message, 'Error', mx.controls.Alert.OK);
69         }
70     }
71               ]]>
72   </fx:Script>
73
74   <mx:Text text="Traces" id="results" />
75   <mx:DataGrid dataProvider="{traces}" width="100%" height="100%" id="traceGrid">
76     <mx:columns>
77             <mx:DataGridColumn editable="false" dataField="id" headerText="id" width="45"/>
78             <mx:DataGridColumn editable="false" dataField="filename" headerText="filename"/>
79             <mx:DataGridColumn editable="false" dataField="description" headerText="description"/>
80             <mx:DataGridColumn editable="false" dataField="tagsText" headerText="tags"/>
81             <mx:DataGridColumn editable="false">
82               <mx:itemRenderer>
83                 <fx:Component>
84                   <mx:VBox>
85                     <mx:Button label="Load"
86                       click="parentDocument.loadFile(data)"/>
87                   </mx:VBox>
88                 </fx:Component>
89               </mx:itemRenderer>
90             </mx:DataGridColumn>
91     </mx:columns>
92   </mx:DataGrid>
93   <mx:ControlBar>
94     <mx:Button label="Refresh List" click="fetchList(true)" />
95   </mx:ControlBar>
96 </mx:TitleWindow>