| 1 | <?xml version="1.0" encoding="utf-8"?> |
|---|
| 2 | <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" |
|---|
| 3 | layout="absolute" |
|---|
| 4 | nativeDragEnter="onNativeDragEnter(event);" |
|---|
| 5 | nativeDragDrop="onNativeDragDrop(event);" |
|---|
| 6 | creationComplete="consumer.subscribe();"> |
|---|
| 7 | |
|---|
| 8 | <mx:Script> |
|---|
| 9 | <![CDATA[ |
|---|
| 10 | import mx.events.DataGridEvent; |
|---|
| 11 | import mx.events.ListEvent; |
|---|
| 12 | import mx.events.FileEvent; |
|---|
| 13 | import flash.desktop.ClipboardFormats; |
|---|
| 14 | import flash.desktop.NativeDragManager; |
|---|
| 15 | import flash.desktop.NativeDragOptions; |
|---|
| 16 | |
|---|
| 17 | import mx.messaging.events.MessageEvent; |
|---|
| 18 | import mx.messaging.messages.AsyncMessage; |
|---|
| 19 | |
|---|
| 20 | import com.adobe.gpslib.GPX; |
|---|
| 21 | |
|---|
| 22 | [Bindable] |
|---|
| 23 | public var gpx : GPX = new GPX(); |
|---|
| 24 | |
|---|
| 25 | private var stream : FileStream; |
|---|
| 26 | private var gpxFile : File; |
|---|
| 27 | |
|---|
| 28 | // Native drag enter function |
|---|
| 29 | public function onNativeDragEnter(event:NativeDragEvent) : void |
|---|
| 30 | { |
|---|
| 31 | // Create a temporary array for the files being dragged in |
|---|
| 32 | var arrFiles : Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array; |
|---|
| 33 | var file : File = arrFiles[0] as File; |
|---|
| 34 | |
|---|
| 35 | // Make sure they're draggin in a .gpx file |
|---|
| 36 | if(event.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT) && file.extension == 'gpx') |
|---|
| 37 | { |
|---|
| 38 | NativeDragManager.acceptDragDrop(this); |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | // Called after a we accept the Drag Drop |
|---|
| 43 | public function onNativeDragDrop(event:NativeDragEvent) : void |
|---|
| 44 | { |
|---|
| 45 | // Create a temporary array to store the files being dragged |
|---|
| 46 | var arrFiles : Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array; |
|---|
| 47 | gpxFile = arrFiles[0] as File; |
|---|
| 48 | |
|---|
| 49 | // Create the filestream and add event listeners |
|---|
| 50 | stream = new FileStream(); |
|---|
| 51 | stream.addEventListener(Event.COMPLETE, onFileOpenComplete); |
|---|
| 52 | stream.openAsync(gpxFile, FileMode.READ); |
|---|
| 53 | |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | // When our file finishes opening |
|---|
| 57 | public function onFileOpenComplete(event:Event) : void |
|---|
| 58 | { |
|---|
| 59 | // Read the data from our GPX file |
|---|
| 60 | var str : String = stream.readMultiByte(stream.bytesAvailable, File.systemCharset); |
|---|
| 61 | stream.close(); |
|---|
| 62 | |
|---|
| 63 | // Create a GPX object from our file data |
|---|
| 64 | gpx.newGpxFromXml(new XML(str)); |
|---|
| 65 | dg.dataProvider = gpx.arrWaypoints; |
|---|
| 66 | |
|---|
| 67 | // Send the message to consumers |
|---|
| 68 | doSend(gpx.arrWaypoints); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | // When we get back a change event make sure the right index is selected |
|---|
| 72 | public function doMessage( event:MessageEvent ):void |
|---|
| 73 | { |
|---|
| 74 | dg.selectedIndex = event.message.body.selectedIndex; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | // Send our changes out to the consumers |
|---|
| 78 | public function doSend( array : Array ):void |
|---|
| 79 | { |
|---|
| 80 | // Create the message and set the message body to our data |
|---|
| 81 | var mess:AsyncMessage = new AsyncMessage(); |
|---|
| 82 | mess.body.waypoints = array; |
|---|
| 83 | producer.send( mess ); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | // When something changes, call this function |
|---|
| 87 | public function doChangeSend( event:ListEvent ) : void |
|---|
| 88 | { |
|---|
| 89 | // Create the message and set the message body to our data |
|---|
| 90 | var mess:AsyncMessage = new AsyncMessage(); |
|---|
| 91 | mess.body.selectedIndex = dg.selectedIndex; |
|---|
| 92 | producer.send( mess ); |
|---|
| 93 | } |
|---|
| 94 | ]]> |
|---|
| 95 | </mx:Script> |
|---|
| 96 | |
|---|
| 97 | <mx:Producer id="producer" destination="GpxShare" /> |
|---|
| 98 | <mx:Consumer id="consumer" destination="GpxShare" message="doMessage( event );" /> |
|---|
| 99 | <mx:DataGrid id="dg" width="100%" height="100%" change="doChangeSend(event);"> |
|---|
| 100 | <mx:columns> |
|---|
| 101 | <mx:Array> |
|---|
| 102 | <mx:DataGridColumn dataField="name" headerText="Name" /> |
|---|
| 103 | <mx:DataGridColumn dataField="latitude" headerText="Latitude" /> |
|---|
| 104 | <mx:DataGridColumn dataField="longitude" headerText="Longitude" /> |
|---|
| 105 | <mx:DataGridColumn dataField="elevation" headerText="Elevation" /> |
|---|
| 106 | </mx:Array> |
|---|
| 107 | </mx:columns> |
|---|
| 108 | </mx:DataGrid> |
|---|
| 109 | </mx:WindowedApplication> |
|---|