<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Spreading Funkyness &#187; actionscript</title>
	<atom:link href="http://spreadingfunkyness.com/category/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://spreadingfunkyness.com</link>
	<description>RIAbilitating the Internet with web apps, ria, iphone and ipad apps.</description>
	<lastBuildDate>Fri, 27 Apr 2012 22:44:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
<cloud domain='spreadingfunkyness.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>6 Tutorials on Copy&amp;Paste and Drag&amp;Drop in Adobe Air</title>
		<link>http://spreadingfunkyness.com/6-tutorials-on-copypaste-and-dragdrop-in-adobe-air/</link>
		<comments>http://spreadingfunkyness.com/6-tutorials-on-copypaste-and-dragdrop-in-adobe-air/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 13:32:20 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[adobeair]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=308</guid>
		<description><![CDATA[I recently published a series of tutorials around the topic of &#8220;moving stuff&#8221; across Adobe Air applications (via copy&#038;paste and drag&#038;drop). Here is the complete list of all the tutorials. Native Drag and Drop in Adobe Air The first tutorial, to get started with drag and drop. Drag and Drop of Text in Adobe Air ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 0px 10px 0px 0px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fspreadingfunkyness.com%2F6-tutorials-on-copypaste-and-dragdrop-in-adobe-air%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2F6-tutorials-on-copypaste-and-dragdrop-in-adobe-air%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I recently published a series of tutorials around the topic of &#8220;moving stuff&#8221; across Adobe Air applications (via copy&#038;paste and drag&#038;drop). Here is the complete list of all the tutorials.</p>
<ul>
<li><a href="http://spreadingfunkyness.com/native-drag-and-drop-in-adobe-air/">Native Drag and Drop in Adobe Air</a><br />
The first tutorial, to get started with drag and drop.</li>
<li><a href="http://spreadingfunkyness.com/drag-and-drop-of-text-in-adobe-air/">Drag and Drop of Text in Adobe Air</a><br />
An overview of how to drag and drop textual content in Adobe Air.</li>
<li><a href="http://spreadingfunkyness.com/drag-and-drop-of-images-in-adobe-air/">Drag and Drop of Images In Adobe Air</a><br />
A tutorial on drag and drop from OS to Adobe air and across Air apps.</li>
<li><a href="http://spreadingfunkyness.com/copy-and-paste-images-in-adobe-air/">Copy and Paste Images in Adobe Air</a><br />
The first tutorial on copy and paste, focused on images.</li>
<li><a href="http://spreadingfunkyness.com/copy-and-paste-a-complex-object-in-adobe-air/">Copy and Paste a complex object in Adobe Air</a><br />
Description of how to move a custom object across Adobe Air applications.
</li>
<li><a href="http://spreadingfunkyness.com/copy-and-paste-sets-of-complex-objects-in-adobe-air/">Copy and Paste Sets of Complex Objects in Adobe Air</a><br />
A tutorial about how to copy and paste arrays of custom objects.
</li>
</ul>
<p>I am planning to release more series about Adobe Air/Flex. Any topic your prefer? Leave a comment below or contact me.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fspreadingfunkyness.com%2F6-tutorials-on-copypaste-and-dragdrop-in-adobe-air%2F&amp;title=6%20Tutorials%20on%20Copy%26%23038%3BPaste%20and%20Drag%26%23038%3BDrop%20in%20Adobe%20Air" id="wpa2a_2"><img src="http://spreadingfunkyness.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/6-tutorials-on-copypaste-and-dragdrop-in-adobe-air/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Copy and Paste Sets of Complex Objects in Adobe Air</title>
		<link>http://spreadingfunkyness.com/copy-and-paste-sets-of-complex-objects-in-adobe-air/</link>
		<comments>http://spreadingfunkyness.com/copy-and-paste-sets-of-complex-objects-in-adobe-air/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 13:37:27 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[adobeair]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=306</guid>
		<description><![CDATA[This is a followup post to the <a href="http://spreadingfunkyness.com/copy-and-paste-a-complex-object-in-adobe-air/">previous one</a>, 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]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 0px 10px 0px 0px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fspreadingfunkyness.com%2Fcopy-and-paste-sets-of-complex-objects-in-adobe-air%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fcopy-and-paste-sets-of-complex-objects-in-adobe-air%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This is a followup post to the <a href="http://spreadingfunkyness.com/copy-and-paste-a-complex-object-in-adobe-air/">previous one</a>, 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&#8217;s easy.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>mx:DataGrid id=<span style="color: #ff0000;">&quot;grid&quot;</span> 
	dataProvider=<span style="color: #ff0000;">&quot;{collection}&quot;</span>
	allowMultipleSelection=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>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).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> copyToClipboard<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">selection</span>:ArrayCollection = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span>grid.<span style="color: #006600;">selectedItems</span><span style="color: #66cc66;">&#41;</span>;
	Clipboard.<span style="color: #006600;">generalClipboard</span>.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	Clipboard.<span style="color: #006600;">generalClipboard</span>.<span style="color: #006600;">setData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;myCustomFormat&quot;</span>, <span style="color: #0066CC;">selection</span><span style="color: #66cc66;">&#41;</span>;
	feedback.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Data copied to the clipboard&quot;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The rest of the code remains the same of the <a href="http://spreadingfunkyness.com/copy-and-paste-a-complex-object-in-adobe-air/">previous tutorial</a>. 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&#8217;s have some fun with actionscript (lines 8-31).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> const <span style="color: #0066CC;">HEIGHT</span>:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">20</span>;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> myItems:ArrayCollection;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> pasteFromClipboard<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
	myItems = Clipboard.<span style="color: #006600;">generalClipboard</span>.<span style="color: #006600;">getData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;myCustomFormat&quot;</span><span style="color: #66cc66;">&#41;</span> 
		as ArrayCollection;
&nbsp;
	<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i <span style="color: #b1b100;">in</span> myItems<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">var</span> nameLabel:FormItem = <span style="color: #000000; font-weight: bold;">new</span> FormItem<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		nameLabel.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Name:&quot;</span>;
		<span style="color: #000000; font-weight: bold;">var</span> nameText:TextArea = <span style="color: #000000; font-weight: bold;">new</span> TextArea<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		nameText.<span style="color: #0066CC;">height</span> = <span style="color: #0066CC;">HEIGHT</span>;
		nameText.<span style="color: #0066CC;">text</span> = i.<span style="color: #0066CC;">name</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">var</span> emailLabel:FormItem = <span style="color: #000000; font-weight: bold;">new</span> FormItem<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		emailLabel.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Email&quot;</span>;
		<span style="color: #000000; font-weight: bold;">var</span> emailText:TextArea = <span style="color: #000000; font-weight: bold;">new</span> TextArea<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		emailText.<span style="color: #0066CC;">height</span> = <span style="color: #0066CC;">HEIGHT</span>;
		emailText.<span style="color: #0066CC;">text</span> = i.<span style="color: #006600;">email</span>;
&nbsp;
		nameLabel.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>nameText<span style="color: #66cc66;">&#41;</span>;
		emailLabel.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>emailText<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		pastedItems.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>nameLabel<span style="color: #66cc66;">&#41;</span>;
		pastedItems.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>emailLabel<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">var</span> hr:HRule = <span style="color: #000000; font-weight: bold;">new</span> HRule<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		hr.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">40</span>;
		pastedItems.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>hr<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Here is an example of the final result.</p>
<p><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=42503' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=42503' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object></p>
<p><a href='http://spreadingfunkyness.com/wp-content/uploads/2010/01/ComplexMultiCopyPaste.zip'>Source code is available.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/copy-and-paste-sets-of-complex-objects-in-adobe-air/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Copy and Paste a complex object in Adobe Air</title>
		<link>http://spreadingfunkyness.com/copy-and-paste-a-complex-object-in-adobe-air/</link>
		<comments>http://spreadingfunkyness.com/copy-and-paste-a-complex-object-in-adobe-air/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 11:49:32 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[adobeair]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=304</guid>
		<description><![CDATA[In 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, ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 0px 10px 0px 0px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fspreadingfunkyness.com%2Fcopy-and-paste-a-complex-object-in-adobe-air%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fcopy-and-paste-a-complex-object-in-adobe-air%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In Adobe Air you can store data in the object to enable cross application data transfer. We have already had a look at <a href="http://spreadingfunkyness.com/copy-and-paste-images-in-adobe-air/">how to copy and paste images</a>. 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.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">Object</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> email:<span style="color: #0066CC;">String</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span>, email:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">name</span> = <span style="color: #0066CC;">name</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">email</span> = email;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>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&#8217;s start with the source application. We create some instance of Person to populate an ArrayCollection which we feed into a DataGrid.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>mx:WindowedApplication 
	xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> 
	layout=<span style="color: #ff0000;">&quot;vertical&quot;</span> 
	creationComplete=<span style="color: #ff0000;">&quot;init()&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
&nbsp;
		<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">collections</span>.<span style="color: #006600;">ArrayCollection</span>;
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span> 
                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> collection:ArrayCollection = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			collection.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;John&quot;</span>, <span style="color: #ff0000;">&quot;john@john.com&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			collection.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Mary&quot;</span>, <span style="color: #ff0000;">&quot;mary@mary.com&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;</span>mx:DataGrid id=<span style="color: #ff0000;">&quot;grid&quot;</span> 
		dataProvider=<span style="color: #ff0000;">&quot;{collection}&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;/</span>mx:DataGrid<span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;/</span>mx:WindowedApplication<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>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.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> copyToClipboard<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">selection</span>:Person = grid.<span style="color: #006600;">selectedItem</span> as Person;
	Clipboard.<span style="color: #006600;">generalClipboard</span>.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	Clipboard.<span style="color: #006600;">generalClipboard</span>.<span style="color: #006600;">setData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;myCustomFormat&quot;</span>, <span style="color: #0066CC;">selection</span><span style="color: #66cc66;">&#41;</span>;
	feedback.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Data copied to the clipboard&quot;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Here we should remember to be consistent with the key, <code>myCustomFormat</code>, that we adopt to store data in the clipboard (line 5). We&#8217;ll use the same key when we want to retrieve data in the Destination application. Below is the complete code of the Source application.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>mx:WindowedApplication 
	xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> 
	layout=<span style="color: #ff0000;">&quot;vertical&quot;</span> 
	creationComplete=<span style="color: #ff0000;">&quot;init()&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">collections</span>.<span style="color: #006600;">ArrayCollection</span>;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span> 
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> collection:ArrayCollection = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
		collection.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;John&quot;</span>, <span style="color: #ff0000;">&quot;john@john.com&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		collection.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Mary&quot;</span>, <span style="color: #ff0000;">&quot;mary@mary.com&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> copyToClipboard<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">selection</span>:Person = grid.<span style="color: #006600;">selectedItem</span> as Person;
		Clipboard.<span style="color: #006600;">generalClipboard</span>.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		Clipboard.<span style="color: #006600;">generalClipboard</span>.<span style="color: #006600;">setData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;myCustomFormat&quot;</span>, <span style="color: #0066CC;">selection</span><span style="color: #66cc66;">&#41;</span>;
		feedback.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Data copied to the clipboard&quot;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;</span>mx:DataGrid id=<span style="color: #ff0000;">&quot;grid&quot;</span> 
		dataProvider=<span style="color: #ff0000;">&quot;{collection}&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;/</span>mx:DataGrid<span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> label=<span style="color: #ff0000;">&quot;Copy to Clipboad&quot;</span> 
		<span style="color: #0066CC;">enabled</span>=<span style="color: #ff0000;">&quot;{grid.selectedItem != null}&quot;</span> 
		click=<span style="color: #ff0000;">&quot;copyToClipboard()&quot;</span><span style="color: #66cc66;">/&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Text</span> id=<span style="color: #ff0000;">&quot;feedback&quot;</span> <span style="color: #66cc66;">/&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;/</span>mx:WindowedApplication<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>Now let&#8217;s move to the Destination application, which we will have just a form and a button to paste data.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>mx:WindowedApplication 
	xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> 
	layout=<span style="color: #ff0000;">&quot;vertical&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span> 
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> myObj:<span style="color: #0066CC;">Object</span>;
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> pasteFromClipboard<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
		myObj = Clipboard.<span style="color: #006600;">generalClipboard</span>.<span style="color: #006600;">getData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;myCustomFormat&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:Form<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>mx:FormItem label=<span style="color: #ff0000;">&quot;Name:&quot;</span><span style="color: #66cc66;">&gt;</span>
			<span style="color: #66cc66;">&lt;</span>mx:TextArea <span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;{myObj.name}&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;20&quot;</span><span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;/</span>mx:FormItem<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>mx:FormItem label=<span style="color: #ff0000;">&quot;Email:&quot;</span><span style="color: #66cc66;">&gt;</span>
			<span style="color: #66cc66;">&lt;</span>mx:TextArea <span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;{myObj.email}&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;20&quot;</span><span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;/</span>mx:FormItem<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:Form<span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> label=<span style="color: #ff0000;">&quot;Paste&quot;</span> click=<span style="color: #ff0000;">&quot;pasteFromClipboard()&quot;</span><span style="color: #66cc66;">/&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:WindowedApplication<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>As said above, to retrieve data in the clipboard we have to use the same key, <code>myCustomFormat</code>. The binding does the rest, populating our form. Here is a video of the result.</p>
<p><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=41246' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=41246' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object></p>
<p><a href='http://spreadingfunkyness.com/wp-content/uploads/2010/01/ComplexCopyPaste.zip'>Source code</a> is available.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fspreadingfunkyness.com%2Fcopy-and-paste-a-complex-object-in-adobe-air%2F&amp;title=Copy%20and%20Paste%20a%20complex%20object%20in%20Adobe%20Air" id="wpa2a_4"><img src="http://spreadingfunkyness.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/copy-and-paste-a-complex-object-in-adobe-air/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Copy and Paste Images in Adobe Air</title>
		<link>http://spreadingfunkyness.com/copy-and-paste-images-in-adobe-air/</link>
		<comments>http://spreadingfunkyness.com/copy-and-paste-images-in-adobe-air/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 12:30:31 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[adobeair]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=298</guid>
		<description><![CDATA[How to copy and paste images in Adobe Air.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 0px 10px 0px 0px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fspreadingfunkyness.com%2Fcopy-and-paste-images-in-adobe-air%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fcopy-and-paste-images-in-adobe-air%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>After learning <a href="http://spreadingfunkyness.com/drag-and-drop-of-images-in-adobe-air/">drag</a> and <a href="http://spreadingfunkyness.com/drag-and-drop-of-text-in-adobe-air/">drop</a> we will dig into copy and paste. We have <a href="http://spreadingfunkyness.com/native-drag-and-drop-in-adobe-air/">already seen</a> how to use the clipboard class to temporary store data during the drag action. Now we will use the same class to copy and paste an image within an Adobe Air application. We will start with the following layout.</p>
<p><a href="http://spreadingfunkyness.com/wp-content/uploads/2010/01/layout.gif"><img src="http://spreadingfunkyness.com/wp-content/uploads/2010/01/layout.gif" alt="" title="Layout of the application" width="500" height="397" class="aligncenter size-full wp-image-299" align="center" /></a></p>
<p>This corresponds to the following mxml code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>mx:WindowedApplication 
	xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> 
	<span style="color: #0066CC;">backgroundColor</span>=<span style="color: #ff0000;">&quot;#222222&quot;</span> <span style="color: #0066CC;">color</span>=<span style="color: #ff0000;">&quot;#f5f5f5&quot;</span> 
	layout=<span style="color: #ff0000;">&quot;vertical&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;!</span>-- Feedback --<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Text</span> id=<span style="color: #ff0000;">&quot;feedback&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;20&quot;</span><span style="color: #66cc66;">/&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;!</span>-- Image to <span style="color: #0066CC;">copy</span> --<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:Image id=<span style="color: #ff0000;">&quot;im&quot;</span> source=<span style="color: #ff0000;">&quot;logo.png&quot;</span><span style="color: #66cc66;">/&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;!</span>-- Commands --<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:HBox<span style="color: #66cc66;">&gt;</span>
	    <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> label=<span style="color: #ff0000;">&quot;copy&quot;</span> 
	 	click=<span style="color: #ff0000;">&quot;copyToClipboard()&quot;</span><span style="color: #66cc66;">/&gt;</span>
	    <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> label=<span style="color: #ff0000;">&quot;paste&quot;</span> 
		click=<span style="color: #ff0000;">&quot;pasteFromClipboard()&quot;</span><span style="color: #66cc66;">/&gt;</span>	
	    <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> label=<span style="color: #ff0000;">&quot;clear&quot;</span> 
		click=<span style="color: #ff0000;">&quot;Clipboard.generalClipboard.clearData(ClipboardFormats.BITMAP_FORMAT)&quot;</span><span style="color: #66cc66;">/&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:HBox<span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;</span>mx:VBox id=<span style="color: #ff0000;">&quot;copies&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;{im.width}&quot;</span><span style="color: #66cc66;">/&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:WindowedApplication<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>Let&#8217;s have a look at the copy mechanism. We want to be sure that, before copying, the clipboard has no bitmap (line 3). Then we create a BitmapData object to store the content of the image (lines 4-5) and we put it in the clipboard (line 6). We gently provide some feedback (line 7).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> copyToClipboard<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    Clipboard.<span style="color: #006600;">generalClipboard</span>.<span style="color: #006600;">clearData</span><span style="color: #66cc66;">&#40;</span>ClipboardFormats.<span style="color: #006600;">BITMAP_FORMAT</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">data</span>:BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>im.<span style="color: #0066CC;">width</span>, im.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0066CC;">data</span>.<span style="color: #006600;">draw</span><span style="color: #66cc66;">&#40;</span>im<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// this copies the content in the clipboard</span>
    Clipboard.<span style="color: #006600;">generalClipboard</span>.<span style="color: #006600;">setData</span><span style="color: #66cc66;">&#40;</span>ClipboardFormats.<span style="color: #006600;">BITMAP_FORMAT</span>, <span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
    feedback.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Data have been copied&quot;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The paste mechanism is pretty similar to the one illustrated in <a href="http://spreadingfunkyness.com/drag-and-drop-of-text-in-adobe-air/">previous tutorials</a>.  We read data from the clipboard (line 3), create a new Image class to store the Bitmap and then add it to the duplicates area (lines 4-7).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> pasteFromClipboard<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> b:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span>Clipboard.<span style="color: #006600;">generalClipboard</span>.<span style="color: #006600;">getData</span><span style="color: #66cc66;">&#40;</span>ClipboardFormats.<span style="color: #006600;">BITMAP_FORMAT</span><span style="color: #66cc66;">&#41;</span> as BitmapData<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> i:Image = <span style="color: #000000; font-weight: bold;">new</span> Image<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    i.<span style="color: #0066CC;">height</span> = b.<span style="color: #0066CC;">height</span>;
    i.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>b<span style="color: #66cc66;">&#41;</span>;
    copies.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This is the intended result.</p>
<p><object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=40450' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=40450' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object></p>
<p><a href='http://spreadingfunkyness.com/wp-content/uploads/2010/01/ImageCopyAndPaste.zip'>Source code</a>  is available.<br />
Next time we will see how to copy and paste a complex object.</p>
]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/copy-and-paste-images-in-adobe-air/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Code sharing between Flex and Air applications</title>
		<link>http://spreadingfunkyness.com/code-sharing-between-flex-and-air-applications/</link>
		<comments>http://spreadingfunkyness.com/code-sharing-between-flex-and-air-applications/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 11:51:01 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[adobeair]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=275</guid>
		<description><![CDATA[A simple way to share code between a Flex and an Air project.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 0px 10px 0px 0px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fspreadingfunkyness.com%2Fcode-sharing-between-flex-and-air-applications%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fcode-sharing-between-flex-and-air-applications%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>If you are planning to build a RIA both in web and desktop format Flex is a good candidate. I suspect that both will be pretty similar, and probably the desktop version will have extended functionalities that exploit the capabilities of the operative system. If you are the programmer of such a RIA you probably might not want to have two separate projects. Or at least you might need to have some core code, shared by projects, which you change just once. Although <a href="http://www.adobe.com/devnet/air/flex/articles/flex_air_codebase.html">some solution</a> has already been proposed, here is my take, a very simple one: a <a href="http://en.wikipedia.org/wiki/Symbolic_link">symbolic link</a>!<br />
Just put the core code in a Flex project and then create a folder in the Air project, which is not a real folder but a symbolic link to the folder already contained in the Flex project.</p>
<h2>Step by Step</h2>
<p>Let&#8217;s start with a Flex project. We&#8217;ll name it <code>MyFlexProject</code>. Then we create a class, called <code>MyClass</code>, as in the picture below.<br />
<a href="http://spreadingfunkyness.com/wp-content/uploads/2009/07/MyClass.gif"><img src="http://spreadingfunkyness.com/wp-content/uploads/2009/07/MyClass.gif" alt="Creation of MyClass" title="MyClass" width="523" height="326" class="size-full wp-image-276" /></a></p>
<p>We will add a <code>trace</code> statement to show when an instance of <code>MyClass</code> is created.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">studiomagnolia</span>.<span style="color: #006600;">shared</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">Object</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Created an instance of MyClass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><br/><br />
Now we create the Air project. Let&#8217;s call it <code>MyAirProject</code>. And here comes the trick. Open a shell and move to the <code>src</code> directory of the Air project. Here you have to create a symbolic link which points to the <code>src/com</code> folder of <code>MyFlexProject</code>.<br />
The code for <a href="http://en.wikipedia.org/wiki/Bash">bash shell</a> is the following:</p>
<pre>

      ln -s ../../MyFlexProject/src/com
</pre>
<p>Now if you refresh MyAirProject you will see that the <code>src</code> folder contains a <code>com</code> subfolder. Cool uh? Now let&#8217;s write some code to see whether we can use MyClass in the Air project.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:WindowedApplication</span> <span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> <span style="color: #000066;">layout</span>=<span style="color: #ff0000;">&quot;absolute&quot;</span> <span style="color: #000066;">creationComplete</span>=<span style="color: #ff0000;">&quot;onCreationComplete()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #339933;">&lt;![CDATA[</span>
&nbsp;
<span style="color: #339933;">			import com.studiomagnolia.shared.MyClass;</span>
&nbsp;
<span style="color: #339933;">			private function onCreationComplete():void {</span>
<span style="color: #339933;">				var c:MyClass = new MyClass();</span>
&nbsp;
<span style="color: #339933;">			}</span>
&nbsp;
<span style="color: #339933;">		]]&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:WindowedApplication<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>In the console you should see the trace statement. You can access/edit/save the core code from both the Flex and the Air project. Try changing the trace statement within the Air project and you&#8217;ll see changes reflected into <code>MyFlexProject</code>.<br />
<br/></p>
<p>Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/code-sharing-between-flex-and-air-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash on Android? Soon</title>
		<link>http://spreadingfunkyness.com/flash-on-android-soon/</link>
		<comments>http://spreadingfunkyness.com/flash-on-android-soon/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 18:24:11 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=258</guid>
		<description><![CDATA[Flash running on the android platform? Yes. Flash was already been demonstrated as running on android. Now Bsquare has been charged to make the porting of the flash technology to the android platform. I started the study of the iphone sdk a while ago, and so did many flexers/actionscripters. But this news makes me mumble. ...]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 0px 10px 0px 0px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fspreadingfunkyness.com%2Fflash-on-android-soon%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fflash-on-android-soon%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Flash running on the <a href="http://www.android.com/">android platform</a>? Yes. Flash was already been <a href="http://news.cnet.com/8301-1001_3-10098883-92.html">demonstrated</a> as running on android. Now Bsquare <a href="http://asia.cnet.com/crave/2009/03/25/bsquare-bringing-flash-to-android-phones/">has been charged to make the porting</a> of the flash technology to the android platform. I started the study of the <a href="http://developer.apple.com/iphone/">iphone sdk</a> a while ago, and so did many <a href="http://www.craftymind.com/2008/04/30/from-as3-to-objective-c-flex-vs-iphone-development/">flexers</a>/<a href="http://andyj.be/blog/?page_id=10">actionscripters</a>. But this news makes me mumble. I like learning new languages/frameworks <em>a priori</em> but I probably will have to set a preference: android or iphone?</p>
<p>I have set up a poll here. State your opinion!</p>
<p><center><br />
<script type="text/javascript" charset="utf-8" language="javascript" src="http://static.polldaddy.com/p/1490436.js"></script><noscript> <a href ="http://answers.polldaddy.com/poll/1490436/" >Actionscripter, you wanna learn programming on a mobile platform: which one?</a>  <br/> <span style="font-size:9px;"> (<a href ="http://www.polldaddy.com">  polls</a>)</span></noscript><br />
</center></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fspreadingfunkyness.com%2Fflash-on-android-soon%2F&amp;title=Flash%20on%20Android%3F%20Soon" id="wpa2a_6"><img src="http://spreadingfunkyness.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/flash-on-android-soon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Passing parameters to Adobe Air at startup</title>
		<link>http://spreadingfunkyness.com/passing-parameters-to-adobe-air-at-startup/</link>
		<comments>http://spreadingfunkyness.com/passing-parameters-to-adobe-air-at-startup/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 15:22:42 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[adobeair]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=251</guid>
		<description><![CDATA[Some example of passing parameters to an Adobe Air application at startup.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 0px 10px 0px 0px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fspreadingfunkyness.com%2Fpassing-parameters-to-adobe-air-at-startup%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fpassing-parameters-to-adobe-air-at-startup%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In case you are wondering: &#8220;Can I pass parameters to an Adobe Air application at startup?&#8221; the answer is &#8230; YES. I didn&#8217;t know and I didn&#8217;t expect to ask myself a question like that. But it happened. For an interanl project there was a requirement to startup an application via shell and pass some parameter as input. A query to google with these keywords &#8220;adobe air passing shell parameters&#8221; does not return relevant information. So I decided to write a blog post.<br />
The <code>WindowedApplication</code> class has an interesting event called <code>InvokeEvent</code>. As you can imagine it is fired when the application is invoked. This is the right place to detect whether there are parameters to be considered before drawing the main window. Let&#8217;s see some code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:WindowedApplication</span> <span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">layout</span>=<span style="color: #ff0000;">&quot;absolute&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">invoke</span>=<span style="color: #ff0000;">&quot;handleOnInvoke(event)&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">	private function handleOnInvoke(event:InvokeEvent):void {</span>
<span style="color: #339933;">	    trace(&quot;arguments: &quot;+ event.arguments.toString());</span>
<span style="color: #339933;">	    feedbackText.text = &quot;arguments: &quot; + event.arguments.toString();</span>
<span style="color: #339933;">	}</span>
<span style="color: #339933;">    ]]&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Text</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;feedbackText&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:WindowedApplication<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>&gt;</pre></td></tr></table></div>

<p>At line 3 we bind the event to a function, <code>handleOnInvoke</code>. The <code>InvokeEvent</code> handled by the function has some interesting properties attached. One is the arguments array. If you simply debug the application, the array is empty. To test it you have two ways: </p>
<ul>
<li> specify parameters in the Flex builder</li>
<li> create an application and run it from the shell</li>
</ul>
<h2> Specify parameters in the Flex builder</h2>
<p>Runner and debugger can handle parameters that are passed to the application. Follow this procedure.</p>
<ol>
<li>Right click on the project&#8217;s folder and select &#8216;Properties&#8217;</li>
<li>On the left select &#8216;Run/Debug settings&#8217;</li>
<li>Select the current launch configuration (there should be one) and click edit</li>
<li>In the field &#8216;Command line arguments&#8217; specify your parameters (e.g. testParameter) and hit Apply (see figure below)</li>
</ol>
<p><a href="http://spreadingfunkyness.com/wp-content/uploads/2009/01/paramsflexbuilder.gif"><img src="http://spreadingfunkyness.com/wp-content/uploads/2009/01/paramsflexbuilder.gif" alt="Specify startup parameters in Flex Builder." title="Specify startup parameters in Flex Builder." width="350" height="339" class="aligncenter size-full wp-image-252" /></a></p>
<p>If you now debug the application you&#8217;ll see the trace message in the console tab, as in the figure, and the text field will display parameters as well.</p>
<p><a href="http://spreadingfunkyness.com/wp-content/uploads/2009/01/fbconsole.gif"><img src="http://spreadingfunkyness.com/wp-content/uploads/2009/01/fbconsole.gif" alt="Message in the Flex Builder Console" title="Message in the Flex Builder Console" width="253" height="78" class="aligncenter size-full wp-image-253" /></a></p>
<h2> Create and run an application</h2>
<p>Another way to see the parameters is to export a .air file and install it. In this case you have to run it from the shell (Terminal for MacOsX users, command line for Windowsians). Once you installed the air application move to the installation directory and try to run it with some parameters, as in the figure below.</p>
<p><a href="http://spreadingfunkyness.com/wp-content/uploads/2009/01/shellexample.png"><img src="http://spreadingfunkyness.com/wp-content/uploads/2009/01/shellexample.png" alt="Example run in the shell." title="Example run in the shell." width="457" height="196" class="aligncenter size-full wp-image-255" /></a></p>
<p>The <a href="http://livedocs.adobe.com/flex/3/langref/flash/events/InvokeEvent.html">InvokeEvent</a> contains interesting data about the application. For example you can easily find out which is the local directory where the application is running.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">    event.<span style="color: #006600;">currentDirectory</span>.<span style="color: #006600;">nativePath</span>;</pre></div></div>

<p>The source code for this post is available <a href='http://spreadingfunkyness.com/wp-content/uploads/2009/01/tutcommadline.zip'>here</a> under the <a href="http://www.opensource.org/licenses/bsd-license.php">New BSD license</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/passing-parameters-to-adobe-air-at-startup/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Garbage Collection with Flex and Adobe Air</title>
		<link>http://spreadingfunkyness.com/garbage-collection-with-flex-and-adobe-air/</link>
		<comments>http://spreadingfunkyness.com/garbage-collection-with-flex-and-adobe-air/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 12:01:53 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[adobeair]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=57</guid>
		<description><![CDATA[Techniques to improve memory consumption in Flex/Adobe Air applications.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin: 0px 10px 0px 0px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fspreadingfunkyness.com%2Fgarbage-collection-with-flex-and-adobe-air%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fgarbage-collection-with-flex-and-adobe-air%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I finally found some spare time to organize the stuff presented at <a href="http://www.flexcamp.it">flexcamp</a> and make it a blog post. I &#8220;argue&#8221; with the Flex profiler almost daily and we had an &#8220;intense&#8221; relationship the month right before the flexcamp. So I felt a talk about profiling and Garbage Collection (GC) was really fit. Right, it is impossible to decouple profiling from GC. If you want to improve the memory management of your application you have to know how the Flash Player (and Adobe Air) manage memory allocation and deallocation.<br />
Let me start  by saying there are many blog posts about issues related to GC and profiling, ranging from documentation, presentations, how-tos, etc. I list them at the bottom.<br />
The present post can be considered useful to beginners and medium experienced people which use Flex almost daily and have the need to optimize memory consumption. After introducing the fundamental concepts I will list a set of lessons learned during the development of <a href="http://spreadingfunkyness.com/posty/">Posty</a> and <a href="http://spreadingfunkyness.com/focused/">Focused</a>. I will particularly focus on the use of renderers&#8217; caches.</p>
<h2>Virtual Machine (VM)</h2>
<p>The flash player is based on a virtual machine (to be precise the machines are 2, one for actionscript2 and one for actionscript3). VMs dynamically allocate memory when you create new objects. For example the following line of code creates a new Object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> o:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>At startup the VM reserves some memory and when the code above is executed decides where the object goes in the application memory and how much space it takes. As you create objects the VM might use all the memory allocated at startup and, when needed, requests some more to the operative system. Unless you have a program which nonsensically just creates new objects, there will be some time during the execution when an object becomes &#8220;useless&#8221;, e.g. it is not needed anymore for the correct execution of the program. You&#8217;ll see that it is not easy to &#8220;understand&#8221; when an object is not needed. For now let&#8217;s assume we are able to detect it.<br />
In the Flash VM achitecture you cannot explicitly say &#8220;delete it&#8221;. Memory usage is managed, that is the VM itself is responsible to check which objects are useless and delete them. Such a mechanism is called Garbage Collection.</p>
<h2>Garbage Collection</h2>
<p>So what can I do as a programmer? Well, you can ease the task of the garbage collector. Let&#8217;s see what we expect to happen with the help of a figure. </p>
<a href="http://spreadingfunkyness.com/wp-content/uploads/2009/01/memblocks.gif"><img src="http://spreadingfunkyness.com/wp-content/uploads/2009/01/memblocks.gif" alt="Memory Blocks Example" title="Memory Blocks Example" width="550" height="369" class="size-full wp-image-243" /></a>
<p>At startup the application reserves some memory to be used, say four blocks. When you create a new object the VM allocates it using the first slot. Let&#8217;s say that after a while <code>o1</code> is not needed anymore and you set it to null. When you create a new object, <code>o2</code>, you expect that it takes the place of <code>o1</code>. Sometimes it happens, sometimes it does not. This depends on the garbage collection mechanism, which is a pretty complex procedure we are not describing here (a good <a href="http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html">article</a> has been written by Grant Skinner).<br />
At this point we already have a lesson learned: &#8220;<u>setting an object to null does not necessarily free the memory it was occupying</u>&#8220;.</p>
<p>This depends on the way GC has been implemented in flash. <u>GC is triggered by allocation&#8221; and not deletion</u>. This means that the GC cycle is run when you say new Object() and not when you set it to null.</p>
<h2>Memory Consumption</h2>
<p>If you have to do with AS3 and Flex you probably know that you can dynamically add UI elements to the graphical interface via a simple method called <code>addChild()</code>. The opposite method is <code>removeChild()</code>, which removes a display element from the UI. To be more precise, the element is deleted from the view (it is not displayed anymore) but this does not mean it has been garbage collected. Let me introduce you to a simple scenario to show you easy is to put too much trust in the <code>removeChild()</code> method.<br />
Many Flex applications load data from a server and display it dynamically, according to the values returned. Usually the view code is isolated in a component, often referred to as renderer, which is responsible of showing the data loaded from the server. We devise a very simple renderer that is made of two text fields, embedded in a VBox. Data shown are <code>field1</code> and <code>field2</code>, properties of the object provided as input.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:VBox</span> <span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">borderStyle</span>=<span style="color: #ff0000;">&quot;solid&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">	    [Bindable] private var _field1:String;</span>
<span style="color: #339933;">	    [Bindable] private var _field2:String;</span>
&nbsp;
<span style="color: #339933;">	    override public function set data(value:Object):void {</span>
<span style="color: #339933;">		_field1 = value.field1;</span>
<span style="color: #339933;">		_field2 = value.field2;</span>
<span style="color: #339933;">	    }</span>
<span style="color: #339933;">	]]&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Text</span> <span style="color: #000066;">text</span>=<span style="color: #ff0000;">&quot;{_field1}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Text</span> <span style="color: #000066;">text</span>=<span style="color: #ff0000;">&quot;{_field2}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:VBox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Let&#8217;s simulate data loading by means of a simple function, which returns an array of objects.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> a:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:uint = <span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">200</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> o:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	o.<span style="color: #006600;">field1</span> = <span style="color: #ff0000;">&quot;field &quot;</span>+<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	o.<span style="color: #006600;">field2</span> = <span style="color: #ff0000;">&quot;field &quot;</span>+<span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	a.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>o<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>			
    <span style="color: #b1b100;">return</span> a;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>To render data we use a simple function which creates a renderer, sets its data, and adds it to the VBox.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> loadData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    vbox.<span style="color: #006600;">removeAllChildren</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">array</span>:<span style="color: #0066CC;">Array</span> = getData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> <span style="color: #0066CC;">array</span>.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> rend:MyRenderer = <span style="color: #000000; font-weight: bold;">new</span> MyRenderer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	rend.<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">array</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
	box.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>rend<span style="color: #66cc66;">&#41;</span>;
        i++;
    <span style="color: #66cc66;">&#125;</span> 
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Do you see the dangerous statement? Not yet? Let me show you. To simulate a repeated action I add a timer which calls the load function each N seconds.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> t:Timer = <span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2000</span><span style="color: #66cc66;">&#41;</span>;
    t.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TimerEvent.<span style="color: #006600;">TIMER</span>, tick<span style="color: #66cc66;">&#41;</span>;
    t.<span style="color: #0066CC;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Now try running the profiler. Do you see an ever growing graph like this?</p>
<p><a href="http://spreadingfunkyness.com/wp-content/uploads/2009/01/growing.gif"><img src="http://spreadingfunkyness.com/wp-content/uploads/2009/01/growing.gif" alt="Memory Leak" title="Memory Leak" width="436" height="152" class="aligncenter size-full wp-image-245" /></a></p>
<p>Congrats! We have found a memory leak! A memory leak happens when the same action is repeated and memory consumption grows instead of being constant. Did you find the dangerous statement? It is when you create the renderer. Why? Because you assume that <code>removeAllChildren()</code> removes renderers from the memory. Wrong! As said above that method only removes renderers from the display tree. In fact, as you can see in the profiler, renderers are still there, consuming memory. </p>
<p>UPDATE: Technically speaking this is not a memory leak, because there is nothing that prevents the garbage collector to clean up memory from renderers. A memory leak happens, in fact, when there is something that is supposed to use the renderer (e.g. a listener) even when you remove it from the display tree. In this example renderers are &#8216;free&#8221; and could be garbage collected. But they are not. So the result is the same of a memory leak, an ever growing memory consumption.</p>
<p><a href="http://spreadingfunkyness.com/wp-content/uploads/2009/01/instancescount.gif"><img src="http://spreadingfunkyness.com/wp-content/uploads/2009/01/instancescount.gif" alt="Instances of renderers" title="Instances of renderers" width="355" height="114" class="aligncenter size-full wp-image-246" /></a></p>
<p>There are many techniques to solve this situation. We will show two: caching renderers and dynamic caching.</p>
<h2>Caching</h2>
<p>Let&#8217;s assume you know in advance how many renderers are needed. One way to solve the memory leak is to create a cache of renderers at startup.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cache:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> initRenderers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">200</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	renderers.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MyRenderer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>We can then modify our loadData method like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> loadData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    container.<span style="color: #006600;">removeAllChildren</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">array</span>:<span style="color: #0066CC;">Array</span> = getData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> <span style="color: #0066CC;">array</span>.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> rend:MyRenderer = cache<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
	rend.<span style="color: #0066CC;">data</span> = <span style="color: #0066CC;">array</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
  	container.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>rend<span style="color: #66cc66;">&#41;</span>;
	i++;
    <span style="color: #66cc66;">&#125;</span> 
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see in the code we don&#8217;t create new renderers but we look for one in the cache. There are cases when you don&#8217;t know in advance which data are returned from the server and then you don&#8217;t know how many rendereres you need. In this case you need a dynamic cache.</p>
<h2>Dynamic cache</h2>
<p>Dynamic caching is based on an elastic mechanism. You have a place where you can look for a renderer: if there is one in the cache, that is returned, otherwise a new one is created temporarily. Better to see some code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyRendererCache <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">Object</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> cache : ArrayCollection = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyRendererCache<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	    <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> x : <span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>; x <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">20</span>; x ++ <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		cache.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MyRenderer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
	    <span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> getRenderer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : MyRenderer <span style="color: #66cc66;">&#123;</span>
	    <span style="color: #000000; font-weight: bold;">var</span> renderer : MyRenderer;
&nbsp;
	    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> cache.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		renderer = <span style="color: #000000; font-weight: bold;">new</span> MyRenderer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span> 
		renderer = cache.<span style="color: #006600;">removeItemAt</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span> as MyRenderer;
	    <span style="color: #66cc66;">&#125;</span>
&nbsp;
	    <span style="color: #b1b100;">return</span> renderer;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> setRenderer<span style="color: #66cc66;">&#40;</span> renderer : MyRenderer <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
	    cache.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> renderer <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>		
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In the constructor you populate the cache with the minimu number of renderers, say 20 (lines 7-9). The cache has two static methods, <code>getRenderer</code> and <code>setRenderer</code>. The first is used to obtain a renderer, the second to give it back when done. If you look and lines 15-16 the cache returns a new renderer when the cache is empty. This way the number of renderers in memory can grow beyond the minimum number set in the constructor, but just temporarily, since the GC will delete them when not referenced anymore.<br />
An important issue is related to the <code>setRenderer</code>. When you don&#8217;t need a renderer anymore you have to return it back to the cache, otherwise we fall again in a memory leak as explained above. To achieve this we exploit the remove event of the renderer. The remove event is triggerer whenever a UI element is removed from the display list. For example when we call <code>removeAllChildren()</code> such event is triggered for each renderer.<br />
We can modify the renderer like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:VBox</span> <span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">borderStyle</span>=<span style="color: #ff0000;">&quot;solid&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">remove</span>=<span style="color: #ff0000;">&quot;onRemove()&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    private function onRemove():void {
	MyRendererCache.setRenderer(this);
    }
....
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/VBox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>If you now run the profiler you will notice that memory grows until a given point and then keeps stable as in the figure below. </p>
<p><a href="http://spreadingfunkyness.com/wp-content/uploads/2009/01/memstable.gif"><img src="http://spreadingfunkyness.com/wp-content/uploads/2009/01/memstable.gif" alt="Stable Memory Consumption" title="Stable Memory Consumption" width="363" height="213" class="aligncenter size-full wp-image-248" /></a></p>
<p>Congratulations! You have solved the memory leak!</p>
<h2>Suggesting the GC</h2>
<p>Besides favoring the job of GC Adobe has allowed programmers to suggest the intervention of the GC. The command is in the <code>flash.system</code> package and it is <code>System.gc()</code>. By documentation this <u>&#8220;Forces the garbage collection process&#8221;</u> but in my experience it is just a vague suggestion of intervention. It can solve some situation, so it is worth trying it at the beginning, when you need a quick way to save some memory.<br />
<br/><br />
Here are the slides of my talk at flexcamp.</p>
<p><embed src="http://data.sliderocket.com/SlideRocketPlayer.swf" flashvars="id=3dcb8122-657c-44c9-a731-6ae32deaded5" width="400" height="300" allowFullScreen="true" type="application/x-shockwave-flash"></embed><br />
<br/><br />
Full screen slides available <a href="http://app.sliderocket.com/app/FullPlayer.aspx?id=3dcb8122-657c-44c9-a731-6ae32deaded5">here</a>.</p>
<p>The <a href='http://spreadingfunkyness.com/wp-content/uploads/2009/01/memleak-src.zip'>source code</a> of this post is available under the <a href="http://www.opensource.org/licenses/bsd-license.php">new BSD license</a>.</p>
<h2>References</h2>
<ul>
<li><a href="http://blogs.adobe.com/aharui/2007/03/garbage_collection_and_memory.html">Garbage Collection and Memory Leaks</a> by Alex Aharui. Somehow outdated but a good introduction.</li>
<li><a href="http://blogs.eyepartner.com/adrian/flex/flex-tip-6-garbage-collection-in-flex/">Tip to force GC</a> via local connection.</li>
<li><a href="http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html">GC and Flash player 9</a>. Good article by G. Skinner.</li>
<li><a href="http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/">GC and reflections on Adobe Air vs the design of Flash player</a> by Sean Christmann (creator of the <a href="http://desktop.ebay.com/">Ebay Desktop application</a>).</li>
<li><a href="http://funkatron.com/site/comments/the-ugly-side-of-developing-air-html-apps/">The downside of developing Adobe Air application via Html/JS.</a> Reflections on the lack of a profiler.</li>
<li><a href="http://www.craftymind.com/2008/11/20/max-2008-session-material/">Tips to optimize Air/Flash applications</a> presented by Sean Christmann at Max 2008 Milan</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/garbage-collection-with-flex-and-adobe-air/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
	</channel>
</rss>

