<?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>Kalen Gibbons &#187; ActionScript</title>
	<atom:link href="http://www.kalengibbons.com/blog/index.php/category/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kalengibbons.com/blog</link>
	<description>The Dead Tree Blog</description>
	<lastBuildDate>Wed, 10 Mar 2010 07:28:09 +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>ActionScript Design Pattern Samples: The Singleton</title>
		<link>http://www.kalengibbons.com/blog/index.php/2010/03/actionscript-design-pattern-samples-the-singleton/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2010/03/actionscript-design-pattern-samples-the-singleton/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 07:28:09 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog/?p=421</guid>
		<description><![CDATA[I&#8217;ve decided to write a short blog series on ActionScript design patterns. This series will NOT contain in-depth discussions about the patterns, when to use them, why to use them, or any of that. Rather, each post will have a working example that demonstrates a pattern in use, and will be accompanied by a brief [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve decided to write a short blog series on ActionScript design patterns.  This series will NOT contain in-depth discussions about the patterns, when to use them, why to use them, or any of that.  Rather, each post will have a working example that demonstrates a pattern in use, and will be accompanied by a brief description of key points.  These working samples may serve as a functional reference for myself or anyone else who may find them useful.  Enjoy.</p>
<h3>The Singleton Pattern</h3>
<p>The Singleton Pattern is a very commonly used pattern in ActionScript; Cairngorm&#8217;s ModelLocator is one of the most popular examples that people may be familiar with.  In short, a Singleton is used when you want to guarantee that only one instance of a class can exist.  </p>
<p>Why would you want this? Well, to avoid conflicts for one.  Imagine you have an application with a shopping cart, and various items can be added to the cart from different sections of your application.  You&#8217;d typically want all a user&#8217;s products in a single shopping cart.  If multiple carts existed each would have their its products, its own total, and it would be difficult (and problematic) to manage them all at checkout.  So how could you ensure that only one shopping cart exists, especially if you have multiple developers working on the different sections of the application?  The answer is to make the shopping cart class a Singleton, which ensures that only a single instance of the Singleton class can exist.</p>
<h3>The Code</h3>
<p>So, how can the Singleton pattern ensure that only one instance of a class exists?  Simple, limit access to the class&#8217; constructor and require the class to instantiate itself.  This is typically done with a private constructor in other languages, but ECMAScript, the current standard that ActionScript 3.0 is based on, does not support private constructors. So instead, we create a class like the following:</p>
<p><script src='http://pastie.org/862699.js'></script></p>
<ol>
<li>
<strong>Prohibit constructor access:</strong> The easiest way to prevent access to a class&#8217; constructor is to require a parameter that only the class itself has access to. In the example, the SingletonEnforcer class is declared in such a way that it cannot be accessed by any class other than <code>ShoppingCart</code>.  Then, by requiring the SingletonEnforcer class to be passed into its constructor, the <code>ShoppingCart</code> class ensures that it cannot be instantiated by anything else; it&#8217;s responsible for it&#8217;s own creation.
</li>
<li>
<strong>Provide a single entry point:</strong> Since Singleton classes cannot be instantiated directly, there needs to be an entry point for external classes to request an instance.  The <code>getInstance()</code> method serves this purpose.  It must be a static method for it to be exposed without requiring an existing instance of the class. This method simply checks to see if an internal instance exists (stored as the <code>instance</code> variable), creates an instance if not, and then returns that single instance.
</li>
</ol>
<h3>The Sample</h3>
<p>And here&#8217;s a working example, you can <a href="/assets/pages/srcview/singletonpattern/" target="_blank">view the source code here</a>.</p>
<div style="text-align:center">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_singletonPattern_2087005201"
			class="flashmovie"
			width="610"
			height="470">
	<param name="movie" value="/assets/flash/singletonPattern.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/assets/flash/singletonPattern.swf"
			name="fm_singletonPattern_2087005201"
			width="610"
			height="470">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
<p>And that&#8217;s it.  Please let me know if you have any questions, comments, or suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2010/03/actionscript-design-pattern-samples-the-singleton/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Learning Parsley, Part 1: Dependency Injection</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/11/learning-parsley-part-1-dependency-injection/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/11/learning-parsley-part-1-dependency-injection/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 04:59:01 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[Parsley]]></category>
		<category><![CDATA[series]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog/?p=373</guid>
		<description><![CDATA[I decided to take a look at SpiceFactory&#8217;s Parsely framework for Flex, and I was pretty impressed. When I started to dig into the framework I discovered that although the framework&#8217;s documentation was very good, there weren&#8217;t many community examples or tutorials for the framework. So I decided to create a short series on getting [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="SpiceFactorys Parsley Framework" src="/assets/images/parsleyCog.gif" title="SpiceFactorys Parsley Framework" width="115" height="100" align="left" />I decided to take a look at <a href="http://www.spicefactory.org/parsley/">SpiceFactory&#8217;s Parsely framework</a> for Flex, and I was pretty impressed.  When I started to dig into the framework I discovered that although the framework&#8217;s documentation was very good, there weren&#8217;t many community examples or tutorials for the framework.  So I decided to create a short series on getting started with Parsley.  The examples in the series will be fairly simple and no advanced topics will be discussed; my aim is to help developers get a jump-start on development, by providing simple examples of working code. </p>
<h3>Defining Object Dependencies</h3>
<p>We&#8217;ll begin by learning how to add dependency injection into an application.  First, we need to recognize our object dependencies.  In the sample application below, the <b>ContactList</b> view requires the <b>ContactManager</b> model object to provide its data.  In Parsley, marking that dependency as an injection point is as simple as adding the <em>[Inject]</em> metadata tag above the property declaration.  </p>
<p>Parsley can also inject properties into constructors and other methods, and you can find more information on that <a href="http://www.spicefactory.org/parsley/docs/2.1/manual/injection.php#intro">here</a>.</p>
<div class="code">
<span class="mxmlSpecialString">&lt;mx:Script&gt;</span></p>
<div class="indent">&lt;![CDATA[</p>
<p>&nbsp;</p>
<div class="indent">
<span class="asReserved">import</span> com.kalengibbons.contactsManager.model.ContactManager;</p>
<p>&nbsp;</p>
<p>[Inject]<br />
[<span class="asReserved">Bindable</span>]<br />
<span class="asReserved">public</span> <span class="asVar">var</span> contactManager:ContactManager; </div>
<p>&nbsp;</p>
<p>]]&gt;</p></div>
<p><span class="mxmlSpecialString">&lt;/mx:Script&gt;</span>
</div>
<h3>Creating the IOC Container</h3>
<p>Next, we need to tell Parsley where to find the <b>ContactManager</b> class before it can inject it into the view.  To do this, we need to add it to the IOC Container, which is responsible for wiring all our dependencies together.  The container can be written as MXML, ActionScript, XML, or a combination of the three.  For more information on using these methods you can view the documentation <a href="http://www.spicefactory.org/parsley/docs/2.1/manual/config.php#intro">here</a>.  For this example, we will look at an MXML configuration.  To do this, simply create an MXML file with mx:Object as the root tag, and add references to the objects that we will need to wire, like so:</p>
<div class="code">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br />
<span class="mxmlComponent">&lt;mx:Object</span> xmlns=&quot;<span class="mxmlString">*</span>&quot; xmlns:mx=&quot;<span class="mxmlString">http://www.adobe.com/2006/mxml</span>&quot;</p>
<div class="indent">
xmlns:model=&quot;<span class="mxmlString">com.kalengibbons.contactsManager.model.*</span>&quot;<span class="mxmlComponent">&gt;</span>
</div>
<p>&nbsp;</p>
<p><span class="mxmlComponent indent">&lt;model:ContactManager /&gt;</span></p>
<p>&nbsp;</p>
<p><span class="mxmlComponent">&lt;/mx:Object&gt;</span>
</div>
<h3>Wiring the View</h3>
<p>The framework won&#8217;t know to look in our <b>ContactList</b> view for injection points unless we tell it to.  To do this, we need to also add the view into the IOC Container.  There are two ways of doing this.  We could explicitly declare it in the container like we did in the previous step for the <b>ContactManager</b>.  But for views, Parsley allows us to dynamically wire them by simply dispatching a single configuration event like so:</p>
<div class="code">
<span class="mxmlComponent">&lt;mx:VBox</span> xmlns:mx=&quot;<span class="mxmlString">http://www.adobe.com/2006/mxml</span>&quot;<br />
<span class="indent">addedToStage=&quot;<span class="asReserved">this</span>.dispatchEvent( <span class="asReserved">new</span> Event(<span class="asString">&#8216;configureView&#8217;</span>, <span class="asReserved">true</span>) );&quot;<span class="mxmlComponent">&gt;</span></span>
</div>
<p>The <em>configureView</em> event tells Parsley that the view should be added to the IOC Container at runtime.</p>
<h3>Initializing the Framework</h3>
<p>Finally, to put it all together, we need to initialize the framework and provide Parsley with the IOC Container we created.  We want to do this as early as possible so doing this on the <em>addedToStage</em> event is common practice.  Simply call the <em>FlexContextBuilder.build()</em> method and provide the IOC Container and the root DisplayObject used for wiring (typically the application root).</p>
<div class="code">
<p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br />
    <span class="mxmlComponent">&lt;mx:Application</span> xmlns:mx=&quot;<span class="mxmlString">http://www.adobe.com/2006/mxml</span>&quot; layout=&quot;<span class="mxmlString">vertical</span>&quot;</p>
<div class="indent">
    xmlns:views=&quot;<span class="mxmlString">com.kalengibbons.contactsManager.views.*</span>&quot;<br />
    addedToStage=&quot;addedToStageHandler()&quot;<span class="mxmlComponent">&gt;</span></p>
<p>&nbsp;</p>
<p> <span class="mxmlSpecialString">&lt;mx:Script&gt;</span></p>
<div class="indent">&lt;![CDATA[</p>
<p>&nbsp;</p>
<div class="indent">
    <span class="asReserved">import</span> com.kalengibbons.contactsManager.config.ContactManagerConfig;<br />
    <span class="asReserved">import</span> org.spicefactory.parsley.flex.FlexContextBuilder;</p>
<p>&nbsp;</p>
<p>    <span class="asReserved">private</span> function addedToStageHandler():<span class="asReserved">void</span>{</p>
<div class="indent">
    <span class="asComment">// configure the IoC container</span><br />
    FlexContextBuilder.build(ContactManagerConfig, <span class="asReserved">this</span>);
    </div>
<p>    }
  </div>
<p>    ]]&gt;
    </p></div>
<p>    <span class="mxmlSpecialString">&lt;/mx:Script&gt;</span></p>
<p>&nbsp;</p>
<p>  <span class="mxmlComponent">&lt;views:ContactList width=&quot;</span><span class="mxmlString">100%</span><span class="mxmlComponent">&quot; height=&quot;</span><span class="mxmlString">100%</span><span class="mxmlComponent">&quot; /&gt;</span></p></div>
<p>&nbsp;</p>
<p>  <span class="mxmlComponent">&lt;/mx:Application&gt;</span></p>
</div>
<p>Here is a working example of the code.  The application is very simple; when you click the button it simply populates the <b>ContactModel</b> object with data, which is bound to the DataGrid.  The key is to notice how the <b>ContactModel</b> has successfully been injected into the view.</p>
<p>You can view the <a href="/assets/pages/srcview/contactsmanager/">source code here</a>.<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_contactsManager_1619622297"
			class="flashmovie"
			width="750"
			height="500">
	<param name="movie" value="/assets/flash/contactsManager.swf" />
	<param name="bgcolor" value="#000000" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/assets/flash/contactsManager.swf"
			name="fm_contactsManager_1619622297"
			width="750"
			height="500">
		<param name="bgcolor" value="#000000" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<h3>Points to Remember</h3>
<ul>
<li>Dependency injection takes time, and your objects might not be immediately available.  In your views, even after the creationComplete event, your objects may still be null.  So what you need to do is use <em>[Init]</em> metadata to designate a function for Parsley to run after injection is complete.  Using this instead of a Flex event will ensure that your objects are all available and ready for use.
</li>
<li>Don&#8217;t forget to add the application root to the IOC Container, if necessary.  Even though you may initialize the framework in the application root, and use it as the view root of the Context, you will still need to dispatch the <em>configureView</em> event if you need dependency injection to take place in the application root.</li>
</ul>
<h3>Conclusion</h3>
<p>If you haven&#8217;t done so already, please read the <a href="http://www.spicefactory.org/parsley/docs/2.1/manual/">Parsley Documentation</a> for more information about dependency injection and the Parsley framework.  And like I stated in the intro, I&#8217;m new to Parsley myself so if anyone would like make corrections or suggest any best practices, please feel free to do so in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/11/learning-parsley-part-1-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Creating a scroll-to list in Flex</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/06/creating-a-scroll-to-list-in-flex/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/06/creating-a-scroll-to-list-in-flex/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 20:31:25 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[AnimatedProperty]]></category>
		<category><![CDATA[easingFunction]]></category>
		<category><![CDATA[verticalScrollPosition]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog/?p=273</guid>
		<description><![CDATA[Here is an example of how you can create a scroll-to list using the AnimateProperty class. In this example I&#8217;m using a repeater instead of a List for simplicity, but for large data sets this would not be recommended. I have a DataGrid on the left hand side that simply lists the release date and [...]]]></description>
			<content:encoded><![CDATA[<p>Here is an example of how you can create a scroll-to list using the <a href="http://livedocs.adobe.com/flex/3/langref/mx/effects/AnimateProperty.html" target="_blank">AnimateProperty</a> class.  In this example I&#8217;m using a repeater instead of a List for simplicity, but for large data sets this would not be recommended.</p>
<p>I have a DataGrid on the left hand side that simply lists the release date and title of several upcoming movie releases.  If you click on a row, the list on the right will scroll to the details for the movie selected.</p>
<p><a href="/assets/pages/srcview/scrollToList/index.html" target="_blank">&#8211; View application source &#8211;</a><br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_scrolltolist_471333059"
			class="flashmovie"
			width="750"
			height="400">
	<param name="movie" value="/assets/flash/scrolltolist.swf" />
	<param name="bgcolor" value="#000000" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/assets/flash/scrolltolist.swf"
			name="fm_scrolltolist_471333059"
			width="750"
			height="400">
		<param name="bgcolor" value="#000000" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>The code is pretty straightforward, but there two main pieces to look at.  The first is how to use the AnimateProperty class to scroll the list to the desired location. The second, is how to allow the last item in the list to scroll all the way to the top.</p>
<h3>Scrolling to an item in the list</h3>
<div class="code">
<span class="asReserved">import</span> mx.effects.easing.Sine;<br />
<span class="asReserved">import</span> mx.effects.AnimateProperty;<br />
<span class="asReserved">import</span> mx.events.ListEvent;<br />
&nbsp;<br />
<span class="asReserved">private</span> <span class="asVar">var</span> scrollAnimation:AnimateProperty = <span class="asReserved">new</span> AnimateProperty();<br />
&nbsp;<br />
<span class="asReserved">private</span> <span class="asFunction">function</span> scrollToMovie(event:ListEvent):y{</p>
<div class="indent"><span class="asVar">var</span> yPosition:int = movieWrapper.getChildAt(event.rowIndex).y;<br />
scrollAnimation.stop();<br />
scrollAnimation.property = &quot;verticalScrollPosition&quot;;<br />
scrollAnimation.easingFunction = Sine.easeOut;<br />
scrollAnimation.duration = 900;<br />
scrollAnimation.toValue = yPosition;<br />
scrollAnimation.play([movieWrapper]);</div>
<p>}</p></div>
<h3>Allowing the last list item to scroll to the top</h3>
<div class="code">
<span class="asReserved">private</span> <span class="asFunction">function</span> addSpaceToBottom():<span class="asReserved">void</span>{</p>
<div class="indent">
<span class="asReserved">if</span>(movieWrapper.numChildren &gt; 2){</p>
<div class="indent">
<span class="asVar">var</span> lastChild:HBox = movieWrapper.getChildAt(movieWrapper.numChildren-2) <span class="asReserved">as</span> HBox;<br />
<span class="asVar">var</span> spacerHeight:int = movieWrapper.height &#8211; lastChild.height;<br />
<span class="asReserved">if</span>(spacerHeight &gt; 0)</p>
<div class="indent">spacerBottom.height = spacerHeight;</div>
</div>
<p>}</p></div>
<p>}
</p></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/06/creating-a-scroll-to-list-in-flex/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Performance testing static methods versus instance methods</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/05/performance-testing-static-methods-versus-instance-methods/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/05/performance-testing-static-methods-versus-instance-methods/#comments</comments>
		<pubDate>Wed, 06 May 2009 04:14:29 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[instance method]]></category>
		<category><![CDATA[performance testing]]></category>
		<category><![CDATA[static method]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog/?p=280</guid>
		<description><![CDATA[I was creating several static methods at work today and I started to wonder about how static methods perform versus instance methods. So I created a quick test, that simply calls a static method 5,000,000 times and an instance method the same number of times. Both methods are identical, they simply return true, but as [...]]]></description>
			<content:encoded><![CDATA[<p>I was creating several static methods at work today and I started to wonder about how static methods perform versus instance methods.  So I created a quick test, that simply calls a static method 5,000,000 times and an instance method the same number of times.  Both methods are identical, they simply return true, but as you can see from the results, the static method executes 20-50% slower than the instance method.</p>
<p>To test for yourself, simply click the &quot;Test Instance Method&quot; button to run the instance method test, and then when it’s done you can click the button again to start the static method test.  Your results for each test are displayed in the DataGrid.  All times are represented in milliseconds.</p>
<div style="text-align: center">
You can <a href="/assets/pages/srcview/MethodTesting/" target="_blank">view the source of this application here</a>.<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_MethodTesting_1902022590"
			class="flashmovie"
			width="575"
			height="250">
	<param name="movie" value="/assets/flash/MethodTesting.swf" />
	<param name="bgcolor" value="#000000" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/assets/flash/MethodTesting.swf"
			name="fm_MethodTesting_1902022590"
			width="575"
			height="250">
		<param name="bgcolor" value="#000000" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/05/performance-testing-static-methods-versus-instance-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Custom Printing with Flex, Part 3: Printing Data</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/04/custom-printing-with-flex-part-3-printing-data/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/04/custom-printing-with-flex-part-3-printing-data/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 06:19:52 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Coldfusion]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[AlivePDF]]></category>
		<category><![CDATA[Custom Printing with Flex]]></category>
		<category><![CDATA[series]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog/?p=254</guid>
		<description><![CDATA[In the first article of this series, I discussed how to prevent users from printing directly from Flash&#8217;s default context menu. In the second article, I wrote about using the AlivePDF library to create PDFs from your application for printing or saving. Today I will be covering how to print application data from Flex. The [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/">first article</a> of this series, I discussed how to prevent users from printing directly from Flash&#8217;s default context menu.  In the <a href="http://www.kalengibbons.com/blog/index.php/2009/03/custom-printing-with-flex-part-2-generating-pdfs-with-alivepdf/">second article</a>, I wrote about using the <a href="http://alivepdf.org/" target="_blank">AlivePDF</a> library to create PDFs from your application for printing or saving. Today I will be covering how to print application data from Flex.</p>
<p>The Flex framework comes with several classes to assist you in printing; however, they don&#8217;t tend to produce the greatest results.  The <a href="http://livedocs.adobe.com/flex/3/langref/mx/printing/PrintDataGrid.html" target="_blank">PrintDataGrid</a> class is probably your best option when it comes to printing data, but whenever I&#8217;ve worked with it in the past I&#8217;ve always ended up disappointed.  Even after formatting, there are always issues; like rows getting cut off and pages breaks in weird places.</p>
<p>There have been talks about AlivePDF offering a Grid class to the library, which may be interesting, but it hasn&#8217;t been released and would probably only handle DataGrid data. So the best option I&#8217;ve found for printing data is to allow a server to generate a PDF, which can then be sent back to Flex and output however you want.</p>
<p>A great benefit of this option is the ability to easily format the print results.  You can use simple HTML and CSS to customize the printout much easier than you could using ActionScript.  You can also add custom styles, letterheads, and footers to fit your needs.</p>
<p>Here is an example, you can <a target="_blank" href="/assets/pages/srcview/PrintDataExample/index.html">view the source here</a>.<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_PrintDataExample_445001152"
			class="flashmovie"
			width="750"
			height="500">
	<param name="movie" value="/assets/flash/PrintDataExample.swf" />
	<param name="bgcolor" value="#000000" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/assets/flash/PrintDataExample.swf"
			name="fm_PrintDataExample_445001152"
			width="750"
			height="500">
		<param name="bgcolor" value="#000000" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>This will work with any server-side script that can generate PDF content.  In this demo I am using a ColdFusion script which simply generates an HTML table and returns it in PDF format.  You can view the ColdFusion code along with the rest of the source code <a target="_blank" href="/assets/pages/srcview/PrintDataExample/index.html">here</a>.</p>
<p>The data from the server is returned to Flex as a ByteArray.  You can take that ByteArray and output it to the user in several different ways:</p>
<h3>Using the Browser</h3>
<p>Similar to the last article in this series, you can send the results to a server-side script to either display the PDF in the browser or prompt the user to open or save the document. This time, however, the AlivePDF library is no longer necessary since the PDF binary has already been created.</p>
<div class="code">
<span class="asReserved">private</span> <span class="asFunction">function</span> generatePDF(pdfBinary:ByteArray, method:String):<span class="asReserved">void</span>{</p>
<div class="indent">
		<span class="asComment">//result comes back as binary, create a new URL request and pass it back to the server</span><br />
        <span class="asVar">var</span> header:URLRequestHeader = <span class="asReserved">new</span> URLRequestHeader(&quot;<span class="asString">Content-type</span>&quot;, &quot;<span class="asString">application/octet-stream</span>&quot;);<br />
        <span class="asVar">var</span> urlString:String = &quot;<span class="asString">http://kalengibbons.com/assets/pages/pdfCreator.cfm</span>&quot;;<br />
        <span class="asReserved">if</span>(method == &quot;<span class="asString">inline</span>&quot;)</p>
<div class="indent">
        urlString += &quot;<span class="asString">?method=inline</span>&quot;;		</div>
<p>        <span class="asReserved">else</span></p>
<div class="indent">
        urlString += &quot;<span class="asString">?method=attachment&amp;name=dataPrintSample.pdf</span>&quot;;		</div>
<p>        <span class="asVar">var</span> sendRequest:URLRequest = <span class="asReserved">new</span> URLRequest(urlString);<br />
        sendRequest.requestHeaders.push(header);<br />
        sendRequest.method = URLRequestMethod.POST;<br />
        sendRequest.data = pdfBinary;<br />
        navigateToURL(sendRequest, &quot;<span class="asString">_blank</span>&quot;);		</div>
<p>        }
</p></div>
<h3>Save locally from Flash</h3>
<p>Thanks to new capabilities in <a href="http://www.adobe.com/products/flashplayer/features/" target="_blank">Flash Player 10</a>, you can also provide users the option to save the PDF directly from Flash.  This eliminates the need for the second server-side script.</p>
<div class="code">
<span class="asReserved">private</span> <span class="asFunction">function</span> savePDF(pdfBinary:ByteArray):<span class="asReserved">void</span>{</p>
<div class="indent">
		<span class="asVar">var</span> fileRef:FileReference = <span class="asReserved">new</span> FileReference();<br />
        fileRef.save(pdfBinary, &quot;<span class="asString">yourPrintout.pdf</span>&quot;);		</div>
<p>        }
</p></div>
<p>If you plan to use the save functionality you&#8217;ll need to set the &#8220;Required Flash Player version&#8221; in your project preferences to 10.0.0 or higher or else Flex Builder will complain.  You have to remember that only users with Flash Player 10 or above will be able to use this functionality, so some type of version validation is a necessity.</p>
<p>Another caveat is that the save method must be triggered by some type of <a href="http://kb.adobe.com/selfservice/viewContent.do?externalId=kb405546" target="_blank">user interaction</a>, that&#8217;s why the sample application prompts for system access before saving.  Although the user has clicked the &#8220;Print Data&#8221; button, that event is lost when the server call is made, so we need another interaction from the user.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/04/custom-printing-with-flex-part-3-printing-data/feed/</wfw:commentRss>
		<slash:comments>38</slash:comments>
		</item>
		<item>
		<title>Custom Printing with Flex, Part 2: Generating PDFs with AlivePDF</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/03/custom-printing-with-flex-part-2-generating-pdfs-with-alivepdf/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/03/custom-printing-with-flex-part-2-generating-pdfs-with-alivepdf/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 08:10:27 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Coldfusion]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[AlivePDF]]></category>
		<category><![CDATA[Custom Printing with Flex]]></category>
		<category><![CDATA[series]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog/?p=191</guid>
		<description><![CDATA[In the first article of this series, Overriding Flash’s Built-In Print Option, I discussed how to prevent users from printing directly from Flash, by replacing the default context menu. I demonstrated how poorly Flash handles printing directly, and now we will start discussing what alternatives are available to developers. In this article I will demonstrate [...]]]></description>
			<content:encoded><![CDATA[<p>In the first article of this series, <a href="http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/">Overriding Flash’s Built-In Print Option</a>, I discussed how to prevent users from printing directly from Flash, by replacing the default context menu.  I demonstrated how poorly Flash handles printing directly, and now we will start discussing what alternatives are available to developers.</p>
<p>In this article I will demonstrate how to print and save any part of your application by utilizing the <a href="http://www.alivepdf.org/" target="_blank">AlivePDF library</a>.  AlivePDF is  an ActionScript 3 open-source PDF library that easily creates PDF documents from your Flex application.  The library is pretty robust and offers much more functionality than I will be able to cover here.  In this article I want to demonstrate how simple it is to dynamically generate PDF documents using ActionScript and AlivePDF.</p>
<p>To give you a better understanding of what can be done with AlivePDF, let&#8217;s revisit one of the printing examples that was used in <a href="http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/">part 1</a> of the series.  The two images below are printouts of the The <a href="http://examples.adobe.com/flex2/inproduct/sdk/flexstore/flexstore.html" target="_blank">Flex Store</a> &#8211; one printed with Flash&#8217;s default print option and the other with AlivePDF.</p>
<table>
<tr>
<td>
<strong>Flex Store printed using Flash&#8217;s default print option</strong><br />
<img src="/assets/images/flexStore_printout.jpg" alt="Flex Store Printed from Flash" />
</td>
<td>
<strong>Flex Store printed using AlivePDF</strong><br />
<img src="/assets/images/flexStore_alivePDF.jpg" alt="Flex Store PDF created with AlivePDF" />
</td>
</tr>
</table>
<p>In addition, here is a sample application for you to play with some of AlivePDF&#8217;s options.  You can <a href="/assets/pages/srcview/AlivePDFExample/index.html" target="_blank">view the source here</a>.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_AlivePDFExample_1614259547"
			class="flashmovie"
			width="750"
			height="500">
	<param name="movie" value="/assets/flash/AlivePDFExample.swf" />
	<param name="bgcolor" value="#000000" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/assets/flash/AlivePDFExample.swf"
			name="fm_AlivePDFExample_1614259547"
			width="750"
			height="500">
		<param name="bgcolor" value="#000000" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>So now that you&#8217;ve had a taste of what AlivePDF can do, lets look at how it works:</p>
<h3>Adding AlivePDF to your project</h3>
<p>First, download the library from <a href="http://www.alivepdf.org/" target="_blank">www.AlivePDF.org</a>, and then add it to your project in one of two ways.  The easiest way is to copy the AlivePDF.swc into your project&#8217;s <em>lib</em> folder.  Flex Builder should automatically detect the swc and add it to the compile settings.  The second approach is to add it manually in Flex Builder, by clicking Project >> Properties >> Flex Build Path >> Library Path >> Add SWC&#8230; and selecting the swc from the file system.</p>
<h3>Displaying PDFs in the Browser</h3>
<p>After the swc has been added to your project you can begin working with the AlivePDF library.  The sample code below could be used to generate a PDF document from any UI component in your application.  We simply turn it into an image (think of it as a screenshot) and publish it to a PDF for the user to save or print.  </p>
<p>The code is fairly simple, we start by creating a new instance of the PDF class and adding a page to it. Then we add our UIComponent to the document as an image.  Finally, we tell AlivePDF how we want to generate the PDF with the save() method.  In this example, the third argument of the method, Download.INLINE, specifies that we want the PDF to be displayed in the browser instead of prompting the user to save (that will be covered in the next section).</p>
<div class="code">
<p><span class="asReserved">import</span> mx.core.UIComponent;</p>
<p>&nbsp;</p>
<p><span class="asReserved">import</span> org.alivepdf.display.Display;<br />
        <span class="asReserved">import</span> org.alivepdf.images.ResizeMode;<br />
        <span class="asReserved">import</span> org.alivepdf.layout.Layout;<br />
        <span class="asReserved">import</span> org.alivepdf.layout.Orientation;<br />
        <span class="asReserved">import</span> org.alivepdf.layout.Size;<br />
        <span class="asReserved">import</span> org.alivepdf.layout.Unit;<br />
        <span class="asReserved">import</span> org.alivepdf.pdf.PDF;<br />
        <span class="asReserved">import</span> org.alivepdf.saving.Download;</p>
<p><span class="asReserved">import</span> org.alivepdf.saving.Method;</p>
<p>&nbsp;</p>
<p>
        <span class="asReserved">private</span> <span class="asFunction">function</span> doPrint(whatToPrint:UIComponent):<span class="asReserved">void</span>{</p>
<div class="indent">
		<span class="asVar">var</span> printPDF:PDF = <span class="asReserved">new</span> PDF( Orientation.LANDSCAPE, Unit.MM, Size.A4 );<br />
        printPDF.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );<br />
        printPDF.addPage();<br />
        printPDF.addImage( whatToPrint, 0, 0, 0, 0, <span class="asString">&#8216;PNG&#8217;</span>, 100, 1, ResizeMode.FIT_TO_PAGE );<br />
        printPDF.save( Method.REMOTE, <span class="asString">&quot;/includes/pdfCreator.cfm&quot;</span>, Download.INLINE, <span class="asString">&quot;test.pdf&quot;</span> );    </div>
<p>    }</p>
</div>
<h3>Prompting Users to Save</h3>
<p>Now that works great and all&#8230; but what if you want to prompt the user to save or open in Adobe Reader?  Fortunately, AlivePDF makes that easy; all you have have to do is change Download.INLINE to Download.ATTACHMENT.</p>
<div class="code">
printPDF.save( Method.REMOTE, <span class="asString">&quot;/includes/pdfCreator.cfm&quot;</span>, Download.ATTACHMENT, <span class="asString">&quot;test.pdf&quot;</span> );
</div>
<h3>Using AlivePDF with ColdFusion</h3>
<p>A PHP script is provided with the download of the AlivePDF library, but if you don&#8217;t like PHP you can use any other server-side language that can generate PDF content.  For those of you who are ColdFusion fans, like myself, here is a basic example of a ColdFusion script that can be used.</p>
<div class="code">
<p><span class="cfComment">&lt;!&ndash;&ndash;&ndash; establish parameters &ndash;&ndash;&ndash;&gt;</span><br />
        <span class="cfTag">&lt;cfparam name=</span><span class="cfString">&quot;URL.method&quot;</span><span class="cfTag"> default=</span><span class="cfString">&quot;&quot;</span><span class="cfTag"> /&gt;<br />
&lt;cfparam name=</span><span class="cfString">&quot;URL.name&quot;</span><span class="cfTag"> default=</span><span class="cfString">&quot;&quot;</span><span class="cfTag"> /&gt;</span></p>
<p>&nbsp;</p>
<p>        <span class="cfComment">&lt;&ndash;&ndash;&ndash; get the content from the http data &ndash;&ndash;&ndash;&gt;</span><br />
        <span class="cfTag">&lt;cfset</span> httpContent = <span class="cfReserved">GetHttpRequestData()</span><span class="cfTag">&gt;</span><br /><br/>
    </p>
<p><span class="cfComment">&lt;!&ndash;&ndash;&ndash; make sure content was passed in &ndash;&ndash;&ndash;&gt;</span><br />
        <span class="cfTag">&lt;cfif</span> <span class="cfReserved">len(</span>httpContent.content<span class="cfReserved">)</span> <span class="cfReserved">gt</span> <span class="cfNumber">0</span><span class="cfTag">&gt;</span></p>
<div class="indent">
			<span class="cfComment">&lt;!&ndash;&ndash;&ndash; write the content to local pdf &ndash;&ndash;&ndash;&gt;</span><br />
<span class="cfTag">&lt;cfheader name=</span><span class="cfString">&quot;Content-Disposition&quot;</span><span class="cfTag"> value=</span><span class="cfString">&quot;#URL.method#; filename=#URL.name#&quot;</span>&nbsp;<span class="cfTag">/&gt;</span><br />
			<span class="cfTag">&lt;cfcontent type=</span><span class="cfString">&quot;application/pdf&quot;</span> <span class="cfTag">variable=</span><span class="cfString">&quot;#httpContent.content#&quot;</span><span class="cfTag">&gt;</span></div>
<p>	    <span class="cfTag">&lt;cfelse&gt;</span></p>
<div class="indent">
		<span class="cfComment">&lt;!&ndash;&ndash;&ndash; redirect to home page &ndash;&ndash;&ndash;&gt;</span><br />
		<span class="cfTag">&lt;cflocation url=</span><span class="cfString">&quot;/&quot;</span><span class="cfTag"> /&gt;</span></div>
<p>	<span class="cfTag">&lt;/cfif&gt;</span> </p>
</div>
<p>AlivePDF has a wide range of capabilities and we&#8217;ve barely scratched the surface here.  I encourage you to take a good look at the library and utilize it to its full potential.</p>
<p>In the next article in this serious we will discuss how to print data from Flex.  See you soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/03/custom-printing-with-flex-part-2-generating-pdfs-with-alivepdf/feed/</wfw:commentRss>
		<slash:comments>67</slash:comments>
		</item>
		<item>
		<title>Custom Printing with Flex, Part 1: Overriding Flash&#8217;s Built-In Print Option</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 21:27:19 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[contextMenu]]></category>
		<category><![CDATA[contextMenuItem]]></category>
		<category><![CDATA[Custom Printing with Flex]]></category>
		<category><![CDATA[series]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog/?p=145</guid>
		<description><![CDATA[If you&#8217;re like me and you&#8217;ve spent any time working with printing in Flex, you&#8217;ve probably realized that Flash&#8217;s built-in printing capabilities leave a lot to be desired. So I&#8217;ve decided to create a short series of articles on how to do custom printing in Flex. This article will be the first in the series [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re like me and you&#8217;ve spent any time working with printing in Flex, you&#8217;ve probably realized that Flash&#8217;s built-in printing capabilities leave a lot to be desired.  So I&#8217;ve decided to create a short series of articles on how to do custom printing in Flex.  This article will be the first in the series and will discuss how to prevent users from printing directly from the context menu.</p>
<div style="text-align: center">
<img alt="Default Flash Context Menu" src="/assets/images/FlashContextMenu.gif" title="Default Flash Context Menu" width="250" height="124" />
</div>
<p>By default, when users right-click on a SWF file, they receive the default context menu, which provides them several options, and one of which is the ability to print.  However, in my experience, using this option for printing returns horrible results.  Below are two examples of this, the first is the <a href=" http://examples.adobe.com/flex2/inproduct/sdk/flexstore/flexstore.html" target="_blank">The Flex Store</a>, a sample application from Adobe, and the second is the <a href="http://mediastore.verizonwireless.com/" target="_blank">Verizon Media Store</a>.  On the left is a screenshot of each and on the right is a capture of the print results that were produced by using the default print option.</p>
<table>
<tr>
<td>
<strong>The Flex Store application:</strong><br />
<img alt="Flex Store Screenshot" src="/assets/images/flexstore_screenshot.jpg" title="Flex Store Screenshot" width="365" height="216" />
</td>
<td>
<strong>Printing the Flex Store:</strong><br />
<img alt="Flex Store Printout" src="/assets/images/flexstore_printout.jpg" title="Flex Store Printout" width="365" height="216" align="center" />
</td>
</tr>
<tr>
<td>
<strong>The Verizon Media Store:</strong><br />
<img alt="Verizon Media Store Screenshot" src="/assets/images/verizonMediaStore_screenshot.jpg" title="Verizone Media Store Screenshot" width="365" height="216" />
</td>
<td>
<strong>Printing the Verizon Media Store:</strong><br />
<img alt="Verizon Media Store Printout" src="/assets/images/verizonMediaStore_printout.jpg" title="Flex Store Printout" width="365" height="216" />
</td>
</tr>
</table>
<p>As you can see, the results were less than satisfactory.  I would consider this unacceptable for my applications.  So, the question arises: <strong>&#8220;How do you prevent users from printing from the context menu?&#8221;</strong></p>
<p>The answer is simple &#8211; override Flash&#8217;s built-in context menu with your own.  This is how you do it:<br/><br/></p>
<div class="code">
<span class="asReserved">private</span> <span class="asFunction">function</span> setContextMenu():<span class="asReserved">void</span>{</p>
<div class="indent">
<span class="asComment">//create the new contextMenu </span><br />
<span class="asVar">var</span> customContextMenu:ContextMenu = <span class="asReserved">new</span> ContextMenu();<br />
<span class="asComment">//disable the default options</span><br />
customContextMenu.hideBuiltInItems();</p>
<p><span class="asComment">//create your own print option, and add it to the new contextMenu</span><br />
<span class="asVar">var</span> printOption:ContextMenuItem = <span class="asReserved">new</span> ContextMenuItem(<span class="asString">&quot;Print Me&quot;</span>);<br />
printOption.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, doPrint);<br />
customContextMenu.customItems.push(printOption);</p>
<p><span class="asComment">//set the newly created contextMenu for this application</span><br />
<span class="asReserved">this</span>.contextMenu = customContextMenu;</div>
<p>}<br />
<br />
<span class="asReserved">private</span> <span class="asFunction">function</span> doPrint(event:ContextMenuEvent):<span class="asReserved">void</span>{<br />
<span class="asComment indent">//now print this application</span><br />
}
</div>
<p>Okay, so let&#8217;s review the code.  The first thing you need to do is to create a new ContextMenu and then call its <strong>hideBuiltInItems</strong> menthod to disable the built-in options (forwardAndBack, loop, play, print, quality, rewind, save, and zoom).  This hides the print option and prevents the user from clicking it.  Next you add our own menu item to replace the default print option.  If you don&#8217;t want to do this, because you are going to provide your own print button on the application, you can skip ahead to the last line of code.</p>
<p>To add our own menu option, just create a new <strong>ContextMenuItem</strong> object and give it a label. Then add an event listener to catch when the user clicks on this option (in this example we have specified <em>doPrint</em> as our listener function; we will be working with that method in the proceeding articles of this series).  Finally, add our new menu item to the customItems array, and set our new menu as the contextMenu for the application.</p>
<p>You can see the results below. Although nothing spectacular has taken place here, we&#8217;ve disabled Flash&#8217;s built-in print capabilities and will be routing all print requests through our own function.</p>
<div style="text-align: center">
<img alt="Default Flash Context Menu" src="/assets/images/CustomContextMenu.gif" title="Custom Context Menu" width="250" height="132" />
</div>
<p><strong>Gotcha:</strong><br/><br />
When creating CustomMenuItems, the labels you provide must be different than those provided by the default items.  For example, you cannot use &#8220;Print&#8221; as your label or the application will ignore the option.  However, you can use variations, such as &#8220;Print This&#8221; or &#8220;Print Page.&#8221;</li>
</p>
<p><br/>In the next article we will discuss how to generate PDF documents for more functional printing and saving.  For more information of the ContextMenu, check out the <a href="http://livedocs.adobe.com/flex/3/langref/flash/ui/ContextMenu.html" target="_blank">Livedocs</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/02/custom-printing-with-flex-part-1-overriding-flashs-built-in-printing/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Filtering a Flex Tree using an ArrayCollection</title>
		<link>http://www.kalengibbons.com/blog/index.php/2009/01/filtering-a-flex-tree-using-an-arraycollection/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2009/01/filtering-a-flex-tree-using-an-arraycollection/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 04:03:45 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[ArrayCollection]]></category>
		<category><![CDATA[Filtering]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[Tree]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog2/?p=89</guid>
		<description><![CDATA[Flex Trees can be difficult to filter because of their hierarchical nature. One possible way to do it is to use an ArraryCollection as your dataProvider, with each node having a &#8220;children&#8221; property that is also an ArrayCollection. The tricky part in filtering the Tree is making sure the children are filtered in addition to [...]]]></description>
			<content:encoded><![CDATA[<p>Flex Trees can be difficult to filter because of their hierarchical nature. One possible way to do it is to use an ArraryCollection as your dataProvider, with each node having a &#8220;children&#8221; property that is also an ArrayCollection.</p>
<p>The tricky part in filtering the Tree is making sure the children are filtered in addition to the root nodes.&nbsp; So, in the example below, I have created a function that will loop through each node and its children recursively. </p>
<p>It is important to note that the filtering takes place from the bottom up, meaning that a node&#8217;s children are always filtered before the node itself.&nbsp; This is important, because normally a filter removes everything that does not match a certain criteria.&nbsp; In our case, to remain in the collection, a node must match a certain criteria OR have children that do.</p>
<div class="code">
        &lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br />
            <span class="mxmlComponent">&lt;mx:Application</span> xmlns:mx=&#8221;<span class="asString">http://www.adobe.com/2006/mxml</span>&#8221;</p>
<div class="indent">layout=&#8221;<span class="asString">absolute</span>&#8220;<span class="mxmlComponent">&gt;</span></div>
<div class="indent">
			<span class="mxmlSpecialString"><br />
			<br />
			&lt;mx:Script&gt;</span></p>
<div class="indent">
    		&lt;![CDATA[<br />
            <span class="asReserved">import</span> vo.Person;<br />
            <span class="asReserved">import</span> mx.collections.ArrayCollection;</p>
<p>            [<span class="asReserved">Bindable</span>]<br />
            <span class="asReserved">private</span> <span class="asVar">var</span> people:ArrayCollection = <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Grandma Susan</span>", <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">John</span>", <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Timmy</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Sammy</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Alan</span>")</div>
<p>			])),<br />
            <span class="asReserved">new</span> Person(&#8220;<span class="asString">Tiffany</span>&#8220;, <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Billy</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Adam</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Graham</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Vennesa</span>")</div>
<p>			])),<br />
            <span class="asReserved">new</span> Person(&#8220;<span class="asString">Michael</span>&#8220;, <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Jannette</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Alan</span>", <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Alice</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Jane</span>")</div>
<p>			]))			</p></div>
<p>			])),<br />
            <span class="asReserved">new</span> Person(&#8220;<span class="asString">Peter</span>&#8220;),</p>
<div class="indent">
			<span class="asReserved">new</span> Person(&#8220;<span class="asString">Cindy</span>&#8220;, <span class="asReserved">new</span> ArrayCollection([</p>
<div class="indent">
			<span class="asReserved">new</span> Person("<span class="asString">Paul</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">David</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Joseph</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Cameron</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Debra</span>"),<br />
            <span class="asReserved">new</span> Person("<span class="asString">Polly</span>")
			</div>
</p></div>
<p>			]))</p></div>
<p>            ])) </p></div>
<p>            ]);</p>
<p>            <span class="asReserved">private</span> <span class="asFunction">function</span> refreshData():<span class="asReserved">void</span>{</p>
<div class="indent">
			<span class="asComment">//reset the root node to its original unfiltered data</span><br />
            people[0].children = <span class="asReserved">new</span> ArrayCollection(people[0].children.source);<br />
            <span class="asComment">//start the recursion at the root node</span><br />
            refreshRecursiveChildren(people.source[0]);<br />
            <span class="asComment">//update the Tree after the data has been filtered</span><br />
            personsTree.invalidateList();</div>
<p>            }</p>
<p>            <span class="asReserved">private</span> <span class="asFunction">function</span> refreshRecursiveChildren(person:Person):<span class="asReserved">void</span>{</p>
<div class="indent">
			<span class="asReserved">if</span>(person.children){</p>
<div class="indent">
			<span class="asComment">//loop through each child and filter its children</span><br />
            <span class="asReserved">for each</span>(<span class="asVar">var</span> _person:Person <span class="asReserved">in</span> person.children.source){</p>
<div class="indent">refreshRecursiveChildren(_person);</div>
<p>            }<br />
            <span class="asComment">//reset each &#8220;children&#8221; ArrayCollection to its original unfiltered data</span><br />
            person.children = <span class="asReserved">new</span> ArrayCollection(person.children.source);<br />
            <span class="asComment">//set the filterfunction for the newly updated node</span><br />
            person.children.filterFunction = filterData;<br />
            <span class="asComment">//run the fitlerFunction</span><br />
            person.children.refresh();			</div>
<p>            }		</p></div>
<p>            }</p>
<p>            <span class="asReserved">public</span> <span class="asFunction">function</span> filterData(item:Object):Boolean{</p>
<div class="indent">
			<span class="asComment">//get the string to filter the nodes by</span><br />
            <span class="asVar">var</span> searchString:String = iNameFilter.text;<br />
            <span class="asComment">//if string is found in node return true.<br />
            //since the recursive filtering takes place from bottom up, if<br />
            //a collection still has children after filtering, also return true</span><br />
            <span class="asReserved">if</span>(searchString.length == 0
<div class="indent">|| item.name.toLowerCase().indexOf(searchString.toLowerCase()) &gt;= 0)<br />
            <span class="asReserved">return true</span>;</div>
<p>            <span class="asReserved">else if</span>(item.children != <span class="asReserved">null</span> &amp;&amp; item.children.length &gt; 0)</p>
<div class="indent"><span class="asReserved">return true</span>;</div>
<p>
            <span class="asReserved">return false</span>;</div>
<p>			}<br />
            ]]&gt;</div>
<p>            <span class="mxmlSpecialString">&lt;/mx:Script&gt;</span></p>
<p>    <span class="mxmlComponent">&lt;mx:VBox</span> width=&#8221;<span class="mxmlString">100%</span>&#8221; height=&#8221;<span class="mxmlString">100%</span>&#8220;</p>
<div class="indent4">
	paddingTop=&#8221;<span class="mxmlString">10</span>&#8220;<br />
	paddingBottom=&#8221;<span class="mxmlString">10</span>&#8220;<br />
    paddingLeft=&#8221;<span class="mxmlString">5</span>&#8220;<br />
    paddingRight=&#8221;<span class="mxmlString">5</span>&#8220;<span class="mxmlComponent">&gt;</span>
    </div>
<div class="indent">
	<span class="mxmlComponent">&lt;mx:Tree</span> id=&#8221;<span class="mxmlString">personsTree</span>&#8220;</p>
<div class="indent4">
	dataProvider=&#8221;<span class="mxmlString">{people}</span>&#8220;<br />
	labelField=&#8221;<span class="mxmlString">name</span>&#8220;<br />
	width=&#8221;<span class="mxmlString">100%</span>&#8220;<br />
	height=&#8221;<span class="mxmlString">100%</span>&#8221; <span class="mxmlComponent">/&gt;</span></div>
<p>	<span class="mxmlComponent">&lt;mx:HBox&gt;</span></p>
<div class="indent">
	<span class="mxmlComponent">&lt;mx:Label</span> text=&#8221;<span class="mxmlString">Filter the Tree:</span>&#8221; <span class="mxmlComponent">/&gt;</span><br />
    <span class="mxmlComponent">&lt;mx:TextInput</span> id=&#8221;<span class="mxmlString">iNameFilter</span>&#8221; change=&#8221;<span class="mxmlString">refreshData()</span>&#8221; <span class="mxmlComponent">/&gt;</span></div>
<p>	<span class="mxmlComponent">&lt;/mx:HBox&gt;</span></div>
<p>    <span class="mxmlComponent">&lt;/mx:VBox&gt;<br />
    </span>
	</div>
<p>        <span class="mxmlComponent">&lt;/mx:Application&gt;</span>
	</div>
<div style="text-align: center;" mce_style="text-align: center">
<p>You may right-click and select &#8220;View Source&#8221; <br />
to view the full source code for the following example.<br />
<iframe id="filteringTreeFrame" src="/assets/pages/treefiltering.html" mce_src="/assets/pages/treefiltering.html" scrolling="no" width="400" frameborder="0" height="560"></iframe>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2009/01/filtering-a-flex-tree-using-an-arraycollection/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
		<item>
		<title>Displaying DataTips when using an itemRenderer</title>
		<link>http://www.kalengibbons.com/blog/index.php/2008/12/displaying-datatips-when-using-an-itemrenderer/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2008/12/displaying-datatips-when-using-an-itemrenderer/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 20:25:21 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[dataGridColumn]]></category>
		<category><![CDATA[dataTipField]]></category>
		<category><![CDATA[dataTipFunction]]></category>
		<category><![CDATA[DataTips]]></category>
		<category><![CDATA[itemRenderer]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog2/?p=77</guid>
		<description><![CDATA[One of the bad things about using itemRenderers in a DataGridColumn is that you lose the dataTip functionality that it normally provides. Well, here is a way to fake that functionality. First, add the dataTipField or dataTipFunction to the DataGridColumn like you normally would. &#60;mx:DataGridColumn &#160;headerText=&#34;DataTip&#34; dataField=&#34;name1&#34; showDataTips=&#34;true&#34; dataTipField=&#34;description1&#34; /&#62; Then, in your itemRenderer add [...]]]></description>
			<content:encoded><![CDATA[<p>One of the bad things about using itemRenderers in a DataGridColumn is that you lose the dataTip functionality that it normally provides.  Well, here is a way to fake that functionality.</p>
<p>First, add the dataTipField or dataTipFunction to the DataGridColumn like you normally would.</p>
<div class="code">
<span class="mxmlComponent">&lt;mx:DataGridColumn</span> &nbsp;headerText=&quot;<span class="mxmlString">DataTip</span>&quot;</p>
<div class="indent5">
<div class="indent">
dataField=&quot;<span class="mxmlString">name1</span>&quot;<br />
showDataTips=&quot;<span class="mxmlString">true</span>&quot;<br />
dataTipField=&quot;<span class="mxmlString">description1</span>&quot;  <span class="mxmlComponent">/&gt;</span>
</div>
</div>
</div>
<p>Then, in your itemRenderer add the following code to be able to tap into that information and display a tooltip instead.</p>
<div class="code">
<p><span class="asReserved">private</span> <span class="asFunction">function</span> getToolTip():String{</p>
<div class="indent">
<span class="asVar">var</span> dg:DataGrid = listData.owner <span class="asReserved">as</span> DataGrid;<br />
<span class="asVar">var</span> func:Function = dg.columns[listData.columnIndex].dataTipFunction;</p>
<p><span class="asReserved">if</span>(func != <span class="asReserved">null</span>){</p>
<div class="indent">
<span class="asReserved">return</span></span> func.call(<span class="asReserved">this</span>, <span class="asReserved">this</span>.data);</div>
<p>}<span class="asReserved">else if</span>(dg.columns[listData.columnIndex].dataTipField.length){</p>
<div class="indent">
<span class="asReserved">return</span> data[dg.columns[listData.columnIndex].dataTipField];</div>
<p>}<span class="asReserved">else</span>{</p>
<div class="indent">
<span class="asReserved">return</span> <span class="asString">&quot;&quot;</span>;</div>
<p>}</p></div>
<p>}</p>
<p>&nbsp;</p>
<p><span class="asReserved">override protected</span> <span class="asFunction">function</span> updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):<span class="asReserved">void</span>{</p>
<div class="indent">
	<span class="asReserved">super</span>.updateDisplayList(unscaledWidth, unscaledHeight);<br />
	<span class="asReserved">this</span>.toolTip = getToolTip();</div>
<p>}
</p></div>
<p>This works with both dataTipFields and dataTipFunctions and lets you treat the dataTips in your columns the same way, regardless of whether you&#8217;re using an itemRenderer or not.  The only minor difference is the positioning of the label, but that can be easily modified with styles. You can download the <a href="/assets/files/psuedoDataTips.zip">full source code here</a>, for a functional example of how this works.</p>
<p><center><br />
<iframe height="200" frameborder="0" width="670" src="/assets/pages/psuedoDataTips.html"></iframe><br />
</center></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2008/12/displaying-datatips-when-using-an-itemrenderer/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Update: Solution to disappearing data in your itemRenderer</title>
		<link>http://www.kalengibbons.com/blog/index.php/2008/12/update-solution-to-disappearing-data-in-your-itemrenderer2/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2008/12/update-solution-to-disappearing-data-in-your-itemrenderer2/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 03:18:40 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[dataChange]]></category>
		<category><![CDATA[itemRenderer]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[validateNow]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog2/?p=125</guid>
		<description><![CDATA[In a previous post I explained how a rendering bug in Flex was causing items to disappear in an itemRenderer. In the example below, you can see that when you scroll, the items in the list disappear.&#160; This is caused by the varying heights of the elements.&#160; If you explicitly set the height for each [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a title="Disappearing data in your itemRenderer?" href="/blog/post.cfm/disappearing-data-in-your-itemrenderer" mce_href="/blog/post.cfm/disappearing-data-in-your-itemrenderer" target="_self">previous post</a> I explained how a rendering bug in Flex was causing items to disappear in an itemRenderer. In the example below, you can see that when you scroll, the items in the list disappear.&nbsp; This is caused by the varying heights of the elements.&nbsp; If you explicitly set the height for each element or set variableRowHeights to false, then the problem goes away.</p>
<p><center><br />
<iframe height="230" frameborder="0" width="310" src="/assets/pages/itemRenderer1.html"></iframe><br />
</center><br />
<br/></p>
<p>What was causing the problem was apparent but the solution was not.&nbsp; Thankfully, Olivier Lalonde was nice enough to shed some light on an easy fix.&nbsp; The itemRenderer needs to be validated as the user scrolls.&nbsp; So, as Olivier suggests, you simply need to listen for the dataChange event and execute the validateNow() function.</p>
<p>You can see in the example below that the list now has varying row heights&#8230; and the data no longer disappears. NICE!&nbsp; The key point to focus on here is the following line: &lt;mx:VBox <span style="font-weight: bold;" mce_style="font-weight:bold">dataChange=&#8221;validateNow()&#8221;</span> verticalGap=&#8221;0&#8243;&gt;</p>
<div class="code">
<span class="mxmlComponent">&lt;mx:List</span> </p>
<div class="indent">
dataProvider=&#8221;<span class="mxmlString">{</span>myData.item<span class="mxmlString">}</span>&#8220;<br />
variableRowHeight=&#8221;<span class="mxmlString">true</span>&#8220;<br />
alternatingItemColors=&#8221;<span class="mxmlString">['#FFFFFF','#E6EEF3']</span>&#8220;<br />
width=&#8221;<span class="mxmlString">100%</span>&#8220;<br />
height=&#8221;<span class="mxmlString">100%</span>&#8220;<span class="mxmlComponent">&gt;</span>
<p>&nbsp;</p>
<p><span class="mxmlComponent">&lt;mx:itemRenderer&gt;</span></p>
<div class="indent">
<span class="mxmlSpecialString">&lt;mx:Component&gt;</span></p>
<div class="indent">
<span class="mxmlComponent">&lt;mx:VBox</span> verticalGap=&#8221;<span class="mxmlString">0</span>&#8221; dataChange=&#8221;validateNow()&#8221;<span class="mxmlComponent">&gt;</span></p>
<div class="indent">
<span class="mxmlComponent">&lt;mx:HBox&gt;</span></p>
<div class="indent">
<span class="mxmlComponent">&lt;mx:Label</span> text=&#8221;<span class="mxmlString">{</span>data.Label<span class="mxmlString">}</span>&#8221; fontWeight=&#8221;<span class="mxmlString">bold</span>&#8221; <span class="mxmlComponent">/&gt;</span><br/><br />
<span class="mxmlComponent">&lt;mx:Label</span> text=&#8221;<span class="mxmlString">{</span>data.date<span class="mxmlString">}</span>&#8221; color=&#8221;<span class="mxmlString">#999999</span>&#8221; <span class="mxmlComponent">/&gt;</span>
</div>
<p><span class="mxmlComponent">&lt;/mx:HBox&gt;</span><br />
<span class="mxmlComponent">&lt;mx:Text</span> text=&#8221;<span class="mxmlString">{</span>data.description<span class="mxmlString">}</span>&#8221; width=&#8221;<span class="mxmlString">100%</span>&#8221; <span class="mxmlComponent">/&gt;</span>
</div>
<p><span class="mxmlComponent">&lt;/mx:VBox&gt;</span>
</div>
<p><span class="mxmlSpecialString">&lt;/mx:Component&gt;</span>
</div>
<p><span class="mxmlComponent">&lt;/mx:itemRenderer&gt;</span>
</div>
<p>&nbsp;</p>
<p><span class="mxmlComponent">&lt;/mx:List&gt;</span>
</div>
<p><br/><br />
<center><br />
<iframe height="230" frameborder="0" width="400" src="/assets/pages/itemRenderer3.html"></iframe><br />
<center><br />
<br/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2008/12/update-solution-to-disappearing-data-in-your-itemrenderer2/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
