Tip:
Highlight text to annotate it
X
In this video, I'm going to describe how to pass complete data sets
from one AIR Application to another using the system clipboard.
As in the previous videos, I'm going to use the standard Clipboard class and it's setData
and getData methods. But this time instead of passing a simple
object, I'm going to use the ArrayCollection class
to hold the data that moves between the applications. Before I show you the code, I'm going to explain,
why this works. The ArrayCollection class implements an interface
named IExternalizable. The rule for moving data between AIR applications
using the Clipboard is that the data object that you pass into
and get out of the Clipboard must implement this interface.
The interface is implemented not just by the ArrayCollection,
but also by also by a class named ObjectProxy, which wraps an object in the same way that
an ArrayCollection wraps an Array. This interface requires two methods named
writeExternal and readExternal. These methods allow you to customize, how
the data is stored, the format of the data and other important aspects
of how the data is represented while it's stored in memory in the Clipboard.
You can actually write your own custom classes that implement this interface.
The exact code for that is beyond the scope of this tutorial.
But what I want to explain here is that the reason we can pass an ArrayCollection between
a source and a Destination Application with the Clipboard
is because it does implement this interface. I will close the Help screen and return back
to the code. I'm using the applications RecordSetSource
and RecordSetDest, that are in the Chapter 02 project.
The source application, the code for which is on my screen right now, starts off as the
same code I used in the last video, that is I'm currently allowing
the user to choose one row of a DataGrid and that I'm copying that data object to the
Clipboard. In this version of the Application I'm going
to allow the users to choose more than one row.
I'll go down to the DataGrid and I will add a property called allow multiple selection
and set its value to true. The user will now be able to click and select
more than one row, while they hold down to Ctrl key on Windows
or the Command key on Mac. Now I'll go back up to the method copytoClipboard,
that's adding the data to the Clipboard. In the old version of the code, I was passing
in a single data object. I'm going to comment out that line of code,
that's getting the data out of the DataGrid and I'll replace it
with a statement, that gets the data out as an ArrayCollection.
It will look like this. I will declare a local variable name of acData
and data type it as an ArrayCollection. I will initialize the ArrayCollection as a
new object using the ArrayCollection class's, no arguments constructor method.
Notice that when you instantiate an ArrayCollection using this syntax, you can pass in an array
as it's source and it just so happens that the DataGrid has
a property called SelectedItems, which returns an Array of all of the currently
selected rows. So, I will pass in personGrid.selectedItems
and now whatever rows the user has selected will be stored
in the ArrayCollection. Now, I'll go to the line of code that's calling
the setData method of the Clipboard. I'll change the name of format.
I already used Custom Format to represent a single object.
Now, I'm going to use something that's more descriptive.
I'll call it simply people and then I'll change the name of the data object that I'm passing
in from dataObj the old single object to acData, the new ArrayCollection.
That's all of the code I need to change in the source application.
I'll save that and go over to the Destination Application.
The Destination Application RecordSetDEST.mxml already has a bindable variable named acPerson
declared as an ArrayCollection and the identical DataGrid
that's the source application, which is binding to the acPerson ArrayCollection
as its data provider. This version of the Application is not getting
its data from the sever. Instead, It will paste the data in from the
Clipboard. Go to the Paste from Clipboard function which
is currently empty. Make a little bit of space between the braces
and assign the value of the bindable, variable acPerson by calling the Clipboard.generalClipboard.getData
method. We have to match the name of the format that
we are passing in. I'll name it People and then as I did in an
earlier video, I'm going to explicitly cast the data as an ArrayCollection.
I know that the data is coming as an ArrayCollection because that's why I put it
into the Clipboard in the source application. That's all of the code I need to put into
this application. The ArrayCollection is already bindable and
it's already bound to the DataGrid as its data provider and the Paste
from Clipboard function is already been called when the user clicks the Paste from Clipboard
button. I'll save the change and run the application.
I'll also go back to Flex Builder and run the source Application and then I'll move
the applications around, so they are sitting side by side on the screen.
Now, I'll hold down the Ctrl key, if you are working on Mac hold
down to Command key instead and then click on Multiple rows.
I'll then click Copy to Clipboard and I will get a verification that the data has been
correctly copied. Now, come over to the Destination Application
and click Paste from Clipboard and you will see the data appear in the destination
DataGrid. In the current version of the Destination
Application each time I select new data in the Source Application and copy it
and then come back over to the destination and paste, I'm replacing the existing data.
I could easily refactor the Destination Application to add Data incrementally if I so chose.
There is one another thing I would like to show you in the Destination Application.
It's a good idea in the Destination Application, before you would retrieve data from the Clipboard
to first make sure that the correct format is stored in the Clipboard.
We can do this using a method of the generalClipboard object named hasFormat.
I'm going to go back to the Paste from Clipboard function and add a conditional clause.
If Clipboard.generalClipboard.hasFormat and then I will pass in the name of the format
I'm looking for and then I will put it in a code block and
I will move the code that retrieves the data from the Clipboard
into the code block.
I'll reformat the code a little bit for readability and now I will only be retrieving data from
the Clipboard if in fact the data is given in the correct
format. There is also a property called formats which
returns an array which is in an ordered list of all
of the formats in which data is currently stored.
Remember that the Clipboard is able to store multiple formats at the same time.
When you use the Clipboard to move data either between AIR Applications or from the operating
system into an AIR application, it's always a good
idea to make sure that the format is already there before you
try to retrieve the data. I'll run the Application one more time just
to make sure that the code still works. I'll try to paste from the Clipboard and you
will see that nothing happens. Now come back to the Source Application and
run it again. I'll select multiple rows, copy it to the
Clipboard, go back to the Destination Application and
paste and there is the result.