<?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>Ayone Blog &#187; Flex</title>
	<atom:link href="http://ayonesoftware.com/blog/category/flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://ayonesoftware.com/blog</link>
	<description>Sharing ideas on Rich Internet Application Development</description>
	<lastBuildDate>Tue, 18 Aug 2009 06:40:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Linked Containers in Flex 4 using Text Layout Framework</title>
		<link>http://ayonesoftware.com/blog/2009/08/linked-containers-in-flex-4-using-text-layout-framework/</link>
		<comments>http://ayonesoftware.com/blog/2009/08/linked-containers-in-flex-4-using-text-layout-framework/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 06:39:46 +0000</pubDate>
		<dc:creator>Adrian Aioanei</dc:creator>
				<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://ayonesoftware.com/blog/?p=101</guid>
		<description><![CDATA[Using the latest capabilities of Flash Player 10 in terms of text engine, Text Layout Framework offers much more extensible and feature-rich text layouts. Here is a list of capabilities for the new text framework:
- Bidirectional text, vertical text and over 30 writing scripts including Arabic, Hebrew, Chinese, Japanese, Korean, Thai, Lao, Vietnamese, and others.
- [...]]]></description>
			<content:encoded><![CDATA[<p>Using the latest capabilities of <a href="http://www.adobe.com/products/flashplayer/">Flash Player 10</a> in terms of text engine, <a href="http://opensource.adobe.com/wiki/display/tlf/Text+Layout+Framework">Text Layout Framework</a> offers much more extensible and feature-rich text layouts. Here is a list of capabilities for the new text framework:</p>
<p>- Bidirectional text, vertical text and over 30 writing scripts including Arabic, Hebrew, Chinese, Japanese, Korean, Thai, Lao, Vietnamese, and others.<br />
- Selection, editing and flowing text across multiple columns and linked containers.<br />
- Vertical text, Tate-Chu-Yoko (horizontal within vertical text) and justifier for East Asian typography.<br />
- Rich typographical controls, including kerning, ligatures, typographic case, digit case, digit width and discretionary hyphens.<br />
- Cut, copy, paste, undo and standard keyboard and mouse gestures for editing<br />
- Rich developer APIs to manipulate text content, layout, markup and create custom text components.<br />
- ActionScript-based object-oriented model for rich text layout enabling live updates.</p>
<p>What we&#8217;ll touch in this post is linked containers. What is this all about is that you can have a flow of text (TextFlow) displayed on two or more separate containers and manage them using controllers and composers. From the last sentence we can clearly distinct the three main part of the framework, following MVC approach:</p>
<p>- Model would be the TextFlow along with all it&#8217;s elements.<br />
- View would be the container.<br />
- Controller would be the controllers and everything that relates to user interaction on the text.</p>
<p>Starting our demo we want to have 2 containers displayed that will share the same text flow. Sizes for containers will be changed when resizing the application to force the text to span across one or both of them, depending on the size. For the sake of simplicity we will build up a SparkSkin and have everything declared in it.<br />
First we need the TextFlow, an xml file actually that declares the TextFlow. This will be converted into actual text layout format.</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="sy0">&lt;</span>fx:Declarations<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>fx:<span class="kw3">XML</span> id=<span class="st0">&quot;xml&quot;</span><span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>TextFlow xmlns=<span class="st0">&quot;http://ns.adobe.com/textLayout/2008&quot;</span> fontSize=<span class="st0">&quot;14&quot;</span> fontFamily=<span class="st0">&quot;Verdana&quot;</span><span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>p<span class="sy0">&gt;</span>The <span class="kw3">Text</span> Layout Framework is an extensible ActionScript library, built <span class="kw3">on</span> the <span class="kw2">new</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">text</span> engine <span class="kw1">in</span> Adobe® Flash® Player <span class="nu0">10</span> <span class="kw3">and</span> Adobe AIR <span class="nu0">1.5</span>, which delivers advanced,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;easy-to-integrate typographic <span class="kw3">and</span> <span class="kw3">text</span> layout features <span class="kw1">for</span> rich, sophisticated <span class="kw3">and</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;innovative typography <span class="kw3">on</span> the web. <span class="me1">The</span> framework is designed to be used <span class="kw3">with</span> Adobe <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Flex® or Adobe Flash Professional <span class="kw3">and</span> is included <span class="kw1">in</span> Flex <span class="nu0">4</span>, code named <span class="st0">&quot;Gumbo&quot;</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="me1">Developers</span> can use or extend existing components, or use the framework to create <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;their own <span class="kw3">text</span> components.<span class="sy0">&lt;/</span>p<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;/</span>TextFlow<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;/</span>fx:XML<span class="sy0">&gt;</span><br />
<span class="sy0">&lt;/</span>fx:Declarations<span class="sy0">&gt;</span></div></td></tr></tbody></table></div>
<p>Second, we need the containers where the text will be displayed:</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">&lt;s:Group id=&quot;contentGroup&quot; horizontalCenter=&quot;0&quot;&gt;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &lt;mx:UIComponent id=&quot;container1&quot;/&gt;<br />
&nbsp; &nbsp; &lt;mx:UIComponent id=&quot;container2&quot; x=&quot;250&quot;/&gt;<br />
&nbsp; &nbsp; <br />
&lt;/s:Group&gt;</div></td></tr></tbody></table></div>
<p>As declarations, we have the text flow which will result from converting the xml object and two controllers for the containers we just displayed. These controllers will be used to compute how much text to display in each container, depending on all properties of the text flow.</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br /></div></td><td><div class="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw3">private</span> <span class="kw2">var</span> textFlow:TextFlow;<br />
<span class="kw3">private</span> <span class="kw2">var</span> controller1:ContainerController;<br />
<span class="kw3">private</span> <span class="kw2">var</span> controller2:ContainerController;</div></td></tr></tbody></table></div>
<p>At creation complete we just link all components together, converting the text flow, creating controllers for containers and add them to the text flow and update them.</p>
<div class="codecolorer-container text " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">protected function creationCompleteHandler( event:FlexEvent ):void<br />
{<br />
&nbsp; &nbsp; textFlow = TextConverter.importToFlow( xml, TextConverter.TEXT_LAYOUT_FORMAT );<br />
&nbsp; &nbsp; controller1 = new ContainerController( container1 );<br />
&nbsp; &nbsp; controller2 = new ContainerController( container2 );<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; textFlow.flowComposer.addController( controller1 );<br />
&nbsp; &nbsp; textFlow.flowComposer.addController( controller2 );<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; updateControllers();<br />
}</div></td></tr></tbody></table></div>
<p>Updating controllers method is separate because we will also call it when resizing the application:</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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 /></div></td><td><div class="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace">protected <span class="kw2">function</span> resizeHandler<span class="br0">&#40;</span> event:ResizeEvent <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> initialized <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; updateControllers<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<br />
<span class="kw3">private</span> <span class="kw2">function</span> updateControllers<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; controller1.<span class="me1">setCompositionSize</span><span class="br0">&#40;</span> <span class="nu0">200</span>, <span class="kw3">height</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; controller2.<span class="me1">setCompositionSize</span><span class="br0">&#40;</span> <span class="nu0">200</span>, <span class="kw3">height</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; textFlow.<span class="me1">flowComposer</span>.<span class="me1">updateAllControllers</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>What&#8217;s happening is that the controllers will adjust the required sizes depending on the height of the application, and while the containers are linked (with flowCompose.addContainer), text will span across both of them in necessary. You can view the example here http://ayonesoftware.com/blog/examples/tlf with right-click for view source.</p>
]]></content:encoded>
			<wfw:commentRss>http://ayonesoftware.com/blog/2009/08/linked-containers-in-flex-4-using-text-layout-framework/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Asynchronous Testing using FlexUnit 4</title>
		<link>http://ayonesoftware.com/blog/2009/08/asynchronous-testing-using-flexunit-4/</link>
		<comments>http://ayonesoftware.com/blog/2009/08/asynchronous-testing-using-flexunit-4/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 15:30:50 +0000</pubDate>
		<dc:creator>Adrian Aioanei</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://ayonesoftware.com/blog/?p=95</guid>
		<description><![CDATA[As you might have known there is a new version of FlexUnit in development on the opensource site at Adobe http://opensource.adobe.com/wiki/display/flexunit/FlexUnit for a few months now. This is a new breed between FlexUnit 1 and Fluint. Before delving into the asynchronous topic i would like to mention some improvements to the framework:
1. You are not [...]]]></description>
			<content:encoded><![CDATA[<p>As you might have known there is a new version of FlexUnit in development on the opensource site at Adobe <a href="http://opensource.adobe.com/wiki/display/flexunit/FlexUnit">http://opensource.adobe.com/wiki/display/flexunit/FlexUnit</a> for a few months now. This is a new breed between FlexUnit 1 and <a href="http://code.google.com/p/fluint/">Fluint</a>. Before delving into the asynchronous topic i would like to mention some improvements to the framework:</p>
<p>1. You are not any more required to explicitly extend TestSuite and TestCase classes. For the suite you just mark it as being a suite by placing [Suite] metadata tag. All test case classes that you want the suite to run should then be added as public properties in this class. The TestCase class does not require having methods that begin with &#8220;test&#8221;. Instead you mark the test methods by [Test] metadata.</p>
<p>2. The runner is externalized, requiring FlexUnit4UIRunner.swc for default runner. You specify the runner which a suite used by placing [RunWith] metadata tag, default runner looking like [RunWith("org.flexunit.runners.Suite")]. This being the case you can always create a custom runner and add new capabilities/functionalities in test runs.</p>
<p>3. Flash Builder 4 has a built in mechanism for testing, allowing one to choose what suites or test cases to run and display results in FlexUnit Results view. I wouldn&#8217;t recommend this approach right now as it has some huge memory issues which eventually will get the builder to a halt.</p>
<p>For a big picture about new capabilities of FlexUnit 4 go to <a href="http://opensource.adobe.com/wiki/display/flexunit/FlexUnit+4+feature+overview">http://opensource.adobe.com/wiki/display/flexunit/FlexUnit+4+feature+overview</a></p>
<p>Now on the asynchronous area, which has been a lot improved since the addition of Fluint. To mark a method as being part of an async process, you just add &#8220;async&#8221; under [Test] metadata [Test(async)]. This will let you access the asynchronous capabilities of the framework. The one that we&#8217;ll exemplify is usage of a RemoteObject call.</p>
<p>First we build the basic test class:</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px;height:300px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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 /></div></td><td><div class="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace">package<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <br />
<span class="kw3">import</span> mx.<span class="me1">rpc</span>.<span class="me1">remoting</span>.<span class="me1">RemoteObject</span>;<br />
<br />
<span class="kw3">public</span> <span class="kw2">class</span> TestRemoteObject<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">// Properties</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp;* Service to be used in testing async calls.<br />
&nbsp; &nbsp; &nbsp;* @private <br />
&nbsp; &nbsp; &nbsp;*/</span> <br />
&nbsp; &nbsp; <span class="kw3">private</span> <span class="kw3">static</span> <span class="kw2">var</span> service:RemoteObject;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">// Methods</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="br0">&#91;</span>BeforeClass<span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw3">static</span> <span class="kw2">function</span> setUp<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service = <span class="kw2">new</span> RemoteObject<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; service.<span class="me1">endpoint</span> = <span class="st0">&quot;yourEndpointHere&quot;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>A simple declaration of the RemoteObject and a [BeforeClass] marked method. What [BeforeClass] states is that the method is called only once before the whole class is executed in the test queue. We just setup the RemoteObject with any valid endpoint. The next and final step of the test case is actually testing a method of the service:</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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 /></div></td><td><div class="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="br0">&#91;</span>Test<span class="br0">&#40;</span>async<span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="kw3">public</span> <span class="kw2">function</span> testMyMethod<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> token:AsyncToken = service.<span class="me1">myMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; token.<span class="me1">addResponder</span><span class="br0">&#40;</span> Async.<span class="me1">asyncResponder</span><span class="br0">&#40;</span> <span class="kw3">this</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> TestResponder<span class="br0">&#40;</span> verifyResult, verifyFault <span class="br0">&#41;</span>, <span class="nu0">2000</span> <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
protected <span class="kw2">function</span> verifyResult<span class="br0">&#40;</span> <span class="kw3">e</span>:ResultEvent, token:AsyncToken <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Assert.<span class="me1">assertTrue</span><span class="br0">&#40;</span> <span class="st0">&quot;Result is what i want it to be&quot;</span>, <span class="kw3">e</span>.<span class="me1">result</span> is ArrayCollection <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
protected <span class="kw2">function</span> verifyFault<span class="br0">&#40;</span> <span class="kw3">e</span>:FaultEvent, token:AsyncToken <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Assert.<span class="me1">fail</span><span class="br0">&#40;</span> <span class="st0">&quot;Unintended fault from service&quot;</span> <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>Taking it from the in out, we define a TestResponder which is nothing else than a simple Responder, only that it adds an extra parameter &#8220;passThroughData&#8221; (more on this in a later post) to the result and fault methods. This responder is then added into the async capabilities of the framework using Async.asyncResponder, registering the test case (this &#8211; first param), responder (which we just built) and the timeout for call failure. On the verifyResult method you can assert everything you want from the result object and under verifyFault, if this fault is not intentional, you can just Assert.fail, telling the framework that the call failed.</p>
<p>Another goodie about the asynchronous testing in FlexUnit 4 is that you&#8217;re no longer needed to chain multiple async methods with events and listeners. You can accomplish this by using &#8220;order&#8221; param under [Test] metadata. So for the first async method tested you would have [Test(async,order=1)], for the second one [Test(async,order=2)] and so on. The framework will route them handy async one after another.</p>
<p>Next step would be to build the suite class, which as described above should look very simple:</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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="font-family:Monaco,Lucida Console,monospace">package<br />
<span class="br0">&#123;</span><br />
<br />
<span class="br0">&#91;</span>Suite<span class="br0">&#93;</span><br />
<span class="br0">&#91;</span>RunWith<span class="br0">&#40;</span><span class="st0">&quot;org.flexunit.runners.Suite&quot;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="kw3">public</span> <span class="kw2">class</span> TestSuite<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">var</span> testRO:TestRemoteObject;<br />
<span class="br0">&#125;</span><br />
<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>And the final step is getting everything to work under an Application. The one that does that is FlexUnitCore, which as the name states is the core component coupling the suite and test cases with the runner:</p>
<div class="codecolorer-container mxml " style="overflow:auto;white-space:nowrap;width:660px;height:300px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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 /></div></td><td><div class="mxml codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="sc3"><span class="re1">&lt;s:Application</span> xmlns:fx=<span class="st0">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span><br />
<span class="sc3">&nbsp; &nbsp; xmlns:s=<span class="st0">&quot;library://ns.adobe.com/flex/spark&quot;</span></span><br />
<span class="sc3">&nbsp; &nbsp; xmlns:flexUnitUIRunner=<span class="st0">&quot;http://www.adobe.com/2009/flexUnitUIRunner&quot;</span></span><br />
<span class="sc3">&nbsp; &nbsp; creationComplete=<span class="st0">&quot;creationCompleteHandler( event )&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;fx:Script</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3">&lt;!<span class="br0">&#91;</span>CDATA<span class="br0">&#91;</span></span><br />
<span class="sc3">&nbsp; &nbsp; </span><br />
<span class="sc3">&nbsp; &nbsp; import mx.events.FlexEvent;</span><br />
<span class="sc3">&nbsp; &nbsp; </span><br />
<span class="sc3">&nbsp; &nbsp; import org.flexunit.runner.FlexUnitCore;</span><br />
<br />
<span class="sc3">&nbsp; &nbsp; protected function creationCompleteHandler<span class="br0">&#40;</span> event:FlexEvent <span class="br0">&#41;</span>:void</span><br />
<span class="sc3">&nbsp; &nbsp; <span class="br0">&#123;</span></span><br />
<span class="sc3">&nbsp; &nbsp; &nbsp; &nbsp; var core:FlexUnitCore = new FlexUnitCore<span class="br0">&#40;</span><span class="br0">&#41;</span>;</span><br />
<span class="sc3">&nbsp; &nbsp; &nbsp; &nbsp; core.addListener<span class="br0">&#40;</span> runner <span class="br0">&#41;</span>;</span><br />
<span class="sc3">&nbsp; &nbsp; &nbsp; &nbsp; </span><br />
<span class="sc3">&nbsp; &nbsp; &nbsp; &nbsp; core.run<span class="br0">&#40;</span> TestSuite <span class="br0">&#41;</span>;</span><br />
<span class="sc3">&nbsp; &nbsp; <span class="br0">&#125;</span></span><br />
<span class="sc3">&nbsp; &nbsp; </span><br />
<span class="sc3">&nbsp; &nbsp; <span class="br0">&#93;</span><span class="br0">&#93;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/fx:Script</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;flexUnitUIRunner:TestRunnerBase</span> id=<span class="st0">&quot;runner&quot;</span> width=<span class="st0">&quot;100%&quot;</span> height=<span class="st0">&quot;100%&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; <br />
<span class="sc3"><span class="re1">&lt;/s:Application</span><span class="re2">&gt;</span></span></div></td></tr></tbody></table></div>
<p>The runner, being externalized, is added into the works using core.addListener, thus receiving events from core regarding the tests. Simply call core.run() on the suite and hope that green checks are everywhere. To view the detailed code for async capabilities in FlexUnit go to <a href="http://opensource.adobe.com/svn/opensource/flexunit/branches/4.x/FlexUnit4/src/org/flexunit/async/">http://opensource.adobe.com/svn/opensource/flexunit/branches/4.x/FlexUnit4/src/org/flexunit/async/</a>.</p>
<p>Download example source code from <a href="http://ayonesoftware.com/blog/wp-content/uploads/2009/08/AsyncTesting.zip">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ayonesoftware.com/blog/2009/08/asynchronous-testing-using-flexunit-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flex 4 Layout Capabilities</title>
		<link>http://ayonesoftware.com/blog/2009/08/flex-4-layout-capabilities/</link>
		<comments>http://ayonesoftware.com/blog/2009/08/flex-4-layout-capabilities/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 14:50:35 +0000</pubDate>
		<dc:creator>Adrian Aioanei</dc:creator>
				<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://ayonesoftware.com/blog/?p=81</guid>
		<description><![CDATA[A major headache in Flex 3 was to slightly change the layout of components. Say you would want to change a List display from vertical to horizontal. No chance. You would need to build up another component, HorizontalList, and have all the logic in there. With the Flex 4 approach to layouts, things are much [...]]]></description>
			<content:encoded><![CDATA[<p>A major headache in Flex 3 was to slightly change the layout of components. Say you would want to change a List display from vertical to horizontal. No chance. You would need to build up another component, <i>HorizontalList</i>, and have all the logic in there. With the Flex 4 approach to layouts, things are much more simple. Having a layout class that takes care of a component&#8217;s children offers support for extensibility. This way there is no difference between a normal vertical list and a horizontal list for example, in terms of data processing/children creations. The only difference is that the components have a different layout attached. This is achieved by specifying the <i>layout</i> property on the component.</p>
<p>The two base components that support layout inside the Flex 4 SDK are:</p>
<p>1. <i>GroupBase</i>, a class for displaying visual elements. Visual elements here means both <i>UIComponent</i> and <i>GraphicElement</i>, elements that can be laid out on the stage.</p>
<p>2. <i>SkinnableDataContainer</i>, a class for displaying visual elements based on data content. This is the base class for all list-based classes on Spark. This is where the journey begins for allowing a <i>List</i> component to display it&#8217;s children in different ways.</p>
<p>As the first component is plain simple, we&#8217;ll do a demo of customizing a DataContainer&#8217;s layout. Let&#8217;s suppose we need a menu in a circle fashion like in the image below.<br />
<img src="http://ayonesoftware.com/blog/wp-content/uploads/2009/08/circle-layout.png" alt="Circle Layout" /><br />
It looks heavy stuff at a first sight, but all we need is a spark <i>List</i> and a <i>CircleLayout</i> class that computes the positions for each element on the list. The layout class takes in functionalities for measurement/sizing/placing elements on the component.</p>
<p>For positioning and sizing the elements on the stage, <i>setLayoutBoundsPosition</i> and <i>setLayoutBoundsSize</i> would be used. In this process, helper methods are called on the elements when needed to get the preferred size/position such as <i>getPreferredBoundsWidth</i>, <i>getPreferredBoundsHeight</i>. All methods are listed in <i>mx.core.ILayoutElement</i> interface.</p>
<p>For the CircleLayout that we want to build, we only need a radius specified that will help to determine the locations of the elements on a circle.</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px;height:300px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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="font-family:Monaco,Lucida Console,monospace"><span class="coMULTI">/**<br />
&nbsp;* @private<br />
&nbsp;*/</span><br />
<span class="kw3">private</span> <span class="kw2">var</span> _radius:<span class="kw3">Number</span>;<br />
<br />
<span class="coMULTI">/**<br />
&nbsp;* Circle radius at which the elements will be laid out.<br />
&nbsp;* @return<br />
&nbsp;*/</span><br />
<span class="kw3">public</span> <span class="kw2">function</span> <span class="kw3">get</span> radius<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Number</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> _radius;<br />
<span class="br0">&#125;</span><br />
<br />
<span class="coMULTI">/**<br />
&nbsp;* @private<br />
&nbsp;* @param value<br />
&nbsp;*/</span><br />
<span class="kw3">public</span> <span class="kw2">function</span> <span class="kw3">set</span> radius<span class="br0">&#40;</span> value:<span class="kw3">Number</span> <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; _radius = value;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">target</span>.<span class="me1">invalidateSize</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw3">target</span>.<span class="me1">invalidateDisplayList</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>The <i>target</i> is the actual component to which the layout is attached. Each time a property is updated that affects the size or display of the component after layout processing, <i>invalidateSize</i> and <i>invalidateDisplayList</i> methods should be called. These are familiar from the Flex 3 invalidation model.<br />
These will translate on calling <i>measure</i> and <i>updateDisplayList</i> on the component which for a ILayoutElement will forward the chain to the layout&#8217;s <i>measure</i> and <i>updateDisplayList</i>.</p>
<p>For our example, <i>measure</i> will set the measured width and height on the component based on the radius and sizes for each children elements. As we are not changing anything on a children&#8217;s size, we let the components size themselves by using <i>getPreferredBoundsWidth</i> and <i>getPreferredBoundsHeight</i>. Here is the measure method in it&#8217;s complete form:</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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 /></div></td><td><div class="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="coMULTI">/**<br />
&nbsp;* @private <br />
&nbsp;*/</span> <br />
override <span class="kw3">public</span> <span class="kw2">function</span> measure<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">super</span>.<span class="me1">measure</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw3">target</span>.<span class="me1">measuredWidth</span> = radius <span class="sy0">*</span> <span class="nu0">2</span>;<br />
&nbsp; &nbsp; <span class="kw3">target</span>.<span class="me1">measuredHeight</span> = radius <span class="sy0">*</span> <span class="nu0">2</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw2">var</span> numElements:<span class="kw3">int</span> = <span class="kw3">target</span>.<span class="me1">numElements</span>;<br />
&nbsp; &nbsp; <span class="kw2">var</span> element:ILayoutElement;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span> <span class="kw2">var</span> i:<span class="kw3">int</span> = <span class="nu0">0</span>; i <span class="sy0">&lt;</span> numElements; i++ <span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; element = <span class="kw3">target</span>.<span class="me1">getElementAt</span><span class="br0">&#40;</span> i <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; element.<span class="me1">setLayoutBoundsSize</span><span class="br0">&#40;</span> element.<span class="me1">getPreferredBoundsWidth</span><span class="br0">&#40;</span><span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;element.<span class="me1">getPreferredBoundsHeight</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>Now we actually need to place the elements at their specific positions inside <i>updateDisplayList</i> method.</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px;height:300px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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 /></div></td><td><div class="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="coMULTI">/**<br />
&nbsp;* @private <br />
&nbsp;*/</span> <br />
override <span class="kw3">public</span> <span class="kw2">function</span> updateDisplayList<span class="br0">&#40;</span> <span class="kw3">width</span>:<span class="kw3">Number</span>, <span class="kw3">height</span>:<span class="kw3">Number</span> <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">super</span>.<span class="me1">updateDisplayList</span><span class="br0">&#40;</span> <span class="kw3">width</span>, <span class="kw3">height</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw2">var</span> stepAngle:<span class="kw3">Number</span> = <span class="nu0">360</span> <span class="sy0">/</span> getNumLayoutElements<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw2">var</span> angle:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; <span class="kw2">var</span> numElements:uint = <span class="kw3">target</span>.<span class="me1">numElements</span>;<br />
&nbsp; &nbsp; <span class="kw2">var</span> element:ILayoutElement;<br />
&nbsp; &nbsp; <span class="kw2">var</span> x:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; <span class="kw2">var</span> y:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; <span class="kw2">var</span> boundsWidth:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; <span class="kw2">var</span> boundsHeight:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span> <span class="kw2">var</span> i:<span class="kw3">int</span> = <span class="nu0">0</span>; i <span class="sy0">&lt;</span> numElements; i++ <span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; element = <span class="kw3">target</span>.<span class="me1">getElementAt</span><span class="br0">&#40;</span> i <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> <span class="sy0">!</span>element.<span class="me1">includeInLayout</span> <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">continue</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; DisplayObject<span class="br0">&#40;</span> element <span class="br0">&#41;</span>.<span class="me1">rotation</span> = stepAngle <span class="sy0">*</span> i;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; boundsWidth = element.<span class="me1">getPreferredBoundsWidth</span><span class="br0">&#40;</span> <span class="kw2">true</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; boundsHeight = element.<span class="me1">getPreferredBoundsHeight</span><span class="br0">&#40;</span> <span class="kw2">true</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; angle = <span class="kw3">Math</span>.<span class="kw3">PI</span> - stepAngle <span class="sy0">*</span> i <span class="sy0">*</span> <span class="kw3">Math</span>.<span class="kw3">PI</span> <span class="sy0">/</span> <span class="nu0">180</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; x = radius + <span class="kw3">Math</span>.<span class="kw3">sin</span><span class="br0">&#40;</span> <span class="kw3">Math</span>.<span class="kw3">PI</span> - angle <span class="br0">&#41;</span> <span class="sy0">*</span> radius - boundsWidth <span class="sy0">/</span> <span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; y = radius + <span class="kw3">Math</span>.<span class="kw3">sin</span><span class="br0">&#40;</span> <span class="kw3">Math</span>.<span class="kw3">PI</span> <span class="sy0">/</span> <span class="nu0">2</span> - angle <span class="br0">&#41;</span> <span class="sy0">*</span> radius - boundsHeight <span class="sy0">/</span> <span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; element.<span class="me1">setLayoutBoundsPosition</span><span class="br0">&#40;</span> x, y <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>Basically what it does it calculates the angles, rotations and positions for each element (if included in layout). One goodie to mention here is the <i>getPreferredBoundsWidth</i> and <i> getPreferredBoundsHeight</i> parameter. This being true, the method will return the value post layout transform. In our case, after the element has been rotated. Other than that it&#8217;s pure math.</p>
<p>In a previous post i detailed how the states work in a spark component. Using that knowledge we will have two states for our demo application, one normal state and one selected state. For the selected state, the circle radius will be a little bigger and we&#8217;ll display something in the middle.</p>
<p>The list component looks like this:</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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="font-family:Monaco,Lucida Console,monospace"><span class="sy0">&lt;</span>s:<span class="kw3">List</span> id=<span class="st0">&quot;list&quot;</span> horizontalCenter=<span class="st0">&quot;0&quot;</span> verticalCenter=<span class="st0">&quot;0&quot;</span><br />
&nbsp; &nbsp; selectionChanging=<span class="st0">&quot;listSelectionChangingHandler()&quot;</span><br />
&nbsp; &nbsp; skinClass=<span class="st0">&quot;com.ayone.examples.circleMenu.ui.skins.ListSkin&quot;</span><br />
&nbsp; &nbsp; clipAndEnableScrolling=<span class="st0">&quot;false&quot;</span> labelField=<span class="st0">&quot;name&quot;</span><span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s:layout<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>layouts:CircleLayout id=<span class="st0">&quot;circleLayout&quot;</span> radius.<span class="me1">normal</span>=<span class="st0">&quot;57&quot;</span> radius.<span class="me1">selected</span>=<span class="st0">&quot;200&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;/</span>s:layout<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s:ArrayCollection<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>fx:<span class="kw3">Object</span> <span class="kw3">name</span>=<span class="st0">&quot;Menu1&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>fx:<span class="kw3">Object</span> <span class="kw3">name</span>=<span class="st0">&quot;Menu2&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>fx:<span class="kw3">Object</span> <span class="kw3">name</span>=<span class="st0">&quot;Menu3&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>fx:<span class="kw3">Object</span> <span class="kw3">name</span>=<span class="st0">&quot;Menu4&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>fx:<span class="kw3">Object</span> <span class="kw3">name</span>=<span class="st0">&quot;Menu5&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;/</span>s:ArrayCollection<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <br />
<span class="sy0">&lt;/</span>s:List<span class="sy0">&gt;</span></div></td></tr></tbody></table></div>
<p>Note the radius property on the layout is set based on the current state, normal or selected. This along with the invalidation mechanism will assure that the component will have its elements updated each time the state is changed. Using a custom skin to display the ellipse on the list and all is ready:</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px;height:300px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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 /></div></td><td><div class="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="sy0">&lt;</span>s:SparkSkin xmlns:fx=<span class="st0">&quot;http://ns.adobe.com/mxml/2009&quot;</span> xmlns:s=<span class="st0">&quot;library://ns.adobe.com/flex/spark&quot;</span><br />
&nbsp; &nbsp; &nbsp; minWidth=<span class="st0">&quot;112&quot;</span> minHeight=<span class="st0">&quot;112&quot;</span><br />
&nbsp; &nbsp; &nbsp; alpha.<span class="me1">disabled</span>=<span class="st0">&quot;0.5&quot;</span><span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s:states<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>s:State <span class="kw3">name</span>=<span class="st0">&quot;normal&quot;</span> <span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>s:State <span class="kw3">name</span>=<span class="st0">&quot;disabled&quot;</span> <span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;/</span>s:states<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s:Ellipse top=<span class="st0">&quot;1&quot;</span> bottom=<span class="st0">&quot;1&quot;</span> <span class="kw3">left</span>=<span class="st0">&quot;1&quot;</span> <span class="kw3">right</span>=<span class="st0">&quot;1&quot;</span><span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>s:stroke<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>s:SolidColorStroke <span class="kw3">color</span>=<span class="st0">&quot;0x000000&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;/</span>s:stroke<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;/</span>s:Ellipse<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s:Scroller <span class="kw3">left</span>=<span class="st0">&quot;1&quot;</span> top=<span class="st0">&quot;1&quot;</span> <span class="kw3">right</span>=<span class="st0">&quot;1&quot;</span> bottom=<span class="st0">&quot;1&quot;</span> id=<span class="st0">&quot;scroller&quot;</span> <span class="kw3">focusEnabled</span>=<span class="st0">&quot;false&quot;</span><span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>s:DataGroup id=<span class="st0">&quot;dataGroup&quot;</span> itemRenderer=<span class="st0">&quot;spark.skins.default.DefaultItemRenderer&quot;</span><span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>s:layout<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>s:VerticalLayout gap=<span class="st0">&quot;0&quot;</span> horizontalAlign=<span class="st0">&quot;contentJustify&quot;</span> <span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;/</span>s:layout<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;/</span>s:DataGroup<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;/</span>s:Scroller<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <br />
<span class="sy0">&lt;/</span>s:SparkSkin<span class="sy0">&gt;</span></div></td></tr></tbody></table></div>
<p>And to add a little flavor to it, we&#8217;ll animate the transition between states with a MotionPath that basically updates the target property over a period of time.</p>
<div class="codecolorer-container actionscript " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="actionscript codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="sy0">&lt;</span>s:transitions<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s:Transition<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>s:Animate <span class="kw3">target</span>=<span class="st0">&quot;{ circleLayout }&quot;</span> <span class="kw3">duration</span>=<span class="st0">&quot;200&quot;</span> effectEnd=<span class="st0">&quot;effectEndHandler(event)&quot;</span><span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>s:SimpleMotionPath property=<span class="st0">&quot;radius&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;/</span>s:Animate<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;/</span>s:Transition<span class="sy0">&gt;</span><br />
<span class="sy0">&lt;/</span>s:transitions<span class="sy0">&gt;</span></div></td></tr></tbody></table></div>
<p>You can view the running application <a href="http://ayonesoftware.com/blog/examples/circlelayout">here</a> and view the source <a href="http://ayonesoftware.com/blog/examples/circlelayout/source/">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ayonesoftware.com/blog/2009/08/flex-4-layout-capabilities/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Architecting a Flex 4 component</title>
		<link>http://ayonesoftware.com/blog/2009/06/architecting-a-flex-4-component/</link>
		<comments>http://ayonesoftware.com/blog/2009/06/architecting-a-flex-4-component/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 10:03:22 +0000</pubDate>
		<dc:creator>Adrian Aioanei</dc:creator>
				<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://ayonesoftware.com/blog/?p=24</guid>
		<description><![CDATA[One of the biggest improvement in the new version of Flex SDK is the methodology by which you design and implement a component. If in Flex 3 you would probably mix skin mxml code with actionscript logic code, a Flex 4 component has 2 main parts: skin and model/controller. Each of these represent a different [...]]]></description>
			<content:encoded><![CDATA[<p>One of the biggest improvement in the new version of Flex SDK is the methodology by which you design and implement a component. If in Flex 3 you would probably mix skin mxml code with actionscript logic code, a Flex 4 component has 2 main parts: skin and model/controller. Each of these represent a different object managed by the component itself. The skin part contains mostly the mxml pieces defining the visual elements of the component while the model component handles all the logics and controlling. At the core of a component are the states, which were in Flex 3 also but are way better handled and improved in 4. When starting designing a component, it&#8217;s states need to be ruled out and logic that handles them. This is achieved on the actionscript side of code, declaring variables by which states are popped into the stage. Lets get this on one example. Suppose we need a contact form for customers to get feedback. Thinking of it&#8217;s functionality it&#8217;s clearly that it needs 3 states: default state where initial text is displayed, input state where client has controls for inserting code and final state where thank you and such message is displayed.<br />
Let&#8217;s call this component ContactForm, the class definition would then look like this:</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="br0">&#91;</span>SkinState<span class="br0">&#40;</span> <span class="st0">&quot;normal&quot;</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="br0">&#91;</span>SkinState<span class="br0">&#40;</span> <span class="st0">&quot;input&quot;</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="br0">&#91;</span>SkinState<span class="br0">&#40;</span> <span class="st0">&quot;final&quot;</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<br />
<span class="kw1">public</span> <span class="kw4">class</span> ContactForm extends SkinnableComponent</div></td></tr></tbody></table></div>
<p>SkinState metadata specifies what states the skinnable component has, linking them with the states declared in the skin class that looks like this:</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="sy0">&lt;</span>s<span class="sy0">:</span>states<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>mx<span class="sy0">:</span>State <span class="kw7">name</span>=<span class="st0">&quot;normal&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>mx<span class="sy0">:</span>State <span class="kw7">name</span>=<span class="st0">&quot;input&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>mx<span class="sy0">:</span>State <span class="kw7">name</span>=<span class="st0">&quot;final&quot;</span><span class="sy0">/&gt;</span><br />
<span class="sy0">&lt;/</span>s<span class="sy0">:</span>states<span class="sy0">&gt;</span></div></td></tr></tbody></table></div>
<p>The next thing would be to declare in the model class all the logic-enabled controls. Logic enabled controls refer to controls that play a role in a component&#8217;s lifecycle, such as buttons that trigger state change, text control that offer text for submitting and so on. So lets go ahead and add the skin parts to our component:</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="br0">&#91;</span>SkinPart<span class="br0">&#40;</span> required=<span class="st0">&quot;true&quot;</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="kw1">public</span> <span class="kw2">var</span> openInputButton<span class="sy0">:</span>Button;<br />
&nbsp; &nbsp; <br />
<span class="br0">&#91;</span>SkinPart<span class="br0">&#40;</span> required=<span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="kw1">public</span> <span class="kw2">var</span> inputTextArea<span class="sy0">:</span>TextArea;<br />
&nbsp; &nbsp; <br />
<span class="br0">&#91;</span>SkinPart<span class="br0">&#40;</span> required=<span class="st0">&quot;true&quot;</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="kw1">public</span> <span class="kw2">var</span> submitButton<span class="sy0">:</span>Button;</div></td></tr></tbody></table></div>
<p>Ok. Once we have all the visual elements defined it&#8217;s time to work on the logic part of it. Adding boolean properties for ruling out in what state the component should be is a good practice. For the contact form demo component we have only 2, isInput which should tell the component that currentSkinState should be input, and isFinal which reflects the final state where thank you message is displayed. So two new lines to the class:</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">private</span> <span class="kw2">var</span> isInput<span class="sy0">:</span><span class="kw5">Boolean</span>;<br />
<span class="kw1">private</span> <span class="kw2">var</span> isFinal<span class="sy0">:</span><span class="kw5">Boolean</span>;</div></td></tr></tbody></table></div>
<p>The main skinning method that is overridden is &#8220;getCurrentSkinState&#8221; which gets called when skin state is invalidated thru &#8220;invalidateSkinState&#8221;. Basically this method returns the current skin based on the logic properties. Pretty straightforward:</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace">override <span class="kw1">protected</span> <span class="kw3">function</span> getCurrentSkinState<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">:</span><span class="kw5">String</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> isFinal <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&quot;final&quot;</span>;<br />
&nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span> isInput <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&quot;input&quot;</span>;<br />
&nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">return</span> <span class="kw1">super</span>.getCurrentSkinState<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>Now skin parts need a little handling in terms of adding/removing event listeners and other data related computations. These are very handy when designing the component for memory optimization as you can remove event listeners and decouple the component from data area of an application (more on this <a href="http://ayonesoftware.com/blog/2009/06/flex-4-memory-optimizations/">here</a>). Here they are:</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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 /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace">override <span class="kw1">protected</span> <span class="kw3">function</span> partAdded<span class="br0">&#40;</span> partName<span class="sy0">:</span><span class="kw5">String</span>, instance<span class="sy0">:</span><span class="kw5">Object</span> <span class="br0">&#41;</span><span class="sy0">:</span><span class="kw1">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">super</span>.partAdded<span class="br0">&#40;</span> partName, instance <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> instance == openInputButton <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; openInputButton.<span class="kw7">addEventListener</span><span class="br0">&#40;</span> <span class="kw5">MouseEvent</span>.<span class="kw8">CLICK</span>, openInputButtonClickHandler <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span> instance == submitButton <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; submitButton.<span class="kw7">addEventListener</span><span class="br0">&#40;</span> <span class="kw5">MouseEvent</span>.<span class="kw8">CLICK</span>, submitButtonClickHandler <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
override <span class="kw1">protected</span> <span class="kw3">function</span> partRemoved<span class="br0">&#40;</span> partName<span class="sy0">:</span><span class="kw5">String</span>, instance<span class="sy0">:</span><span class="kw5">Object</span> <span class="br0">&#41;</span><span class="sy0">:</span><span class="kw1">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">super</span>.partRemoved<span class="br0">&#40;</span> partName, instance <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> instance == openInputButton <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; openInputButton.<span class="kw7">removeEventListener</span><span class="br0">&#40;</span> <span class="kw5">MouseEvent</span>.<span class="kw8">CLICK</span>, openInputButtonClickHandler <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>And here are the event handlers that rule out the transitions between component states:</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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 /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">private</span> <span class="kw3">function</span> openInputButtonClickHandler<span class="br0">&#40;</span> e<span class="sy0">:</span><span class="kw5">MouseEvent</span> <span class="br0">&#41;</span><span class="sy0">:</span><span class="kw1">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; isInput = <span class="kw1">true</span>;<br />
&nbsp; &nbsp; invalidateSkinState<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
<span class="kw1">private</span> <span class="kw3">function</span> submitButtonClickHandler<span class="br0">&#40;</span> e<span class="sy0">:</span><span class="kw5">MouseEvent</span> <span class="br0">&#41;</span><span class="sy0">:</span><span class="kw1">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; isInput = <span class="kw1">false</span>;<br />
&nbsp; &nbsp; isFinal = <span class="kw1">true</span>;<br />
&nbsp; &nbsp; invalidateSkinState<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>What happens under the hood is that when &#8220;invalidateSkinState()&#8221; gets called, framework will eventually call &#8220;getCurrentSkinState&#8221; and based on the state received it invalidates the skin and update based on that value.<br />
These is pretty much it on the model class. It should look like this:</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px;height:300px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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 /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw4">package</span> com.ayone.examples.contactForm.ui<br />
<span class="br0">&#123;</span><br />
<br />
<span class="kw1">import</span> <span class="kw6">flash.events</span>.<span class="kw5">MouseEvent</span>;<br />
<br />
<span class="kw1">import</span> spark.components.Button;<br />
<span class="kw1">import</span> spark.components.TextArea;<br />
<span class="kw1">import</span> spark.components.supportClasses.SkinnableComponent;<br />
<br />
<span class="br0">&#91;</span>SkinState<span class="br0">&#40;</span> <span class="st0">&quot;normal&quot;</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<br />
<span class="br0">&#91;</span>SkinState<span class="br0">&#40;</span> <span class="st0">&quot;input&quot;</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<br />
<span class="br0">&#91;</span>SkinState<span class="br0">&#40;</span> <span class="st0">&quot;final&quot;</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<br />
<span class="kw1">public</span> <span class="kw4">class</span> ContactForm extends SkinnableComponent<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">// Properties</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//------------------------------</span><br />
&nbsp; &nbsp; <span class="co1">// Skin Parts</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="br0">&#91;</span>SkinPart<span class="br0">&#40;</span> required=<span class="st0">&quot;true&quot;</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw2">var</span> openInputButton<span class="sy0">:</span>Button;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="br0">&#91;</span>SkinPart<span class="br0">&#40;</span> required=<span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw2">var</span> inputTextArea<span class="sy0">:</span>TextArea;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="br0">&#91;</span>SkinPart<span class="br0">&#40;</span> required=<span class="st0">&quot;true&quot;</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw2">var</span> submitButton<span class="sy0">:</span>Button;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//------------------------------</span><br />
&nbsp; &nbsp; <span class="co1">// State Related Logic</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw2">var</span> isInput<span class="sy0">:</span><span class="kw5">Boolean</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw2">var</span> isFinal<span class="sy0">:</span><span class="kw5">Boolean</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">// Methods</span><br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <span class="co1">// Skinning</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; override <span class="kw1">protected</span> <span class="kw3">function</span> getCurrentSkinState<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">:</span><span class="kw5">String</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> isFinal <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&quot;final&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span> isInput <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&quot;input&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">return</span> <span class="kw1">super</span>.getCurrentSkinState<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; override <span class="kw1">protected</span> <span class="kw3">function</span> partAdded<span class="br0">&#40;</span> partName<span class="sy0">:</span><span class="kw5">String</span>, instance<span class="sy0">:</span><span class="kw5">Object</span> <span class="br0">&#41;</span><span class="sy0">:</span><span class="kw1">void</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">super</span>.partAdded<span class="br0">&#40;</span> partName, instance <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> instance == openInputButton <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openInputButton.<span class="kw7">addEventListener</span><span class="br0">&#40;</span> <span class="kw5">MouseEvent</span>.<span class="kw8">CLICK</span>, openInputButtonClickHandler <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span><span class="br0">&#40;</span> instance == submitButton <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; submitButton.<span class="kw7">addEventListener</span><span class="br0">&#40;</span> <span class="kw5">MouseEvent</span>.<span class="kw8">CLICK</span>, submitButtonClickHandler <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; override <span class="kw1">protected</span> <span class="kw3">function</span> partRemoved<span class="br0">&#40;</span> partName<span class="sy0">:</span><span class="kw5">String</span>, instance<span class="sy0">:</span><span class="kw5">Object</span> <span class="br0">&#41;</span><span class="sy0">:</span><span class="kw1">void</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">super</span>.partRemoved<span class="br0">&#40;</span> partName, instance <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> instance == openInputButton <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; openInputButton.<span class="kw7">removeEventListener</span><span class="br0">&#40;</span> <span class="kw5">MouseEvent</span>.<span class="kw8">CLICK</span>, openInputButtonClickHandler <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <span class="co1">// Event Handling</span><br />
&nbsp; &nbsp; <span class="co1">//------------------------------------------------------------</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw3">function</span> openInputButtonClickHandler<span class="br0">&#40;</span> e<span class="sy0">:</span><span class="kw5">MouseEvent</span> <span class="br0">&#41;</span><span class="sy0">:</span><span class="kw1">void</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; isInput = <span class="kw1">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; invalidateSkinState<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw3">function</span> submitButtonClickHandler<span class="br0">&#40;</span> e<span class="sy0">:</span><span class="kw5">MouseEvent</span> <span class="br0">&#41;</span><span class="sy0">:</span><span class="kw1">void</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; isInput = <span class="kw1">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; isFinal = <span class="kw1">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; invalidateSkinState<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<br />
<span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>But wait there&#8217;s more. We still didn&#8217;t rule out the skin class. First thing to be added is the state declaration which links the states from the main class to skin class:</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="sy0">&lt;</span>s<span class="sy0">:</span>states<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>mx<span class="sy0">:</span>State <span class="kw7">name</span>=<span class="st0">&quot;normal&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>mx<span class="sy0">:</span>State <span class="kw7">name</span>=<span class="st0">&quot;input&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>mx<span class="sy0">:</span>State <span class="kw7">name</span>=<span class="st0">&quot;final&quot;</span><span class="sy0">/&gt;</span><br />
<span class="sy0">&lt;/</span>s<span class="sy0">:</span>states<span class="sy0">&gt;</span></div></td></tr></tbody></table></div>
<p>Nothing new here. The newly addition to the state management in Flex 4 is the state-related properties that enables one to set let&#8217;s say height for a skin depending on what state it is. Something like this:</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="sy0">&lt;</span>s<span class="sy0">:</span>SparkSkin xmlns<span class="sy0">:</span>fx=<span class="st0">&quot;http://ns.adobe.com/mxml/2009&quot;</span> xmlns<span class="sy0">:</span>s=<span class="st0">&quot;library://ns.adobe.com/flex/spark&quot;</span><br />
&nbsp; &nbsp; xmlns<span class="sy0">:</span>mx=<span class="st0">&quot;library://ns.adobe.com/flex/halo&quot;</span><br />
&nbsp; &nbsp; <span class="kw7">width</span>=<span class="st0">&quot;400&quot;</span> <span class="kw7">height</span>=<span class="st0">&quot;30&quot;</span> <span class="kw7">height</span>.input=<span class="st0">&quot;200&quot;</span><span class="sy0">&gt;</span></div></td></tr></tbody></table></div>
<p>Notice the two height declaration that are translated like this: height in &#8220;input&#8221; state should be 200 and 30 in any other state. All we need to do now is declaring the visual elements that forms the skin:</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><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 /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="sy0">&lt;</span>s<span class="sy0">:</span>Group includeIn=<span class="st0">&quot;normal&quot;</span> <span class="kw7">width</span>=<span class="st0">&quot;100%&quot;</span> <span class="kw7">height</span>=<span class="st0">&quot;100%&quot;</span><span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s<span class="sy0">:</span>layout<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>s<span class="sy0">:</span>HorizontalLayout verticalAlign=<span class="st0">&quot;middle&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;/</span>s<span class="sy0">:</span>layout<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s<span class="sy0">:</span>SimpleText <span class="kw7">text</span>=<span class="st0">&quot;Click here to get in touch with us&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s<span class="sy0">:</span>Button label=<span class="st0">&quot;Contact&quot;</span> id=<span class="st0">&quot;openInputButton&quot;</span><span class="sy0">/&gt;</span>&nbsp; &nbsp; <br />
<span class="sy0">&lt;/</span>s<span class="sy0">:</span>Group<span class="sy0">&gt;</span><br />
<br />
<span class="sy0">&lt;</span>s<span class="sy0">:</span>Group includeIn=<span class="st0">&quot;input&quot;</span> <span class="kw7">width</span>=<span class="st0">&quot;100%&quot;</span> <span class="kw7">height</span>=<span class="st0">&quot;100%&quot;</span><span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s<span class="sy0">:</span>layout<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sy0">&lt;</span>s<span class="sy0">:</span>VerticalLayout horizontalAlign=<span class="st0">&quot;right&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;/</span>s<span class="sy0">:</span>layout<span class="sy0">&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s<span class="sy0">:</span>TextArea id=<span class="st0">&quot;inputTextArea&quot;</span> <span class="kw7">width</span>=<span class="st0">&quot;100%&quot;</span><span class="sy0">/&gt;</span><br />
&nbsp; &nbsp; <span class="sy0">&lt;</span>s<span class="sy0">:</span>Button id=<span class="st0">&quot;submitButton&quot;</span> label=<span class="st0">&quot;Send&quot;</span><span class="sy0">/&gt;</span><br />
<span class="sy0">&lt;/</span>s<span class="sy0">:</span>Group<span class="sy0">&gt;</span><br />
<br />
<span class="sy0">&lt;</span>s<span class="sy0">:</span>SimpleText includeIn=<span class="st0">&quot;final&quot;</span> <span class="kw7">text</span>=<span class="st0">&quot;Thank you for contacting us&quot;</span><span class="sy0">/&gt;</span></div></td></tr></tbody></table></div>
<p>Note that actual skin parts declared in main class don&#8217;t have any property related to state management. Instead those are wrapped in Group components and those are state-enabled thru &#8220;includeIn&#8221; property. &#8220;includeIn&#8221; basically states that a visual element should be present only in that specific state (or state group, more on that in a future post). And this is the beauty of it, once the main class has defined the skin parts that it requires for a functional component, you can mix and play with the skin as much as you want.</p>
<p>That would be pretty much it. You can download the source code from <a href="http://ayonesoftware.com/blog/wp-content/uploads/2009/06/ComponentArchitecture.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ayonesoftware.com/blog/2009/06/architecting-a-flex-4-component/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Flex 4 Memory Optimizations</title>
		<link>http://ayonesoftware.com/blog/2009/06/flex-4-memory-optimizations/</link>
		<comments>http://ayonesoftware.com/blog/2009/06/flex-4-memory-optimizations/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 19:18:15 +0000</pubDate>
		<dc:creator>Adrian Aioanei</dc:creator>
				<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://ayonesoftware.com/blog/?p=17</guid>
		<description><![CDATA[One of the most problematic and discussed area in Flex development is garbage collection and memory optimization. Even with new tools added to Flex Builder (now Flash Builder) like Profiling, struggling to obtain a memory clear application is not easy. One of the methods is the undocumented hack with LocalConnection
where you connect two of them [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most problematic and discussed area in Flex development is garbage collection and memory optimization. Even with new tools added to Flex Builder (now Flash Builder) like Profiling, struggling to obtain a memory clear application is not easy. One of the methods is the undocumented hack with LocalConnection<br />
where you connect two of them and do nothing at all after.</p>
<div class="codecolorer-container actionscript3 " style="overflow:auto;white-space:nowrap;width:660px"><table cellspacing="0" cellpadding="0"><tbody><tr><td class="line-numbers"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="actionscript3 codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">try</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> lc1<span class="sy0">:</span><span class="kw5">LocalConnection</span> = <span class="kw1">new</span> <span class="kw5">LocalConnection</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw2">var</span> lc2<span class="sy0">:</span><span class="kw5">LocalConnection</span> = <span class="kw1">new</span> <span class="kw5">LocalConnection</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; lc1.<span class="kw7">connect</span><span class="br0">&#40;</span> <span class="st0">&quot;gcConnection&quot;</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; lc2.<span class="kw7">connect</span><span class="br0">&#40;</span> <span class="st0">&quot;gcConnection&quot;</span> <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">catch</span> <span class="br0">&#40;</span>e<span class="sy0">:</span><span class="kw5">Error</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span></div></td></tr></tbody></table></div>
<p>This will trigger the internal garbage collection and clear everything that is decoupled from application.<br />
But doing just this doesn&#8217;t mean your code will work as expected. The whole architecture needs to be ruled out with GC in mind and here is where the Flex 4 framework came in big help. Offering methods like partAdded() and partRemoved() to skinnable components is a path that assures every listener added to skin parts is removed in partRemoved(). This means if you show/hide some parts without removing the whole component from stage, all you need to do is call partRemoved() and partAdded() on the part you need to garbage collect. Make sure you call theese methods only if the part exists first as the first partAdded() is called from withing the component. This technique applies mostly to DataGroup where itemRenderers are used and bound with data that might have a longer lifespan than the renderer itself. Another critical time where some extra logic for memory should be added is when renderer is removed from stage, aka when &#8220;data&#8221; property is set to null. Here is where you might want to call detachSkin() to make sure all parts are decouples from the external resources and available for garbage collection.</p>
<p>Another handy method relates to images, components that are very problematic when comes to garbage collecting, where there is a new functionality inserted in Flash Player 10. SWFLoader has an unloadAndStop method that takes &#8220;invokeGarbageCollector&#8221; as a param which eventually result in calling &#8220;unloadAndStop&#8221; method available in Flash Player 10.</p>
<p>Working on a data intensive project in Flex 4 i&#8217;ve noticed that the framework better supports memory optimization that it did before so kudos for SDK guys. Will get back here with any interesting idea related to garbage collector in Flex 4 and where is the next critical place where you need to do extra work for it.</p>
]]></content:encoded>
			<wfw:commentRss>http://ayonesoftware.com/blog/2009/06/flex-4-memory-optimizations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
