<?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>Thanks, Mister! &#187; Flex 3</title>
	<atom:link href="http://www.thanksmister.com/index.php/archive/category/flex-3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thanksmister.com</link>
	<description>Adobe Flex &#38; AIR Development</description>
	<lastBuildDate>Mon, 19 Jul 2010 17:28:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>AIR:  Migrating Expired Certificates using AIR 1.5.3</title>
		<link>http://www.thanksmister.com/index.php/archive/migrating-expired-certificates-air153/</link>
		<comments>http://www.thanksmister.com/index.php/archive/migrating-expired-certificates-air153/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 08:39:19 +0000</pubDate>
		<dc:creator>Mister</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flex 3]]></category>
		<category><![CDATA[Flex Builder 3]]></category>
		<category><![CDATA[Migrating Certificates]]></category>

		<guid isPermaLink="false">http://thanksmister.com/?p=762</guid>
		<description><![CDATA[If you renew your certificate and you are running an earlier version of Adobe AIR than 1.5.3, you are in a world of hurt. The process of updating and renewing your certificate is enough of a pain, but trying to update your AIR application after your certificate expires prior to 1.5.3 required you to just [...]]]></description>
			<content:encoded><![CDATA[<p>If you renew your certificate and you are running an earlier version of Adobe AIR than 1.5.3, you are in a world of hurt.    The process of updating and renewing your certificate is enough of a pain, but trying to update your AIR application after your certificate expires prior to 1.5.3 required you to just uninstall your previous version and reinstall a new version of your application.    </p>
<p><strong>Adobe AIR 1.5.3 Update</strong></p>
<p>Luckily with AIR 1.5.3 you now have a grace period of 6 months to renew your certificate and still be able to publish an update to your application that users can install over the previous application.  The down side is that it&#8217;s is not easy to update your certificate once you update to 1.5.3.   You can read more about the Adobe AIR 1.5.3 update and changes to certificate migration here:</p>
<p><span id="more-762"></span></p>
<p><a href="http://www.adobe.com/support/documentation/en/air/1_5_3/releasenotes_developers.html">Release notes for Adobe AIR developers</a><br />
<a href="http://www.adobe.com/support/documentation/en/air/1_5_3/releasenotes_developers.html">Adobe AIR 1.5.3 Now Available</a></p>
<p><strong>Updating Flex Builder 3 for AIR 1.5.3</strong></p>
<p>First off, grab and install the latest and greatest AIR 1.5.3 from <a href="http://www.adobe.com/products/air/tools/sdk/">Adobe&#8217;s download sit</a>e.  You will also need to download the Adobe AIR 1.5.3 SDK to install within your Flex Builder 3.  If you just change the descriptor file and not update the SDK to use AIR 1.5.3, you will receive the following error when compiling the project from the IDE:  </p>
<blockquote><p><strong>invalid application descriptor: descriptor version does not match runtime version</strong></p>
<p>or </p>
<p><strong>EncryptedLocalStore may not use publisher IDs passed in from ADL</strong>
</p></blockquote>
<p>The instructions for updating Flex Builder 3 to use AIR 1.5.3 are not really clear, at least not for me.   The best way to upgrade on the MAC is to download the AIR 1.5.3 SDK and manually drag the files to your Flex Builder 3 SDK folder.   When you download the AIR 1.5.3 SDK and expand it, you can see the following folder structure:</p>
<p><img src="http://www.thanksmister.com/images/MigratingCertificates/AIRSDK153.png" alt="AIR SDK 1.5.3" /></p>
<p>You can just drag the folders one-by-one ( or the contents of the folder to be more careful ) to your Flex Builder 3 application directory inside the sdks folder.   Now restart your Flex Builder 3 and create a new dummy project.   If all goes well, your new project will have the 1.5.3 namespace already in the descriptor file.  Now for existing Flex Builder 3 projects,  you need to manually update your application&#8217;s namespace to 1.5.3  and add a new descriptor tag for publisherID (you don&#8217;t need to do this for a new AIR application, only if you are updating an project to AIR 1.5.3):</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><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><br />
<span style="color: #66cc66;">&lt;</span>application xmlns=<span style="color: #ff0000;">&quot;http://ns.adobe.com/air/application/1.5.3&quot;</span><span style="color: #66cc66;">&gt;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;!</span>-- The application identifier <span style="color: #0066CC;">string</span>, unique to <span style="color: #0066CC;">this</span> application. <span style="color: #006600;">Required</span>. --<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>id<span style="color: #66cc66;">&gt;</span>MyApplication<span style="color: #66cc66;">&lt;/</span>id<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>publisherID<span style="color: #66cc66;">&gt;</span>3782AD3EDB99182DA9E106898998986691F7E39C8DBA6A3.1<span style="color: #66cc66;">&lt;/</span>publisherID<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ...</div></td></tr></tbody></table></div>
<p>The publisherID is a combination of your application name and the publish id for the application.   You can find the publisherID manually by looking at your currently installed application directory on MAC (Windows users can find it in the release notes):</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MaciontoshHD <span style="color: #66cc66;">&gt;</span> Users <span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#91;</span>username<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&gt;</span> Library <span style="color: #66cc66;">&gt;</span> Application Support <span style="color: #66cc66;">&gt;</span> Adobe <span style="color: #66cc66;">&gt;</span> AIR <span style="color: #66cc66;">&gt;</span> ELS <span style="color: #66cc66;">&gt;</span> MyApplication.3782AD3EDB99182DA9E106898998986691F7E39C8DBA6A3.1</div></td></tr></tbody></table></div>
<p>The long number after the application name is the publisherID, copy this and add it to your application descriptor file.  You can also get the application <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=reading_app_settings_1.html">publisherID from the NativeApplication</a>:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>NativeApplication.<span style="color: #006600;">nativeApplication</span>.<span style="color: #006600;">publisherID</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>It should be noted that bye adding the
<publisherID>&#8230;.</publisherID> to the descriptor file will create an error when compiling your application using Flex Builder 3:</p>
<p><strong>invocation forwarded to primary instance</strong></p>
<p>So this must be commented out of the descriptor file until you are ready to publish.  If you leave this commented out when you publish the application, you won&#8217;t be able to migrate the application. So I am not sure how tracing the publisherID is going to be a help to you from the IDE when testing. You should also update the debug version of your <a href="http://www.adobe.com/support/flashplayer/downloads.html">Flash Player</a> to the latest (10.0.42.34) before compiling your application.    Now you can test your application from the IDE and it should work.</p>
<p><strong>Migrating Expired Certificate</strong></p>
<p>Now your application should run from the IDE and you can prepare to publish the application using your renewed certificate.  Publish the application as normal using the renewed certificate to create the AIR compiled application.   Now you will need to migrate your previous expired certificate using the ADT migration tools.   The instructions for migrating certificates using the ADT migration tools can be found here:</p>
<p><a href="http://www.insideria.com/2009/04/migrating-air-application-cert.html">http://www.insideria.com/2009/04/migrating-air-application-cert.html</a></p>
<p>Once you successfully migrated your previous expired certificate, your compiled AIR application is ready to update your previous version of the installed application on your system.   If things didn&#8217;t go correctly, you will get an error that an application already exists with the same name, and you need to install the application in a different directory.   That would be bad, and you probably missed one of the delicate steps and need to go through it all again.   If you do get an update option to replace the existing application, you can no celebrate. </p>
<p><strong>My Opinion about Certificates</strong></p>
<p>In my opinion, using 3rd party certificate vendors (and paying for them) like Thawte, Verisign, ect. is total nonsense and just way to cumbersome.   Adobe could easily provide a way to authenticate the authors of AIR applications with minimal cost to developers.  This is especially important when you just want to publish an application for free and distribute it through the Adobe market place.    </p>
<p>Adobe could borrow some ideas from Android Market, especially since Adobe plans on allowing developers to create more and more Flash/Flex content for mobile devices.    Developers don&#8217;t need some complicated and expensive system in place to review code and authorize updates (i.e. iPhone app store), we simply need a way to verify to the end user that the author is who she says she is and the application is protected from someone else writing an application with identical name.    </p>
<p>The renewal process for a certificate and the above steps to migrate certificates is a total pain, and I think an unnecessary barrier for Adobe AIR application developers.  Anything Adobe could to do to lower the barrier for the average AIR developer to create certified applications would be greatly appreciated.    There are also other ways to freely publish and distribute your application freely as described on this post at polyGeek.com: </p>
<p><a href="http://polygeek.com/862_flex_deploying-trusted-air-apps-without-certification-for-free">http://polygeek.com/862_flex_deploying-trusted-air-apps-without-certification-for-free</a></p>
<p>-Mister</p>
<p><map name='google_ad_map_762_1452acacd61817fb'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/762?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_762_1452acacd61817fb' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=762&amp;url= http%3A%2F%2Fwww.thanksmister.com%2Findex.php%2Farchive%2Fmigrating-expired-certificates-air153%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://www.thanksmister.com/index.php/archive/migrating-expired-certificates-air153/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AIR:: Adding Application Menu Bar Items</title>
		<link>http://www.thanksmister.com/index.php/archive/air-adding-application-menu-bar-items/</link>
		<comments>http://www.thanksmister.com/index.php/archive/air-adding-application-menu-bar-items/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 18:55:57 +0000</pubDate>
		<dc:creator>Mister</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flex 3]]></category>
		<category><![CDATA[Adobe AIR]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[garbage collection]]></category>
		<category><![CDATA[NativeApplication]]></category>
		<category><![CDATA[NativeMenuItem]]></category>
		<category><![CDATA[WindowedApplication]]></category>

		<guid isPermaLink="false">http://thanksmister.com/?p=669</guid>
		<description><![CDATA[Just a quick how-to post on adding application menu bar items to your Adobe AIR application when running. Certain operating systems can support application menu bars, so this example will not be true for all systems. You can check this using &#8220;NativeApplication.supportsMenu == true&#8221;, which basically means you are on Mac OSX dude. If you [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick how-to post on adding application menu bar items to your Adobe AIR application when running.  Certain operating systems can support application menu bars, so this example will not be true for all systems.  You can check this using &#8220;NativeApplication.supportsMenu == true&#8221;, which basically means you are on Mac OSX dude.  If you were on a Windows or Linux box you would check for &#8220;NativeWindow.supportsMenu == true&#8221; and add the menu items to the NativeWindow rather than the NativeApplication.    </p>
<p>As it turns out, I ran into just a small confusing snag that initially prevented the added application menu bar item from firing a select event.   I eventually reached out to the <a href="http://groups.google.com/group/air-tight/browse_thread/thread/a719522bdb10b7a6?hl=en">Adobe AIR Tight</a> discussion group and Jesse Warden had a quick fix.   It turns out that if you do not use strong item references, they are swept away by Garbage Collection (GC) before you actually use the items.    For example instead of creating the <a href="http://">NativeMenuItem</a> within the scope of the function, I had to create it within the scope of the class or in this case, the WindowedApplication.     </p>
<p>In my application I only needed to add a single menu bar item.   So instead of blowing away the entire application menu and then creating all my own menu items (<a href="http://livedocs.adobe.com/flex/3/html/help.html?content=Menus_9.html">as many of the examples show</a>). I just wanted to add a single item to the exiting application menu bar.   On  Mac OS, you get 4 application menu items: YourAppName, File, Edit, and Window.  YourAppName would be your applications name or if you are running in the Eclipse IDE, just simply &#8220;adl&#8221;.  In my case I wanted to add a &#8220;Preferences&#8221; option under the YourAppName.    You can do this by accessing the NativeMenuItem representing the YourAppName. Something like NativeApplication.nativeApplication.menu.items[0] will return a NativeMenuItem with that reference.   Using that reference you can then add a new NativeMenuItem to the submenu of the referenced NativeMenuItem.   In addition to adding items to the submenu, you can also detect when the new item is selected.    </p>
<p>In any event, here is the complete application code example:</p>
<p><span id="more-669"></span></p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:640px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><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><br />
<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> windowComplete=<span style="color: #ff0000;">&quot;createApplicationMenu()&quot;</span> &nbsp;<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Style source=<span style="color: #ff0000;">&quot;assets/css/whitelabel.css&quot;</span><span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">controls</span>.<span style="color: #006600;">Alert</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> prefItem:NativeMenuItem;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> menuItem:NativeMenuItem;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> createApplicationMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>NativeApplication.<span style="color: #006600;">supportsMenu</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefItem = <span style="color: #000000; font-weight: bold;">new</span> NativeMenuItem<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Preferences...&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefItem.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">SELECT</span>, handlePreferencesSelected<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefItem.<span style="color: #006600;">keyEquivalent</span> = <span style="color: #ff0000;">&quot;,&quot;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; menuItem = NativeMenuItem<span style="color: #66cc66;">&#40;</span>NativeApplication.<span style="color: #006600;">nativeApplication</span>.<span style="color: #0066CC;">menu</span>.<span style="color: #006600;">items</span><span style="color: #66cc66;">&#91;</span>0<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; menuItem.<span style="color: #006600;">submenu</span>.<span style="color: #006600;">addItemAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> NativeMenuItem<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>, 1<span style="color: #66cc66;">&#41;</span>;&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; menuItem.<span style="color: #006600;">submenu</span>.<span style="color: #006600;">addItemAt</span><span style="color: #66cc66;">&#40;</span>prefItem, 2<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handlePreferencesSelected<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Alert.<span style="color: #0066CC;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;handlePreferencesSelected&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span><br />
<span style="color: #66cc66;">&lt;/</span>mx:WindowedApplication<span style="color: #66cc66;">&gt;</span></div></td></tr></tbody></table></div>
<p>That&#8217;s it, so now you can have a new item under your application name in the application menu bar to fire whatever function you desire.   </p>
<p>-Mister</p>
<p><map name='google_ad_map_669_1452acacd61817fb'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/669?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_669_1452acacd61817fb' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=669&amp;url= http%3A%2F%2Fwww.thanksmister.com%2Findex.php%2Farchive%2Fair-adding-application-menu-bar-items%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://www.thanksmister.com/index.php/archive/air-adding-application-menu-bar-items/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Flex :: Truncating HTML Text</title>
		<link>http://www.thanksmister.com/index.php/archive/flex-truncating-html-text/</link>
		<comments>http://www.thanksmister.com/index.php/archive/flex-truncating-html-text/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 00:51:16 +0000</pubDate>
		<dc:creator>Mister</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Flex 3]]></category>
		<category><![CDATA[Flash Builder 4]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Label]]></category>
		<category><![CDATA[Text]]></category>
		<category><![CDATA[TextArea]]></category>

		<guid isPermaLink="false">http://thanksmister.com/?p=613</guid>
		<description><![CDATA[With Flex truncating text within a Label control is easily done by setting the property &#8220;truncateToFit&#8221; to true. This parameter doesn&#8217;t do much when using the Text or TextArea controls in Flex. To truncate text in these controls you would have to build your own function to count characters and add the ellipses. There is [...]]]></description>
			<content:encoded><![CDATA[<p>With Flex truncating text within a Label control is easily done by setting the property &#8220;truncateToFit&#8221; to true.   This parameter doesn&#8217;t do much when using the Text or TextArea controls in Flex.  To truncate text in these controls you would have to build your own function to count characters and add the ellipses.   There is one <a href=" http://skovalyov.blogspot.com/2007/02/text-control-with-truncatetofit.html" target="_blank">good example</a> of creating your own custom Text control that truncates the text in the same manner as the Label control based on size of the control.   </p>
<p>Now if you want to truncate HTML text within a Text or TextArea control thinks become more complicated.   Currently in Flex doing anything with HTML is pretty dismal as there are not many ways to control the display of HTML or capture user interaction with HTML text. This seems to be changing with the upcoming release of Flash Builder 4 and the <a href="http://blog.flexexamples.com/2009/06/03/truncating-text-using-the-spark-simpletext-control-in-flex-4/" target="_blank">new spark components</a> for displaying text and also the new <a href="http://corlan.org/2009/01/19/how-to-use-text-layout-framework-in-flex-32-or-air-15/" target="_blank">Text Layout Framework </a>.  In the mean time you still might like to truncate HTML text in Text and TextArea controls. </p>
<p>I did find one blog post describing one method for truncating HTML text based on the size of the control.   However, I wanted more restriction of the length of my HTML displayed and also wanted a way to better handle links that use the TextEvent to fire events from links clicks.    There seem to be plenty of examples of HTML truncation within JavaScript and Java.  I am lucky to have a girlfriend who also develops Java (and is also good looking <img src='http://www.thanksmister.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ).  I asked her for an example of HTML truncation and then translated that code in to working Flex code, which was surprisingly  easy to convert.</p>
<p><span id="more-613"></span></p>
<p>Below is an example application that will truncate HTML text to any character length given.  The truncation will not occur mid-link tag, only the text inside the links is counted you have the option to put a &#8220;&#8230; more&#8221; link at the end of the truncation to expand the text to its full length.   I also gave the HTML a little extra formatting to distinguish links from regular text and add some rollover behavior for HTML links (something also missing from Flex).  If the text  Some limitations include image tags, break tags, and wrapping the entire block of text into a tag (However there is an easy work around that you can see in source code).   The reason these limitations exist is because I only needed to truncate text that contain links.  So the example will have to be expanded to include other tags.</p>
<p><strong>Example</strong></p>
<div id="truncatehtml">
        <object type="application/x-shockwave-flash" id="superimage" data="http://thanksmister.com/examples/htmltruncation/htmltruncation.swf" height="393" width="588"><param name="movie" value="http://thanksmister.com/examples/htmltruncation/htmltruncation.swf"><param name="allowScriptAccess" value="always"><param name="wmode" value="transparent"></object>
</div>
</p>
<p><p><strong>Complete Code</strong></p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:640px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><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><br />
<span style="color: #66cc66;">&lt;</span>mx:Application 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: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;588&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;393&quot;</span> applicationComplete=<span style="color: #ff0000;">&quot;init()&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">navigateToURL</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// string data to play with</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loremIpsum:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;Curabitur ultrices orci non felis luctus non sollicitudin magna aliquet.&quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot; Curabitur lacinia dignissim &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>event:link:http://www.google.com<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;accumsan&lt;/a&gt;. &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;Mauris ac dui in enim tristique egestas eu ac arcu. &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;Quisque scelerisque, odio et luctus tempus, felis ante euismod felis, quis laoreet quam &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;turpis in diam &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>event:link:http://www.google.com<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;http://www.google.com&lt;/a&gt;. &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;Integer ut lectus id justo feugiat posuere. Vestibulum tempor porttitor &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;justo, sed consequat dui lobortis ut. &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>event:link:http://www.google.com<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Aliquam&lt;/a&gt; &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;a posuere diam. Vestibulum turpis purus, &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;dapibus id sagittis nec, volutpat ultricies leo. &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>event:link:http://www.google.com<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Pellentesque&lt;/a&gt; tempus pulvinar ornare. &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;Cras vel sapien vitae mauris tincidunt &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>event:link:http://www.google.com<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;http://www.google.com&lt;/a&gt;&quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot; laoreet quis congue nisl. Aliquam pharetra nunc &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;quis tortor adipiscing quis &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>event:link:http://www.google.com<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;elementum&lt;/a&gt; nulla venenatis. &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;Suspendisse potenti. Mauris &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;vitae enim sed nisl viverra venenatis &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>event:link:http://www.google.com<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;http://www.google.com&lt;/a&gt;. &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;Proin auctor &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>event:link:http://www.google.com<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;mattis&lt;/a&gt; mollis. Phasellus ultrices &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;ornare ullamcorper. Sed &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>event:link:http://www.google.com<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;turpis&lt;/a&gt; quam, tempus eget luctus eget, posuere vitae lorem. &quot;</span> + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">&quot;In vitae sem id lorem aliquam &lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>event:link:http://www.google.com<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;viverra&lt;/a&gt;.&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">destinationText</span>.<span style="color: #006600;">styleSheet</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">getTextStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">destinationText</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TextEvent.<span style="color: #006600;">LINK</span>, handleLinkClick<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">destinationText</span>.<span style="color: #0066CC;">htmlText</span> = <span style="color: #ff0000;">&quot;&lt;span class=<span style="color: #000099; font-weight: bold;">\&quot;</span>body<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> + loremIpsum + <span style="color: #ff0000;">&quot;&lt;/span&gt;&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">sourceText</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TextEvent.<span style="color: #006600;">LINK</span>, handleLinkClick<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">sourceText</span>.<span style="color: #006600;">styleSheet</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">getTextStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">sourceText</span>.<span style="color: #0066CC;">htmlText</span> = <span style="color: #ff0000;">&quot;&lt;span class=<span style="color: #000099; font-weight: bold;">\&quot;</span>body<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> + loremIpsum + <span style="color: #ff0000;">&quot;&lt;/span&gt;&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> truncateText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> limit:<span style="color: #0066CC;">Number</span> = limitStepper.<span style="color: #006600;">value</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">destinationText</span>.<span style="color: #0066CC;">htmlText</span> = <span style="color: #ff0000;">&quot;&lt;span class=<span style="color: #000099; font-weight: bold;">\&quot;</span>body<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> + <span style="color: #0066CC;">this</span>.<span style="color: #006600;">truncateHTMLText</span><span style="color: #66cc66;">&#40;</span>loremIpsum, limit, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;&lt;/span&gt;&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">destinationText</span>.<span style="color: #006600;">styleSheet</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">getTextStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Handle the click action on links within the html text using TextEvent.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleLinkClick<span style="color: #66cc66;">&#40;</span>event:TextEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> prefix:<span style="color: #0066CC;">String</span> = event.<span style="color: #0066CC;">text</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">split</span>:<span style="color: #0066CC;">Array</span> = prefix.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;:&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#91;</span>0<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> id:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#91;</span>1<span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">split</span>.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> 2<span style="color: #66cc66;">&#41;</span> id = <span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> + <span style="color: #ff0000;">&quot;:&quot;</span> + <span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#91;</span>2<span style="color: #66cc66;">&#93;</span>; <span style="color: #808080; font-style: italic;">// recreate the http link</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span> == <span style="color: #ff0000;">&quot;more&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">destinationText</span>.<span style="color: #0066CC;">htmlText</span> = <span style="color: #ff0000;">&quot;&lt;span class=<span style="color: #000099; font-weight: bold;">\&quot;</span>body<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> &nbsp;+ loremIpsum + <span style="color: #ff0000;">&quot;&lt;/span&gt;&quot;</span>; <span style="color: #808080; font-style: italic;">// reset to original text length</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">destinationText</span>.<span style="color: #006600;">styleSheet</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">getTextStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> request:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; navigateToURL<span style="color: #66cc66;">&#40;</span>request,<span style="color: #ff0000;">&quot;_blank&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Truncate html text.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param value The original text value<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param limit The maximum number of characters to show<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param ellipses &nbsp;Boolean value to show elipses at the end of truncation.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> truncateHTMLText<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">String</span>, limit:<span style="color: #0066CC;">Number</span>, ellipses:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>limit == 0<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">var</span> original:<span style="color: #0066CC;">String</span> = value;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value = value.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;[<span style="color: #000099; font-weight: bold;">\\</span>t<span style="color: #000099; font-weight: bold;">\\</span>n<span style="color: #000099; font-weight: bold;">\\</span>x0B<span style="color: #000099; font-weight: bold;">\\</span>f<span style="color: #000099; font-weight: bold;">\\</span>r<span style="color: #000099; font-weight: bold;">\\</span>u00A0]+&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> isTag:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> count:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">position</span>:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> limitLength:<span style="color: #0066CC;">int</span> = value.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> closeTag:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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> value.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> c:<span style="color: #0066CC;">String</span> = value.<span style="color: #0066CC;">charAt</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>isTag<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>c == <span style="color: #ff0000;">'&gt;'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isTag = <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>closeTag <span style="color: #66cc66;">||</span> i == limitLength<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">position</span> = i;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">continue</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">continue</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>c == <span style="color: #ff0000;">'&lt;'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isTag = <span style="color: #000000; font-weight: bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>i == limitLength <span style="color: #66cc66;">||</span> <span style="color: #66cc66;">&#40;</span>count == limit<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>i+1<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span> limitLength<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>i+2<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;</span> limitLength<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>value.<span style="color: #0066CC;">charAt</span><span style="color: #66cc66;">&#40;</span>i+<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #ff0000;">'&lt;'</span> <span style="color: #66cc66;">&amp;&amp;</span> value.<span style="color: #0066CC;">charAt</span><span style="color: #66cc66;">&#40;</span>i+<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #ff0000;">'/'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; closeTag = <span style="color: #000000; font-weight: bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">continue</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">position</span> = i;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> result:<span style="color: #0066CC;">String</span> = value.<span style="color: #0066CC;">substring</span><span style="color: #66cc66;">&#40;</span>0, <span style="color: #0066CC;">position</span> + 1<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> last:<span style="color: #0066CC;">String</span> = result.<span style="color: #0066CC;">charAt</span><span style="color: #66cc66;">&#40;</span>result.<span style="color: #0066CC;">length</span> - 1<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">length</span>:<span style="color: #0066CC;">int</span> = result.<span style="color: #0066CC;">length</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> nextChar:<span style="color: #0066CC;">String</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span>= value.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #ff0000;">' '</span> : value.<span style="color: #0066CC;">charAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>last <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">' '</span> <span style="color: #66cc66;">&amp;&amp;</span> last <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">'&gt;'</span> <span style="color: #66cc66;">&amp;&amp;</span> nextChar <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">' '</span> <span style="color: #66cc66;">&amp;&amp;</span> nextChar <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">'&lt;'</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = result.<span style="color: #0066CC;">substring</span><span style="color: #66cc66;">&#40;</span>0, result.<span style="color: #0066CC;">lastIndexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span> + 1<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> lastStartTag:<span style="color: #0066CC;">int</span> = result.<span style="color: #0066CC;">lastIndexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&lt;'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>lastStartTag <span style="color: #66cc66;">!</span>= -1<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> ch:<span style="color: #0066CC;">String</span> = result.<span style="color: #0066CC;">charAt</span><span style="color: #66cc66;">&#40;</span>lastStartTag + 1<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>ch <span style="color: #66cc66;">!</span>= <span style="color: #ff0000;">'/'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = result.<span style="color: #0066CC;">substring</span><span style="color: #66cc66;">&#40;</span>0, lastStartTag<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>original.<span style="color: #0066CC;">length</span> == result.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> original;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>result.<span style="color: #0066CC;">length</span> == 0<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> result;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> pattern:RegExp = <span style="color: #000000; font-weight: bold;">new</span> RegExp<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;(.*?)(<span style="color: #000099; font-weight: bold;">\\</span>s*<span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\.</span><span style="color: #000099; font-weight: bold;">\\</span>s*)([<span style="color: #000099; font-weight: bold;">\&lt;</span>/[a-z]*?<span style="color: #000099; font-weight: bold;">\&gt;</span><span style="color: #000099; font-weight: bold;">\\</span>s*$]+)&quot;</span>, <span style="color: #ff0000;">&quot;i&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>result.<span style="color: #006600;">search</span><span style="color: #66cc66;">&#40;</span>pattern<span style="color: #66cc66;">&#41;</span> == -<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&amp;&amp;</span> ellipses<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += <span style="color: #ff0000;">&quot;...&quot;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>result + &nbsp;<span style="color: #ff0000;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>event:more<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;[more]&lt;/a&gt;&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Let's add a little style to the HTML text and mouse roll-over actions.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getTextStyle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : StyleSheet <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> fonts:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Arial, _sans&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> textStyle:StyleSheet = <span style="color: #000000; font-weight: bold;">new</span> StyleSheet<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textStyle.<span style="color: #0066CC;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;.body&quot;</span>, <span style="color: #66cc66;">&#123;</span>fontFamily:fonts, fontSize:<span style="color: #ff0000;">&quot;12&quot;</span>, fontWeight:<span style="color: #ff0000;">&quot;normal&quot;</span>, <span style="color: #0066CC;">color</span>:<span style="color: #ff0000;">&quot;#222222&quot;</span>, textDecoration:<span style="color: #ff0000;">&quot;none&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textStyle.<span style="color: #0066CC;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;a&quot;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">color</span>:<span style="color: #ff0000;">&quot;#245290&quot;</span>, textDecoration:<span style="color: #ff0000;">&quot;none&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textStyle.<span style="color: #0066CC;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;a:link&quot;</span>, <span style="color: #66cc66;">&#123;</span> textDecoration: <span style="color: #ff0000;">&quot;none&quot;</span>, <span style="color: #0066CC;">color</span>: <span style="color: #ff0000;">&quot;#245290&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textStyle.<span style="color: #0066CC;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;a:hover&quot;</span>, <span style="color: #66cc66;">&#123;</span> textDecoration: <span style="color: #ff0000;">&quot;underline&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> textStyle;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:TextArea id=<span style="color: #ff0000;">&quot;sourceText&quot;</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;36&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;280&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;316&quot;</span><span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:TextArea id=<span style="color: #ff0000;">&quot;destinationText&quot;</span> x=<span style="color: #ff0000;">&quot;298&quot;</span> y=<span style="color: #ff0000;">&quot;36&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;280&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;316&quot;</span><span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Label x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;HTML Text&quot;</span><span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Label x=<span style="color: #ff0000;">&quot;298&quot;</span> y=<span style="color: #ff0000;">&quot;363&quot;</span> <span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;Char Limit&quot;</span><span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Label x=<span style="color: #ff0000;">&quot;298&quot;</span> y=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;Truncated HTML Text&quot;</span><span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> x=<span style="color: #ff0000;">&quot;452&quot;</span> y=<span style="color: #ff0000;">&quot;361&quot;</span> label=<span style="color: #ff0000;">&quot;Truncate&quot;</span> click=<span style="color: #ff0000;">&quot;truncateText()&quot;</span><span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:NumericStepper id=<span style="color: #ff0000;">&quot;limitStepper&quot;</span> x=<span style="color: #ff0000;">&quot;368&quot;</span> y=<span style="color: #ff0000;">&quot;361&quot;</span> stepSize=<span style="color: #ff0000;">&quot;10&quot;</span> minimum=<span style="color: #ff0000;">&quot;100&quot;</span> maximum=<span style="color: #ff0000;">&quot;1000&quot;</span> value=<span style="color: #ff0000;">&quot;200&quot;</span><span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&lt;/</span>mx:Application<span style="color: #66cc66;">&gt;</span></div></td></tr></tbody></table></div>
<p>- Mister</p>
<p><map name='google_ad_map_613_1452acacd61817fb'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/613?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_613_1452acacd61817fb' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=613&amp;url= http%3A%2F%2Fwww.thanksmister.com%2Findex.php%2Farchive%2Fflex-truncating-html-text%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://www.thanksmister.com/index.php/archive/flex-truncating-html-text/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Garbage Collection and Event Listeners</title>
		<link>http://www.thanksmister.com/index.php/archive/be-good-clean-up-your-event-listeners/</link>
		<comments>http://www.thanksmister.com/index.php/archive/be-good-clean-up-your-event-listeners/#comments</comments>
		<pubDate>Fri, 29 May 2009 07:59:17 +0000</pubDate>
		<dc:creator>Mister</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Flex 3]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[DisplayObject]]></category>
		<category><![CDATA[EventDispatcher]]></category>
		<category><![CDATA[garbage collection]]></category>
		<category><![CDATA[useWeakReference]]></category>

		<guid isPermaLink="false">http://thanksmister.com/?p=575</guid>
		<description><![CDATA[On a recent AIR project I have become aware and subsequently obsessed with memory. My concerns have centered around garbage collection and event listeners, specifically how you clean up event listeners to allow Garbage Collection (GC) to clean up objects. Most DisplayObjects implement the EventDispatcher class that allows it to broadcast events. Trolling around the [...]]]></description>
			<content:encoded><![CDATA[<p>On a recent AIR project I have become aware and subsequently obsessed with memory.   My concerns have centered around garbage collection and event listeners, specifically how you clean up event listeners to allow <a href="http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html">Garbage Collection (GC)</a> to clean up objects.  Most DisplayObjects implement the <a href="http://livedocs.adobe.com/flex/2/langref/flash/events/EventDispatcher.html#addEventListener()">EventDispatcher</a> class that allows it to broadcast events.    Trolling around the googleverse I found some interesting ways to insure that you break strong references to allow clean up or manually remove event listeners on your own to make your application more memory savvy.   </p>
<p>When you use addEventListener() method and register a event listener to an object  you create reference.  By default any object that has a reference to a listener will keep that reference until it is removed using the removeEventListener() method.    Strong references will not be cleaned up by Flash player GC and remain in memory until the application is closed or the world ends, whichever comes first.   This makes for a horribly leaky AIR applications, especially those applications that may run for days on the user&#8217;s system without being restarted.    </p>
<p><strong>Use Additional Parameters of Listener</strong></p>
<p>The addEventListener() method does have some additional (and seldom used) parameters to create a weak reference when adding the listener to an object.   On of those parameters is &#8220;useWeakReference&#8221;, the 5th parameter of the method.   To utilize this parameter you need to set it from false to true:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">addEventListener<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>, listener:<span style="color: #000000; font-weight: bold;">Function</span>, useCapture:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>, priority:<span style="color: #0066CC;">int</span> = 0, useWeakReference:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;<br />
.....<br />
<br />
<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">button</span>:<span style="color: #0066CC;">Button</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #0066CC;">button</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">click</span>, handleMouseClick, <span style="color: #000000; font-weight: bold;">false</span>, 0, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleMouseClick<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp;<span style="color: #808080; font-style: italic;">// handle the event</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p><span id="more-575"></span></p>
<p>This will allow the event to be cleaned up at some point by Flash garbage collection.  If you are like me and and find it annoying that the useWeakReference parameter of the addEventListener() method is false by default, you can can extend the object and make useWeakReference true by default, then use that object in your project:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">custom</span>.<span style="color: #006600;">controls</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CustomButton <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">Button</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> CustomButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addEventListener<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>, listener:<span style="color: #000000; font-weight: bold;">Function</span>, useCapture:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span>, priority:<span style="color: #0066CC;">int</span>=0, useWeakReference:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">super</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, listener, useCapture, priority, useWeakReference<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p><strong>Explicitly Removing Listeners</strong></p>
<p>Even though setting useWeakReference=true will most likely mark an object for clean up, to be even more certain you should explicitly remove the event listener with the removeEventListener() method:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">button</span>:<span style="color: #0066CC;">Button</span>;<br />
<br />
<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><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">button</span>:<span style="color: #0066CC;">Button</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">button</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">click</span>, handleMouseClick, <span style="color: #000000; font-weight: bold;">false</span>, 0, <span style="color: #66cc66;">&lt;</span>strong<span style="color: #66cc66;">&gt;</span>true<span style="color: #66cc66;">&lt;/</span>strong<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleMouseClick<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066CC;">button</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>handleMouseClick<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #0066CC;">button</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// handle the event</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now you are certain that you have cleaned up and removed the listener manually and the object will be removed at some point during GC since there is no longer an explicit reference to the object. </p>
<p><strong>Anonymous Functions</strong></p>
<p>Another technique that comes in handy is to removing event listeners when using anonymous functions as event handlers.  The trick to this techniques is the <a href="http://livedocs.adobe.com/flex/2/langref/arguments.html">arguments.callee</a>:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">button</span>:<span style="color: #0066CC;">Button</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #0066CC;">button</span>.<span style="color: #006600;">addEventListner</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; event.<span style="color: #006600;">currentTarget</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #0066CC;">type</span>, <span style="color: #0066CC;">arguments</span>.<span style="color: #0066CC;">callee</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p><strong>Bummer</strong></p>
<p>Now for the disappointment.  If you create an event listener using MXML they are always going to have strong reference by default with no way to change useWeakReference and they make it difficult to use removeEventListener() method:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleClickEvent<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// handle event</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;handleClickEvent(event)&quot;</span><span style="color: #66cc66;">/&gt;</span></div></td></tr></tbody></table></div>
<p>So these objects will not be cleaned up when created in this way unless you have some clean up method that explicitly removes the event listeners or you add the event listeners to MXML object using ActionScript instead of binding.</p>
<p>So basically you need to add objects or listeners in ActionScript rather than use MXML when using the addEventListener() method, which sort of makes MXML less valuable in some respects because binding to objects and listening to events is what drives the framework. </p>
<p><strong>Additional Links</strong></p>
<p>For a great discussion on the topic, see <a href="http://www.onflex.org/ted/2008/09/useweakreferencesboolean-false.php">Ted Patricks</a> post and be sure to read all the comments as many of the examples were taken from discussion. </p>
<p>Garbage Collection Articles:</p>
<p><a href="http://spreadingfunkyness.com/garbage-collection-with-flex-and-adobe-air/">http://spreadingfunkyness.com/garbage-collection-with-flex-and-adobe-air/</a><br />
<a href="http://blogs.adobe.com/aharui/2007/03/garbage_collection_and_memory.html">http://blogs.adobe.com/aharui/2007/03/garbage_collection_and_memory.html</a><br />
<a href="http://gskinner.com/blog/archives/2006/08/as3_resource_ma_2.html">http://gskinner.com/blog/archives/2006/08/as3_resource_ma_2.html</a><br />
<a href="http://www.adobe.com/devnet/flashplayer/articles/resource_management.html">http://www.adobe.com/devnet/flashplayer/articles/resource_management.html</a></p>
<p><map name='google_ad_map_575_1452acacd61817fb'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/575?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_575_1452acacd61817fb' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=575&amp;url= http%3A%2F%2Fwww.thanksmister.com%2Findex.php%2Farchive%2Fbe-good-clean-up-your-event-listeners%2F' /></p>]]></content:encoded>
			<wfw:commentRss>http://www.thanksmister.com/index.php/archive/be-good-clean-up-your-event-listeners/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
