<?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; programming</title>
	<atom:link href="http://spreadingfunkyness.com/category/programming/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>Tue, 06 Jul 2010 21:37:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</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" 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 addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://spreadingfunkyness.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/6-tutorials-on-copypaste-and-dragdrop-in-adobe-air/feed/</wfw:commentRss>
		<slash:comments>0</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" 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> each <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>0</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" 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 addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://spreadingfunkyness.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></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" 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>Drag and Drop of Images In Adobe Air</title>
		<link>http://spreadingfunkyness.com/drag-and-drop-of-images-in-adobe-air/</link>
		<comments>http://spreadingfunkyness.com/drag-and-drop-of-images-in-adobe-air/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 12:04:36 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[adobeair]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=296</guid>
		<description><![CDATA[In this tutorial we will see how to show images dragged from the file system into an Adobe Air application. As we have seen in other tutorials to intercept when something is dragged and dropped on an Adobe Air application we need to specify two handlers: nativeDragEnter and nativeDragDrop. To accept the drag&#038;drop the content ...]]></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%2Fdrag-and-drop-of-images-in-adobe-air%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fdrag-and-drop-of-images-in-adobe-air%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In this tutorial we will see how to show images dragged from the file system into an Adobe Air application. As we have seen in <a href="http://spreadingfunkyness.com/drag-and-drop-of-text-in-adobe-air/">other</a> <a href="http://spreadingfunkyness.com/native-drag-and-drop-in-adobe-air/">tutorials</a> to intercept when something is dragged and dropped on an Adobe Air application we need to specify two handlers: <code>nativeDragEnter</code> and <code>nativeDragDrop</code>. To accept the drag&#038;drop the content of the clipboard has to be a list of files (line 12). When there is a drop we store the content in an array (line 18). For each element of the array we check the extension (png in our case) and we create an image object, which we add to a tile list (lines 20-26).</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: #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> 
    nativeDragEnter=<span style="color: #ff0000;">&quot;dragEnterHandler(event)&quot;</span> 
    nativeDragDrop=<span style="color: #ff0000;">&quot;dropHandler(event)&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;">controls</span>.<span style="color: #006600;">Image</span>;
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragEnterHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NativeDragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
	    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">clipboard</span>.<span style="color: #006600;">hasFormat</span><span style="color: #66cc66;">&#40;</span>ClipboardFormats.<span style="color: #006600;">FILE_LIST_FORMAT</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		NativeDragManager.<span style="color: #006600;">acceptDragDrop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
	    <span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dropHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NativeDragEvent<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> files:<span style="color: #0066CC;">Array</span> = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">clipboard</span>.<span style="color: #006600;">getData</span><span style="color: #66cc66;">&#40;</span>ClipboardFormats.<span style="color: #006600;">FILE_LIST_FORMAT</span><span style="color: #66cc66;">&#41;</span> as <span style="color: #0066CC;">Array</span>;
&nbsp;
	    <span style="color: #b1b100;">for</span> each <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> f:<span style="color: #0066CC;">Object</span> <span style="color: #b1b100;">in</span> files<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	  	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>f.<span style="color: #006600;">extension</span> == <span style="color: #ff0000;">&quot;png&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		    <span style="color: #000000; font-weight: bold;">var</span> image:Image = <span style="color: #000000; font-weight: bold;">new</span> Image<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		    image.<span style="color: #006600;">source</span> = <span style="color: #ff0000;">&quot;file:///&quot;</span> + f.<span style="color: #006600;">nativePath</span>
		    droppedImages.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>image<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	    <span style="color: #66cc66;">&#125;</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:Tile id=<span style="color: #ff0000;">&quot;droppedImages&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>The result should look something like this video.</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=39801' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=39801' 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/NativeImageDragAndDrop.zip'>Source code</a> is available.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://spreadingfunkyness.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/drag-and-drop-of-images-in-adobe-air/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Drag and Drop of Text in Adobe Air</title>
		<link>http://spreadingfunkyness.com/drag-and-drop-of-text-in-adobe-air/</link>
		<comments>http://spreadingfunkyness.com/drag-and-drop-of-text-in-adobe-air/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 13:42:05 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[adobeair]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=294</guid>
		<description><![CDATA[In a previous tutorial we have seen how to drag and drop images from a native application (Fireworks) to Adobe Air and between two air apps. In this post I will show how to drag and drop text from a document (pdf, html, txt) into a text area of an Adobe Air application. Our application ...]]></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%2Fdrag-and-drop-of-text-in-adobe-air%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fdrag-and-drop-of-text-in-adobe-air%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In a <a href="http://spreadingfunkyness.com/native-drag-and-drop-in-adobe-air/">previous tutorial</a> we have seen how to drag and drop images from a native application (Fireworks) to Adobe Air and between two air apps. In this post I will show how to drag and drop text from a document (pdf, html, txt) into a text area of an Adobe Air application.<br />
Our application has to specify two handlers: <code>nativeDragEnter</code> and <code>nativeDragDrop</code> (lines 3-4). In the first we will check whether the content in the clipboard is textual (lines 11-12), in the second we will dump the content in a Text component (line 17).</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
</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> 
    nativeDragEnter=<span style="color: #ff0000;">&quot;dragEnterHandler(event)&quot;</span> 
    nativeDragDrop=<span style="color: #ff0000;">&quot;dropHandler(event)&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;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragEnterHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NativeDragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
	    <span style="color: #808080; font-style: italic;">// check whether content is textual</span>
	    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">clipboard</span>.<span style="color: #006600;">hasFormat</span><span style="color: #66cc66;">&#40;</span>ClipboardFormats.<span style="color: #006600;">TEXT_FORMAT</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	 	NativeDragManager.<span style="color: #006600;">acceptDragDrop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
	    <span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dropHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NativeDragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
	    droppedText.<span style="color: #0066CC;">text</span> = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">clipboard</span>.<span style="color: #006600;">getData</span><span style="color: #66cc66;">&#40;</span>ClipboardFormats.<span style="color: #006600;">TEXT_FORMAT</span><span style="color: #66cc66;">&#41;</span> as <span style="color: #0066CC;">String</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:<span style="color: #0066CC;">Text</span> id=<span style="color: #ff0000;">&quot;droppedText&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&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 check whether it works!</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=39210' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=39210' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object></p>
<p>Happy drag&#038;dropping :)<br />
<a href='http://spreadingfunkyness.com/wp-content/uploads/2010/01/NativeTextDragAndDrop.zip'>Source code</a> is available.<br />
In the next tutorial we will see how to drag and drop images from the file system.<br />
Stay tuned.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://spreadingfunkyness.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/drag-and-drop-of-text-in-adobe-air/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Native Drag and Drop in Adobe Air</title>
		<link>http://spreadingfunkyness.com/native-drag-and-drop-in-adobe-air/</link>
		<comments>http://spreadingfunkyness.com/native-drag-and-drop-in-adobe-air/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 12:06:37 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[adobeair]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=292</guid>
		<description><![CDATA[In this tutorial we will learn the basics of native drag and drop in Adobe Air. Specifically we will see two examples: drag an image from a native application (Fireworks) and drop it onto an air application drag an image from an air application and drop it onto another air application We will start by ...]]></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%2Fnative-drag-and-drop-in-adobe-air%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fnative-drag-and-drop-in-adobe-air%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In this tutorial we will learn the basics of native drag and drop in Adobe Air. Specifically we will see two examples:</p>
<ul>
<li>drag an image from a native application (Fireworks) and drop it onto an air application</li>
<li>drag an image from an air application and drop it onto another air application</li>
</ul>
<p>We will start by implementing the target application, which accepts the drop action. To enable this there are two behaviors to specify: what to do when something is dragged over the app and what to do when there is a drop action. These correspond to two events: nativeDragEnter and nativeDragDrop, both belonging to the WindowedApplication class. So we will start with this code.</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: #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;absolute&quot;</span> 
	nativeDragEnter=<span style="color: #ff0000;">&quot;dragEnterHandler(event)&quot;</span> 
	nativeDragDrop=<span style="color: #ff0000;">&quot;dropHandler(event)&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>Once associated the handlers we need to define them.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><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;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragEnterHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NativeDragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">clipboard</span>.<span style="color: #006600;">hasFormat</span><span style="color: #66cc66;">&#40;</span>ClipboardFormats.<span style="color: #006600;">BITMAP_FORMAT</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			NativeDragManager.<span style="color: #006600;">acceptDragDrop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dropHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NativeDragEvent<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;">data</span>:BitmapData = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">clipboard</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: #000000; font-weight: bold;">var</span> myBit:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> Bitmap<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>myBit<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></pre></td></tr></table></div>

<p>In the nativeDragEnter event we have to check whether the dragged item is of type bitmap, if yes we can accept the drag (lines 29-30). During the drag a copy of the item is stored in the system&#8217;s clipboard. To replicate it in the target application we have to read it (line 35), clone it (line 36) and then add it to the display tree (line 37). This is all we need to do in the target application. At this point we can already test the application. Let&#8217;s run it, open an image in Fireworks and drag&#038;drop it. See the video below.</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><param name='flashvars' value='i=38951' ></param><param name='allowFullScreen' value='true' ></param><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=38951' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer' ></embed></object></p>
<p>Now let&#8217;s move to the other case: we start dragging an image from another air application. Here we have to mimic the behavior of Fireworks above: copy in the system clipboard the image when there is a startDrag action. Let&#8217;s start  by adding an image to the application.</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: #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;absolute&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
	<span style="color: #66cc66;">&lt;</span>mx:Image id=<span style="color: #ff0000;">&quot;image&quot;</span> 
		source=<span style="color: #ff0000;">&quot;logo.png&quot;</span> 
		mouseDown=<span style="color: #ff0000;">&quot;startDragDrop(event)&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 you can see we attached a function to the mouseDown event. Now let&#8217;s define this function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><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;">private</span> <span style="color: #000000; font-weight: bold;">function</span> startDragDrop<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<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;">data</span>:BitmapData = <span style="color: #000000; font-weight: bold;">new</span> BitmapData<span style="color: #66cc66;">&#40;</span>image.<span style="color: #0066CC;">width</span>, image.<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>image<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// create my clipboard object to be passed to dragmanager</span>
		<span style="color: #000000; font-weight: bold;">var</span> cb:Clipboard = <span style="color: #000000; font-weight: bold;">new</span> Clipboard<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		cb.<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>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// data here is a 'representative' of the dragged object during dragging.</span>
		NativeDragManager.<span style="color: #006600;">doDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, cb, <span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>; 
&nbsp;
	<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></pre></td></tr></table></div>

<p>We create a copy of the image (lines 25-26). We use this in two cases: to put it in a clipboard object (line 30) and to provide feedback during the dragging (line 33). Now we can run both applications and check if everything is correct. </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=38956' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=38956' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object></p>
<p>Simple uh? Please drop a comment if you have any followup question.<br />
<a href='http://spreadingfunkyness.com/wp-content/uploads/2010/01/NativeDragDrop.zip'>Source code</a> is available.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://spreadingfunkyness.com/wp-content/plugins/add-to-any/share_save_120_16.gif" width="120" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/native-drag-and-drop-in-adobe-air/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Code or not in your spare time? Does it really matter?</title>
		<link>http://spreadingfunkyness.com/code-or-not-in-your-spare-time/</link>
		<comments>http://spreadingfunkyness.com/code-or-not-in-your-spare-time/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 13:50:19 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[productivity]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=280</guid>
		<description><![CDATA[Code or not in your spare time? I think it doesn't matter. I show you pros and cons of both.]]></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-or-not-in-your-spare-time%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fcode-or-not-in-your-spare-time%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>There is some buzz around the topic &#8220;I prefer or not you to code in your spare time&#8221;. I guess it all started with <a href="http://teddziuba.com/2009/10/i-dont-code-in-my-free-time.html">Ted&#8217;s post</a>. I think it is just an excuse if somebody tells you: &#8220;we are not hiring you because you don&#8217;t program in your spare time&#8221;. I like both developing and designing <a href="http://spreadingfunkyness.com/posty/">personal</a> <a href="http://spreadingfunkyness.com/focused/">projects</a>. By definition personal projects don&#8217;t provide any income (at least at the beginning) so you end up working on them during the evening or the weekend. But &#8230; there are weekends I don&#8217;t want to see anything which has to do with technology. Will you hire me anyway? Will you like the part of me which refuses technology during Saturday and Sunday? More provocative: is it negative to the success of your project that I live 48 hours of my life avoiding technology and specifically computers?<br />
I will anticipate my final moral: you shouldn&#8217;t care, but let&#8217;s see why.</p>
<p>Let&#8217;s exaggerate by reversing the reasoning. I am a project manager and I say: &#8220;you absolutely have to code in your spare time, on some project of yours, otherwise I&#8217;ll not hire you&#8221;. How many people will file a cv in this case? Probably a few. The same holds for the opposite statement: &#8220;you absolutely have not to code in your spare time. Don&#8217;t touch any computer during the weekend&#8221;. More or less the result should be the same, few candidates. Why? Because you sort of <strong>invade privacy</strong>. There are many people around that don&#8217;t care of the job itself, it is just a means to get an income. If you want somebody committed to your project then prepare yourself to do many interviews and, moreover, you will discover the commitment only after one month (or more) you hired the programmer. You can be more lucky if you spread an inspiring call for job, with a few technical requirements and much more details about the motivation behind the project. Something like: &#8220;we are working on a project that will revolutionize the way people blog&#8221; can help you to be surrounded by people that share with you an objective.</p>
<p>In case you already have a project, somebody working on that, and you need someone&#8217;s help to not pass the deadline, <strong>don&#8217;t bother with their private life</strong>. Let&#8217;s say you need a php programmer to finalize part of a project. Let&#8217;s also pretend you find somebody so in love with php and technology which contributes to some open source project during his spare time. This can be be a <strong>double-edged</strong> sword. In the best case you are lucky, because the person you hire knows tips and tricks of php, many of which -probably &#8211; have been learnt during spare time programming. You hired a genius, and you are sure your project will be successful. Let me be evil.</p>
<ul>
<li>what if the genius gets involved in a toy project, more engaging than yours? </li>
<li>What if he starts working during the evening, maybe going to bed late? </li>
<li>Will his brain be fresh and ready for your project the day after?</li>
<li>Will his attention be the same of the day before?</li>
<li>More evil: what if he works on the open source project during the weekend? That&#8217;s what you want right? </li>
<li>What if on Monday he is a bit tired of staying in front of a monitor with a php editor open? </li>
<li>What if his spare time passion causes even a temporary indigestion?</li>
</ul>
<p>I think that you don&#8217;t want that.<br />
<strong>A spare time passion can feed or subtract energy</strong> to your project, whatever the passion is. If I go skying during the weekend and I arrive home sunday night, late, on monday I can be satisfied and ready to work as well as tired and not ready to code for your project. In the second case, will you ever tell me I should quit skying? Will your job post include a &#8220;no snow riders&#8221; restriction? I think no.<br />
Passion for &#8216;x&#8217; does not mean you do or you want to do &#8216;x&#8217; everyday. Also, passion for programming does not imply you code all day and night. It means you read and write books/posts about programming, subscribe to rss and podcasts, stay updated with news, look at someone&#8217;s code etc. EVERY developer does it, otherwise he is not a developer.  Just like ANY designer feeds his creativity by taking pictures or getting inspired by the nature. Just like ANYBODY chooses his on way to spend the weekend.<br />
Last provocation: in my life I met many developers and I noticed that many of them like to drink beer. Will you include that as a requirement when you post a job call? :)<br />
I think you should not focus your search on what a developer/designer does during his spare time. You should rather focus on professionalism and seriousness. <strong>If I do what you want, without passing the deadline, isn&#8217;t enough?</strong> </p>
]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/code-or-not-in-your-spare-time/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The Iphone is a</title>
		<link>http://spreadingfunkyness.com/the-iphone-is-a/</link>
		<comments>http://spreadingfunkyness.com/the-iphone-is-a/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 12:11:39 +0000</pubDate>
		<dc:creator>Cesare</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://spreadingfunkyness.com/?p=277</guid>
		<description><![CDATA[My reflections after 6 months of Iphone 3Gs]]></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%2Fthe-iphone-is-a%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fspreadingfunkyness.com%2Fthe-iphone-is-a%2F&amp;source=_funkyboy&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>After six months of iPhone these are my reflections, in the form of &#8220;The iPhone is a &#8230;&#8221;</p>
<h2>Time saver</h2>
<p>The only thing to complete your daily todo list is to wait for an email and redirect it to someonelse? Go to the beach, play volley and in the pause between one set and the other check the email and reply/redirect accordingly. </p>
<h2>Angryness preventer</h2>
<p>You have to go to pick your girlfriend up. You&#8217;d really like to check some more news or finish reading a blog post, but you agreed to be at her place at 9 pm. You are on time, she is late. With no iPhone you&#8217;d probably feel angry. Grab your iphone out of the pocket and finish checking news. Stumbled upon something interesting? Use <a href="http://readitlaterlist.com/iphone/">Read It Later</a>.</p>
<h2>Boredom preventer</h2>
<p>Bored to wait at the bus stop? Nobody to talk to and the bus is late? Open one of the tons of applications and play with it. Side effect: might loose the bus.</p>
<h2>Good guide</h2>
<p>Looking for a bank around you? a bar? Open up <a href="http://www.tweakersoft.com/mobile/aroundme.html">Around Me</a> and find what you need.<br />
Got lost. Open the Maps application and follow the path to get to your destination.</p>
<h2>Cool to program</h2>
<p>Tired of virtual machined/garbage collected languages? Looking for some new development platform to expand your knowledge and your business? Have a look at the iPhone. I bought it because of development purposed (top secret at the moment) and I ended up with the iPhone as my daily technological companion. There is plenty of tutorials, books, forums and open source code to learn from.</p>
<p><br/><br />
Probably everything I described above can be experienced with some other 3G enabled phone, except the <em>cool-to-programness</em>, which is what I appreciate mostly at the moment.</p>
]]></content:encoded>
			<wfw:commentRss>http://spreadingfunkyness.com/the-iphone-is-a/feed/</wfw:commentRss>
		<slash:comments>13</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" 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>33</slash:comments>
		</item>
	</channel>
</rss>
