<?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>DaveOnCode &#187; actionscript</title>
	<atom:link href="http://www.daveoncode.com/tag/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>coder and technology lover</description>
	<lastBuildDate>Sun, 16 May 2010 16:55:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Handling doubleclick on Flex components</title>
		<link>http://www.daveoncode.com/2010/04/07/handling-doubleclick-on-flex-components/</link>
		<comments>http://www.daveoncode.com/2010/04/07/handling-doubleclick-on-flex-components/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 10:34:53 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[actionscript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=598</guid>
		<description><![CDATA[Handling a common event like a double click is not so simple as we may assume when it comes to Flex programming. In fact there are several critical aspects related to this event, first of all we have to set the attribute doubleClickEnabled to true in order to make our components double clickable, and this]]></description>
			<content:encoded><![CDATA[<p>Handling a common event like a double click is not so simple as we may assume when it comes to Flex programming.<br />
In fact there are several critical aspects related to this event, first of all we have to set the attribute <strong>doubleClickEnabled</strong> to <strong>true</strong> in order to make our components double clickable, and this sounds pretty silly to me. Why should I enable an object to receive an event which is so common and &#8220;natural&#8221;? This is incoherent from a software design perspective end totally unexpected by the user. Anyway this is not a real problem, because once we know about it we have just to use a setter, but double click is somehow buggy in the framework and its broadcasting can be blocked mysteriously when using certain components. I faced this issue by trying to handle a double click on a DataGrid which makes use of custom item renderers and I tested that by double clicking on the &#8220;special cells&#8221; (those rendered by my own renderers) the <strong>MouseEvent.DOUBLE_CLICK</strong> is not dispatched/cacthed. To solve the problem I had to draw an invisible rectangle using the underlying Actionscript graphics API! This is my workaround:</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 /></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;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xffffff, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #0066CC;">width</span>, <span style="color: #0066CC;">this</span>.<span style="color: #006600;">parent</span>.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>Finally, it seems that double click is <strong>completely ignored</strong> by Firefox 3.6 on Mac OS X, but fortunately this seems to been fixed in the last release (3.6.3)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2010/04/07/handling-doubleclick-on-flex-components/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get hours, minutes and seconds from a number without maths operations</title>
		<link>http://www.daveoncode.com/2010/02/05/get-hours-minutes-and-seconds-from-number-without-maths-operations/</link>
		<comments>http://www.daveoncode.com/2010/02/05/get-hours-minutes-and-seconds-from-number-without-maths-operations/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 15:09:11 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=589</guid>
		<description><![CDATA[Today I&#8217;m gonna show you a secret ninja technique to extract hours, minutes and seconds from a number (representing an amount of seconds). In my example I will show an Actionscript code, but this can be implemented in JavaScript and maybe other languages too. So, the scenario is the following: we have a number representing]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;m gonna show you a secret ninja technique to extract hours, minutes and seconds from a number (representing an amount of seconds). In my example I will show an Actionscript code, but this can be implemented in JavaScript and maybe other languages too.<br />
So, the scenario is the following: we have a number representing seconds and we want to know how many hours this amount of seconds contains, how many minutes and how many remaining seconds. We know that a minute is composed by 60 seconds and an hour by 60 minutes and we could write a series of maths operations in order to accomplish our objective, but there is a way far simple and fast: use the Date class!<br />
Date class already implements all the methods we need:</p>
<ul>
<li><strong>getHours()</strong></li>
<li><strong>getMinutes()</strong></li>
<li><strong>getSeconds()</strong></li>
</ul>
<p>So, in order to take advantage of these useful methods, all we have to do is initialize a &#8220;fake&#8221; date using the amount of seconds we want to &#8220;split&#8221; into hours, minutes and seconds. The Date class has several OPTIONAL arguments that can be specified during its initializations, these are:</p>
<ul>
<li><strong>year</strong></li>
<li><strong>month</strong></li>
<li><strong>date</strong> (day number)</li>
<li><strong>hours</strong></li>
<li><strong>minutes</strong></li>
<li><strong>seconds</strong></li>
<li><strong>milliseconds</strong></li>
</ul>
<p>Because theme all are optional, we can create a date object by specifying only the know arguments (in our case seconds) and by assigning null or zero to the others:</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: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">date</span>:<span style="color: #0066CC;">Date</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">9137</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>Then by calling getHours, getMinutes and getSeconds we will obtain what we expect:</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 /></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><span style="color: #ff0000;">&quot;hours: &quot;</span>, <span style="color: #0066CC;">date</span>.<span style="color: #0066CC;">getHours</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 2 hours</span><br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;minutes: &quot;</span>, <span style="color: #0066CC;">date</span>.<span style="color: #0066CC;">getMinutes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 32 minutes</span><br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;seconds: &quot;</span>, <span style="color: #0066CC;">date</span>.<span style="color: #0066CC;">getSeconds</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 17 seconds</span></div></td></tr></tbody></table></div>
<p>Not sure about the result? Let&#8217;s test it:</p>
<p>60 * 60 * 2 = <strong>7200</strong>; (seconds contained in 2 hours)<br />
32 * 60 = <strong>1920</strong>; (seconds contained in 32 minutes)<br />
7200 + 1920 + 17 = <strong>9137</strong>; (original seconds!)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2010/02/05/get-hours-minutes-and-seconds-from-number-without-maths-operations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>AIR applications logging&#8230; my own extension to Flex&#8217;s logging framework</title>
		<link>http://www.daveoncode.com/2009/05/11/air-application-logging-flex-extension/</link>
		<comments>http://www.daveoncode.com/2009/05/11/air-application-logging-flex-extension/#comments</comments>
		<pubDate>Mon, 11 May 2009 09:16:17 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[air]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=396</guid>
		<description><![CDATA[Finally it&#8217;s time to release my little project and my first personal and public Actionscript library. What I&#8217;m talking about? I realized a little &#8220;subframework&#8221; which goal is to extend Flex&#8217;s logging framework, in order to write log messages to files on the disk and provide a custom Flex&#8217;s UI component (which can be easy]]></description>
			<content:encoded><![CDATA[<p>Finally it&#8217;s time to release my little project and my first personal and public Actionscript library.<br />
What I&#8217;m talking about? I realized a little &#8220;subframework&#8221; which goal is to extend Flex&#8217;s logging framework, in order to write log messages to files on the disk and provide a custom Flex&#8217;s UI component (which can be easy used into every AIR application) to read/filtering these log files.</p>
<p>Learn more here: <a href="http://www.daveoncode.com/air-logging-framework/">http://www.daveoncode.com/air-logging-framework</a></p>
<p>Just a note: although I tested the library (windows and mac) and it works fine, this project may contains bugs or doesn&#8217;t work under some circumstances. Comments and feedback are welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/05/11/air-application-logging-flex-extension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deploy and work with Flex swc library</title>
		<link>http://www.daveoncode.com/2009/04/07/deploy-and-work-with-flex-swc-library/</link>
		<comments>http://www.daveoncode.com/2009/04/07/deploy-and-work-with-flex-swc-library/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 13:49:01 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[RSL]]></category>
		<category><![CDATA[swc]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=371</guid>
		<description><![CDATA[When we need to use a series of Actionscript classes in several projects and centralize them into a single place, the best option available is to deploy our classes as an SWC library and then add it to the &#8220;library path&#8221; under Flex Builder&#8217;s &#8220;build path&#8221; panel. Deploy an swc file To obtain an SWC]]></description>
			<content:encoded><![CDATA[<p>When we need to use a series of Actionscript classes in several projects and centralize them into a single place, the best option available is to deploy our classes as an SWC library and then add it to the &#8220;library path&#8221; under Flex Builder&#8217;s &#8220;build path&#8221; panel.</p>
<p><span id="more-371"></span></p>
<p><strong style="margin: 20px 0">Deploy an swc file</strong></p>
<p>To obtain an SWC file, we have first to create a library project by selecting &#8220;<strong>New > Flex Library Project</strong>&#8221; from Flex builder&#8217;s menu.<br />
The file will be automatically generated under the bin folder as soon a new class is created and/or modified (under <strong>src </strong>folder) and it will have the same name of the project. Is also possible to copy .as files from a folder and paste them into src folder, but in this case the SWC file won&#8217;t be (re)deployed until we select the new added classes under &#8220;<strong>Flex Library Build Path</strong>&#8220;.<br />
There are two other cases for which Flex Builder won&#8217;t (re)deploy the SWC file and is important to keep these in mind, because no errors nor notification messages will be showed by the ide and this could lead to a waste of time trying to figure out what is going on.<br />
The two big things to remember are:</p>
<p>1. Be sure to check &#8220;<strong>Include Adobe AIR libraries</strong>&#8221; under &#8220;<strong>Flex library compiler</strong>&#8221; settings (this can be defined during library creation step or after) when the library make use of AIR api<br />
2. Be sure that package path, declared into imported classes, match the structure of src folder (if we import a class &#8220;MyClass&#8221; with a package declaration like &#8220;com.mysite.ui&#8221; then src folder must contain &#8220;com/mysite/ui&#8221; and &#8220;MyClass&#8221; must be placed there, if we put it under the src root it won&#8217;t be compiled into SWC)</p>
<p><strong style="margin: 20px 0">Import the swc file into a project</strong></p>
<p>Once we get our library project and our compiled swc, we can import it into others Flex projects by going to &#8220;<strong>Flex build path > Library path</strong>&#8221; and selecting &#8220;<strong>Add project</strong>&#8220;. There are two ways of using the imported library and to determine the type of linkage to it: a static link (by choosing the default option &#8220;<strong>Merge into code</strong>&#8220;) or dynamic link (by choosing the default option &#8220;<strong>Runtime shared library (RSL)</strong>&#8220;). The difference between the two types of linkage are that, in the first all library content are compiled into the final project&#8217;s swf file, so once you import and merge the library, its swc file is not necessary anymore and you can distribute your application without the external library (it&#8217;s already embedded into the app), by using an RSL instead the library is loaded at runtime and the application swf doesn&#8217;t keep it&#8217;s data, but only a reference to its location, so the resulting file is smaller than the previous one, but you must be sure to release the Flex application in conjunction with the external library in order to works, otherwise Flex Builder will show an error saying &#8220;unable to open path/yourlib.swc&#8221; (where &#8220;path&#8221; is the path to the library you previously imported).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/04/07/deploy-and-work-with-flex-swc-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ArrayCollection: filterFuntion with multiple filter functions using decorator pattern</title>
		<link>http://www.daveoncode.com/2009/03/30/arraycollection-filterfuntion-with-multiple-filter-functions-using-decorator-pattern/</link>
		<comments>http://www.daveoncode.com/2009/03/30/arraycollection-filterfuntion-with-multiple-filter-functions-using-decorator-pattern/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 10:21:11 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[design patterns]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=358</guid>
		<description><![CDATA[I was looking for an elegant solution to implement multiple filter functions on an ArrayCollection (which provides a method filterFunction() that can be assigned dynamically in order to filter data inside the collection). The first and only valid solution I found was that one from Cristian Rotundu, he has extended ArrayCollection class providing a filterFunctions]]></description>
			<content:encoded><![CDATA[<p>I was looking for an elegant solution to implement multiple filter functions on an <strong>ArrayCollection</strong> (which provides a method <strong><em>filterFunction()</em></strong> that can be assigned dynamically in order to filter data inside the collection). The first and only valid solution I found was that one from <a href="http://blog.rotundu.eu/flex/arraycollection-with-multiple-filter-functions/">Cristian Rotundu</a>, he has extended <strong>ArrayCollection</strong> class providing a <strong>filterFunctions</strong> properties which accepts an Array of functions which will be executed in sequence (into a loop) once filterFunction is triggered. Anyway I didn&#8217;t want to subclass <strong>ArrayCollection</strong> and by using the decorator pattern I implemented my own multiple filters version.</p>
<p>So, this is what I done:</p>
<ol>
<li>I defined an interface <strong>IFilter</strong>, which declares only one method: <strong><em>apply()</em></strong></li>
<li>I defined a dummy <strong>Filter</strong> class (which implements <strong>IFilter</strong> in a basic way just to agree to the interface), that is used as a basic empty filter to decorate. It also has a constant called ALL_VALUES which is used as a wildcard for filters (it&#8217;s a simple string with value &#8220;*&#8221;, which means &#8220;all values are allowed&#8221;)</li>
<li>I defined an abstract <strong>AbstractFilterDecorator</strong> class</li>
<li>I created as many subclass of <strong>AbstractFilterDecorator</strong> as the filters I need (yes I treat filters as classes not mere functions)</li>
</ol>
<p>The implementation code is the following:</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 /></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;">data</span>:ArrayCollection = ArrayCollection<span style="color: #66cc66;">&#40;</span>grid.<span style="color: #006600;">dataProvider</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">var</span> filter:IFilter = <span style="color: #000000; font-weight: bold;">new</span> Filter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
filter = <span style="color: #000000; font-weight: bold;">new</span> LevelFilter<span style="color: #66cc66;">&#40;</span>filter, levelValue<span style="color: #66cc66;">&#41;</span>;<br />
filter = <span style="color: #000000; font-weight: bold;">new</span> CategoryFilter<span style="color: #66cc66;">&#40;</span>filter, categoryValue<span style="color: #66cc66;">&#41;</span>;<br />
filter = <span style="color: #000000; font-weight: bold;">new</span> DateFilter<span style="color: #66cc66;">&#40;</span>filter, dateValue, <span style="color: #ff0000;">&quot;DD/MM/YY&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">filterFunction</span> = filter.<span style="color: #0066CC;">apply</span>;<br />
<span style="color: #0066CC;">data</span>.<span style="color: #006600;">refresh</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>The filter object is wrapped by decorators which all accepts 2 common arguments: an IFilter reference and an Object representing a value. The DateFilter has a third parameter which is used to configure an internal DateFormatter (from mx.formatters package).<br />
I&#8217;m pretty satisfied :)<br />
Uh&#8230; and if you are worried about performance, it is fast enough because filter object are created once and then the function assigned to <strong>filterFunction</strong> is a reference to the resulting decorated filter.</p>
<p><strong>UPDATE:</strong><br />
I realized a very simple <a href="http://www.daveoncode.com/_dave_stuff/samples/Filters/bin-debug/Filters.html">example application</a> which can be downloaded <a href="http://www.daveoncode.com/_dave_stuff/samples/Filters.zip">here</a>.<br />
This following is the content of the mxml file:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 /></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;vertical&quot;</span><span style="color: #66cc66;">&gt;</span><br />
<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; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">collections</span>.<span style="color: #006600;">ArrayCollection</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">daveoncode</span>.<span style="color: #006600;">filters</span>.<span style="color: #66cc66;">*</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> applyFilters<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</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> <span style="color: #0066CC;">data</span>:ArrayCollection &nbsp;= ArrayCollection<span style="color: #66cc66;">&#40;</span>userGrid.<span style="color: #006600;">dataProvider</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> filter:IFilter = <span style="color: #000000; font-weight: bold;">new</span> Filter<span style="color: #66cc66;">&#40;</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; filter = <span style="color: #000000; font-weight: bold;">new</span> AgeFilter<span style="color: #66cc66;">&#40;</span>filter, ageFilterValue.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filter = <span style="color: #000000; font-weight: bold;">new</span> DateFilter<span style="color: #66cc66;">&#40;</span>filter, dateFilterValue.<span style="color: #006600;">selectedDate</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filter = <span style="color: #000000; font-weight: bold;">new</span> SexFilter<span style="color: #66cc66;">&#40;</span>filter, sexFilterValue.<span style="color: #006600;">value</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: #0066CC;">data</span>.<span style="color: #006600;">filterFunction</span> = filter.<span style="color: #0066CC;">apply</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">data</span>.<span style="color: #006600;">refresh</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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; <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:Panel title=<span style="color: #ff0000;">&quot;Filters&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;600&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Form<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:FormItem label=<span style="color: #ff0000;">&quot;Age:&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:TextInput id=<span style="color: #ff0000;">&quot;ageFilterValue&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:FormItem<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:FormItem label=<span style="color: #ff0000;">&quot;Sex:&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:ComboBox id=<span style="color: #ff0000;">&quot;sexFilterValue&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{[{data: Filter.ALL_VALUES, label: 'ALL'}, {data: 'm', label: 'Male'}, {data: 'f', label: 'Female'}]}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:FormItem<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:FormItem label=<span style="color: #ff0000;">&quot;Join date:&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:DateField id=<span style="color: #ff0000;">&quot;dateFilterValue&quot;</span> formatString=<span style="color: #ff0000;">&quot;DD/MM/YY&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:FormItem<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:FormItem<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Button</span> label=<span style="color: #ff0000;">&quot;Apply filters&quot;</span> click=<span style="color: #ff0000;">&quot;{this.applyFilters()}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:FormItem<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:Form<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:Panel<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Panel title=<span style="color: #ff0000;">&quot;Users&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;600&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:DataGrid id=<span style="color: #ff0000;">&quot;userGrid&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:dataProvider<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:ArrayCollection<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:Array<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;24&quot;</span> sex=<span style="color: #ff0000;">&quot;f&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Susan&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2007, 5, 15)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;36&quot;</span> sex=<span style="color: #ff0000;">&quot;f&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Ashley&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(1998, 7, 20)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;24&quot;</span> sex=<span style="color: #ff0000;">&quot;f&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Jennifer&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2001, 3, 24)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;19&quot;</span> sex=<span style="color: #ff0000;">&quot;f&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Emma&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2002, 3, 24)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;44&quot;</span> sex=<span style="color: #ff0000;">&quot;f&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Carol&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(1999, 9, 16)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;28&quot;</span> sex=<span style="color: #ff0000;">&quot;m&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Peter&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2005, 3, 12)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;35&quot;</span> sex=<span style="color: #ff0000;">&quot;m&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Mike&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2008, 10, 10)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;26&quot;</span> sex=<span style="color: #ff0000;">&quot;m&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Dave&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2008, 10, 10)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;44&quot;</span> sex=<span style="color: #ff0000;">&quot;m&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;William&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2004, 9, 16)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Object</span> age=<span style="color: #ff0000;">&quot;24&quot;</span> sex=<span style="color: #ff0000;">&quot;m&quot;</span> <span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;Sean&quot;</span> joinDate=<span style="color: #ff0000;">&quot;{new Date(2006, 3, 24)}&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:Array<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:ArrayCollection<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:dataProvider<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:DataGrid<span style="color: #66cc66;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&lt;/</span>mx:Panel<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>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/30/arraycollection-filterfuntion-with-multiple-filter-functions-using-decorator-pattern/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Learning design patterns with Actionscript 3: episode 2 &#8211; Decorator pattern</title>
		<link>http://www.daveoncode.com/2009/03/30/learning-design-patterns-with-actionscript-3-episode-2-decorator-pattern/</link>
		<comments>http://www.daveoncode.com/2009/03/30/learning-design-patterns-with-actionscript-3-episode-2-decorator-pattern/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 09:21:26 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[design patterns]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=354</guid>
		<description><![CDATA[The objective of the decorator pattern is to provide a way to add extra features and responsibilities to a class at runtime without to subclass it. The classes diagram of this pattern is composed by an interface or an abstract class which will define the supertype of the class that will be extended (decorated) by]]></description>
			<content:encoded><![CDATA[<p>The objective of the decorator pattern is to provide a way to add extra features and responsibilities to a class at runtime without to subclass it.</p>
<p>The classes diagram of this pattern is composed by an interface or an abstract class which will define the supertype of the class that will be extended (decorated) by one or more classes implementing the same interface and extending an abstract class which identify these as decorator classes.</p>
<p><span id="more-354"></span></p>
<p>A real scenario into which apply this pattern can be a cars store. A car is the base class and the accessories such dvd player, gps, air conditioning and so on, can be thought as decorators. Once decorated, the car (which has a method getPrice() which returns its cost) will get updated by accessories, so calling the getPrice() method will include the price of the “naked” car plus the price of the various accessories installed.</p>
<p>Let’s take a look to the classes…</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">package com.<span style="color: #006600;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IVehicle <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>The interface above will be implemented by all the classes and we will treat the car as an IVehicle not a concrete Car class.<br />
This is our simple Car class:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 /></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;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Car <span style="color: #0066CC;">implements</span> IVehicle <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Car<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">50000</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;Base car&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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 />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now is time to see the decorators (in our scenario the accessories). This is the abstract Accessory class, all accessories will extend it:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 /></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;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Accessory <span style="color: #0066CC;">implements</span> IVehicle <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; protected <span style="color: #000000; font-weight: bold;">var</span> _vehicle:IVehicle;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Accessory<span style="color: #66cc66;">&#40;</span>vehicle:IVehicle<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>._vehicle = vehicle;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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 />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>In Accessory we can notice that the class encapsulate an object of IVehicle which is passed to the constructor (of course since this class is abstract the object will be passed to the constructor of Accessory&#8217;s subclasses). This is the key concept of decorator pattern, all decorators keep a reference of the interface they are going to decorate and this reference is used as the base for methods business logic. Let&#8217;s see how:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 /></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;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DVDPlayer <span style="color: #0066CC;">extends</span> Accessory <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DVDPlayer<span style="color: #66cc66;">&#40;</span>vehicle:IVehicle<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>vehicle<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">550</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;, DVD Player&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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 />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>DVDPlayer is a concrete decorator and it uses the encapsulated IVehicle object in order to provide an extended version of the interface methods (getPrice() and getDescription()). Basically it uses first the reference methods and then it adds some other values.</p>
<p>To use the pattern we will first instantiate a Car object, than we will wrap it with one or more decorators:</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 /></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> car:IVehicle = <span style="color: #000000; font-weight: bold;">new</span> Car<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Price: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; - description: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
car = <span style="color: #000000; font-weight: bold;">new</span> DVDPlayer<span style="color: #66cc66;">&#40;</span>car<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Price: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; - description: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
car = <span style="color: #000000; font-weight: bold;">new</span> GPSSystem<span style="color: #66cc66;">&#40;</span>car<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Price: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; - description: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>The output will be:</p>
<pre>
Price: 50000 - description: Base car
Price: 50550 - description: Base car, DVD Player
Price: 50875 - description: Base car, DVD Player, GPS System
</pre>
<p>The order into which we wrap the concrete component (Car) is indifferent (we can add GPSSystem before or after DVDPlayer) and of course despite its readability, we can wrap the Car in a single statement:</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: #000000; font-weight: bold;">var</span> car2:IVehicle = <span style="color: #000000; font-weight: bold;">new</span> GPSSystem<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DVDPlayer<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Car<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>In this example I used very simple classes, however in a real implementation we can create more sophisticated decorators which will accept extra arguments beyond the IVehicle reference.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/30/learning-design-patterns-with-actionscript-3-episode-2-decorator-pattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Set DataGridColumn&#8217;s width in percentage with ease</title>
		<link>http://www.daveoncode.com/2009/03/18/set-datagridcolumns-width-in-percentage-with-ease/</link>
		<comments>http://www.daveoncode.com/2009/03/18/set-datagridcolumns-width-in-percentage-with-ease/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 22:40:06 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[actionscript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=350</guid>
		<description><![CDATA[I was trying to set the columns of a DataGrid to a certain percentage, but I discovered that inexplicably DataGridColumn doesn&#8217;t provide percentageWidth property nor it accepts a percentage string as width property&#8230; so I did a quick search on google and I found that is sad but true and moreover the solution I found]]></description>
			<content:encoded><![CDATA[<p>I was trying to set the columns of a DataGrid to a certain percentage, but I discovered that inexplicably DataGridColumn doesn&#8217;t provide percentageWidth property nor it accepts a percentage string as width property&#8230; so I did a quick search on google and I found that is sad but true and moreover the solution I found on the web is to use a function to calculate each single column width after creationComplete event is dispatched&#8230; terrible! However I did a test before, by assign a decimal value to width (0.5 instead of 50%) and it works! The width property in fact accepts a Number as value not an int, so we can specify a value under 1, which is treated as a percentage&#8230; what surprise me, is that no one seems to had this idea&#8230; anyway I hope this will be helpful for all.</p>
<p>This is a piece of my AS code to set DataGridColumn width in percentage:</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 /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">col = <span style="color: #000000; font-weight: bold;">new</span> DataGridColumn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
col.<span style="color: #0066CC;">width</span> = options<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> == <span style="color: #ff0000;">&quot;Message&quot;</span> ? <span style="color: #cc66cc;">0.6</span> : <span style="color: #cc66cc;">0.1</span>;</div></td></tr></tbody></table></div>
<p>In my code I&#8217;m setting the columns to 10% and to 60% only for &#8220;Message&#8221; column.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/18/set-datagridcolumns-width-in-percentage-with-ease/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>parseInt(): difference between Javascript and Actionscript</title>
		<link>http://www.daveoncode.com/2009/03/06/parseint-difference-between-javascript-and-actionscript/</link>
		<comments>http://www.daveoncode.com/2009/03/06/parseint-difference-between-javascript-and-actionscript/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 15:27:06 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=334</guid>
		<description><![CDATA[Today is the day of the little big discoveries. I realized that the parseInt() function, which objective is the same in both languages (Actionscript and Javascript), is a bit different between them. In Actionscript the function is based on base 10 numeration, in Javascript instead the funny thing is that it try to guess the]]></description>
			<content:encoded><![CDATA[<p>Today is the day of the little big discoveries. I realized that the parseInt() function, which objective is the same in both languages (Actionscript and Javascript), is a bit different between them. In Actionscript the function is based on base 10 numeration, in Javascript instead the funny thing is that it try to guess the radix by analyzing the string received as first argument, so if you want to be sure of the result returned you have to explicitly pass the radix in the second argument.</p>
<p>Example:</p>
<pre>var myString = "000123";
alert(parseInt(myString));</pre>
<p>The code above will alert <strong>83</strong> instead of <strong>123</strong> (because the default base guessed by Javascript isn&#8217;t 10 like in Actionscript), to get <strong>123</strong> the code must be:</p>
<pre>var myString = "000123";
alert(parseInt(myString, 10));</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/06/parseint-difference-between-javascript-and-actionscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending Flex&#8217;s logging framework to write custom log files</title>
		<link>http://www.daveoncode.com/2009/02/23/extending-flexs-logging-framework-to-write-custom-log-files/</link>
		<comments>http://www.daveoncode.com/2009/02/23/extending-flexs-logging-framework-to-write-custom-log-files/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 18:19:06 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=308</guid>
		<description><![CDATA[In these days I played with Flex&#8217;s logging framework and I extended it to write log messages to files, both plain text .log files and dynamic HTML, CSS formatted and Javascript powered files with the ability to filter message type (debug, error, fatal and so on). The logging framework is a powerful feature that comes]]></description>
			<content:encoded><![CDATA[<p>In these days I played with Flex&#8217;s logging framework and I extended it to write log messages to files, both plain text .log files and dynamic HTML, CSS formatted and Javascript powered files with the ability to filter message type (debug, error, fatal and so on).</p>
<p>The logging framework is a powerful feature that comes with Flex&#8217;s sdk and it&#8217;s composed by the classes under <strong>mx.logging </strong>package, which includes the two &#8220;subpackages&#8221; <strong>mx.logging.errors</strong> and <strong>mx.logging.target</strong>.</p>
<p>Java developers should be already confident whit such framework, because logging api are commonly used in Java programming,  Actionscript /Flash/Flex developers may find this tool a novelty (as I did).</p>
<p>The objective of logging framework is to provide a tool that offers a far better, flexible and centralized way to debug an application than simple use a lot of <strong>trace()</strong> callings. With Flex&#8217;s debugging framework we are able to print and filtering among different types of message based on their severity, such:</p>
<ul>
<li>DEBUG</li>
<li>ERROR</li>
<li>FATAL</li>
<li>INFO</li>
<li>WARN</li>
</ul>
<p>We can also print the timestamp of the message and the class it refers to, and finally we can simultaneously print messages to different targets.</p>
<p><span id="more-308"></span></p>
<h3 style="margin: 20px 0">How it works</h3>
<p>The logging framework is basically represented by 3 units: a logger, a target and a log event. The first component is the main class of the framework and its job is to register the different &#8220;loggable classes&#8221;, register one or more targets and dispatch log events. A target is a class which receives a LogEvent event and handles it by printing its information (message, date, level&#8230;) somewhere. A LogEvent event is dispatched when one of the logger methods such: <strong>debug()</strong>, <strong>error()</strong>, <strong>fatal()</strong>, <strong>info()</strong>, <strong>log()</strong> or <strong>warn()</strong> is called.</p>
<p>The steps required to use the logging system are the following:</p>
<p>1. Registering the classes for logging:</p>
<div class="codecolorer-container actionscript3 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="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #9900cc; font-weight: bold;">package</span> com<span style="color: #000066; font-weight: bold;">.</span>daveoncode <span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>logging<span style="color: #000066; font-weight: bold;">.*;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Foo <span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _logger<span style="color: #000066; font-weight: bold;">:</span>ILogger<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Foo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>_logger = Log<span style="color: #000066; font-weight: bold;">.</span>getLogger<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;com.daveoncode.Foo&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>_logger<span style="color: #000066; font-weight: bold;">.</span>debug<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;debugging message from Foo class&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>_logger<span style="color: #000066; font-weight: bold;">.</span>warn<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;warning message from Foo class&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>The code above will register the class Foo under the package com.daveoncode and then it will broadcast a debug and a warning message</p>
<p>2. Initialize and configure a logging target such TraceTarget (typically in the main class):</p>
<div class="codecolorer-container actionscript3 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 /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> basicTarget<span style="color: #000066; font-weight: bold;">:</span>TraceTarget = <span style="color: #0033ff; font-weight: bold;">new</span> TraceTarget<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Apply filters</span><br />
basicTarget<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">filters</span> = <span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;com.daveoncode.*&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Log all log levels.</span><br />
basicTarget<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">level</span> = LogEventLevel<span style="color: #000066; font-weight: bold;">.</span>ALL<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add date, time, category, and log level to the output</span><br />
basicTarget<span style="color: #000066; font-weight: bold;">.</span>includeDate = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
basicTarget<span style="color: #000066; font-weight: bold;">.</span>includeTime = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
basicTarget<span style="color: #000066; font-weight: bold;">.</span>includeCategory = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
basicTarget<span style="color: #000066; font-weight: bold;">.</span>includeLevel = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>The code above will print log messages of all &#8220;subscribed&#8221; classes inside the package com.daveoncode, including all information available (date, time, category, level)</p>
<p>3. Add the target to the logger:</p>
<div class="codecolorer-container actionscript3 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="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Log<span style="color: #000066; font-weight: bold;">.</span>addTarget<span style="color: #000000;">&#40;</span>basicTarget<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>The previous steps can be repeated as many times as we need, in order to registering several classes and several targets (<span onclick="dr4sdgryt(event)">perhaps </span>with different filters and info settings).</p>
<h3 style="margin: 20px 0">How to extend the framework</h3>
<p>To extend the framework we can create new target classes, which print LogEvent data to different location and display data as we like. In my experiment I created a &#8220;mini subframework&#8221; which can print logs to text files or html files. I created the following 7 classes:</p>
<ol>
<li>LogFileTarget</li>
<li>IStreamDataWriter</li>
<li>AbstractDataWriter</li>
<li>PlainTextDataWriter</li>
<li>HTMLDataWriter</li>
<li>InvalidLogFileTargetError</li>
<li>UnsupportedWriteModeError</li>
</ol>
<p>The first, as the name suggest is a custom target class, which extends LineFormattedTarget (a target class available in Flex&#8217;s framework), it has a parametric constructor which allows the user to choose the name of the file, its directory, the type of file (txt or html) and the charset that will be used to write the file. IStreamDataWriter is an interface which simply declares one method called writeData(), the main purpose of this interface is to use it in polymorphism. IStreamDataWriter is implemented by AbstractDataWriter as an abstract method (abstract is not supported yet in as3, but is treated as abstract), this class also has a secondary method which is used by two subclasses <strong>PlainTextDataWriter </strong>and <strong>HTMLDataWriter</strong>. These classes have the duty to physically write data to the file by handling the event received by <strong>LogFileTarget </strong>which delegates the business logic to them and are used based on the writeMode configuration of  <strong>LogFileTarget</strong>, which for ease has two public constants called <strong>MODE_PLAIN_TEXT</strong> and <strong>MODE_HTML. InvalidLogFileTargetError</strong> and <strong>UnsupportedWriteModeError </strong>are obviously two custom error which are both (potentially) thrown by <strong>LogFileTarget.</strong></p>
<p>This is a sample code using my own classes:</p>
<div class="codecolorer-container actionscript3 mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">// this will print to an html file</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> HTMLTarget<span style="color: #000066; font-weight: bold;">:</span>LogFileTarget = <span style="color: #0033ff; font-weight: bold;">new</span> LogFileTarget<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;applicationLog&quot;</span><span style="color: #000066; font-weight: bold;">,</span> File<span style="color: #000066; font-weight: bold;">.</span>desktopDirectory<span style="color: #000066; font-weight: bold;">,</span> LogFileTarget<span style="color: #000066; font-weight: bold;">.</span>MODE_HTML<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// this will print to a txt file</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> txtTarget<span style="color: #000066; font-weight: bold;">:</span>LogFileTarget = <span style="color: #0033ff; font-weight: bold;">new</span> LogFileTarget<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;applicationLog&quot;</span><span style="color: #000066; font-weight: bold;">,</span> File<span style="color: #000066; font-weight: bold;">.</span>desktopDirectory<span style="color: #000066; font-weight: bold;">,</span> LogFileTarget<span style="color: #000066; font-weight: bold;">.</span>MODE_PLAIN_TEXT<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
HTMLTarget<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">filters</span> = <span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;*&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp;<span style="color: #009900; font-style: italic;">// Log all log levels.</span><br />
HTMLTarget<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">level</span> = LogEventLevel<span style="color: #000066; font-weight: bold;">.</span>ALL<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add date, time, category, and log level to the output.</span><br />
HTMLTarget<span style="color: #000066; font-weight: bold;">.</span>includeDate = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
HTMLTarget<span style="color: #000066; font-weight: bold;">.</span>includeTime = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
HTMLTarget<span style="color: #000066; font-weight: bold;">.</span>includeCategory = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
HTMLTarget<span style="color: #000066; font-weight: bold;">.</span>includeLevel = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
txtTarget<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">filters</span> = <span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;*&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Log all log levels.</span><br />
txtTarget<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">level</span> = LogEventLevel<span style="color: #000066; font-weight: bold;">.</span>ALL<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add date, time, category, and log level to the output.</span><br />
txtTarget<span style="color: #000066; font-weight: bold;">.</span>includeDate = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
txtTarget<span style="color: #000066; font-weight: bold;">.</span>includeTime = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
txtTarget<span style="color: #000066; font-weight: bold;">.</span>includeCategory = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
txtTarget<span style="color: #000066; font-weight: bold;">.</span>includeLevel = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// Add targets</span><br />
Log<span style="color: #000066; font-weight: bold;">.</span>addTarget<span style="color: #000000;">&#40;</span>HTMLTarget<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
Log<span style="color: #000066; font-weight: bold;">.</span>addTarget<span style="color: #000000;">&#40;</span>txtTarget<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>Of course this experiment will works only in AIR applications, because we need to use flash.fileystem classes.</p>
<p>These are the two different output that my classes will generate:<br />
<a href="http://www.daveoncode.com/_dave_stuff/samples/applicationLog.log">Text file</a><br />
<a href="http://www.daveoncode.com/_dave_stuff/samples/applicationLog.html">HTML file</a></p>
<p>The cool aspect of html version (LogFileTarget.MODE_HTML) is that you can filter by log type :)</p>
<p>If you want to use or take a look to my experiment, feel free to download my logging package <a href="http://www.daveoncode.com/_dave_stuff/doc_logging_framework-1.0.zip">here</a> (let me know your opinion)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/02/23/extending-flexs-logging-framework-to-write-custom-log-files/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Converting Java to Actionscript&#8230; maybe is not so hard (I hope)</title>
		<link>http://www.daveoncode.com/2009/02/12/converting-java-to-actionscript-maybe-is-not-so-hard-i-hope/</link>
		<comments>http://www.daveoncode.com/2009/02/12/converting-java-to-actionscript-maybe-is-not-so-hard-i-hope/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 12:06:19 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[code conversion]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[porting]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=294</guid>
		<description><![CDATA[After valuate my own realization of a certain library, I opted to try the porting of an already existent one&#8230; which is made in Java. This library has a lot of classes and interfaces and the manually translation would be very time-expensive, considering also that it uses a lot of classes and functions which are]]></description>
			<content:encoded><![CDATA[<p>After valuate my own realization of a certain library, I opted to try the porting of an already existent one&#8230; which is made in Java. This library has a lot of classes and interfaces and the manually translation would be very time-expensive, considering also that it uses a lot of classes and functions which are unavailable in Actionscript 3. Fortunately I found a small and useful AIR application which automatically converts .java files to .as files (the application is here: <a href="http://thunderhead.esri.com/readonlyurl/J2AS3.air">http://thunderhead.esri.com/readonlyurl/J2AS3.air</a>), this application anyway helps a lot but many extra-work is required to properly convert the classes&#8230; for example Actionscript has not the <em><strong>abstract</strong></em> key, variable can&#8217;t be market as <em><strong>final</strong></em> and so on, so we have to manually adjust these issues. Another useful stuff I found is an Actionscript library which aim is to provide to AS3 developers the same (more or less) features and power of Java Collection framework, which provide classes like HashMap, ArrayList, LinkedList and so on (the library can be found here: <a href="http://code.google.com/p/addicted2flash/" target="_blank">http://code.google.com/p/addicted2flash/</a>). Despite these helps I&#8217;m still facing some incompatibility issues&#8230; classes/methods AS3 is missing, but implementing them by myself is quite simple. For example I implemented the Java&#8217;s System.arraycopy() in 2 minutes, by following the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int" target="_blank">JavaDoc</a> ).</p>
<p>In conclusion Actionscript and Java have a  lot of stuff in common, the syntax and structure is quite the same, except for some issues like abstract classes and methods, multiple methods and constructors declaration (override). The main difference is that Java provides an huge number of Classes that Actionscript doesn&#8217;t, however it seems not so hard to translate Java code and implement Java functionality in Actionscript.</p>
<p>I hope I won&#8217;t have to deny my words :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/02/12/converting-java-to-actionscript-maybe-is-not-so-hard-i-hope/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
