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