<?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>Saturnboy &#187; flex4</title>
	<atom:link href="http://saturnboy.com/tag/flex4/feed/" rel="self" type="application/rss+xml" />
	<link>http://saturnboy.com</link>
	<description>Code, Work, and Life</description>
	<lastBuildDate>Mon, 19 Dec 2011 21:56:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Drag-and-Drop Revisited</title>
		<link>http://saturnboy.com/2010/12/drag-and-drop-revisited/</link>
		<comments>http://saturnboy.com/2010/12/drag-and-drop-revisited/#comments</comments>
		<pubDate>Wed, 15 Dec 2010 04:02:54 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[drag-and-drop]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[skinning]]></category>
		<category><![CDATA[ux]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1668</guid>
		<description><![CDATA[It&#8217;s time to revisit drag-and-drop in Flex 4. For a long time now, my previous Drag-and-Drop in Flex 4 post has been the most popular post on this blog. I get something like 1,200 pageview/month on that one post. This time around, I&#8217;ll try to expand on the thinking that goes on before I write [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s time to revisit drag-and-drop in Flex 4.  For a long time now, my previous <a href="http://saturnboy.com/2009/08/drag-and-drop-flex-4/">Drag-and-Drop in Flex 4</a> post has been the most popular post on this blog.  I get something like 1,200 pageview/month on that one post.  This time around, I&#8217;ll try to expand on the thinking that goes on <i>before</i> I write the code to add drag-and-drop functionality to an app.</p>
<h3>UX 101</h3>
<p>It is impossible to develop an RIA without some basic understanding of interaction design.  As they say in one of my favorite movies, &quot;Many men have tried.  They tried and failed?  They tried and died.&quot;  So a short UX primer on drag-and-drop goes like this&#8230;</p>
<p>Users are typically familiar with drag-and-drop for two basic actions: moving-stuff-around and putting-stuff-into.  For example, we move windows around the desktop or move songs around in a playlist or move icons around in a folder.  Also, we put stuff into the trash or put a picture into an album or put an attachment into an email.  At a fundmental level, users have an understanding of these actions because they mimic the physical world.  Performing these actions via a touch interface (rather than a mouse) is even better, but that&#8217;s a story for another day.</p>
<p class="bottom">Given these two basic actions, I&#8217;m going to design a sample app that exercises them both.  This is what I came up with:</p>
<div class="span-14 last">
<img src="http://saturnboy.com/proj/flex4/drag_drop/colors/diagrams/drag-drop-diagram1.png" alt="diagram 1" title="diagram 1" width="490" height="327" />
</div>
<p>Our sample app has three drag actions.  In #1, you can drag to put items from the palette into the list.  In #2a and #2b, you can drag items from the list into a target box (that preforms some action).  In #3, you can drag items around inside the list to reorder them.  I&#8217;ll break them down one by one.</p>
<h3>#1 &mdash; Drag List Item to Another List</h3>
<p class="bottom">Dragging from one list and dropping on another is super easy in Flex 4.  Here&#8217;s a snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> id=<span style="color: #ff0000;">&quot;sourceList&quot;</span> dragEnabled=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ArrayList</span> source=<span style="color: #ff0000;">&quot;['A','B','C','D']&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:List</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> id=<span style="color: #ff0000;">&quot;targetList&quot;</span> dropEnabled=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ArrayList</span><span style="color: #7400FF;">&gt;</span><span style="color: #7400FF;">&lt;/s:ArrayList</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:List</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>We set <code>dragEnable=true</code> on the source list and <code>dropEnable=true</code> on the target list.  For bi-directional drag-and-drop, just set both attributes true for both lists.  In a feat of awesomeness, Flex 4 does everything else for you.  You don&#8217;t need to listen for any events or write any handlers.  And you even get some nice eye candy for free, because the target list glows when you drag an item over it.</p>
<blockquote style="color:#333"><p><b>Trick 1</b> &mdash; When the drop target is an empty list, you <i>must</i> given it an empty <code>dataProvider</code>.  In the above example, I just use an empty <code>ArrayList</code> pair of tags in MXML, but I could just as easily say <code>dataProvider=&quot;{new ArrayList()}&quot;</code>, or even do everything in Actionscript.</p></blockquote>
<blockquote style="color:#333"><p><b>Trick 2</b> &mdash; The source list accepts a drop&mdash;if and only if&mdash;the item being dropped is compatible with the items in the target <code>dataProvider</code>.  For example, you can <i>not</i> drop a <code>Foo</code> object on a list of <code>Bar</code> objects.  Setting the target list to have an empty <code>dataProvider</code> (à la Trick 1), accepts anything.</p></blockquote>
<h3>#2 &mdash; Drag List Item to <code>UIComponent</code> Target</h3>
<p class="bottom">Again, to use a list as the drag source, we just set <code>dragEnable=true</code>.  But this time the target is some non-<code>List</code> <code>UIComponent</code>, so we need to do what we <a href="http://saturnboy.com/2009/08/drag-and-drop-flex-4/">always do</a> to enable drop functionality on a component&mdash;namely, attach handlers to the <code>dragEnter</code> and <code>dragDrop</code> events.  Here&#8217;s a snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> id=<span style="color: #ff0000;">&quot;sourceList&quot;</span> dragEnabled=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ArrayList</span> source=<span style="color: #ff0000;">&quot;['A','B','C','D']&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:List</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> id=<span style="color: #ff0000;">&quot;targetA&quot;</span> </span>
<span style="color: #000000;">         dragEnter=<span style="color: #ff0000;">&quot;dragEnterHandler(event)&quot;</span></span>
<span style="color: #000000;">         dragDrop=<span style="color: #ff0000;">&quot;dragDropHandler(event)&quot;</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Panel</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p class="bottom">And here are the handlers:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragEnterHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    DragManager.<span style="color: #006600;">acceptDragDrop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">currentTarget</span> as IUIComponent<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragDropHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">//do something with dropped item here</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As always, we <i>accept</i> the drop in the <code>dragEnter</code> handler via the static <code>acceptDragDrop()</code> method on the <code>DragManager</code>.  And once the object is dropped, we can do whatever we want with it in the <code>dragDrop</code> handler.</p>
<h3>#3 &mdash; Drag List Item to Reorder Items</h3>
<p class="bottom">Finally, we want to be able to reorder the items in a list.  Again, the spark <code>List</code> does all the work for us.  We just set both <code>dragEnable=true</code> and <code>dropEnable=true</code> on the list we want to reorder.  We must also set the magical <code>dragMoveEnabled=true</code>, which has numerous ramifications. Here is the snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> id=<span style="color: #ff0000;">&quot;reorderList&quot;</span></span>
<span style="color: #000000;">        dragEnabled=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #000000;">        dropEnabled=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #000000;">        dragMoveEnabled=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ArrayList</span> source=<span style="color: #ff0000;">&quot;['A','B','C','D']&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:List</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>The code is trivial, but what&#8217;s going on with <code>dragMoveEnabled=true</code>?  Well, <code>dragMoveEnable</code>, which defaults to <code>false</code>, controls whether or not an object is <i>moved</i> (aka removed from the source and added to the target) or <i>copied</i> (aka added to the target leaving the source untouched).  So to reorder the items in a list, we need the <i>move</i> behavior, thus we set <code>dragMoveEnabled=true</code>.</p>
<blockquote style="color:#333"><p><b>Trick 3</b> &mdash; If the source list is <code>dragMoveEnabled=true</code> and the target is a non-<code>List</code> <code>UIComponent</code>, you will see some seemingly <i>odd</i> behavior.  Every time to drop an object on your component, it will be deleted from the source list.  This is because you are only seeing the first half of the <i>move</i> (the item is removed from the source), and not the second half (the item is added to the target) because the target is not a <code>List</code>.</p></blockquote>
<blockquote style="color:#333"><p><b>Trick 4</b> &mdash; As an alternative to <code>dragMoveEnabled=true</code>, you can make a call to  <code>DragManager.showFeedback(DragManager.MOVE)</code> in the <code>dragEnter</code> handler to control whether or not an object is <i>moved</i> or <i>copied</i>.  Do <i>NOT</i> use both <code>showFeedback</code> and <code>dragMoveEnabled</code> at the same time.</p></blockquote>
<h3>Eye candy</h3>
<p>What&#8217;s a Flex app without eye candy?  But with drag-and-drop, I&#8217;d say most of the eye candy is more of a UX necessity than anything else.  It&#8217;s used to indicate potential drop targets, or highlight a target that is ready to receive a drop, etc.</p>
<h5>Glow On Enter</h5>
<p class="bottom">With list to list drag-and-drop, we get a glow on the target list for free, which tells the user that the target list is ready to receive the drop.  But when dragging to a non-<code>List</code> <code>UIComponent</code>, we need to add the glow ourselves:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragEnterHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    myGlow.<span style="color: #006600;">alpha</span> = <span style="color: #cc66cc;">1</span>;
    DragManager.<span style="color: #006600;">acceptDragDrop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">currentTarget</span> as IUIComponent<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragExitHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    myGlow.<span style="color: #006600;">alpha</span> = <span style="color: #cc66cc;">0</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> dragDropHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DragEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    myGlow.<span style="color: #006600;">alpha</span> = <span style="color: #cc66cc;">0</span>;
    <span style="color: #808080; font-style: italic;">//do something with dropped item here</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p class="bottom">In the <code>dragEnter</code> handler, we turn the glow on, and we turn the glow off on either a <code>dragExit</code> or <code>dragDrop</code> event.  And we need to add a simple <code>GlowFilter</code> to the target component:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> id=<span style="color: #ff0000;">&quot;targetA&quot;</span> </span>
<span style="color: #000000;">         dragEnter=<span style="color: #ff0000;">&quot;dragEnterHandler(event)&quot;</span></span>
<span style="color: #000000;">         dragDrop=<span style="color: #ff0000;">&quot;dragDropHandler(event)&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:filters</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:GlowFilter</span> id=<span style="color: #ff0000;">&quot;myGlow&quot;</span> blurX=<span style="color: #ff0000;">&quot;16&quot;</span> blurY=<span style="color: #ff0000;">&quot;16&quot;</span></span>
<span style="color: #000000;">                color=<span style="color: #ff0000;">&quot;#3399ff&quot;</span> alpha=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:filters</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Panel</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>No magic, just use MXML to add a <code>GlowFilter</code> to the target component, which in this snippet is a spark <code>Panel</code> component.</p>
<h5>Custom Drag Proxy</h5>
<p>Customizing the drag proxy (what an item looks like while dragging) is fun, but <a href="http://saturnboy.com/2009/08/drag-and-drop-flex-4/">I covered that previously</a>, so there&#8217;s no need to revisit it here.  On the other hand, I haven&#8217;t discussed how to customizing an item dragged from a spark <code>List</code> yet.  It is not only fun, but also quite a bit easier.</p>
<p class="bottom">Flex 4 makes is super easy to create a custom drag proxy by simply adding a magic <code>dragging</code> state to the <code>List</code>&#8216;s <code>itemRenderer</code>.  For this snippet, imagine I have a trivial <code>ItemRenderer</code> that just shows a centered label with black text, but when it&#8217;s being dragged I want to turn the text red:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ItemRenderer</span> ...<span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:states</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;normal&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;hovered&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;selected&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;dragging&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:states</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> left=<span style="color: #ff0000;">&quot;0&quot;</span> right=<span style="color: #ff0000;">&quot;0&quot;</span> top=<span style="color: #ff0000;">&quot;0&quot;</span> bottom=<span style="color: #ff0000;">&quot;0&quot;</span></span>
<span style="color: #000000;">            text=<span style="color: #ff0000;">&quot;{data.label}&quot;</span> textAlign=<span style="color: #ff0000;">&quot;center&quot;</span> verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span></span>
<span style="color: #000000;">            color=<span style="color: #ff0000;">&quot;#000000&quot;</span></span>
<span style="color: #000000;">            color.hovered=<span style="color: #ff0000;">&quot;#0000ff&quot;</span></span>
<span style="color: #000000;">            color.dragging=<span style="color: #ff0000;">&quot;#ff0000&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:ItemRenderer</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>As you can see from the above code, customizing the drag indicator is a <i>huge</i> nothing.  Just add the magic <code>dragging</code> state to the list of states, and use Flex 4&#8242;s inline state syntax to do whatever you want.  In this snippet, we set <code>color.dragging=&quot;#ff0000&quot;</code> to turn the text red when its being dragged.  The sample app below does quite a few more things to customize the dragged item, so check out that code too.</p>
<h5>Custom Drop Indicator</h5>
<p>When a user drags an item over a target spark <code>List</code>, a line appears to show the user where the item will be inserted.  This line is known as the <i>drop indicator</i>.  To change things up, we will make a custom drop indicator that is a dashed line.</p>
<blockquote style="color:#333"><p><b>Trick 5</b> &mdash; Alas, Flex 4 does not have direct support for dashed strokes, so we need to fake it.  The easy way to make a dashed line in Flex 4 is to use <code>BitmapFill</code>.  First, we create an image that is a tileable pattern of squares.  Then, we make a <code>Rect</code> and set its fill to <code>BitmapFill</code> and <code>fillMode=&quot;repeat&quot;</code>.</p></blockquote>
<p class="bottom">The <code>dropIndicator</code> component is found in the <code>fx:Declarations</code> block of the default spark <code>ListSkin</code>, so we must create a custom <code>List</code> skin with our custom <code>dropIndicator</code> component.  Here&#8217;s a snippet from our custom skin&#8217;s <code>fx:Declarations</code> block:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Component</span> id=<span style="color: #ff0000;">&quot;dropIndicator&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Group</span> minWidth=<span style="color: #ff0000;">&quot;1&quot;</span> minHeight=<span style="color: #ff0000;">&quot;1&quot;</span> maxWidth=<span style="color: #ff0000;">&quot;1&quot;</span> maxHeight=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Rect</span> left=<span style="color: #ff0000;">&quot;0&quot;</span> right=<span style="color: #ff0000;">&quot;0&quot;</span> top=<span style="color: #ff0000;">&quot;0&quot;</span> bottom=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #7400FF;">&gt;</span></span>
                <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:fill</span><span style="color: #7400FF;">&gt;</span></span>
                    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:BitmapFill</span> source=<span style="color: #ff0000;">&quot;@Embed('dash.png')&quot;</span></span>
<span style="color: #000000;">                            fillMode=<span style="color: #ff0000;">&quot;repeat&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
                <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:fill</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Rect</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Group</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Component</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>As described above, we have a <code>Rect</code> with a <code>BitmapFill</code>, and an image <code>dash.png</code> that is a tileable pattern of squares.  But the real magic is in the <code>Group</code> wrapper, by setting all of <code>minWidth</code>, <code>maxWidth</code>, <code>minHeight</code>, <code>maxHeight</code> to 1px we ensure we get a one pixel line as our drop indicator independent of the layout applied to our <code>List</code>.  This is because <code>HorizontalLayout</code> ignore min and max height, and <code>VerticalLayout</code> ignore min and max width.  So if we take a 1px view of our pattern of squares, we see dashes.</p>
<h3>Conclusion</h3>
<p class="bottom">Here&#8217;s the complete drag-and-drop sample (view source enabled):</p>
<div id="flashcontent-drag-drop-colors">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<p>Hopefully, I covered the full spectrum of drag-and-drop functionality that the average Flex app (or even above average) is ever likely to need.  If you want more, check out the links below, or just leave me a comment.</p>
<h5>Useful Links</h5>
<ul>
<li>My first post on <a href="http://saturnboy.com/2009/08/drag-and-drop-flex-4/">drag-and-drop in Flex 4</a> covered a lot of drag proxy stuff.</li>
<li>Flex After Dark has a solid <a href="http://www.flexafterdark.com/tutorials/Flex-Drag-and-Drop">drag-and-drop tutorial</a> that is definitely worth reading if you are implementing drag-and-drop functionality in your app.</li>
<li>Evtim, aka The Godfather, as I like to call him, has a <a href="http://evtimmy.com/2010/01/drag-and-drop-skinning-in-spark/">cool post</a> on customizing both the drag indicator and the drop indicator of the spark <code>List</code> component.</li>
</ul>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex4/drag_drop/colors/DragDropColors.html">DragDropColors</a> (<a href="http://saturnboy.com/proj/flex4/drag_drop/colors/srcview/DragDropColors.zip">download</a>)</li>
</ul>
<div>
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex4/drag_drop/colors/DragDropColors.swf', 'flashcontent-drag-drop-colors', '510', '380', '10.1.0', 'playerProductInstall.swf', false, { bgColor:'#f8f8f8', base:'.' });
</script>
</div>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/12/drag-and-drop-revisited/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Programmatic Skinning in Flex 4 &#8212; an FXG Clock</title>
		<link>http://saturnboy.com/2010/11/programmatic-skinning-in-flex-4-an-fxg-clock/</link>
		<comments>http://saturnboy.com/2010/11/programmatic-skinning-in-flex-4-an-fxg-clock/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 06:22:27 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[custom component]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[skinning]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1576</guid>
		<description><![CDATA[I tried to push my Flex 4 skinning skills to the limit, and the result is a fancy clock skin. (click to see it in action) The fancy clock skin overlays a very simple clock component. The Clock component is quite underwhelming, but here it is in all of its abbreviated glory: public class Clock [...]]]></description>
			<content:encoded><![CDATA[<p>I tried to push my Flex 4 skinning skills to the limit, and the result is a fancy clock skin.</p>
<div class="span-14 top" style="min-height:510px; text-align:center;">
<a href="http://saturnboy.com/proj/flex4/clock/AnalogClock.html"><img src="http://saturnboy.com/proj/flex4/clock/clock.png" width="500" height="500" alt="clock" title="clock" /></a>
</div>
<div style="text-align:center; padding-bottom:10px;">(click to see it in action)</div>
<p class="bottom">The fancy clock skin overlays a very simple clock component.  The Clock component is quite underwhelming, but here it is in all of its abbreviated glory:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Clock <span style="color: #0066CC;">extends</span> SkinnableComponent <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> secAngle:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0.0</span>;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> minAngle:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0.0</span>;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> hourAngle:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0.0</span>;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _timer:Timer;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _tween:GTween;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Clock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        ...
        _tween = <span style="color: #000000; font-weight: bold;">new</span> GTween<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #cc66cc;">2</span>, ...<span style="color: #66cc66;">&#41;</span>;
        _timer = <span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2000</span>,<span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>;
        _timer.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TimerEvent.<span style="color: #006600;">TIMER</span>, tickHandler<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> tickHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TimerEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> t:<span style="color: #0066CC;">Date</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> h:<span style="color: #0066CC;">Number</span> = t.<span style="color: #0066CC;">getHours</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">var</span> m:<span style="color: #0066CC;">Number</span> = t.<span style="color: #0066CC;">getMinutes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + h <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">60</span>;
        <span style="color: #000000; font-weight: bold;">var</span> s:<span style="color: #0066CC;">Number</span> = t.<span style="color: #0066CC;">getSeconds</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + m <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">60</span>;
        <span style="color: #000000; font-weight: bold;">var</span> ms:<span style="color: #0066CC;">Number</span> = t.<span style="color: #0066CC;">getMilliseconds</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        _tween.<span style="color: #006600;">proxy</span>.<span style="color: #006600;">secAngle</span> = -<span style="color: #cc66cc;">90</span> + s <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">6</span> + ms <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.006</span>;
        _tween.<span style="color: #006600;">proxy</span>.<span style="color: #006600;">minAngle</span> = -<span style="color: #cc66cc;">90</span> + s <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.1</span>;
        _tween.<span style="color: #006600;">proxy</span>.<span style="color: #006600;">hourAngle</span> = -<span style="color: #cc66cc;">90</span> + <span style="color: #66cc66;">&#40;</span>s <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    ...
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>At the core of the Clock component is a single timer.  When the timer fires, it uses the current date to compute the angle of the hour hand, minute hand, and second hand.  The actual motion of the hands is managed via the awesome proxy functionality in Grant Skinner&#8217;s <a href="http://www.gskinner.com/libraries/gtween/">GTween</a> library.</p>
<p class="bottom">All the interesting FXG action takes place in the skin, so let&#8217;s check it out:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Skin</span></span>
<span style="color: #000000;">        xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #000000;">        xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;complete()&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Metadata</span><span style="color: #7400FF;">&gt;</span></span>
        [HostComponent(&quot;components.Clock&quot;)]
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Metadata</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">            private function complete<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                ...lots of programmatic FXG here...</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    ...lots of vanilla FXG here...
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Path</span> rotation=<span style="color: #ff0000;">&quot;{hostComponent.minAngle}&quot;</span> ...</span>
<span style="color: #000000;">    <span style="color: #7400FF;">&lt;s:Path</span> rotation=<span style="color: #ff0000;">&quot;{hostComponent.hourAngle}&quot;</span> ...</span>
<span style="color: #000000;">    <span style="color: #7400FF;">&lt;s:Path</span> rotation=<span style="color: #ff0000;">&quot;{hostComponent.secAngle}&quot;</span> ...</span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Skin</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>Getting data to the skin is done via the standard binding to <code>hostComponent</code>.  There is no need for the more complicated <code>partAdded()</code> and <code>partRemoved()</code> mechanism, because the Clock component is <i>not</i> intended for general use, unlike my previous components: <a href="http://saturnboy.com/2010/08/terrifictabbar-custom-component/">Terrific TabBar</a>, <a href="http://saturnboy.com/2010/07/supertextinput-building-a-custom-component/">SuperTextInput</a>, and <a href="http://saturnboy.com/2010/06/drawer-component-flex-4/">Drawer</a>.</p>
<p>The skin has a big block of programmatic FXG skinning, done in actionscript code inside the <code>complete()</code> handler which is fired by the <code>creationComplete</code> event.  It also has a big block of vanilla MXML declarative FXG skinning, which, most importantly, includes the clock hands and the previously mentioned binding to <code>hostComponent</code>.</p>
<p class="bottom">By its nature, a clock has a lot of identical elements that are positioned in a circle.  The obvious attack is to put the repeated elements in a loop, or set of loops, and go the programmatic route to construct the skin.  For example, the long thin ticks along the outer ring are constructed with a double loop.  Here&#8217;s the relevant snippet of code from inside the <code>complete()</code> handler:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> <span style="color: #b1b100;">in</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #cc66cc;">90</span>,<span style="color: #cc66cc;">120</span>,<span style="color: #cc66cc;">150</span>,<span style="color: #cc66cc;">180</span>,<span style="color: #cc66cc;">210</span>,<span style="color: #cc66cc;">240</span>,<span style="color: #cc66cc;">270</span>,<span style="color: #cc66cc;">300</span>,<span style="color: #cc66cc;">330</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> j:<span style="color: #0066CC;">int</span> <span style="color: #b1b100;">in</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">12</span>,<span style="color: #cc66cc;">18</span>,<span style="color: #cc66cc;">24</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> p:Path = <span style="color: #000000; font-weight: bold;">new</span> Path<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        p.<span style="color: #0066CC;">data</span> = <span style="color: #ff0000;">&quot;M 174,-0.5 L 184,-0.5 184,0.5 174,0.5 Z&quot;</span>;
        p.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">250</span>;
        p.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">250</span>;
        p.<span style="color: #006600;">rotation</span> = i + j;
        p.<span style="color: #006600;">fill</span> = gray;
        <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addElementAt</span><span style="color: #66cc66;">&#40;</span>p, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>A new <code>Path</code> element is created and its <code>data</code> property is set.  Our tick mark is 10px long, but only 1px wide.  With each iteration, a tick mark is created and rotated into place.  There are two important tricks in this code that are worth pointing out.  First, the rotation origin is at (0,0) for each element, so we must construct our path from -0.5 to 0.5 for it to rotate correctly.  Second, we must use <code>addElementAt()</code> to place each element into the proper layer of the drawing because the programmatic FXG code runs after all the vanilla (aka non-programmatic) FXG has been drawn.</p>
<h3>Conclusion</h3>
<p>No one in their right mind would ever build a clock skin like this.  To do this the right way, you&#8217;d definitely want to use an image or two to construct the skin.  The only reason I went the programmatic FXG skinning route was to verify to myself that it could be done.  So, yes it can be done, and I enjoyed the trip.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex4/clock/AnalogClock.html">AnalogClock</a> (<a href="http://saturnboy.com/proj/flex4/clock/srcview/AnalogClock.zip">download</a>)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/11/programmatic-skinning-in-flex-4-an-fxg-clock/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>FlexLayouts.org Launches, Custom Flex 4 Layouts for the Masses</title>
		<link>http://saturnboy.com/2010/08/flexlayouts-launchs/</link>
		<comments>http://saturnboy.com/2010/08/flexlayouts-launchs/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 03:23:09 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[flexlayouts]]></category>
		<category><![CDATA[layout]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1557</guid>
		<description><![CDATA[FlexLayouts is an open source library of custom Flex 4 layouts that I&#8217;ve veen working on with Gilles Guillemin. Today, I&#8217;m happy to announce the first public beta release, v0.5, is available for immediate download. The FlexLayouts Gallery will be your source for layouts, sample code, and documentation. Today&#8217;s release includes beta versions of two [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://flexlayouts.org/">FlexLayouts</a> is an open source library of custom Flex 4 layouts that I&#8217;ve veen working on with <a href="http://www.flexstuff.co.uk/">Gilles Guillemin</a>.  Today, I&#8217;m happy to announce the first public beta release, v0.5, is available for immediate <a href="http://flexlayouts.org/download/">download</a>.  The FlexLayouts <a href="http://flexlayouts.org/gallery/">Gallery</a> will be your source for layouts, sample code, and documentation.  Today&#8217;s release includes beta versions of two simple 2D layouts: <a href="http://flexlayouts.org/2010/08/flowlayout/">FlowLayout</a> and <a href="http://flexlayouts.org/2010/08/snakelayout/">SnakeLayout</a>.</p>
<h3>Our Dream</h3>
<p>We admit v0.5 is not much to look at but imagine a future where a huge number of robust, well tested layouts (both 2D and 3D) exists and is readily available with both sample code and exceptional documentation.  You could just slap together some vanilla Flex components, drop in an awesome custom layout (like Gilles&#8217; <a href="http://www.flexstuff.co.uk/2009/12/23/flex-4-coverflow-layout/">CoverFlowLayout</a>), and ka-BOOM, you have an awesome custom component that impresses co-workers, bosses, and clients equally.  That&#8217;s the dream: to take one of the most portable, re-usable pieces of the Flex framework, make a massive library, and empower the next generation of custom components.</p>
<h3>Ransom Note</h3>
<p>I whipped together a quick sample application using <a href="http://flexlayouts.org/2010/08/flowlayout/">FlowLayout</a> that I&#8217;m calling Ransom Note.  It pulls together a list of images of letters that look as if they were cut out of a magazine or newspaper and pasted together.  It follows one of the most common custom layout patterns which is: <code>List</code> + <code>ItemRenderer</code> + custom layout.  The actual implementation uses the lighter weight version: <code>DataGroup</code> + <code>DataRenderer</code> + custom layout, because I don&#8217;t need any of the list selection stuff.</p>
<p>Check out the finished product:</p>
<p>&raquo; <a href="http://saturnboy.com/proj/flex4/ransom_note/RansomNote.html">RansomNote</a> (view source enabled)</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex4/ransom_note/RansomNote.html">RansomNote</a> (<a href="http://saturnboy.com/proj/flex4/ransom_note/srcview/RansomNote.zip">download</a>)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/08/flexlayouts-launchs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>TerrificTabBar – Another Custom Component in Flex 4</title>
		<link>http://saturnboy.com/2010/08/terrifictabbar-custom-component/</link>
		<comments>http://saturnboy.com/2010/08/terrifictabbar-custom-component/#comments</comments>
		<pubDate>Mon, 02 Aug 2010 16:37:41 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[custom component]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[skinning]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1530</guid>
		<description><![CDATA[Another day, another custom component in Flex 4. This time it&#8217;s a closeable TabBar that I&#8217;m calling TerrificTabBar, because SuperTabBar is taken. This post is basically Part 2 of my previous post on SuperTextInput. And much like that post, TerrificTabBar follows the Enhanced Component Pattern by extending TabBar and adding some new functionality. Extend TabBar [...]]]></description>
			<content:encoded><![CDATA[<p>Another day, another custom component in Flex 4.  This time it&#8217;s a closeable <code>TabBar</code> that I&#8217;m calling TerrificTabBar, because SuperTabBar is <a href="http://code.google.com/p/flexlib/">taken</a>.  This post is basically Part 2 of my previous post on <a href="http://saturnboy.com/2010/07/supertextinput-building-a-custom-component/">SuperTextInput</a>.  And much like that post, TerrificTabBar follows the <i>Enhanced Component Pattern</i> by extending <code>TabBar</code> and adding some new functionality.</p>
<h3>Extend TabBar and TabBarButton</h3>
<p>I just want to build your average web browser tab in Flex 4, no more, no less.  Browser tabs have two cool features: each tab has a close button, and the tabs can be reordered by dragging.  Actually, I just lied about the no less part, because I&#8217;m going to ignore dragging and focus my attention on the closeable tab part.  Please see my previous post on <a href="http://saturnboy.com/2009/08/drag-and-drop-flex-4/">drag-and-drop in Flex 4</a> if you are interested in the dragging part.</p>
<p class="bottom">A <code>TabBar</code> is just a bag of tabs, where each individual tab is a button, or a <code>TabBarButton</code> to be more precise.  To create a closeable tab, we need to add a new button that can be clicked to close the tab, duh.  And this is in addition to the default behavior of clicking the tab to select it.   Since we are disciples of <i>The Flex 4 Way</i> of building custom components, we know this means we must extend <code>TabBarButton</code> and add a new <code>Button</code> <code>SkinPart</code> to it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package components <span style="color: #66cc66;">&#123;</span>
    ...
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TerrificTabBarButton <span style="color: #0066CC;">extends</span> ButtonBarButton <span style="color: #66cc66;">&#123;</span>
        <span style="color: #66cc66;">&#91;</span>SkinPart<span style="color: #66cc66;">&#40;</span>required=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> closeButton:<span style="color: #0066CC;">Button</span>;
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _closeable:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">true</span>;
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> TerrificTabBarButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">//IMPORTANT: this enables the button's children (aka the close button) to receive mouse events</span>
            <span style="color: #0066CC;">this</span>.<span style="color: #006600;">mouseChildren</span> = <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> closeable<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> _closeable;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> closeable<span style="color: #66cc66;">&#40;</span>val:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_closeable <span style="color: #66cc66;">!</span>= val<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                _closeable = val;
                closeButton.<span style="color: #0066CC;">visible</span> = val;
                labelDisplay.<span style="color: #0066CC;">right</span> = <span style="color: #66cc66;">&#40;</span>val ? <span style="color: #cc66cc;">30</span> : <span style="color: #cc66cc;">14</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> closeHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> TerrificTabBarEvent<span style="color: #66cc66;">&#40;</span>TerrificTabBarEvent.<span style="color: #006600;">CLOSE_TAB</span>, itemIndex, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override protected <span style="color: #000000; font-weight: bold;">function</span> partAdded<span style="color: #66cc66;">&#40;</span>partName:<span style="color: #0066CC;">String</span>, instance:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">partAdded</span><span style="color: #66cc66;">&#40;</span>partName, instance<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == closeButton<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                closeButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, closeHandler<span style="color: #66cc66;">&#41;</span>;
                closeButton.<span style="color: #0066CC;">visible</span> = closeable;
            <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == labelDisplay<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                labelDisplay.<span style="color: #0066CC;">right</span> = <span style="color: #66cc66;">&#40;</span>closeable ? <span style="color: #cc66cc;">30</span> : <span style="color: #cc66cc;">14</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override protected <span style="color: #000000; font-weight: bold;">function</span> partRemoved<span style="color: #66cc66;">&#40;</span>partName:<span style="color: #0066CC;">String</span>, instance:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">partRemoved</span><span style="color: #66cc66;">&#40;</span>partName, instance<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == closeButton<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                closeButton.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, closeHandler<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>First, we have the <code>closeButton</code> <code>SkinPart</code> and it’s <code>closeHandler()</code> event handler. Wiring the handler function to the button is done in the <code>partAdded()</code> override, and un-wiring in the <code>partRemoved()</code> override. The handler just bubbles a <code>closeTab</code> custom event which will be handled by the parent <code>TabBar</code>.  Next, we use the <code>closeable</code> property to manage the close button&#8217;s visibility.  Finally, an additional button visibility check is necessary in <code>partAdded()</code> to set the initial visibility.</p>
<p>There are two other interesting pieces of code.  One, we must set <code>mouseChildren = true</code> in the constructor to enable our button within a button to receive and respond to mouse events.  Two, in a somewhat hackish fashion, I add or remove padding from the tab&#8217;s label when the close button is present or not.</p>
<p class="bottom">Now that we have a nice close button on the individual tabs, we extend <code>TabBar</code> to make a pretty API for managing tabs and their new functionality.  Here is an abbreviated <code>TerrificTabBar</code> component showing the relevant public methods:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package components <span style="color: #66cc66;">&#123;</span>
    ...
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TerrificTabBar <span style="color: #0066CC;">extends</span> TabBar <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> TerrificTabBar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setCloseableTab<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">index</span>:<span style="color: #0066CC;">int</span>, value:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">index</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">index</span> <span style="color: #66cc66;">&lt;</span> dataGroup.<span style="color: #006600;">numElements</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">var</span> btn:TerrificTabBarButton = dataGroup.<span style="color: #006600;">getElementAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">index</span><span style="color: #66cc66;">&#41;</span> as TerrificTabBarButton;
                btn.<span style="color: #006600;">closeable</span> = value;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCloseableTab<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">index</span>:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">index</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">index</span> <span style="color: #66cc66;">&lt;</span> dataGroup.<span style="color: #006600;">numElements</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">var</span> btn:TerrificTabBarButton = dataGroup.<span style="color: #006600;">getElementAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">index</span><span style="color: #66cc66;">&#41;</span> as TerrificTabBarButton;
                <span style="color: #b1b100;">return</span> btn.<span style="color: #006600;">closeable</span>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> closeHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TerrificTabBarEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            closeTab<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">index</span>, <span style="color: #0066CC;">this</span>.<span style="color: #006600;">selectedIndex</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> closeTab<span style="color: #66cc66;">&#40;</span>closedTab:<span style="color: #0066CC;">int</span>, selectedTab:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            ...
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        protected override <span style="color: #000000; font-weight: bold;">function</span> partAdded<span style="color: #66cc66;">&#40;</span>partName:<span style="color: #0066CC;">String</span>, instance:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">partAdded</span><span style="color: #66cc66;">&#40;</span>partName, instance<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == dataGroup<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                dataGroup.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TerrificTabBarEvent.<span style="color: #006600;">CLOSE_TAB</span>, closeHandler<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        protected override <span style="color: #000000; font-weight: bold;">function</span> partRemoved<span style="color: #66cc66;">&#40;</span>partName:<span style="color: #0066CC;">String</span>, instance:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">partRemoved</span><span style="color: #66cc66;">&#40;</span>partName, instance<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == dataGroup<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                dataGroup.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>TerrificTabBarEvent.<span style="color: #006600;">CLOSE_TAB</span>, closeHandler<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>There is a public getter and setter for toggling the close-ability of individual tabs, <code>setCloseableTab()</code> and <code>getCloseableTab()</code>.  And also a public method for closing a tab, <code>closeTab()</code>, which can be used to force close an un-closeable tab.  Additionally, <code>closeTab()</code> is used internally to close a tab when its close button is clicked.  As is standard, the wiring and un-wiring of the <code>closeHandler()</code> handler function is done in the <code>partAdded()</code> and <code>partRemoved()</code> overrides.</p>
<p>Very vanilla, but there is one interesting part worth mentioning.  I made the decision to attach the listener to the <code>dataGroup</code> property of <code>TabBar</code>, this is the default container of individual tabs (aka <code>TerrificTabBarButton</code>s).  Yes, I could just as easily have attached the handler directly to TabBar in the constructor and used weak references.  But after having built enough custom Flex 4 components, it feels funny to me to do any event wiring outside of <code>partAdded()</code> and <code>partRemoved()</code>.  Thus, I made the decision to attached to <code>dataGroup</code>.  Without doing a bunch of testing (which I haven&#8217;t done), I can&#8217;t say if this is better or worse from a memory or performance perspective, I just like the way this code looks better.</p>
<h3>Conclusion</h3>
<p class="bottom">I put a pretty skin on everything using some of my favorite drawing tricks, but that&#8217;s the subject of a future post.  Some brief highlights include:</p>
<ul>
<li>I used CSS to wire my components to my skins, so I minimize the use of <code>skinClass</code> attributes for better decoupling</li>
<li>I used a big negative <code>gap</code> in <code>TerrificTabBarSkin</code> to make the tabs overlap</li>
<li>I set a <code>maxWidth</code> and a <code>minWidth</code> on the individual tabs in <code>TerrificTabBarButtonSkin</code>, which in combination with <code>maxDisplayedLines=&quot;1&quot;</code> on the <code>Label</code>, make the tabs truncate with <code>...</code> when they get too big</li>
<li>The curved tab shape is drawn with a <code>Path</code>, but I wrap it with a <code>Group</code> and set the <code>Group</code>&#8216;s scale-9 grid (<code>scaleGridLeft</code>, <code>scaleGridTop</code>, etc.) so it expands and contracts correctly</li>
<li>The close button&#8217;s <b>x</b> is drawn as a <b>+</b> around the coordinate origin, also known as (0,0), and then rotated 45 degrees</li>
</ul>
<p class="bottom">Here&#8217;s the finished product (view source enabled):</p>
<div id="flashcontent-terrifictabbar">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<p>Use it and enjoy.</p>
<p><b>UPDATE:</b> <a href="http://gorillajawn.com/">Jon Rose</a> has written an excellent <a href="http://www.infoq.com/articles/flexmonkey-deep-dive">FlexMonkey Deep Dive</a> article on how to bring a custom component under automation.  He uses TerrificTabBar as an example.  Thanks Jon.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex4/terrific_tab_bar/TerrificTabBarSample.html">TerrificTabBar</a> (<a href="http://saturnboy.com/proj/flex4/terrific_tab_bar/srcview/TerrificTabBar.zip">download</a>)</li>
</ul>
<div>
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex4/terrific_tab_bar/TerrificTabBarSample.swf', 'flashcontent-terrifictabbar', '500', '64', '10.0.0', 'playerProductInstall.swf', false, { bgColor:'#eeeeee', base:'.' });
</script>
</div>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/08/terrifictabbar-custom-component/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>SuperTextInput &#8211; Building a Custom Component in Flex 4</title>
		<link>http://saturnboy.com/2010/07/supertextinput-building-a-custom-component/</link>
		<comments>http://saturnboy.com/2010/07/supertextinput-building-a-custom-component/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 18:59:29 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[custom component]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[skinning]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1489</guid>
		<description><![CDATA[I&#8217;ve been building a lot of Flex 4 custom components lately, including a sliding drawer, a multiple content area container, and now SuperTextInput. Nor will this be that last, because I think I have a few more in me (update: see TerrificTabBar). I thought it would be useful to spend some time in the details, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been building a lot of Flex 4 custom components lately, including a <a href="http://saturnboy.com/2010/06/drawer-component-flex-4/">sliding drawer</a>, a <a href="http://saturnboy.com/2010/07/multiple-content-area-containers/">multiple content area container</a>, and now SuperTextInput.  Nor will this be that last, because I think I have a few more in me (<b>update</b>: see <a href="http://saturnboy.com/2010/08/terrifictabbar-custom-component/">TerrificTabBar</a>).  I thought it would be useful to spend some time in the details, explaining <i>The Flex 4 Way</i> and how I try to walk the path.</p>
<p>SuperTextInput is a prompting, clearable <code>TextInput</code> extension in Flex 4.  It&#8217;s just an enhanced version of the default <code>TextInput</code> control, and as such, it follows a fairly standard pattern of custom component creation.</p>
<h3>Enhanced Component Pattern</h3>
<p>It&#8217;s almost too stupid to call this a pattern, but it&#8217;s <b>so</b> common in custom component creation that I&#8217;ll run with it.  Also, I&#8217;ve found it to be worthwhile to distinguish between adding new functionality to a component already present in the framework (aka an enhanced component) versus creating a truly custom component.</p>
<p class="bottom">The enhanced component pattern is just two simple steps:</p>
<ol>
<li><b>Extend</b> &ndash; extend some default component and add some new functionality</li>
<li><b>Skin</b> &ndash; make it look good</li>
</ol>
<p>In my version of reality, these steps carry equal weight, because almost all worthwhile functionality in Flex touches the UI in some fashion, so the design and UX (the look-and-feel, it&#8217;s usability, the integration into the rest of the app, etc.) are critical. Don&#8217;t forget or skimp on step #2 because it&#8217;s all the client, team, customer ever sees.</p>
<h3>A Prompting TextInput</h3>
<p>Since SuperTextInput has two new pieces of functionality (the prompt and the clear button), I&#8217;ll split them apart, and consider each part separately.  First, the prompt is merely the text you see when the <code>TextInput</code> is empty.  It often becomes a space saving label, because it can be used to tell the user what goes into the <code>TextInput</code> without costing the UI any screen real estate.</p>
<p>Thinking more about the prompt, we want the prompt text to be visible initially, but it should disappear when the user clicks (or tabs) to the control, and only returns when the control loses focus and is still empty.  So this tells us that we need to communicate both the prompt text and it&#8217;s visibility to our skin.  The prompt text can just be a simple <code>Label</code> <code>SkinPart</code>, but it&#8217;s visibility is complicated enough that it makes sense to add a new <code>prompting</code> <code>SkinState</code>.</p>
<p class="bottom">Here&#8217;s a functioning <code>PromptingTextInput</code> custom component (which is simply the prompting code lifted from <code>SuperTextInput.as</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package components <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">FocusEvent</span>;
    <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">events</span>.<span style="color: #006600;">FlexEvent</span>;
    <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #006600;">Label</span>;
    <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #006600;">TextInput</span>;
    <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">events</span>.<span style="color: #006600;">TextOperationEvent</span>;
&nbsp;
    <span style="color: #66cc66;">&#91;</span>SkinState<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;prompting&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PromptingTextInput <span style="color: #0066CC;">extends</span> TextInput <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #66cc66;">&#91;</span>SkinPart<span style="color: #66cc66;">&#40;</span>required=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> promptDisplay:Label;
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _prompt:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">''</span>;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _focused:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PromptingTextInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">//watch for programmatic changes to text property</span>
            <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>FlexEvent.<span style="color: #006600;">VALUE_COMMIT</span>, textChangedHandler, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">//watch for user changes (aka typing) to text property</span>
            <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TextOperationEvent.<span style="color: #006600;">CHANGE</span>, textChangedHandler, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> prompt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> _prompt;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> prompt<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_prompt <span style="color: #66cc66;">!</span>= value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                _prompt = value;
                <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>promptDisplay <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    promptDisplay.<span style="color: #0066CC;">text</span> = value;
                <span style="color: #66cc66;">&#125;</span>
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> textChangedHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            invalidateSkinState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override protected <span style="color: #000000; font-weight: bold;">function</span> focusInHandler<span style="color: #66cc66;">&#40;</span>event:FocusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">focusInHandler</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
            _focused = <span style="color: #000000; font-weight: bold;">true</span>;
            invalidateSkinState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
        override protected <span style="color: #000000; font-weight: bold;">function</span> focusOutHandler<span style="color: #66cc66;">&#40;</span>event:FocusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">focusOutHandler</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
            _focused = <span style="color: #000000; font-weight: bold;">false</span>;
            invalidateSkinState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override protected <span style="color: #000000; font-weight: bold;">function</span> partAdded<span style="color: #66cc66;">&#40;</span>partName:<span style="color: #0066CC;">String</span>, instance:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">partAdded</span><span style="color: #66cc66;">&#40;</span>partName, instance<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == promptDisplay<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                promptDisplay.<span style="color: #0066CC;">text</span> = prompt;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override protected <span style="color: #000000; font-weight: bold;">function</span> getCurrentSkinState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>prompt.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">length</span> == <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">!</span>_focused<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">'prompting'</span>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">super</span>.<span style="color: #006600;">getCurrentSkinState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In addition to the <code>promptDisplay</code> <code>SkinPart</code> and the new <code>prompting</code> <code>SkinState</code>, there is a lot of other stuff going on in the above code.  First, as is typical with data-driven <code>SkinPart</code>s, we back the <code>promptDisplay</code> with a good old <code>prompt</code> property.  The net is the fairly common pattern of: check if the <code>SkinPart</code> is not null, then do something to it.  So in the <code>prompt</code> setter, we assign the incoming value to the private <code>_prompt</code> variable, then check if <code>promptDisplay</code> is available and if yes, set it&#8217;s <code>text</code> property.  The setter does the job of updating the prompt, but only once everything is happily running.  In order to get the data to the skin initially, we must use the <code>partAdded()</code> override to pass the local prompt to the <code>promptDisplay</code>&#8216;s <code>text</code> property.  And that&#8217;s it for the prompt text.</p>
<p>The prompt visibility part requires lots of event watching, and also <code>SkinState</code> stuff because we made the choice to push visibility via the <code>prompting</code> <code>SkinState</code>.  First, we wire up both the programmatic text change events and the user text change events to a handler, <code>textChangedHandler()</code>, that does nothing more than invalidate the state.  <code>TextInput</code> change events are a <a href="http://stackoverflow.com/questions/283497/capturing-user-input-from-flex-textinput-control-which-event-to-use">little wacky</a>, but the code works fine.  Next, instead of wiring the focus events to another handler (as seen in this <a href="http://www.andymcintosh.com/?p=207">prompting <code>TextInput</code> component</a> by Andy McIntosh), we simply override the protected handlers in the parent and add our focus-tracking logic directly.  Finally, we override <code>getCurrentSkinState()</code> to do the work of figuring out whether or not the prompt should be displayed.</p>
<p class="bottom">A skin for <code>PromptingTextInput</code> is now trivial because our component does the work of pushing the important information to the skin.  If we ignore all the pretty stuff, the skin is very simple:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:SparkSkin</span> ...<span style="color: #7400FF;">&gt;</span></span>
    ...
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:states</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;normal&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;prompting&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;disabled&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:states</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:RichEditableText</span> id=<span style="color: #ff0000;">&quot;textDisplay&quot;</span> ... <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> id=<span style="color: #ff0000;">&quot;promptDisplay&quot;</span> includeIn=<span style="color: #ff0000;">&quot;prompting&quot;</span> ... <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:SparkSkin</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>We add the <code>prompting</code> <code>State</code> to the list of states and also add the <code>promptDisplay</code> <code>Label</code> component.  By using the standard inline state syntax, <code>includeIn=&quot;prompting&quot;</code> our <code>Label</code> is shown only in the <code>prompting</code> state.</p>
<h3>A Clearable TextInput</h3>
<p>The second piece of SuperTextInput functionality is the clear button.  The clear button appears when the <code>TextInput</code> has a value, and when clicked, it clears that value (which re-displays the prompt).  Again, there are two pieces of information the need to be communicated to the skin to create the clear button functionality: the button itself and it&#8217;s visibility.  In this case, since the visibility is so simple (on if <code>TextInput</code> has a value, otherwise off), we&#8217;ll just punt and manage it directly in the component.  Therefore, the only a <code>Button</code> <code>SkinPart</code> for the clear button will be pushed to the skin.</p>
<p class="bottom">Here&#8217;s a functioning <code>ClearableTextInput</code> custom component (which is simply the clear button code lifted from <code>SuperTextInput.as</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package components <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
    <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">events</span>.<span style="color: #006600;">FlexEvent</span>;
    <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #0066CC;">Button</span>;
    <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #006600;">TextInput</span>;
    <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">events</span>.<span style="color: #006600;">TextOperationEvent</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ClearableTextInput <span style="color: #0066CC;">extends</span> TextInput <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #66cc66;">&#91;</span>SkinPart<span style="color: #66cc66;">&#40;</span>required=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> clearButton:<span style="color: #0066CC;">Button</span>;
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ClearableTextInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">//watch for programmatic changes to text property</span>
            <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>FlexEvent.<span style="color: #006600;">VALUE_COMMIT</span>, textChangedHandler, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">//watch for user changes (aka typing) to text property</span>
            <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TextOperationEvent.<span style="color: #006600;">CHANGE</span>, textChangedHandler, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> textChangedHandler<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>clearButton<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                clearButton.<span style="color: #0066CC;">visible</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> clearClick<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">''</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override protected <span style="color: #000000; font-weight: bold;">function</span> partAdded<span style="color: #66cc66;">&#40;</span>partName:<span style="color: #0066CC;">String</span>, instance:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">partAdded</span><span style="color: #66cc66;">&#40;</span>partName, instance<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == clearButton<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                clearButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, clearClick<span style="color: #66cc66;">&#41;</span>;
                clearButton.<span style="color: #0066CC;">visible</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">text</span> <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override protected <span style="color: #000000; font-weight: bold;">function</span> partRemoved<span style="color: #66cc66;">&#40;</span>partName:<span style="color: #0066CC;">String</span>, instance:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">partRemoved</span><span style="color: #66cc66;">&#40;</span>partName, instance<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == clearButton<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                clearButton.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, clearClick<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>After the <code>PromptingTextInput</code>, the <code>ClearableTextInput</code> is a little more straightforward.  First, we have the <code>clearButton</code> <code>SkinPart</code> and it&#8217;s <code>clearClick()</code> event handler.  Wiring the handler function to the button is done in the <code>partAdded()</code> override, and un-wiring in the <code>partRemoved()</code> override.  Next, button visibility is managed by watching for both programmatic text change events and user text change events.  The handler, <code>textChangedHandler()</code>, sets the button as visible when the control has text in it.</p>
<p>As I mentioned above, I decided against pushing the <code>clearButton</code>&#8216;s visibility down to the skin via a <code>SkinState</code>, and instead chose to manage it inside the component by setting <code>clearButton.visible</code> directly.  I tend to favor the <code>SkinState</code> method when more than one thing needs to change in the skin or if I need advanced visuals (like transitions).  If I need to do just one thing and I don&#8217;t care about visuals, I&#8217;ll do it inside the component.  The two examples here aren&#8217;t the best to illustrate the two options, but that&#8217;s my general thought process when building a custom component and custom skin.</p>
<p class="bottom">A skin for <code>ClearingTextInput</code> is super trivial.  Again, ignoring all the pretty stuff, the skin is:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:SparkSkin</span> ...<span style="color: #7400FF;">&gt;</span></span>
    ...
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:states</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;normal&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;disabled&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:states</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:RichEditableText</span> id=<span style="color: #ff0000;">&quot;textDisplay&quot;</span> ... <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> id=<span style="color: #ff0000;">&quot;clearButton&quot;</span> ... <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:SparkSkin</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>Just add the <code>clearButton</code> <code>Button</code> and position it.</p>
<h3>Fusion, Glorious Fusion</h3>
<p>The fusion process of creating <code>SuperTextInput</code> from <code>PromptingTextInput</code> and <code>ClearableTextInput</code> is nothing more than copy and paste.  <code>SuperTextInput</code> has lots of uses, but my favorite is to use it to capture text input to filter a list.  It also works great as a search box, or in any smart form UI.  Enjoy.</p>
<p class="bottom">Here&#8217;s the finished product showing all three custom components skinned and ready for action (view source enabled):</p>
<div id="flashcontent-supertextinput">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex4/super_text_input/SuperTextInput.html">SuperTextInput</a> (<a href="http://saturnboy.com/proj/flex4/super_text_input/srcview/SuperTextInput.zip">download</a>)</li>
</ul>
<div>
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex4/super_text_input/SuperTextInput.swf', 'flashcontent-supertextinput', '240', '94', '10.0.0', 'playerProductInstall.swf', false, { bgColor:'#ffffff', base:'.' });
</script>
</div>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/07/supertextinput-building-a-custom-component/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Building Flex 4 Containers with Multiple Content Areas</title>
		<link>http://saturnboy.com/2010/07/multiple-content-area-containers/</link>
		<comments>http://saturnboy.com/2010/07/multiple-content-area-containers/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 22:48:18 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[custom component]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[skinning]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1454</guid>
		<description><![CDATA[Back in the days of Flex 3, if you wanted multiple content areas in your main application, you&#8217;d need to arrange some set of containers (Canvas, HBox, VBox) in the app and fill them with content. It was just your basic Flex 3 development process. The danger, of course, is that you are mixing content [...]]]></description>
			<content:encoded><![CDATA[<p>Back in the days of Flex 3, if you wanted multiple content areas in your main application, you&#8217;d need to arrange some set of containers (<code>Canvas</code>, <code>HBox</code>, <code>VBox</code>) in the app and fill them with content.  It was just your basic Flex 3 development process.  The danger, of course, is that you are mixing content with presentation, aka bad separation of concerns.  Today, with the power of Flex 4 skins, we can avoid this issue by moving the presentation layer into a skin (or set of skins).  And thus, we can do a much better job achieving a happy level of separation of concerns.</p>
<h3>The Flex 3 Way</h3>
<p class="bottom">To give a concrete example, I&#8217;ll build a blog layout (yes, another blog layout) with a header, footer, sidebar, and main content areas.  But before we get started, let&#8217;s review the old Flex 3 way:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:HBox</span> id=<span style="color: #ff0000;">&quot;header&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Image</span> source=<span style="color: #ff0000;">&quot;@Embed('assets/logo.png')&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:HBox</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Canvas</span> id=<span style="color: #ff0000;">&quot;body&quot;</span> width=<span style="color: #ff0000;">&quot;800&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Text</span> text=<span style="color: #ff0000;">&quot;main content&quot;</span> width=<span style="color: #ff0000;">&quot;600&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:VBox</span> id=<span style="color: #ff0000;">&quot;sidebar&quot;</span> x=<span style="color: #ff0000;">&quot;600&quot;</span> width=<span style="color: #ff0000;">&quot;200&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Text</span> text=<span style="color: #ff0000;">&quot;Sidebar&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Text</span> text=<span style="color: #ff0000;">&quot;sidebar content&quot;</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:VBox</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Canvas</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:VBox</span> id=<span style="color: #ff0000;">&quot;footer&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Text</span> text=<span style="color: #ff0000;">&quot;2010 saturnboy&quot;</span> styleName=<span style="color: #ff0000;">&quot;footer&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:VBox</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>The above code comes from a previous post, <a href="http://saturnboy.com/2009/02/designing-in-flex/">Designing in Flex 3</a>, but has been modified to make sense here.  You&#8217;ve got you basic blog design: a box for the header, footer, and body, where body is subsequently is divided into a main content area and a sidebar.</p>
<h3>The 3-in-4 Way, aka The Wrong Way</h3>
<p>The unfortunate next step in a Flex developer&#8217;s evolution is what I like to call the Flex 3-in-4 way.  This is a the way of neanderthals,  which is to say, it is an evolutionary dead end.  If you ever have the bad luck to see 3-in-4 code, you can be sure you are dealing with a novice Flex 4 developer.  In general, the 3-in-4 way consists of making the simple transcription: <code>Canvas</code> &rarr; <code>Group</code>, <code>HBox</code> &rarr; <code>HGroup</code>, <code>VBox</code> &rarr; <code>VGroup</code>.  But the most damning tipoff of a 3-in-4 developer is the assertion that one is now a Flex 4 developer and the learning curve wasn&#8217;t all that bad.  While I do think Flex 4 is more of an evolutionary release than a revolutionary release, it&#8217;s different enough.  And it is particularly different on the design side of the framework, how it handles skins, layout, etc.</p>
<p class="bottom">If we just transcribe the above example, we get some classic 3-in-4 code:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:HGroup</span> id=<span style="color: #ff0000;">&quot;header&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> text=<span style="color: #ff0000;">&quot;Multi Content Area Example&quot;</span> styleName=<span style="color: #ff0000;">&quot;header&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:HGroup</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Group</span> id=<span style="color: #ff0000;">&quot;body&quot;</span> width=<span style="color: #ff0000;">&quot;800&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> text=<span style="color: #ff0000;">&quot;main content&quot;</span> width=<span style="color: #ff0000;">&quot;600&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:VGroup</span> x=<span style="color: #ff0000;">&quot;600&quot;</span> width=<span style="color: #ff0000;">&quot;200&quot;</span> styleName=<span style="color: #ff0000;">&quot;sidebarBox&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> text=<span style="color: #ff0000;">&quot;Sidebar&quot;</span> styleName=<span style="color: #ff0000;">&quot;title&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> text=<span style="color: #ff0000;">&quot;sidebar content&quot;</span> styleName=<span style="color: #ff0000;">&quot;sidebar&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:VGroup</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Group</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:VGroup</span> id=<span style="color: #ff0000;">&quot;footer&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> text=<span style="color: #ff0000;">&quot;2010 saturnboy&quot;</span> styleName=<span style="color: #ff0000;">&quot;footer&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:VGroup</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>*Barf*, please <b>not</b> do this.  This code has all the same issues as the Flex 3 code in the first example, and moreover it is a slap in the face of <i>The Flex 4 Way</i> and all of its improvements.</p>
<h3>The Flex 4 Way</h3>
<p>Yes, there is a Flex 4 Way and it looks like this.</p>
<p class="bottom">First, we rewrite the main app using a custom container.  Ignoring the specifics of the custom container for a moment, here is the re-written main app (minus some clutter):</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;containers:headerContent</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> text=<span style="color: #ff0000;">&quot;Multi Content Area Example&quot;</span> styleName=<span style="color: #ff0000;">&quot;header&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/containers:headerContent</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;containers:sidebarContent</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:RichText</span> left=<span style="color: #ff0000;">&quot;0&quot;</span> right=<span style="color: #ff0000;">&quot;0&quot;</span> styleName=<span style="color: #ff0000;">&quot;sidebar&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:content</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:p</span> fontSize=<span style="color: #ff0000;">&quot;20&quot;</span><span style="color: #7400FF;">&gt;</span></span>Sidebar<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:p</span><span style="color: #7400FF;">&gt;</span></span>
            <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:p</span><span style="color: #7400FF;">&gt;</span></span>sidebar content<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:p</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:content</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:RichText</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/containers:sidebarContent</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;containers:footerContent</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> text=<span style="color: #ff0000;">&quot;2010 saturnboy&quot;</span> styleName=<span style="color: #ff0000;">&quot;footer&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/containers:footerContent</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:RichText</span> left=<span style="color: #ff0000;">&quot;0&quot;</span> right=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:content</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:p</span> fontSize=<span style="color: #ff0000;">&quot;30&quot;</span><span style="color: #7400FF;">&gt;</span></span>Content<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:p</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:p</span><span style="color: #7400FF;">&gt;</span></span>main content<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:p</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:content</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:RichText</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>As you can see, the main app is now a nice set of semantic buckets, one for each of the content areas.  Header stuff goes in the <code>headerContent</code> bucket, footer stuff goes in the <code>footerContent</code> bucket, etc.</p>
<h3>Building a Multi Content Area Container</h3>
<p>Second, we need to create a custom container with the nice set of semantic buckets used in the above code.  This is achieved by following a straightforward formula:</p>
<ol>
<li><b>Extend SkinnableContainer</b> &ndash; Extend <code>SkinnableContainer</code> or some child class.  In our sample app, our custom container extends <code>Application</code> (which extends <code>SkinnableContainer</code>).</li>
<li><b>Add Buckets</b> &ndash; add some content buckets (in the form of <i>xxx</i><code>Content</code>) as <code>Array</code>s.  These become the MXML tags used to bucket components together.  Each content bucket has a public getter, but most importantly a public setter that accepts an incoming <code>Array</code> of <code>IVisualElement</code>s and uses the magical <code>mxmlContent</code> property to assign it to the associated <code>SkinPart</code>.</li>
<li><b>Add SkinParts</b> &ndash; add some matching SkinParts (in the form of <i>xxx</i><code>Group</code>) as spark <code>Group</code>s.  There are used in the custom skin to display the content.  Also, I usually set <code>required=&quot;false&quot;</code> to make everything optional.</li>
<li><b>Add partAdded() &amp; partRemoved()</b> &ndash; override the new Flex 4 skinning lifecycle methods to wire the incoming content to the outgoing <code>SkinPart</code>.</li>
</ol>
<p class="bottom">The custom component code is actually easier to follow then the description.  Here is a custom container with only one additional content bucket, <code>sidebarContent</code>, and its matching <code>SkinPart</code>, <code>sidebarGroup</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package containers <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #006600;">Group</span>;
    <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #006600;">Application</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainApp <span style="color: #0066CC;">extends</span> Application <span style="color: #66cc66;">&#123;</span>
        <span style="color: #66cc66;">&#91;</span>SkinPart<span style="color: #66cc66;">&#40;</span>required=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> sidebarGroup:Group;
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _sidebarContent:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MainApp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #66cc66;">&#91;</span>ArrayElementType<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mx.core.IVisualElement&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> sidebarContent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> _sidebarContent;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> sidebarContent<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            _sidebarContent = value;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>sidebarGroup<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                sidebarGroup.<span style="color: #006600;">mxmlContent</span> = value;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override protected <span style="color: #000000; font-weight: bold;">function</span> partAdded<span style="color: #66cc66;">&#40;</span>partName:<span style="color: #0066CC;">String</span>, instance:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">partAdded</span><span style="color: #66cc66;">&#40;</span>partName, instance<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == sidebarGroup<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                sidebarGroup.<span style="color: #006600;">mxmlContent</span> = _sidebarContent;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override protected <span style="color: #000000; font-weight: bold;">function</span> partRemoved<span style="color: #66cc66;">&#40;</span>partName:<span style="color: #0066CC;">String</span>, instance:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">super</span>.<span style="color: #006600;">partRemoved</span><span style="color: #66cc66;">&#40;</span>partName, instance<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == sidebarGroup<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                sidebarGroup.<span style="color: #006600;">mxmlContent</span> = <span style="color: #000000; font-weight: bold;">null</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Following the four steps: we extend <code>Application</code>, have a <code>sidebarContent</code> bucket and its associated <code>sidebarGroup</code> <code>SkinPart</code>, and override <code>partAdded()</code> and <code>partRemoved()</code> to wire everything together.</p>
<h3>Skinning a Multi Content Area Container</h3>
<p>Skinning in Flex 4 is awesome, and like everyone says, it&#8217;s easily one of the best new features in the framework.  While I find the skinning process fairly straightforward, I would never call it trivial, mostly due to the depth and flexibility of the skinning system.</p>
<p>We need a custom skin for our custom multi content area component.  This is probably the 10% case for skinning, but it&#8217;s also the coolest.  In my experience, an average Flex 4 app has many <code>Button</code> skins (like 10 or even 20), a few default component skins (skins for <code>List</code>, <code>DropDownList</code>, <code>TextInput</code>, etc.), and maybe only two or three skins for custom components.</p>
<p>The skin itself is nothing special.  To display our custom component&#8217;s <code>SkinPart</code>s, we simply include a <code>Group</code> with the matching <code>id</code> attribute.  For example, our skin will include a <code>&lt;s:Group id="sidebarGroup" /&gt;</code> to display the <code>sidebarGroup</code> <code>SkinPart</code>.  Just rinse, wash, repeat, to add all of our custom content areas in the container to the skin.</p>
<p class="bottom">Here is a trivial skin:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Skin</span> ... <span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Metadata</span><span style="color: #7400FF;">&gt;</span></span>
        [HostComponent(&quot;containers.MainApp&quot;)]
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Metadata</span><span style="color: #7400FF;">&gt;</span></span> 
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:states</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;normal&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;disabled&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:states</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:VGroup</span> left=<span style="color: #ff0000;">&quot;40&quot;</span> right=<span style="color: #ff0000;">&quot;40&quot;</span> top=<span style="color: #ff0000;">&quot;40&quot;</span> bottom=<span style="color: #ff0000;">&quot;40&quot;</span> gap=<span style="color: #ff0000;">&quot;20&quot;</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Group</span> id=<span style="color: #ff0000;">&quot;headerGroup&quot;</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Group</span> id=<span style="color: #ff0000;">&quot;contentGroup&quot;</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Group</span> id=<span style="color: #ff0000;">&quot;sidebarGroup&quot;</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Group</span> id=<span style="color: #ff0000;">&quot;footerGroup&quot;</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:VGroup</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Skin</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>In this trivial skin, we just shove all the content groups (including <code>SkinnableContainer</code>&#8216;s default <code>Group</code>, <code>contentGroup</code>) into a <code>VGroup</code>.  Also note, we correctly set <code>HostComponent</code> to our custom container.  If you are thinking, &quot;Hey, this skin looks similar to the Flex 3 and 3-in-4 example code, just minus the content&quot; that&#8217;s exactly the point.</p>
<p class="bottom">Lastly, we wire out skin to our custom component via CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">containers|MainApp <span style="color: #00AA00;">&#123;</span>
    skinClass<span style="color: #00AA00;">:</span>ClassReference<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'skins.TrivialAppSkin'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Using <code>skinClass</code> to wire a skin to a component is <b>so</b> 2009.  The sample app has its CSS inline, but in any real app I&#8217;ll always put this in an external file.</p>
<h3>Conclusion</h3>
<p>After this, there&#8217;s really not much more to say.  You can certainly create a more complicated arrangement of the multiple content areas by making a more complicated skin.  I&#8217;ve done exactly this in the final sample, which includes three different skins and a skin switcher (click 1, 2, or 3 to switch skins).</p>
<p>&raquo; <a href="http://saturnboy.com/proj/flex4/multi_content_area/MultiContentArea.html">view MultiConentArea sample</a> (view source enabled)</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex4/multi_content_area/MultiContentArea.html">MultiContentArea</a> (<a href="http://saturnboy.com/proj/flex4/multi_content_area/srcview/MultiContentArea.zip">download</a>)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/07/multiple-content-area-containers/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Drawer Component in Flex 4</title>
		<link>http://saturnboy.com/2010/06/drawer-component-flex-4/</link>
		<comments>http://saturnboy.com/2010/06/drawer-component-flex-4/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 04:31:09 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[custom component]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[flexlib]]></category>
		<category><![CDATA[skinning]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1418</guid>
		<description><![CDATA[I needed a good way to have a large settings panel with a minimal visual impact. The obvious answer is to hide or minimize or collapse the settings panel when not in use. I thought about using Flexlib&#8216;s WindowShade component (which I&#8217;ve dicussed in detail in Styling Flexlib&#8217;s WindowShade), but why reuse something when you [...]]]></description>
			<content:encoded><![CDATA[<p>I needed a good way to have a large settings panel with a minimal visual impact.  The obvious answer is to hide or minimize or collapse the settings panel when not in use.  I thought about using <a href="http://code.google.com/p/flexlib/">Flexlib</a>&#8216;s WindowShade component (which I&#8217;ve dicussed in detail in <a href="http://saturnboy.com/2009/03/styling-flexlib-windowshade/">Styling Flexlib&#8217;s WindowShade</a>), but why reuse something when you can reinvent the wheel?  Plus, it always helps to hone my Flex 4 custom component kung-fu.  So, I chose to implement a simple sliding drawer component from scratch as a Flex 4 component.</p>
<p class="bottom">The Drawer component is a vanilla container (it actually extends <code>SkinnableContainer</code>), so it will happily take any spark component for its children.  Before I dive into my implementation, let&#8217;s check out the finished drawer component in action (view source enabled):</p>
<div id="flashcontent-drawer" style="background-color:#eee">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<p> Just click on the handle to open and close the drawer.</p>
<h3>Extending <code>SkinnableContainer</code></h3>
<p>The Drawer component itself, is just pure AS3, but the demo above uses a few MXML skins to achieve the desired look-and-feel.  This is a pretty standard pattern that I see during Flex 4 development, so expect it when you write your own custom components.</p>
<p class="bottom">We&#8217;ll review the component implementation in two steps.  First, we focus on the skin state management aspect of the drawer.  Here&#8217;s the relevant code (taken from <code>Drawer.as</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>SkinState<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;opened&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Drawer <span style="color: #0066CC;">extends</span> SkinnableContainer <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _opened:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> opened<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> _opened;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> opened<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_opened <span style="color: #66cc66;">!</span>= value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            _opened = value;
            invalidateSkinState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    override protected <span style="color: #000000; font-weight: bold;">function</span> getCurrentSkinState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>opened ? <span style="color: #ff0000;">'opened'</span> : <span style="color: #0066CC;">super</span>.<span style="color: #006600;">getCurrentSkinState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    ...
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The <code>Drawer</code> component can either be closed (the default) or opened.  To model theses states, we use the <code>normal</code> state from the superclass to represent the closed drawer, and add a new <code>opened</code> <code>SkinState</code> to represent the opened drawer.  We just expose a simple <code>opened</code> boolean property with a custom getter and setter, and then override the <code>getCurrentSkinState()</code> method.  It&#8217;s important to remember the states we are talking about are skin states, and not <i>component</i> states (see <a href="http://saturnboy.com/2009/09/flex4-component-states-skin-states/">Flex 4 Component States vs. Skin States</a> for the difference).</p>
<p class="bottom">Second, we focus on the action of opening and closing the drawer.  Here&#8217;s the relevant code (taken from <code>Drawer.as</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Drawer <span style="color: #0066CC;">extends</span> SkinnableContainer <span style="color: #66cc66;">&#123;</span>
    ...
    <span style="color: #66cc66;">&#91;</span>SkinPart<span style="color: #66cc66;">&#40;</span>required=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> openButton:<span style="color: #0066CC;">Button</span>;
&nbsp;
    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        opened = <span style="color: #66cc66;">!</span>opened;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    override protected <span style="color: #000000; font-weight: bold;">function</span> partAdded<span style="color: #66cc66;">&#40;</span>partName:<span style="color: #0066CC;">String</span>, instance:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">super</span>.<span style="color: #006600;">partAdded</span><span style="color: #66cc66;">&#40;</span>partName, instance<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == openButton<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            openButton.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, clickHandler<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    override protected <span style="color: #000000; font-weight: bold;">function</span> partRemoved<span style="color: #66cc66;">&#40;</span>partName:<span style="color: #0066CC;">String</span>, instance:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">super</span>.<span style="color: #006600;">partRemoved</span><span style="color: #66cc66;">&#40;</span>partName, instance<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == openButton<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            openButton.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, clickHandler<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The <code>Drawer</code> component includes a simple spark button, as an optional <code>SkinPart</code>, that is used to initiate the state change.  The <code>partAdded()</code> and <code>partRemoved()</code> methods are overridden to manage and adding and removing of the button&#8217;s click event handler.  And lastly, the <code>clickHandler()</code> method flips between skin states by toggling the <code>opened</code> boolean property.</p>
<h3>Usage</h3>
<p class="bottom">Using the <code>Drawer</code> is the same as any container.  In MXML, just put any child components you want between the container&#8217;s open and close tags:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;containers:Drawer</span> ... skinClass=<span style="color: #ff0000;">&quot;skins.DrawerSkin&quot;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- components go here --&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/containers:Drawer</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>Here we also apply the <code>DrawerSkin</code> to our container.</p>
<h3>Skins</h3>
<p class="bottom">The <code>DrawerSkin</code> is responsible for creating the desired look-and-feel and generally making the <code>Drawer</code> component look cool.  Here are the interesting parts of the skin:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Skin</span> ...<span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Metadata</span><span style="color: #7400FF;">&gt;</span></span>
        [HostComponent(&quot;containers.Drawer&quot;)]
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Metadata</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:states</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;normal&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;opened&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:State</span> name=<span style="color: #ff0000;">&quot;disabled&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:states</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
   ...
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> id=<span style="color: #ff0000;">&quot;openButton&quot;</span> ...</span>
<span style="color: #000000;">            skinClass=<span style="color: #ff0000;">&quot;skins.DrawerOpenButtonSkin&quot;</span></span>
<span style="color: #000000;">            skinClass.opened=<span style="color: #ff0000;">&quot;skins.DrawerCloseButtonSkin&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Group</span> id=<span style="color: #ff0000;">&quot;contentGroup&quot;</span> ... includeIn=<span style="color: #ff0000;">&quot;opened&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Skin</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>The <code>DrawerSkin</code> has three skin states, <code>normal</code> and <code>disabled</code> are inherited from <code>SkinnableContainer</code>, but <code>opened</code> is our custom skin state.  The skin also includes the optional <code>openButton</code> <code>SkinPart</code>, which itself uses two custom buttons skins, one for the open drawer and one for the closed drawer.  Lastly, note that the container&#8217;s content is only displayed when the skin is in the <code>opened</code> state via the newfangled inline state syntax: <code>includeIn=&quot;opened&quot;</code>.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex4/drawer/Drawer.html">Drawer</a> (<a href="http://saturnboy.com/proj/flex4/drawer/srcview/Drawer.zip">download</a>)</li>
</ul>
<div>
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex4/drawer/Drawer.swf', 'flashcontent-drawer', '520', '340', '10.0.0', 'playerProductInstall.swf', false, { bgColor:'#eeeeee', base:'.' });
</script>
</div>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/06/drawer-component-flex-4/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Managing Event Listeners in Flex 4</title>
		<link>http://saturnboy.com/2010/03/event-listeners-in-flex-4/</link>
		<comments>http://saturnboy.com/2010/03/event-listeners-in-flex-4/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 04:59:47 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[flex4]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1089</guid>
		<description><![CDATA[Event-driven programming is at the heart of Flex. It also lies close to the heart of insanity for the developer. When an application becomes too loosely coupled, things get painful fast. The perfect storm arrives when you marry an event-driven UI with an event-driven asynchronous backend. Better async testing becomes the least of your problems, [...]]]></description>
			<content:encoded><![CDATA[<p>Event-driven programming is at the heart of Flex.  It also lies close to the heart of insanity for the developer.  When an application becomes too loosely coupled, things get painful fast.  The perfect storm arrives when you marry an event-driven UI with an event-driven asynchronous backend.  <a href="http://saturnboy.com/2010/02/async-testing-with-flexunit4/">Better async testing</a> becomes the least of your problems, and you are constantly trying to figure out who is listening to what, when.</p>
<p>In these cases, I often find that some simple runtime management of event listeners can really help.  To begin, I start with some component whose listeners I want to manage, then I override the <code>addEventListener()</code> and <code>removeEventListener()</code> functions in that component to always keep track of who is actively listening.</p>
<p class="bottom">Here is the basic code that can be added to any component (actually any descendent of <code>EventDispatcher</code> which is basically everything in Flex):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _listeners:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addEventListener<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>, listener:<span style="color: #000000; font-weight: bold;">Function</span>, useCapture:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span>, priority:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>, useWeakReference:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">super</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, listener, useCapture, priority, useWeakReference<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_listeners.<span style="color: #006600;">hasOwnProperty</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #66cc66;">&#40;</span>_listeners<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#93;</span> as <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>listener<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
        _listeners<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#91;</span>listener<span style="color: #66cc66;">&#93;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> removeEventListener<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>, listener:<span style="color: #000000; font-weight: bold;">Function</span>, useCapture:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">super</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, listener, useCapture<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_listeners.<span style="color: #006600;">hasOwnProperty</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> listeners:<span style="color: #0066CC;">Array</span> = _listeners<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#93;</span> as <span style="color: #0066CC;">Array</span>;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>listeners.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">delete</span> _listeners<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#93;</span>;
        <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">var</span> idx:<span style="color: #0066CC;">int</span> = listeners.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span>listener<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>idx <span style="color: #66cc66;">!</span>= -<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                listeners.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span>idx,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>We just store all listeners of a given event type in an array, and store the array as the values in a hash keyed by event type.  When a new listener is added via <code>addEventListener()</code>, we check if any listeners of that event type exist.  If yes, we push the new listener onto the array.  If no, we create a new one-item array containing just the new listener.  The tracking process for <code>removeEventListener()</code> is just the opposite.</p>
<p class="bottom">Once we have all the listener bookkeeping in place, we can add any type of event listener management that we want.  For example, he&#8217;s a <code>removeAllEventListeners()</code> function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> removeAllEventListeners<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>.<span style="color: #0066CC;">length</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> t:<span style="color: #0066CC;">String</span> <span style="color: #b1b100;">in</span> _listeners<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            removeAllEventListeners<span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_listeners.<span style="color: #006600;">hasOwnProperty</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>hasEventListener<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> listener:<span style="color: #000000; font-weight: bold;">Function</span> <span style="color: #b1b100;">in</span> _listeners<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                removeEventListener<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, listener<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Nothing cosmic, as <a href="http://stu-stern.blogspot.com/">my boss</a> likes to say, just walk all the listeners of a given event type and remove them.  If the event type is blank, just use some dirty recursion to walk all the types to remove everything.</p>
<p class="bottom">Here is a sample application which is tracking all the listeners on a <code>Panel</code> component (view source enabled):</p>
<div id="flashcontent-listener-tracker">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<p>You can add/remove a custom listener for the <i>foo</i> custom event type using the buttons.  Or hit <i>remove all</i> to clear everything (including the listeners attached by default by the Flex framework).</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex4/listener_tracker/ListenerTracker.html">ListenerTracker</a> (<a href="http://saturnboy.com/proj/flex4/listener_tracker/srcview/ListenerTracker.zip">download</a>)</li>
</ul>
<div>
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex4/listener_tracker/ListenerTracker.swf', 'flashcontent-listener-tracker', '320', '150', '10.0.0', 'playerProductInstall.swf', false, { bgColor:'#ffffff', base:'.' });
</script>
</div>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/03/event-listeners-in-flex-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Faking Transaction in LCDS 3</title>
		<link>http://saturnboy.com/2010/02/faking-transaction-in-lcds-3/</link>
		<comments>http://saturnboy.com/2010/02/faking-transaction-in-lcds-3/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 11:58:09 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[LCDS]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1000</guid>
		<description><![CDATA[Alas, LCDS 3 doesn&#8217;t support the notion of transaction (in the sense of a Database Transaction). I assume that somewhere deep within the system transactions are used to ensure correctness of data in the db, but none of this is exposed to the user. Fortunately, it is possible to bundle up a bunch of updates [...]]]></description>
			<content:encoded><![CDATA[<p>Alas, <a href="http://www.adobe.com/products/livecycle/dataservices/">LCDS 3</a> doesn&#8217;t support the notion of transaction (in the sense of a <a href="http://en.wikipedia.org/wiki/Database_transaction">Database Transaction</a>). I assume that somewhere deep within the system transactions are used to ensure correctness of data in the db, but none of this is exposed to the user. Fortunately, it is possible to bundle up a bunch of updates on the client and push them all at once to the server. <i>Fake transactions</i>, as I like to call them, are quite useful.  But they can also be dangerous because they are not really transactions (hence the <i>fake</i> part), and don&#8217;t always work as you would hope.</p>
<h5>Fake It</h5>
<p>Faking a tranaction is done using the <code>autoCommit</code> property on a generated service.  According to the <a href="http://help.adobe.com/en_US/LiveCycleDataServicesES/3.0/Developing/index.html">docs</a>, setting <code>autoCommit</code> to <code>false</code> blocks Data Management from pushing any changes to the server until <code>commit()</code> is called manually.</p>
<p class="bottom">Here&#8217;s a <a href="http://help.adobe.com/en_US/LiveCycleDataServicesES/3.0/Developing/WSc3ff6d0ea77859461172e0811f00f7045b-7f83.html#WSc3ff6d0ea77859461172e0811f00f6f23a-7ff7">direct quote</a>:</p>
<blockquote><p>&#8220;&#8230;set a <code>DataService</code> component <code>autoCommit</code> property to false to allow only manual calls to the <code>commit()</code> method. It is important to set <code>autoCommit</code> to <code>false</code> when you are going to make more than one change&#8230;so that the <code>DataService</code> component can batch those changes and send them in one batch to the destination.&#8221;</p></blockquote>
<p class="bottom">So to make life easy, I made a simple static function that does the <code>autoCommit</code> gymnastics:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> fakeIt<span style="color: #66cc66;">&#40;</span>service:DataService, func:<span style="color: #000000; font-weight: bold;">Function</span><span style="color: #66cc66;">&#41;</span>:AsyncToken <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>service.<span style="color: #006600;">autoCommit</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ERROR: autoCommit is already off.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>service.<span style="color: #006600;">commitRequired</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ERROR: another transaction is already open.&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    service.<span style="color: #006600;">autoCommit</span> = <span style="color: #000000; font-weight: bold;">false</span>;
    func<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken = service.<span style="color: #006600;">commit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    service.<span style="color: #006600;">autoCommit</span> = <span style="color: #000000; font-weight: bold;">true</span>;
    <span style="color: #b1b100;">return</span> token;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>After some error handling, we toggle <code>autoCommit</code> to false, call our function, then commit any updates, toggle <code>autoCommit</code> back on, and return the token.</p>
<h5>Usage</h5>
<p>Using our fake transaction function is straight forward, and typically involves passing in and inline anonymous function.</p>
<p class="bottom">Imagine our favorite example of teams and players, where each team has one-to-many players.  We might choose to perform a sequence of operations to add a new player to an existing team, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken = FakeTransaction.<span style="color: #006600;">fakeIt</span><span style="color: #66cc66;">&#40;</span>
    teamService.<span style="color: #006600;">serviceControl</span>,
    <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">//rename the team</span>
        team.<span style="color: #0066CC;">name</span> = <span style="color: #ff0000;">'Denver Nuggetz'</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">//create player</span>
        <span style="color: #000000; font-weight: bold;">var</span> p:Player = <span style="color: #000000; font-weight: bold;">new</span> Player<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        p.<span style="color: #0066CC;">name</span> = <span style="color: #ff0000;">'Carmelo Anthony'</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">//wire both side of relationship</span>
        p.<span style="color: #006600;">team</span> = team;
        team.<span style="color: #006600;">players</span>.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>p<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        playerService.<span style="color: #006600;">createPlayer</span><span style="color: #66cc66;">&#40;</span>p<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> AsyncResponder<span style="color: #66cc66;">&#40;</span>successHandler, faultHandler<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>In this case, the fake transaction is used to ensure the team is renamed and the player is added.  Both operations occur together, so it should not be possible to have the new player on the old team, or have the renamed team without the new player.</p>
<h5>Warning! Danger!</h5>
<p>Experience has shown me that fake transactions don&#8217;t always work as one would expect if they were real transactions. Sometimes, particularly when you are manipulating entities already under Data Management, it seems like operations are not really batched but instead executed one at a time.  Thankfully, it appears that LCDS is order preserving, and by that I mean it doesn&#8217;t magically re-order things.  LCDS always executes operations in the order they come in.  I can only recommend you test your application vigorously to ensure what you expect to happen actually happens (<a href="http://flexunit.org/">FlexUnit4</a> is pretty awesome for <a href="http://saturnboy.com/2010/02/async-testing-with-flexunit4/">testing async backends</a>). </p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/wp-content/uploads/2010/01/FakeTransaction.as">FakeTransaction.as</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/02/faking-transaction-in-lcds-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Like Peas and Carrots: LCDS 3 and UTF-8</title>
		<link>http://saturnboy.com/2010/02/lcds-and-utf-8/</link>
		<comments>http://saturnboy.com/2010/02/lcds-and-utf-8/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 17:54:32 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[LCDS]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[UTF-8]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=958</guid>
		<description><![CDATA[I just wrote about how to handle special characters in Flex 4 when written as HTML entities in MXML. Now I&#8217;ve moved my data with the special characters out of MXML and down into a MySQL database. Data access is provided by a vanilla LCDS 3 backend. I now have a very different problem than [...]]]></description>
			<content:encoded><![CDATA[<p>I just wrote about how to handle <a href="http://saturnboy.com/2010/01/special-characters-flex-4/">special characters in Flex 4</a> when written as HTML entities in MXML. Now I&#8217;ve moved my data with the special characters out of MXML and down into a MySQL database.  Data access is provided by a vanilla <a href="http://www.adobe.com/products/livecycle/dataservices/">LCDS 3</a> backend. I now have a very different problem than what I had before: How do I get UTF-8 data out of the database with LCDS and onto the display?</p>
<h5>MySQL and UTF-8</h5>
<p>In theory, LCDS is perfectly happy with special characters and foreign languages (here&#8217;s a link to <a href="http://help.adobe.com/en_US/livecycle/9.0/programLC/langref/charset-codes.html">supported characters sets</a> in LiveCycle ES2). So this time around, our problem has nothing to do with Flex 4 or LCDS, instead it&#8217;s all about the database. For our example, we&#8217;ll skip the pure model driven development route and just start with a simple database with a single <code>players</code> table.</p>
<p class="bottom">Create the database:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> ballerz <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_general_ci;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">USER</span> <span style="color: #ff0000;">'baller'</span>@<span style="color: #ff0000;">'localhost'</span> <span style="color: #993333; font-weight: bold;">IDENTIFIED</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">'password'</span>;
<span style="color: #993333; font-weight: bold;">GRANT</span> <span style="color: #993333; font-weight: bold;">ALL</span> PRIVILEGES <span style="color: #993333; font-weight: bold;">ON</span> ballerz<span style="color: #66cc66;">.*</span> <span style="color: #993333; font-weight: bold;">TO</span> baller@localhost;</pre></div></div>

<p>It is possible to configure MySQL to default to UTF-8 friendly behavior but the <code>CREATE DATABASE</code> command guarantees that the newly created db will be happy.</p>
<p class="bottom">Create the <code>players</code> table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> players <span style="color: #66cc66;">&#40;</span>
  id <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  name <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Nothing special here, just use <code>VARCHAR</code> for the text fields.  In this case, we only have the player&#8217;s name.</p>
<p class="bottom">Insert some sample data:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Carmelo Anthony&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Chaunçey Billups&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Nenê&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Këñÿõn Martin&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;LeBrøn James&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Mo Williams&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Shaquille O†Neal&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Ænderson Varejao&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> players <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;Zýdrunãs Ílgauskãs&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>I put some extra special characters into the <code>INSERT</code> statements just for fun.</p>
<blockquote class="deeper"><p><b>Digging Deeper:</b> When connecting to MySQL from the commandline you can use the <code>--default-character-set=utf8</code> option to force your terminal to show UTF-8 characters correctly.</p></blockquote>
<h5>LCDS and UTF-8</h5>
<p>Now that the database is correctly setup to handle UTF-8, the rest of the LCDS setup is straight forward (see my getting started <a href="http://saturnboy.com/2009/07/get-started-lcds-mysql-1/">part 1</a> and <a href="http://saturnboy.com/2009/07/get-started-lcds-mysql-2/">part 2</a> posts). Create a new LCDS webapp via copy-and-paste from the template app, then fire up Flash Builder 4 and get to work.  In the Modeler plugin, configure a new RDS connection and just drag-and-drop the players table into the model.</p>
<p class="bottom">Here&#8217;s a screenshot of our LCDS model:</p>
<div class="span-14 last" style="min-height:274px;">
<img src="http://saturnboy.com/wp-content/uploads/2010/01/lcds_utf8_model.png" alt="model" width="391" height="264" />
</div>
<p class="bottom">And here&#8217;s a screenshot of the running app (remember this is backed by LCDS, so no running demo):</p>
<div class="span-14 last" style="min-height:180px;">
<img src="http://saturnboy.com/wp-content/uploads/2010/01/lcds_utf8_screenshot.png" alt="screenshot" width="390" height="170" />
</div>
<p class="bottom">Lastly, the frontend code showing just the highlights:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Application</span> ...</span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;complete()&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">           private function complete<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">                getPlayers.token = playerService.getAll<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">            <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">        <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #7400FF;">&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:CallResponder</span> id=<span style="color: #ff0000;">&quot;getPlayers&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;Ballerz:PlayerService</span> id=<span style="color: #ff0000;">&quot;playerService&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> </span>
<span style="color: #000000;">            dataProvider=<span style="color: #ff0000;">&quot;{getPlayers.lastResult}&quot;</span></span>
<span style="color: #000000;">            labelField=<span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>Again, no magic here, I use a simple <code>getAll()</code> query to retrieve the entire <code>players</code> table then feed it into a <code>List</code> via the <code>CallResponder</code>&#8216;s <code>lastResult</code> property.</p>
<h5>Conclusion</h5>
<p>So the moral of our story is: if you correctly configure your database to support UTF-8 and you correctly get UTF-8 data into your tables, then everything just works.  LCDS will transparently get data out of the db and Flex will transparently get it onto the screen.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/lcds/ballerz/Ballerz.tgz">Ballerz.tgz</a></li>
<li><a href="http://saturnboy.com/proj/lcds/ballerz/create_db.sql">create_db.sql</a></li>
<li><a href="http://saturnboy.com/proj/lcds/ballerz/create_tables.sql">create_tables.sql</a></li>
<li><a href="http://saturnboy.com/proj/lcds/ballerz/data.sql">data.sql</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/02/lcds-and-utf-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

