<?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>Iwo Banaś - Flex Blog &#187; Panel</title>
	<atom:link href="http://www.iwobanas.com/category/reusable-components/panel/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.iwobanas.com</link>
	<description>Adobe Flex and AIR thoughts, custom components, how-to’s...</description>
	<lastBuildDate>Tue, 02 Feb 2010 22:54:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating Collapsible Panel in Flex 4</title>
		<link>http://www.iwobanas.com/2009/09/creating-collapsible-panel-in-flex-4/</link>
		<comments>http://www.iwobanas.com/2009/09/creating-collapsible-panel-in-flex-4/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 17:57:39 +0000</pubDate>
		<dc:creator>Iwo Banas</dc:creator>
				<category><![CDATA[Panel]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex 4]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[MXML]]></category>

		<guid isPermaLink="false">http://www.iwobanas.com/?p=230</guid>
		<description><![CDATA[It is quite common in Flex applications (and other RIAs) to divide screen content into two parts: the navigation/configuration panel on the left and the actual content on the right.
The examples of such apps are FlexStore sample and famous Style Explorer.
I followed this pattern in many projects and several times we come to the point [...]]]></description>
			<content:encoded><![CDATA[<p>It is quite common in Flex applications (and other RIAs) to divide screen content into two parts: the navigation/configuration panel on the left and the actual content on the right.<br />
The examples of such apps are <a href="http://examples.adobe.com/flex2/inproduct/sdk/flexstore/flexstore.html">FlexStore sample</a> and famous <a href="http://examples.adobe.com/flex3/consulting/styleexplorer/Flex3StyleExplorer.html#">Style Explorer</a>.</p>
<p>I followed this pattern in many projects and several times we come to the point that navigation/configuration panel requires too much space and that there should be a possibility to minimize it. Probably designers of Style Explorer has a similar idea since their navigation control can be minimized. Unfortunately they haven’t created reusable component to do this.<br />
<span id="more-230"></span></p>
<p>Some time ago I have created the collapsible panel in Flex 3. The idea of this component was to minimize panel by hiding panel content and rotating the header by 90 degree. So that, minimized panel was visible as vertical bar with the title. It was the nightmare to use rotation in Flex 3 since Flex 3 layouts doesn’t care about rotation. Placing component in the right place requires many tricks and extensive usage of trigonometric functions…</p>
<p>When I read that Gumbo (now Flex 4) layouts supports rotation I decided to test it by reimplementing my collapsible panel. Flex 4 definitely passed the test. Spark component architecture is really cool and it is much easier to create reusable/extendible components in Flex 4 than it was before. You can see the result of my work below.</p>
<p>This example is created using 4.0.0.8847, it will not work with SDK shipped with beta 1.<br />
<a title="Sources" href="/wp-content/uploads/collapsible_panel_01/srcview/index.html" target="_blank">View source</a> is enabled, you can download zipped sources from <a title="CollapsiblePanel sources" href="/wp-content/uploads/collapsible_panel_01/srcview/CollapsiblePanel.zip">here</a>.<br />
<iframe src="http://www.iwobanas.com/wp-content/uploads/collapsible_panel_01/CollapsiblePanelExample.html" width="100%" height="350" frameborder="no"></iframe><br />
Following <a href="http://opensource.adobe.com/wiki/display/flexsdk/Gumbo+Skinning">Spark components architecture</a> I have extended Panel class and created new MXML skin.</p>
<h3>Component class</h3>
<p>Below you can find the list of steps taken to create CollapsiblePanel main component class. At first sight this list may look long and complicated but after understanding Spark architecture it is really simple.</p>
<ul>
<li>Adding &#8220;collapsed&#8221; skin state
<pre>
[SkinState("collapsed")]
public class CollapsiblePanel extends Panel
{
</pre>
</li>
<li>Adding new <code>collapsed</code> field indicating if panel is collapsed (invalidating skin state in setter)
<pre>
public function get collapsed():Boolean
{
	return _collapsed;
}
public function set collapsed(value:Boolean):void
{
	_collapsed = value;
	invalidateSkinState();
}
protected var _collapsed:Boolean;
</pre>
</li>
<li>Overriding <code>getCurrentSkinState() </code>function to support &#8220;collapsed&#8221; skin state
<pre>
override protected function getCurrentSkinState():String
{
	return collapsed ? "collapsed" : super.getCurrentSkinState();
}
</pre>
</li>
<li>Adding <code>collapseButton</code> optional skin part
<pre>
[SkinPart(required="false")]
public var collapseButton:Button;
</pre>
</li>
<li>Implementing handler to toggle <code>collapsed</code> property on <code>collapseButton</code> click
<pre>
protected function collapseButtonClickHandler(event:MouseEvent):void
{
	collapsed = !collapsed;
}
</pre>
</li>
<li>Overriding <code>partAdded()/partRemoved()</code> functions to add <code>collapseButton</code> event handler
<pre>
override protected function partAdded(partName:String, instance:Object) : void
{
	super.partAdded(partName, instance);

	if (instance == collapseButton)
	{
		Button(instance).addEventListener(MouseEvent.CLICK, collapseButtonClickHandler);
	}
}
override protected function partRemoved(partName:String, instance:Object) : void
{
	if (instance == collapseButton)
	{
		Button(instance).removeEventListener(MouseEvent.CLICK, collapseButtonClickHandler);
	}
	super.partRemoved(partName, instance);
}
</pre>
</li>
</ul>
<h3>MXML Skin</h3>
<p>The CollapsiblePanelSkin.mxml skin is a copy of default Spark Panel skin with a number of modification. I used <a href="http://opensource.adobe.com/wiki/display/flexsdk/Enhanced+States+Syntax">enhanced styles syntax</a> to modify skin in &#8220;collapsed&#8221; state.</p>
<ul>
<li>Adding &#8220;collapsed&#8221; state
<pre>&lt;s:states&gt;
    &lt;s:State name="normal" /&gt;
    &lt;s:State name="collapsed" /&gt;
    &lt;s:State name="disabled" /&gt;
&lt;/s:states&gt;
</pre>
</li>
<li>Grouping all title bar layers inside one Group tag
<pre>
&lt;s:Group id="titleBarGroup" left="0" top="0" right="0" bottom="0"
     maxHeight="32" rotation.collapsed="90"&gt;
</pre>
</li>
<li>Adding collapse button
<pre>&lt;s:Button id="collapseButton" width="16" height="16" top="7" right="7"
    label="-" label.collapsed="+" toolTip="Collapse" toolTip.collapsed="Open" /&gt;
</pre>
</li>
<li>Excluding contentGroup from collapsed state
<pre>
&lt;s:Group id="contentGroup" left="1" right="1" top="32" bottom="1" minWidth="0" minHeight="0"
     visible.collapsed="false" excludeFrom="collapsed"&gt;
&lt;/s:Group&gt;
</pre>
</li>
<li>Adding transitions
<pre>
&lt;s:transitions&gt;
    &lt;s:Transition fromState="normal" toState="collapsed"&gt;
        &lt;s:Sequence&gt;
            &lt;s:Fade target="{contentGroup}" duration="250" /&gt;
            &lt;s:Parallel targets="{[titleBarGroup, this]}" &gt;
                &lt;s:Rotate target="{titleBarGroup}" duration="250" /&gt;
                &lt;s:Resize target="{this}" duration="250" easer="{collapseEaser}" /&gt;
            &lt;/s:Parallel&gt;
        &lt;/s:Sequence&gt;
    &lt;/s:Transition&gt;
    &lt;s:Transition fromState="collapsed" toState="normal"&gt;
        &lt;s:Sequence&gt;
            &lt;s:Parallel targets="{[titleBarGroup, this]}" &gt;
                &lt;s:Rotate target="{titleBarGroup}" duration="250" /&gt;
                &lt;s:Resize target="{this}" duration="250" easer="{uncollapseEaser}" /&gt;
            &lt;/s:Parallel&gt;
            &lt;s:Fade target="{contentGroup}" duration="250" /&gt;
        &lt;/s:Sequence&gt;
    &lt;/s:Transition&gt;
&lt;/s:transitions&gt;
</pre>
</li>
</ul>
<p>Summarizing,  creating CollapsiblePanel in Flex 4 was much easier than in Flex 3 and the resulting component is of much higher quality. Although understanding Spark components architecture and MXML 2009 may require some time it is definitely worth doing!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.iwobanas.com/2009/09/creating-collapsible-panel-in-flex-4/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
