Copy and Paste Sets of Complex Objects in Adobe Air
Posted by CesareThis is a followup post to the previous one, where we have seen how to copy and paste a complex object across two Adobe Air applications. We will see how to copy and paste more than one object from an Adobe Air application to another. First of all we have to enable multiple selection in our Datagrid, that’s easy.
1 2 3 | <mx:DataGrid id="grid" dataProvider="{collection}" allowMultipleSelection="true" > |
We then modify the function that copies data to the clipboard. Instead of a single element we want the list of the selected items in the datagrid (line 3).
1 2 3 4 5 6 7 8 | private function copyToClipboard():void { var selection:ArrayCollection = new ArrayCollection(grid.selectedItems); Clipboard.generalClipboard.clear(); Clipboard.generalClipboard.setData("myCustomFormat", selection); feedback.text = "Data copied to the clipboard"; } |
The rest of the code remains the same of the previous tutorial. The destination application needs a bit more of tweaking to handle an array of items. We have to store data in an ArrayCollection (line 2,5). At this point we could set up a renderer and a repeater to populate the form, but let’s have some fun with actionscript (lines 8-31).
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 | private const HEIGHT:int = 20; private var myItems:ArrayCollection; private function pasteFromClipboard():void { myItems = Clipboard.generalClipboard.getData("myCustomFormat") as ArrayCollection; for each (var i in myItems) { var nameLabel:FormItem = new FormItem(); nameLabel.label = "Name:"; var nameText:TextArea = new TextArea(); nameText.height = HEIGHT; nameText.text = i.name; var emailLabel:FormItem = new FormItem(); emailLabel.label = "Email"; var emailText:TextArea = new TextArea(); emailText.height = HEIGHT; emailText.text = i.email; nameLabel.addChild(nameText); emailLabel.addChild(emailText); pastedItems.addChild(nameLabel); pastedItems.addChild(emailLabel); var hr:HRule = new HRule(); hr.width = 40; pastedItems.addChild(hr); } } |
Here is an example of the final result.

Leave a Reply