Copy and Paste a complex object in Adobe Air
Posted by CesareIn Adobe Air you can store data in the object to enable cross application data transfer. We have already had a look at how to copy and paste images. In this tutorial we will see how to copy complex objects. By complex objects we mean a type of object which is not the usual String, but something a bit more complicated, like an instance of Person.
1 2 3 4 5 6 7 8 9 10 11 12 | public class Person extends Object { public var name:String; public var email:String; public function Person(name:String, email:String) { super(); this.name = name; this.email = email; } } |
We will create two Adobe Air apps: the first is the source from which we copy data, the second is the destination where we paste data in the clipboard. Let’s start with the source application. We create some instance of Person to populate an ArrayCollection which we feed into a DataGrid.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()"> <mx:Script> <![CDATA[ import mx.collections.ArrayCollection; [Bindable] private var collection:ArrayCollection = new ArrayCollection() private function init():void { collection.addItem(new Person("John", "john@john.com")); collection.addItem(new Person("Mary", "mary@mary.com")); } ]]> </mx:Script> <mx:DataGrid id="grid" dataProvider="{collection}"> </mx:DataGrid> </mx:WindowedApplication> |
We will add a button to trigger the copy action of the selected item. Then we need a way to detect the selected item in the grid and store it into the clipboard. We do this in the copyToClipboard function.
1 2 3 4 5 6 7 8 | private function copyToClipboard():void { var selection:Person = grid.selectedItem as Person; Clipboard.generalClipboard.clear(); Clipboard.generalClipboard.setData("myCustomFormat", selection); feedback.text = "Data copied to the clipboard"; } |
Here we should remember to be consistent with the key, myCustomFormat, that we adopt to store data in the clipboard (line 5). We’ll use the same key when we want to retrieve data in the Destination application. Below is the complete code of the Source application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()"> <mx:Script> <![CDATA[ import mx.collections.ArrayCollection; [Bindable] private var collection:ArrayCollection = new ArrayCollection() private function init():void { collection.addItem(new Person("John", "john@john.com")); collection.addItem(new Person("Mary", "mary@mary.com")); } private function copyToClipboard():void { var selection:Person = grid.selectedItem as Person; Clipboard.generalClipboard.clear(); Clipboard.generalClipboard.setData("myCustomFormat", selection); feedback.text = "Data copied to the clipboard"; } ]]> </mx:Script> <mx:DataGrid id="grid" dataProvider="{collection}"> </mx:DataGrid> <mx:Button label="Copy to Clipboad" enabled="{grid.selectedItem != null}" click="copyToClipboard()"/> <mx:Text id="feedback" /> </mx:WindowedApplication> |
Now let’s move to the Destination application, which we will have just a form and a button to paste data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"> <mx:Script> <![CDATA[ [Bindable] private var myObj:Object; private function pasteFromClipboard():void { myObj = Clipboard.generalClipboard.getData("myCustomFormat"); } ]]> </mx:Script> <mx:Form> <mx:FormItem label="Name:"> <mx:TextArea text="{myObj.name}" height="20"/> </mx:FormItem> <mx:FormItem label="Email:"> <mx:TextArea text="{myObj.email}" height="20"/> </mx:FormItem> </mx:Form> <mx:Button label="Paste" click="pasteFromClipboard()"/> </mx:WindowedApplication> |
As said above, to retrieve data in the clipboard we have to use the same key, myCustomFormat. The binding does the rest, populating our form. Here is a video of the result.
Source code is available.
Leave a Reply