<?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>Gaurav&#039;s Blog &#187; Modules</title>
	<atom:link href="http://gauravj.com/blog/tag/modules/feed/" rel="self" type="application/rss+xml" />
	<link>http://gauravj.com/blog</link>
	<description>Tips and Tools of Flex</description>
	<lastBuildDate>Thu, 18 Mar 2010 18:34:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Styles and popups with Flex 4</title>
		<link>http://gauravj.com/blog/2010/03/styles-and-popups-with-flex-4/</link>
		<comments>http://gauravj.com/blog/2010/03/styles-and-popups-with-flex-4/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 18:25:06 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[Flex Framework]]></category>
		<category><![CDATA[Alert]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[PopUpManager]]></category>
		<category><![CDATA[Styles]]></category>
		<category><![CDATA[TitleWindow]]></category>
		<category><![CDATA[ToolTipManager]]></category>

		<guid isPermaLink="false">http://gauravj.com/blog/?p=703</guid>
		<description><![CDATA[<p>If you use modules/sub apps and pop ups(Alert, ToolTip, TitleWindow, etc.) then you probably would be interested in some of the api changes that were introduced by the “Per-Module Style Management” feature in Flex 4. As I mentioned in my previous posts that StyleManager is no longer a singleton class and all modules and sub apps <span style="color:#777"> . . . &#8594; Read More: <a href="http://gauravj.com/blog/2010/03/styles-and-popups-with-flex-4/">Styles and popups with Flex 4</a></span>]]></description>
			<content:encoded><![CDATA[<p>If you use modules/sub apps and pop ups(Alert, ToolTip, TitleWindow, etc.) then you probably would be interested in some of the api changes that were introduced by the “Per-Module Style Management” feature in Flex 4. As I mentioned in my previous posts that StyleManager is no longer a singleton class and all modules and sub apps have their own instance of the Style Manager. So when you define a style in a module or sub app, the style definition is managed by the style manager instance of the module/sub app. This works well for most components but pop ups are a special case (all pop ups depend on the PopUpManager) </p>
<p>PopUpManager is a singleton and hence it lives in the top level application domain. When a component is displayed using the PopUpManager it will try to get its styles via the StyleManager in the top level application domain and so the styles that are defined in your module/sub app will be ignored. In order to provide a solution for this situation some apis were modified to accept a new optional parameter. Via the optional parameter the module factory can be specified (which is then internally used to locate the correct style manager). Some apis that were modified are:</p>
<ul>
<li>mx.controls.Alert &#8211; show() method was modified to take an optional moduleFactory parameter</li>
<li>mx.manager.PopUpManager- createPopUp() and addPopUp() methods were modified to take an optional moduleFactory parameter</li>
</ul>
<p>So if you are defining styles in your modules/sub apps, you should then pass the moduleFactory parameter to these methods. Lets look at some examples:</p>
<p>Following the code for a main app that loads one module(which shows Alert and TitleWindow) and one sub application (which shows ToolTip). </p>
<blockquote>

<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> xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #000000;">			   xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span> width=<span style="color: #ff0000;">&quot;600&quot;</span> height=<span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Style</span><span style="color: #7400FF;">&gt;</span></span>
		@namespace s &quot;library://ns.adobe.com/flex/spark&quot;;
		@namespace mx &quot;library://ns.adobe.com/flex/mx&quot;;
&nbsp;
		s|TitleWindow
		{
			chromeColor: #FF0000;
		}
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Style</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>
		<span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">			import mx.events.FlexEvent;</span>
<span style="color: #000000;">			import mx.managers.PopUpManager;</span>
&nbsp;
<span style="color: #000000;">			import spark.components.TitleWindow;</span>
&nbsp;
<span style="color: #000000;">			private var placeHolder: PopUpManager;</span>
<span style="color: #000000;">			private var placeHolder2: TitleWindow;</span>
&nbsp;
<span style="color: #000000;">			protected function popup_load_clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				if<span style="color: #66cc66;">&#40;</span>mod_loader_pop.url == null<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">					mod_loader_pop.url = <span style="color: #ff0000;">&quot;PopUpModule.swf&quot;</span>;</span>
<span style="color: #000000;">				<span style="color: #66cc66;">&#125;</span> else <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">					mod_loader_pop.loadModule<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;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			protected function popup_unload_clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				mod_loader_pop.unloadModule<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			protected function toolTip_load_clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				swfLoader.load<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ToolTipSubApp.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			protected function toolTip_unload_clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				swfLoader.unloadAndStop<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>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:layout</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:VerticalLayout</span> paddingTop=<span style="color: #ff0000;">&quot;5&quot;</span> paddingLeft=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:layout</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>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> id=<span style="color: #ff0000;">&quot;popup_load&quot;</span> label=<span style="color: #ff0000;">&quot;Load PopUp Module&quot;</span> click=<span style="color: #ff0000;">&quot;popup_load_clickHandler(event)&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;popup_unload&quot;</span> label=<span style="color: #ff0000;">&quot;Unload PopUp Module&quot;</span> click=<span style="color: #ff0000;">&quot;popup_unload_clickHandler(event)&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;toolTip_load&quot;</span> label=<span style="color: #ff0000;">&quot;Load ToolTip App&quot;</span> click=<span style="color: #ff0000;">&quot;toolTip_load_clickHandler(event)&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;toolTip_unload&quot;</span> label=<span style="color: #ff0000;">&quot;Unload ToolTip App&quot;</span> click=<span style="color: #ff0000;">&quot;toolTip_unload_clickHandler(event)&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>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:HGroup</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:ModuleLoader</span> id=<span style="color: #ff0000;">&quot;mod_loader_pop&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:SWFLoader</span> id=<span style="color: #ff0000;">&quot;swfLoader&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>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

</blockquote>
<p>If you notice, the above app sets the chromeColor for the TitleWindow to #FF0000. Also it does not use any Alert.</p>
<p>Let&#8217;s look at the code for the module (PopUpModule.mxml):</p>
<blockquote>

<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;mx:Module</span> 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;">		   xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span> <span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Style</span><span style="color: #7400FF;">&gt;</span></span>
		@namespace s &quot;library://ns.adobe.com/flex/spark&quot;;
		@namespace mx &quot;library://ns.adobe.com/flex/mx&quot;;
&nbsp;
		s|TitleWindow
		{
			fontSize: 32;
		}
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Style</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>
		<span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">			import mx.events.CloseEvent;</span>
<span style="color: #000000;">			import mx.managers.PopUpManager;</span>
<span style="color: #000000;">			import spark.components.TitleWindow;</span>
<span style="color: #000000;">			import mx.controls.Alert;</span>
&nbsp;
<span style="color: #000000;">			public var titleWindow: TitleWindow;</span>
&nbsp;
<span style="color: #000000;">			protected function btn1_clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				Alert.show<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;clicked&quot;</span>,<span style="color: #ff0000;">&quot;module&quot;</span>,<span style="color: #cc66cc;">4</span>,null,null,null,<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			protected function btn2_clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				Alert.show<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;clicked&quot;</span>,<span style="color: #ff0000;">&quot;module&quot;</span>,<span style="color: #cc66cc;">4</span>,null,null,null,<span style="color: #cc66cc;">4</span> , this.moduleFactory<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			protected function btn1_1_clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				titleWindow = PopUpManager.createPopUp<span style="color: #66cc66;">&#40;</span>DisplayObject<span style="color: #66cc66;">&#40;</span>systemManager<span style="color: #66cc66;">&#41;</span>,TitleWindow, true, null<span style="color: #66cc66;">&#41;</span> as TitleWindow;</span>
<span style="color: #000000;">				titleWindow.title =<span style="color: #ff0000;">&quot;Title Window&quot;</span>;</span>
<span style="color: #000000;">				titleWindow.width = <span style="color: #cc66cc;">136</span>;</span>
<span style="color: #000000;">				titleWindow.addEventListener<span style="color: #66cc66;">&#40;</span>CloseEvent.CLOSE, closeMe, false, <span style="color: #cc66cc;">0</span>, true<span style="color: #66cc66;">&#41;</span>; </span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			protected function btn2_1_clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				titleWindow = PopUpManager.createPopUp<span style="color: #66cc66;">&#40;</span>DisplayObject<span style="color: #66cc66;">&#40;</span>systemManager<span style="color: #66cc66;">&#41;</span>,TitleWindow, true, null, moduleFactory<span style="color: #66cc66;">&#41;</span> as TitleWindow;</span>
<span style="color: #000000;">				titleWindow.title =<span style="color: #ff0000;">&quot;Module Title Window&quot;</span>;</span>
<span style="color: #000000;">				titleWindow.width = <span style="color: #cc66cc;">426</span>;</span>
<span style="color: #000000;">				titleWindow.addEventListener<span style="color: #66cc66;">&#40;</span>CloseEvent.CLOSE, closeMe, false, <span style="color: #cc66cc;">0</span>, true<span style="color: #66cc66;">&#41;</span>; </span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			private function closeMe<span style="color: #66cc66;">&#40;</span>e:CloseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				PopUpManager.removePopUp<span style="color: #66cc66;">&#40;</span>titleWindow<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">				titleWindow.removeEventListener<span style="color: #66cc66;">&#40;</span>CloseEvent.CLOSE, closeMe<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>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span> height=<span style="color: #ff0000;">&quot;100%&quot;</span> title=<span style="color: #ff0000;">&quot;PopUp Module &quot;</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:layout</span><span style="color: #7400FF;">&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:VerticalLayout</span> paddingTop=<span style="color: #ff0000;">&quot;5&quot;</span> paddingLeft=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:layout</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> label=<span style="color: #ff0000;">&quot;Show alert&quot;</span> id=<span style="color: #ff0000;">&quot;btn1&quot;</span> click=<span style="color: #ff0000;">&quot;btn1_clickHandler(event)&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> label=<span style="color: #ff0000;">&quot;Show alert (with moduleFactory)&quot;</span> id=<span style="color: #ff0000;">&quot;btn2&quot;</span> click=<span style="color: #ff0000;">&quot;btn2_clickHandler(event)&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> label=<span style="color: #ff0000;">&quot;show title window&quot;</span> id=<span style="color: #ff0000;">&quot;btn1_1&quot;</span> click=<span style="color: #ff0000;">&quot;btn1_1_clickHandler(event)&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> label=<span style="color: #ff0000;">&quot;show title window (with moduleFactory)&quot;</span> id=<span style="color: #ff0000;">&quot;btn2_1&quot;</span> click=<span style="color: #ff0000;">&quot;btn2_1_clickHandler(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>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Module</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

</blockquote>
<p>The module defines the fontSize for the TitleWindow. Also it has two calls to Alert.show() (with and without the moduleFactory parameter). Similarly two calls to PopUpManager.createPopUp() (with and without the moduleFactory parameter). When you run the example code, you will notice the only the Alert and TitleWindow displayed with the moduleFactory parameter are able to pick the correct styles. </p>
<p>Before looking at the running swf, lets also look at the code for the sub application (ToolTipSubApp.mxml):</p>
<blockquote>

<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> xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> height=<span style="color: #ff0000;">&quot;200&quot;</span> width=<span style="color: #ff0000;">&quot;200&quot;</span> </span>
<span style="color: #000000;">			   xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</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>
		<span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">			import flash.events.MouseEvent;</span>
&nbsp;
<span style="color: #000000;">			import mx.core.IToolTip;</span>
<span style="color: #000000;">			import mx.managers.ToolTipManager;</span>
&nbsp;
<span style="color: #000000;">			public var mytoolTip:IToolTip;</span>
&nbsp;
<span style="color: #000000;">			protected function panel1_mouseOverHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				if<span style="color: #66cc66;">&#40;</span>useContext.selected<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">					mytoolTip = ToolTipManager.createToolTip<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;This is a custom tooltip in a sub App&quot;</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</span>, null,this<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">				<span style="color: #66cc66;">&#125;</span> else <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">					mytoolTip = ToolTipManager.createToolTip<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;This is a custom tooltip in a sub App&quot;</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</span>, null<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;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			protected function panel1_mouseOutHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				ToolTipManager.destroyToolTip<span style="color: #66cc66;">&#40;</span>mytoolTip<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>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Style</span><span style="color: #7400FF;">&gt;</span></span>
		@namespace ns &quot;library://ns.adobe.com/flex/mx&quot;;
		@namespace s &quot;library://ns.adobe.com/flex/spark&quot;;
&nbsp;
		ns|ToolTip
		{
			color: blue;
			fontSize: 22;
		}
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Style</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:layout</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:VerticalLayout</span> <span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:layout</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> width=<span style="color: #ff0000;">&quot;100%&quot;</span> height=<span style="color: #ff0000;">&quot;90%&quot;</span> mouseOver=<span style="color: #ff0000;">&quot;panel1_mouseOverHandler(event)&quot;</span> mouseOut=<span style="color: #ff0000;">&quot;panel1_mouseOutHandler(event)&quot;</span> title=<span style="color: #ff0000;">&quot;Sub App&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>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:CheckBox</span> label=<span style="color: #ff0000;">&quot;pass context&quot;</span> id=<span style="color: #ff0000;">&quot;useContext&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>

</blockquote>
<p>The sub app contains a Panel and a CheckBox, it also contains a style declaration for the ToolTip. When you move the mouse over the panel it displays a tooltip. By default it does not passes the context to the createToolTip method, but when the checkbox is selected it passes the content parameter. In the previous releases the context parameter was not used, but starting from Flex 4 it is being used to locate the current moduleFactory. Only when you pass the context (by selecting the checkbox) the styles for ToolTip defined in the sub app are picked. </p>
<p>Let&#8217;s look at the running swf now:<br />
<center><br />
<iframe src ="http://gauravj.com/blog/examples/Styles/PerModuleStyles_3/StylesAndPopUps.html" width="600" height="500"></p>
<p>Your browser does not support iframes.</p>
<p></iframe><br />
</center></p>
<p>If you load the PopUp Module and click on the &#8220;Show alert&#8221; button, you would notice that the Alert doesn&#8217;t show up with the proper window or title bar. This reason for this broken appearance of Alert is that when the module factory is *not* passed, Alert will try to get its style from the top level style manager. But the main app does not use Alert, so the styles for Alert are not available in the main app. The solution is to pass the moduleFactory to the Alert.show() method. When you click on the &#8220;Show Alert (with moduleFactory)&#8221; button, you would see an Alert with correct Styles.</p>
<p>Also when you click on the &#8220;Show title window&#8221; button, you would notice that the title window shows up with chromeColor red but the fontSize style defined in the module is ignored. This reason for this is same (i.e. when the module factory is *not* passed, TitleWindow will try to get its style from the top level style manager. But the main app only sets chromeColor to red, and the modified fontSize style is not available in the main app) . The solution is to pass the moduleFactory to the PopUpManager.createPopUp() method. And when you click on the &#8220;Show title window (with moduleFactory)&#8221; button, you would see an TitleWindow with correct fontSize and chromeColor.</p>
<p>You would also notice similar behavioral difference when you load the Tool Tip app and play with the tool tip. The ToolTip styles defined in the sub app will only be picked when you pass the context (by selecting the checkbox) to the ToolTipManager.createToolTip() method. Passing the context sets the correct moduleFactory (which is then used to get to the correct style manager). </p>
<p>So, I hope you find this information useful and continue to create awesome looking and mind blowing apps.  </p>
]]></content:encoded>
			<wfw:commentRss>http://gauravj.com/blog/2010/03/styles-and-popups-with-flex-4/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Stylish modules and sub applications.</title>
		<link>http://gauravj.com/blog/2010/02/stylish-modules-and-sub-applications/</link>
		<comments>http://gauravj.com/blog/2010/02/stylish-modules-and-sub-applications/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 15:35:19 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[Flex Framework]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[Styles]]></category>

		<guid isPermaLink="false">http://gauravj.com/blog/?p=616</guid>
		<description><![CDATA[<p>One of the features that&#8217;s coming with Flex 4 is &#8220;Per-Module Style Management&#8221;. There is a lot that can be written about this feature and it probably can&#8217;t be covered in a single post. So for starters here is a small introduction.</p>
<p>The feature for Per-Module Style Management was done to allow modules and sub applications to <span style="color:#777"> . . . &#8594; Read More: <a href="http://gauravj.com/blog/2010/02/stylish-modules-and-sub-applications/">Stylish modules and sub applications.</a></span>]]></description>
			<content:encoded><![CDATA[<p>One of the features that&#8217;s coming with Flex 4 is &#8220;Per-Module Style Management&#8221;. There is a lot that can be written about this feature and it probably can&#8217;t be covered in a single post. So for starters here is a small introduction.</p>
<p>The feature for Per-Module Style Management was done to allow modules and sub applications to be able to manage their own styles. With this feature StyleManager will no longer be a singleton and as a result that will allow modules and sub applications to have their own instances of StyleManager. </p>
<p>Modules and sub applications will continue to inherit styles from the parent application/module. But only the style properties not defined by the child modules or sub applications will trickle down (i.e. child module/application will be able to override a style property defined by the parent). There will also be merging of styles properties. </p>
<p>To understand this better, lets take a look at a sample. Following is the code for a parent application:</p>
<blockquote>

<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> 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;">			   xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span> minWidth=<span style="color: #ff0000;">&quot;1024&quot;</span> minHeight=<span style="color: #ff0000;">&quot;768&quot;</span> creationComplete=<span style="color: #ff0000;">&quot;application1_creationCompleteHandler(event)&quot;</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Style</span><span style="color: #7400FF;">&gt;</span></span>
		@namespace s &quot;library://ns.adobe.com/flex/spark&quot;;
		@namespace mx &quot;library://ns.adobe.com/flex/mx&quot;;
&nbsp;
		global {
			chromeColor: #DDAA66;	
		}
&nbsp;
		s|DropDownList
		{
			fontStyle: italic;
		}
&nbsp;
		s|Button#gumboButton
		{
			fontStyle: italic;
		}
&nbsp;
		s|Panel s|Label
		{
			fontStyle: italic;
		}
&nbsp;
		s|Button.myStyle
		{
			fontStyle: italic;
		}
&nbsp;
		s|HGroup s|RichText
		{
			fontStyle: italic;
		}
&nbsp;
		.classOfStyle
		{
			fontStyle: italic;
		}
&nbsp;
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Style</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;">			import mx.events.FlexEvent;</span>
<span style="color: #000000;">			import mx.collections.ArrayList;</span>
<span style="color: #000000;">			import mx.events.ModuleEvent;</span>
<span style="color: #000000;">			import mx.events.StyleEvent;</span>
<span style="color: #000000;">			import mx.controls.Alert;</span>
&nbsp;
<span style="color: #000000;">			private var arr:Array =</span>
<span style="color: #000000;">				<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Apple'</span>, data:<span style="color: #cc66cc;">10.00</span><span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Banana'</span>, data:<span style="color: #cc66cc;">15.00</span> <span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Melon'</span>, data:<span style="color: #cc66cc;">3.50</span> <span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Kiwi'</span>, data:<span style="color: #cc66cc;">7.65</span><span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Strawberry'</span>,data:<span style="color: #cc66cc;">12.35</span> <span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Other'</span>, data:<span style="color: #cc66cc;">00.00</span><span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">				<span style="color: #66cc66;">&#93;</span>;</span>
&nbsp;
<span style="color: #000000;">			private var listArr :ArrayList = new ArrayList<span style="color: #66cc66;">&#40;</span>arr<span style="color: #66cc66;">&#41;</span>;</span>
&nbsp;
<span style="color: #000000;">			protected function application1_creationCompleteHandler<span style="color: #66cc66;">&#40;</span>event:FlexEvent<span style="color: #66cc66;">&#41;</span>:void</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				appList.dataProvider = listArr;</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			public function load<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:void</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				if<span style="color: #66cc66;">&#40;</span>mod_loader.url == null<span style="color: #66cc66;">&#41;</span></span>
<span style="color: #000000;">				<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">					mod_loader.url = <span style="color: #ff0000;">&quot;LoadStylesModule.swf&quot;</span>;	</span>
<span style="color: #000000;">				<span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">				else</span>
<span style="color: #000000;">				<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">					mod_loader.loadModule<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">				<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			public function unload<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:void</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				mod_loader.unloadModule<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<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>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:layout</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:VerticalLayout</span> <span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:layout</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>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:VGroup</span> id=<span style="color: #ff0000;">&quot;groupId&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:CheckBox</span> id=<span style="color: #ff0000;">&quot;checkBox&quot;</span> label=<span style="color: #ff0000;">&quot;Check Box&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;Label Outside Panel&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:HGroup</span> rotation=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #7400FF;">&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:NumericStepper</span> id=<span style="color: #ff0000;">&quot;numericStepper&quot;</span> stepSize=<span style="color: #ff0000;">&quot;1&quot;</span> minimum=<span style="color: #ff0000;">&quot;1&quot;</span> maximum=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:RichText</span> text=<span style="color: #ff0000;">&quot;This Text is Rich !!&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>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:RichText</span> text=<span style="color: #ff0000;">&quot;This Text is also Rich !!&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> title=<span style="color: #ff0000;">&quot;Gumbo Panel&quot;</span> id=<span style="color: #ff0000;">&quot;appPanel&quot;</span> rotation=<span style="color: #ff0000;">&quot;-5&quot;</span><span style="color: #7400FF;">&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:layout</span><span style="color: #7400FF;">&gt;</span></span>
					<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:VerticalLayout</span> <span style="color: #7400FF;">/&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:layout</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;gumboButton&quot;</span> label=<span style="color: #ff0000;">&quot;Gumbo Button&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;gumboButton2&quot;</span> label=<span style="color: #ff0000;">&quot;Second Gumbo Button&quot;</span> styleName=<span style="color: #ff0000;">&quot;myStyle&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;Spark Label&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;Spark Label class selector&quot;</span> styleName=<span style="color: #ff0000;">&quot;classOfStyle&quot;</span>  <span style="color: #7400FF;">/&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:DropDownList</span> id=<span style="color: #ff0000;">&quot;appList&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>
		<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;mx:ModuleLoader</span> id=<span style="color: #ff0000;">&quot;mod_loader&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:Button</span> label=<span style="color: #ff0000;">&quot;Load Module&quot;</span> click=<span style="color: #ff0000;">&quot;load()&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> label=<span style="color: #ff0000;">&quot;Unload Module&quot;</span> click=<span style="color: #ff0000;">&quot;unload()&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>

</blockquote>
<p>If you notice, the above code defines a bunch of styles. It sets the chrome color to DDAA66 in the global selector and it sets the fontStyle to italic using various selectors (class, Id, Type, descendant).</p>
<p>Now let&#8217;s take a look at the code of the module (LoadStylesModule.mxml):</p>
<blockquote>

<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;mx:Module</span> 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;">		   xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span> creationComplete=<span style="color: #ff0000;">&quot;module1_creationCompleteHandler(event)&quot;</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>
		<span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">			import mx.collections.ArrayList;</span>
<span style="color: #000000;">			import mx.controls.Alert;</span>
<span style="color: #000000;">			import mx.events.FlexEvent;</span>
<span style="color: #000000;">			import mx.events.StyleEvent;</span>
&nbsp;
<span style="color: #000000;">			private var arr:Array =</span>
<span style="color: #000000;">				<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Apple'</span>, data:<span style="color: #cc66cc;">10.00</span><span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Banana'</span>, data:<span style="color: #cc66cc;">15.00</span> <span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Melon'</span>, data:<span style="color: #cc66cc;">3.50</span> <span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Kiwi'</span>, data:<span style="color: #cc66cc;">7.65</span><span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Strawberry'</span>,data:<span style="color: #cc66cc;">12.35</span> <span style="color: #66cc66;">&#125;</span>,</span>
<span style="color: #000000;">					<span style="color: #66cc66;">&#123;</span> label:<span style="color: #ff0000;">'Other'</span>, data:<span style="color: #cc66cc;">00.00</span><span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;">				<span style="color: #66cc66;">&#93;</span>;</span>
&nbsp;
<span style="color: #000000;">			private var listArr :ArrayList = new ArrayList<span style="color: #66cc66;">&#40;</span>arr<span style="color: #66cc66;">&#41;</span>;</span>
&nbsp;
<span style="color: #000000;">			protected function module1_creationCompleteHandler<span style="color: #66cc66;">&#40;</span>event:FlexEvent<span style="color: #66cc66;">&#41;</span>:void</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				moduleList.dataProvider = listArr;</span>
<span style="color: #000000;">				localStyleManager  = StyleManager.getStyleManager<span style="color: #66cc66;">&#40;</span>this.moduleFactory<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			private var localStyleManager:IStyleManager2 = null;</span>
&nbsp;
<span style="color: #000000;">			public var eventDispatcher:IEventDispatcher = null;</span>
<span style="color: #000000;">			protected function loadStylesButton_clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#123;</span></span>
&nbsp;
<span style="color: #000000;">				eventDispatcher = localStyleManager.loadStyleDeclarations<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;testStyles.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #000000;">			protected function unloadStylesButton_clickHandler<span style="color: #66cc66;">&#40;</span>event:MouseEvent<span style="color: #66cc66;">&#41;</span>:void</span>
<span style="color: #000000;">			<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">				localStyleManager.unloadStyleDeclarations<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;testStyles.swf&quot;</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>
	<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:CheckBox</span> id=<span style="color: #ff0000;">&quot;checkBox&quot;</span> label=<span style="color: #ff0000;">&quot;Check Box Module&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;Label Outside Panel&quot;</span>  <span style="color: #7400FF;">/&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:HGroup</span> rotation=<span style="color: #ff0000;">&quot;-10&quot;</span><span style="color: #7400FF;">&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:NumericStepper</span> id=<span style="color: #ff0000;">&quot;numericStepper&quot;</span> stepSize=<span style="color: #ff0000;">&quot;1&quot;</span> minimum=<span style="color: #ff0000;">&quot;1&quot;</span> maximum=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:RichText</span> text=<span style="color: #ff0000;">&quot;This Text is Rich Module !!&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>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:RichText</span> text=<span style="color: #ff0000;">&quot;This Text is also Rich !!&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Panel</span> title=<span style="color: #ff0000;">&quot;Gumbo Module Panel&quot;</span> id=<span style="color: #ff0000;">&quot;appPanel&quot;</span> rotation=<span style="color: #ff0000;">&quot;-5&quot;</span><span style="color: #7400FF;">&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:layout</span><span style="color: #7400FF;">&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:VerticalLayout</span> <span style="color: #7400FF;">/&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:layout</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> id=<span style="color: #ff0000;">&quot;gumboButton&quot;</span> label=<span style="color: #ff0000;">&quot;Gumbo Module Button&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;gumboButton2&quot;</span> label=<span style="color: #ff0000;">&quot;Second Gumbo Module Button&quot;</span> styleName=<span style="color: #ff0000;">&quot;myStyle&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;Spark Module Label&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;Spark Label Module class selector&quot;</span> styleName=<span style="color: #ff0000;">&quot;classOfStyle&quot;</span>  <span style="color: #7400FF;">/&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:ComboBox</span> id=<span style="color: #ff0000;">&quot;moduleList&quot;</span> height=<span style="color: #ff0000;">&quot;24&quot;</span> width=<span style="color: #ff0000;">&quot;147&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>
	<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;mx:Button</span> label=<span style="color: #ff0000;">&quot;Load styles&quot;</span> id=<span style="color: #ff0000;">&quot;loadStylesButton&quot;</span> click=<span style="color: #ff0000;">&quot;loadStylesButton_clickHandler(event)&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> label=<span style="color: #ff0000;">&quot;Unload styles&quot;</span> id=<span style="color: #ff0000;">&quot;unloadStylesButton&quot;</span> click=<span style="color: #ff0000;">&quot;unloadStylesButton_clickHandler(event)&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Module</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

</blockquote>
<p>The code for the module above does not define any styles. So what that means is that it will inherit the styles from the parent app. It will have the chrome color of DDAA66 for all components and also the font styles will be italic. So far that&#8217;s all normal and boring stuff. Now you probably also noticed that the module  loads a styles declaration swf (which is a css compiled to a swf), and when you load the styles swf you will see the magic of per-module style management. The module will override style definition for some properties (originally defined in the main app) and for other properties styles merging will happen.</p>
<p>Here is the code for the css (testStyles.css):</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;">@namespace s &quot;library://ns.adobe.com/flex/spark&quot;;
@namespace mx &quot;library://ns.adobe.com/flex/mx&quot;;
&nbsp;
s|DropDownList
{
	borderColor: #FF0000;
	fontSize: 16;
}
&nbsp;
s|Button#gumboButton
{
	chromeColor: #AAFFAA;
	fontSize: 16;
}
&nbsp;
s|Panel s|Label
{
	fontSize: 16;
}
&nbsp;
s|Button.myStyle
{
	chromeColor: #FFFFAA;
	fontSize: 14;
}
&nbsp;
s|HGroup s|RichText
{
	chromeColor: #BBAAAA;
	fontSize: 14;
}
&nbsp;
.classOfStyle
{
	color: #0FFFFA;	
	fontSize: 20;
}</pre></div></div>

</blockquote>
<p>Here is the running swf:<br />
<center><br />
<iframe src ="http://gauravj.com/blog/examples/Styles/PerModuleStyles_1/StylesApp.html" width="700" height="400"></p>
<p>Your browser does not support iframes.</p>
<p></iframe><br />
</center></p>
<p>Click on the &#8220;Load Module&#8221; button to load the module. Once the module is loaded, notice the chrome color of most components is the same as in the parent app and the font is italicized for most components &#8211; this shows that the module inherits styles from the parent app. Now click on the &#8220;Load styles&#8221; button in the module, once the styles swf is loaded you can notice that the font size changes for most components but the fonts in the module are still italicized &#8211; this shows styles merging because we were only overriding the fontSize in the css and not the fontStyle property. Also notice the new chrome color for components in the module, its now different from the global chrome color (DDAA66). So the module is able to override the styles properties it chooses.</p>
<p>So, hopefully you find this short introduction useful. Stay tuned for more on this.. And if you want me to write about something else, please say so with your comments (which btw are welcome <img src='http://gauravj.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  )</p>
]]></content:encoded>
			<wfw:commentRss>http://gauravj.com/blog/2010/02/stylish-modules-and-sub-applications/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Creating smaller swfs (Part III)</title>
		<link>http://gauravj.com/blog/2009/07/creating-smaller-swfs-part-iii/</link>
		<comments>http://gauravj.com/blog/2009/07/creating-smaller-swfs-part-iii/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 19:41:15 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[Flex Framework]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Fonts]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[RSL]]></category>

		<guid isPermaLink="false">http://gauravj.com/blog/?p=376</guid>
		<description><![CDATA[<p>Here is the part three.. </p>
<p>The very fact that you are writing applications using Flex proves that you care about user interface. And one of the features of Flex that enables you to provide a better user experience is font embedding. There are many benefits of using embedded fonts but the most important one is that <span style="color:#777"> . . . &#8594; Read More: <a href="http://gauravj.com/blog/2009/07/creating-smaller-swfs-part-iii/">Creating smaller swfs (Part III)</a></span>]]></description>
			<content:encoded><![CDATA[<p>Here is the part three.. </p>
<p>The very fact that you are writing applications using Flex proves that you care about user interface. And one of the features of Flex that enables you to provide a better user experience is font embedding. There are many benefits of using embedded fonts but the most important one is that it provides consistency regarding how the application will appear across different client environments and you don&#8217;t need to make sure whether the font is pre-installed. But there can also be few disadvantages of using embedded fonts, like when you use embedded font it gets stitched into your SWF which results in increasing the overall size of the SWF. </p>
<p>So lets look at how you can reap the benefits of using embedded fonts while still avoiding the start up time cost due to a larger SWF. The trick for doing this is to use Modules (for embedding fonts) and avoid embedding the fonts in the main SWF. That way you can achieve smaller start up time and fetch the fonts when you need to use them.</p>
<p>Here is an example of an application that displays text using four different fonts: </p>
<blockquote>

<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;mx:Application</span> xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> </span>
<span style="color: #000000;">		layout=<span style="color: #ff0000;">&quot;vertical&quot;</span> width=<span style="color: #ff0000;">&quot;650&quot;</span> height=<span style="color: #ff0000;">&quot;400&quot;</span></span>
<span style="color: #000000;">		verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span> horizontalAlign=<span style="color: #ff0000;">&quot;center&quot;</span> </span>
<span style="color: #000000;">		creationComplete=<span style="color: #ff0000;">&quot;creationCompleteHandler(event)&quot;</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">		&lt;![CDATA[</span>
<span style="color: #339933;">			import mx.events.ModuleEvent;</span>
<span style="color: #339933;">			import mx.modules.ModuleManager;</span>
<span style="color: #339933;">			import mx.modules.IModuleInfo;</span>
<span style="color: #339933;">			import mx.events.FlexEvent;</span>
<span style="color: #339933;">			import mx.controls.Alert;</span>
<span style="color: #339933;">			import mx.validators.Validator;</span>
<span style="color: #339933;">			private var fontModuleInfo:IModuleInfo;</span>
&nbsp;
<span style="color: #339933;">			protected function button_clickHandler(event:MouseEvent):void {</span>
<span style="color: #339933;">				if(currentState != 'LoggedIn') {</span>
<span style="color: #339933;">					var validators:Array = [];</span>
<span style="color: #339933;">					validators[0] = this.v0;</span>
<span style="color: #339933;">					validators[1] = this.v1;</span>
&nbsp;
<span style="color: #339933;">					var inValid:Array = Validator.validateAll(validators);</span>
<span style="color: #339933;">					if(inValid.length ==0) {</span>
<span style="color: #339933;">						if(tiUserName.text == 'guest' </span>
<span style="color: #339933;">							&amp;&amp; tiPassword.text =='guest') {</span>
<span style="color: #339933;">							currentState = 'LoggedIn';</span>
<span style="color: #339933;">						} else {</span>
<span style="color: #339933;">							Alert.show(&quot;Invalid username&quot; +</span>
<span style="color: #339933;">								&quot; or password.&quot;); </span>
<span style="color: #339933;">						}</span>
<span style="color: #339933;">					}</span>
<span style="color: #339933;">				} else {</span>
<span style="color: #339933;">					currentState = '';</span>
<span style="color: #339933;">				}</span>
<span style="color: #339933;">			}</span>
&nbsp;
<span style="color: #339933;">			protected function creationCompleteHandler(event:FlexEvent):void </span>
<span style="color: #339933;">			{</span>
<span style="color: #339933;">				fontModuleInfo = ModuleManager.getModule(&quot;FontModule.swf&quot;);</span>
<span style="color: #339933;">				fontModuleInfo.addEventListener(ModuleEvent.READY,</span>
<span style="color: #339933;">					readyHandler);</span>
<span style="color: #339933;">				fontModuleInfo.load();</span>
<span style="color: #339933;">			}</span>
&nbsp;
<span style="color: #339933;">			private function readyHandler(event:Event):void {</span>
<span style="color: #339933;">				fontModuleInfo.factory.create();</span>
<span style="color: #339933;">			}</span>
&nbsp;
<span style="color: #339933;">		]]&gt;</span>
<span style="color: #339933;">	&lt;/mx:Script&gt;</span>
&nbsp;
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:StringValidator</span> source=<span style="color: #ff0000;">&quot;{tiUserName}&quot;</span> property=<span style="color: #ff0000;">&quot;text&quot;</span> id=<span style="color: #ff0000;">&quot;v0&quot;</span></span>
<span style="color: #000000;">						required=<span style="color: #ff0000;">&quot;true&quot;</span> maxLength=<span style="color: #ff0000;">&quot;10&quot;</span> </span>
<span style="color: #000000;">						trigger=<span style="color: #ff0000;">&quot;{loginButton}&quot;</span> triggerEvent=<span style="color: #ff0000;">&quot;click&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:StringValidator</span> source=<span style="color: #ff0000;">&quot;{tiPassword}&quot;</span> property=<span style="color: #ff0000;">&quot;text&quot;</span> id=<span style="color: #ff0000;">&quot;v1&quot;</span></span>
<span style="color: #000000;">						required=<span style="color: #ff0000;">&quot;true&quot;</span> maxLength=<span style="color: #ff0000;">&quot;10&quot;</span> </span>
<span style="color: #000000;">						trigger=<span style="color: #ff0000;">&quot;{loginButton}&quot;</span> triggerEvent=<span style="color: #ff0000;">&quot;click&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:states</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:State</span> name=<span style="color: #ff0000;">&quot;LoggedIn&quot;</span><span style="color: #7400FF;">&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:RemoveChild</span> target=<span style="color: #ff0000;">&quot;{loginPanel}&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:AddChild</span> relativeTo=<span style="color: #ff0000;">&quot;{loginButton}&quot;</span> position=<span style="color: #ff0000;">&quot;before&quot;</span><span style="color: #7400FF;">&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Panel</span> id=<span style="color: #ff0000;">&quot;pnl&quot;</span> title=<span style="color: #ff0000;">&quot;Labels with different fonts. </span>
<span style="color: #000000;">						  Embedded fonts can be rotated.&quot;</span> paddingTop=<span style="color: #ff0000;">&quot;10&quot;</span> </span>
<span style="color: #000000;">						  paddingBottom=<span style="color: #ff0000;">&quot;10&quot;</span> paddingLeft=<span style="color: #ff0000;">&quot;10&quot;</span> paddingRight=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #7400FF;">&gt;</span></span>
					<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> id=<span style="color: #ff0000;">&quot;lblVerdana&quot;</span> </span>
<span style="color: #000000;">						  styleName=<span style="color: #ff0000;">&quot;myVerdanaDescriptor&quot;</span> rotation=<span style="color: #ff0000;">&quot;1&quot;</span>  </span>
<span style="color: #000000;">						  text=<span style="color: #ff0000;">&quot;The quick brown fox jumps over the lazy dog.&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
					<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> id=<span style="color: #ff0000;">&quot;lblCandara&quot;</span> </span>
<span style="color: #000000;">						  styleName=<span style="color: #ff0000;">&quot;myCandaraDescriptor&quot;</span> rotation=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #000000;">						  text=<span style="color: #ff0000;">&quot;The quick brown fox jumps over the lazy dog.&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
					<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> id=<span style="color: #ff0000;">&quot;lblVrinda&quot;</span> </span>
<span style="color: #000000;">						  styleName=<span style="color: #ff0000;">&quot;myVrindaDescriptor&quot;</span> rotation=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #000000;">						  text=<span style="color: #ff0000;">&quot;The quick brown fox jumps over the lazy dog.&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
					<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> id=<span style="color: #ff0000;">&quot;lblMinion&quot;</span> </span>
<span style="color: #000000;">						  styleName=<span style="color: #ff0000;">&quot;myMinionDescriptor&quot;</span> rotation=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #000000;">						  text=<span style="color: #ff0000;">&quot;The quick brown fox jumps over the lazy dog.&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Panel</span><span style="color: #7400FF;">&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:AddChild</span><span style="color: #7400FF;">&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:SetProperty</span> target=<span style="color: #ff0000;">&quot;{loginButton}&quot;</span> </span>
<span style="color: #000000;">							name=<span style="color: #ff0000;">&quot;label&quot;</span> value=<span style="color: #ff0000;">&quot;Log Out&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:State</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:states</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Panel</span> id=<span style="color: #ff0000;">&quot;loginPanel&quot;</span> </span>
<span style="color: #000000;">			  title=<span style="color: #ff0000;">&quot;Login&quot;</span>  borderAlpha=<span style="color: #ff0000;">&quot;0.15&quot;</span></span>
<span style="color: #000000;">			  horizontalScrollPolicy=<span style="color: #ff0000;">&quot;off&quot;</span> verticalScrollPolicy=<span style="color: #ff0000;">&quot;off&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Form</span> id=<span style="color: #ff0000;">&quot;loginForm&quot;</span> <span style="color: #7400FF;">&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:FormItem</span> label=<span style="color: #ff0000;">&quot;Username:&quot;</span><span style="color: #7400FF;">&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:TextInput</span> id=<span style="color: #ff0000;">&quot;tiUserName&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:FormItem</span><span style="color: #7400FF;">&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:FormItem</span> label=<span style="color: #ff0000;">&quot;Password:&quot;</span><span style="color: #7400FF;">&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:TextInput</span> id=<span style="color: #ff0000;">&quot;tiPassword&quot;</span> </span>
<span style="color: #000000;">							  displayAsPassword=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:FormItem</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Form</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Panel</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Button</span> label=<span style="color: #ff0000;">&quot;Login&quot;</span> id=<span style="color: #ff0000;">&quot;loginButton&quot;</span> </span>
<span style="color: #000000;">			   click=<span style="color: #ff0000;">&quot;button_clickHandler(event)&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

</blockquote>
<p>And here is the module (FontModule.mxml) used to embed fonts:</p>
<blockquote>

<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;mx:Module</span> xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Style</span><span style="color: #7400FF;">&gt;</span></span>
		@font-face {
			src:url(&quot;verdana.TTF&quot;);
			fontFamily: myVerdana;
		}
&nbsp;
		.myVerdanaDescriptor
		{
			fontFamily: myVerdana;
			color: red;
			fontSize: 24;
		}
&nbsp;
		@font-face {
			src:url(&quot;MinionPro-Regular.otf&quot;);
			fontFamily: myMinion;
		}
&nbsp;
		.myMinionDescriptor
		{
			fontFamily: myMinion;
			color: green;
			fontSize: 24;
		}
&nbsp;
		@font-face {
			src:url(&quot;Vrinda.ttf&quot;);
			fontFamily: myVrinda;
		}
&nbsp;
		.myVrindaDescriptor
		{
			fontFamily: myVrinda;
			color: red;
			fontSize: 24;
		}
&nbsp;
		@font-face {
			src:url(&quot;CANDARA.TTF&quot;);
			fontFamily: myCandara;
		}
&nbsp;
		.myCandaraDescriptor
		{
			fontFamily: myCandara;
			color: green;
			fontSize: 24;
		}
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Style</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Module</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

</blockquote>
<p>Below is the running sample, to log in <strong>use &#8220;guest&#8221; as user name and password</strong> and click on the button.<br />
<center><br />
<iframe src ="http://gauravj.com/blog/examples/Modules/Fonts/Sample.html" width="650" height="400"></p>
<p>Your browser does not support iframes.</p>
<p></iframe><br />
</center></p>
<p>The size benefits:</p>
<table border="1">
<tr>
<th>Configuration</th>
<th>Swf size</th>
<th>Comments</th>
</tr>
<tr>
<td>If fonts are embedded in main swf</td>
<td>607 KB</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>If fonts are fonts embedded in module</td>
<td>267 KB</td>
<td>The font Module is 354 KB</td>
</tr>
<tr>
<td>If fonts are fonts embedded in module and main app uses default SDK RSLs. (as is the case with the above example)</td>
<td>84 KB</td>
<td>This app was compiled with Flex 4 beta build. With a more recent Flex 4 build there should be increased benefits for swf size reduction.</td>
</tr>
</table>
<p>So we trimmed the SWF by 86% for this sample.</p>
<p>The above approach works if you don&#8217;t need embedded fonts right at the application start up. In the above example the font module is loaded after the main app is initialized, as we don&#8217;t need to show text using embedded fonts on the log in screen. This way the user experiences a reduced start up time and you can still use the fonts you like <img src='http://gauravj.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>If you app uses multiple fonts and some of them are not required at the application start up, consider embedding fonts in a module.</p>
]]></content:encoded>
			<wfw:commentRss>http://gauravj.com/blog/2009/07/creating-smaller-swfs-part-iii/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Creating smaller swfs (Part II)</title>
		<link>http://gauravj.com/blog/2009/06/creating-smaller-swfs-part-ii/</link>
		<comments>http://gauravj.com/blog/2009/06/creating-smaller-swfs-part-ii/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 04:00:51 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[Flex Framework]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Modules]]></category>
		<category><![CDATA[MXML]]></category>
		<category><![CDATA[RSL]]></category>

		<guid isPermaLink="false">http://gauravj.com/blog/?p=149</guid>
		<description><![CDATA[<p>Here is the &#8220;Part II&#8221; of creating smaller swfs. In case you haven&#8217;t checked out the &#8220;Part I&#8221;, it can be found here: Creating smaller swfs (Part I).  In this post, let&#8217;s focus on modules and how they can help in reducing the swf size.</p>
What are Modules?
<p>Modules are Flex swf files that can be dynamically <span style="color:#777"> . . . &#8594; Read More: <a href="http://gauravj.com/blog/2009/06/creating-smaller-swfs-part-ii/">Creating smaller swfs (Part II)</a></span>]]></description>
			<content:encoded><![CDATA[<p>Here is the &#8220;Part II&#8221; of creating smaller swfs. In case you haven&#8217;t checked out the &#8220;Part I&#8221;, it can be found here: <font color="#AAAAFF"><strong><em><a href="http://gauravj.com/blog/?p=20">Creating smaller swfs (Part I)</a></em></strong></font>.  In this post, let&#8217;s focus on modules and how they can help in reducing the swf size.</p>
<h2>What are Modules?</h2>
<p>Modules are Flex swf files that can be <em>dynamically</em> loaded by other swf files. So they allow you to break your large application into smaller pieces and gives you the flexibility to load the the required piece when/if required. In case you are wondering &#8220;How is Module different than RSL (since RSL can also be a swf and can be loaded by your application)&#8221;, the difference is that RSLs can <em><strong>only</strong></em> be loaded in frame 1 (i.e before your application initializes) but Modules can be loaded and unloaded <em><strong>whenever you want</strong></em> (after your application initializes) through out the life cycle of your application. Also note that you can not run modules independently, they always need to be loaded by another application. Benefits of modules are much more than just helping you to make your swf smaller, some of the benefits are:</p>
<ul>
<li><em>Improve start up time</em> by reducing the swf size.</li>
<li><em>Improves development time</em> because modules allows for code separation and can be independently compiled. So you can break you app into different functional pieces and have different developers/teams work on the different pieces as Modules.</li>
<li><em>Improves build time</em> because you need not recompile your entire application if changes are made to an independent module.</li>
<li><em>Improves integration</em> where you can&#8217;t upgrade all your code to the most recent version of Flex.</li>
</ul>
<h2>How to write Modules?</h2>
<p>You can write a module either using ActionScript or MXML. If your module is going to use any part of the Flex framework code then you must write a module based on mx.modules.Module. In case your module is <strong><em>not</em></strong>  going to use any part of the Flex framework code then you can write a module based on mx.modules.ModuleBase but then you must write it using ActionScript.</p>
<p>Here is an example of a module written using MXML (DataGridModule.mxml):</p>
<blockquote><pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
		   width="400" height="300" creationComplete="module1_creationCompleteHandler(event)"&gt;
	&lt;mx:Script&gt;
		&lt;![CDATA[
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.events.FlexEvent;

			protected function module1_creationCompleteHandler(event:FlexEvent):void
			{
				srv.send();
			}

			protected function srv_faultHandler(event:FaultEvent):void
			{
				Alert.show("Can not retrieve data");
			}
		]]&gt;
	&lt;/mx:Script&gt;

	&lt;mx:HTTPService id="srv" url="catalog.xml" fault="srv_faultHandler(event)" /&gt;

	&lt;mx:DataGrid dataProvider="{srv.lastResult.report.account}" width="100%" height="100%"&gt;
		&lt;mx:columns&gt;
			&lt;mx:DataGridColumn dataField="month" headerText="Month"/&gt;
			&lt;mx:DataGridColumn dataField="sales" headerText="Sales" textAlign="right"/&gt;
			&lt;mx:DataGridColumn dataField="expenses" headerText="Expenses" textAlign="right"/&gt;
		&lt;/mx:columns&gt;
	&lt;/mx:DataGrid&gt;
&lt;/mx:Module&gt;
</code></pre>
</blockquote>
<p>As you can see the only thing special about the above code is the wrapping &lt;mx:Module&gt; tag, otherwise it looks pretty similar to a custom component (You can simply replace &lt;mx:Module&gt;, with say &lt;mx:VBox&gt; or &lt;mx:HBox&gt; and use it as a custom component)</p>
<p>The following is an example of a Module using ActionScript (ActionModule.as) which extends mx.modules.ModuleBase:</p>
<blockquote><pre><code>package
{
	import mx.modules.ModuleBase;

	public class ActionModule extends ModuleBase
	{
		public function ActionModule()
		{
			super();
		}

		public function calculateAverageProfit(arr:Array): String
		{
			var profit:Number = 0;

			for(var counter:int = 0; counter < arr.length; counter++)
			{
				var obj:Object = arr[counter];
				profit += obj.sales - obj.expenses;
			}

			return new Number(profit/arr.length).toFixed(2);
		}
	}
}</code></pre>
</blockquote>
<p>Since the above module (ActionModule.as) is for calculation purpose only, it doesn't depend upon any framework code and hence can extend from mx.modules.ModuleBase instead of mx.modules.Module</p>
<h2>How to use Modules?</h2>
<p>So now you probably have some idea about writing the modules, once you write the Modules, there are two techniques to load them into your application. </p>
<ol>
<li>Use ModuleLoader</li>
<li>Use ModuleManager and IModuleInfo</li>
</ol>
<p>Following is an example of an app which loads Module(s) (using both the techniques):</p>
<blockquote><pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
creationComplete="srv.send();"&gt;
	&lt;mx:Script&gt;
		&lt;![CDATA[
			import mx.modules.IModuleInfo;
			import mx.modules.ModuleManager;
			import mx.events.ModuleEvent;
			import mx.collections.ArrayCollection;
			import mx.rpc.events.ResultEvent;
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.events.FlexEvent;

			private var result:ArrayCollection;

			private var moduleInfo:IModuleInfo;

			private var obj:Object;

			protected function srv_faultHandler(event:FaultEvent):void
			{
				Alert.show("Can not retrieve data");
			}
			protected function modLoader_errorHandler(event:ModuleEvent):void
			{
				Alert.show("Error in loading the Module.");
			}

			protected function srv_resultHandler(event:ResultEvent):void
			{
				result = event.result.report.account;
				linechart.dataProvider = result;
			}

			protected function showDataGrid_clickHandler(event:MouseEvent):void
			{
				modLoader.url = "DataGridModule.swf";
			}

			protected function showProfit_clickHandler(event:MouseEvent):void
			{
				moduleInfo = ModuleManager.getModule("ActionModule.swf");
				moduleInfo.addEventListener(ModuleEvent.READY, readyHandler);
				moduleInfo.addEventListener(ModuleEvent.ERROR, modLoader_errorHandler);
				moduleInfo.load();
			}

			private function readyHandler(event:ModuleEvent):void
			{
				obj = moduleInfo.factory.create();
				profitLbl.text = "Average Profit: $" + obj.calculateAverageProfit(result.source)
                                                 + " (in millions)";
			}
		]]&gt;
	&lt;/mx:Script&gt;
	&lt;mx:HTTPService id="srv" url="catalog.xml" fault="srv_faultHandler(event)"
                         result="srv_resultHandler(event)"/&gt;

	&lt;mx:LineChart id="linechart" color="0x323232" height="50%" showDataTips="true" &gt;
		&lt;mx:horizontalAxis&gt;
			&lt;mx:CategoryAxis categoryField="month"/&gt;
		&lt;/mx:horizontalAxis&gt;
		&lt;mx:series&gt;
			&lt;mx:LineSeries yField="sales" form="curve" displayName="Sales"/&gt;
			&lt;mx:LineSeries yField="expenses" form="curve" displayName="Expenses"/&gt;
		&lt;/mx:series&gt;
	&lt;/mx:LineChart&gt;
	&lt;mx:Legend dataProvider="{linechart}" color="0x323232"/&gt;
	&lt;mx:Label id="profitLbl" /&gt;
	&lt;mx:Button id="showProfit" click="showProfit_clickHandler(event)"
                 label="Show Average Profit"/&gt;
	&lt;mx:Button id="showDataGrid" click="showDataGrid_clickHandler(event)"
                 label="Show Details"/&gt;

	&lt;mx:ModuleLoader id="modLoader" error="modLoader_errorHandler(event)" /&gt;
&lt;/mx:Application&gt;
</code></pre>
</blockquote>
<p>In the above example the DataGridModule.swf is loaded using &lt;mx:ModuleManager/&gt;, the load is invoked when the url property is set (modLoder.url ="DataGridModule.swf"), which fires the urlChange event. Also you can invoke the load operation using the loadModule() method of ModuleLoader.  You can unload a module using the unloadModule() method.</p>
<p>The ActionModule.swf is loaded using ModuleManager and IModuleInfo, ModuleManager is used to get a reference to the IModuleInfo for ActionModule.swf and the actual load is invoked using the load() method on IModuleInfo.   </p>
<p>Notice that both techniques allow you to add a error handler (see modLoader_errorHandler() which takes event of type ModuleEvent), a error handler can be helpful to debug issue with module loading. Also there are other event types that you can use to find info related to loading (ModuleEvent.PROGRESS, ModuleEvent.READY, ModuleEvent.SETUP)</p>
<p>Here is the running swf. When you press on the button to "Show Average Profit", it loads the ActionModule.swf (which calculates the average profit). When you press on "Show Details", it loads the data grid module which shows the data in a datagrid.</p>
<p><center><br />
<iframe src ="http://gauravj.com/blog/examples/Modules/Dashboard.html" width="500" height="700"></p>
<p>Your browser does not support iframes.</p>
<p></iframe><br />
</center></p>
<p>Now for the size benefits:</p>
<table border="1">
<tr>
<th>Configuration</th>
<th>Swf size</th>
<th>Comments</th>
</tr>
<tr>
<td>Without Modules and RSLs</td>
<td>484 KB</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>With Modules</td>
<td>415 KB</td>
<td>In this case the benefit is small because we moved only a small portion of code into the module. Reduction in swf size depends upon the amount of code you move into modules. Expect higher benefits for large applications.</td>
</tr>
<tr>
<td>With Modules and RSLs</td>
<td>159 KB</td>
<td>This app was compiled with Flex 4 beta build. With a more recent Flex 4 build there should be increased benefits for swf size reduction.</td>
</tr>
</table>
<h2>Optimizing Modules</h2>
<p>So far you have seen how to use modules and how they can help to reduce the swf size. But when you compile a module swf, lot of core classes of the Flex framework (which are dependencies of mx.modules.Module) also get stitched into the module swf file, this creates a situation where you duplicate the same classes into your application (once in the main app and then in each module which is loaded by the app). However there is a compiler option to avoid this situation. You can compile you main app with the <strong>-link-report</strong> option, it will give you the link-report (a report that has the information regarding the byte code linkage) for your application. Then you can compile your module(s) with the <strong>-load-externs</strong> option and you can point the load-externs to the generated link report from the compilation of the main app. Its like telling the compiler that you already have taken care of classes which are in the link report, so don't add those to the module swf file.</p>
<p>Here are the example command I used:</p>
<p># compile main app - this will generate the main app swf and a link report named myLinkReport.xml<br />
./mxmlc -link-report=myLinkReport.xml Dashboard.mxml</p>
<p># compile module<br />
./mxmlc -load-externs=myLinkReport.xml DataGridModule.mxml</p>
<p>Or you can also use Flex/Flash Builder to set up the module optimization. You can do this via the project properties. Here are the steps:</p>
<ol>
<li>Right Click on the project and go to properties</li>
<li>Choose Flex Modules</li>
<p><img src="examples/Modules/modulesDialog.jpg" /><br />
You can see the modules in this dialog and whether they are being optimized or not. You can add new modules using the "Add" button.</p>
<li>Edit setting using the edit button or double click on the module</li>
<p><img src="examples/Modules/editModule.jpg" /><br />
Using this dialog, you can choose whether to optimize(and for which application) or not
</ol>
<p>The <strong>only situation</strong> where you should <strong>not</strong> optimize your modules is when you want to use your modules in different applications. Because optimized modules can only be used with application for which they were optimized.</p>
<h2>Shared Code problem</h2>
<p>When modules are loaded, the classes that are contained in the module are added into the child domain of the current application domain. So classes that are part of the module are owned by the module and not be the application. This can create an issue when two modules are using the same classes. When the first module has loaded, its class definitions gets registered, and module loaded after wards finds itself in a situation where the class definition that it contains doesn't matches with what is already registered. This usually creates issue with the manager classes like HistoryManager, PopUpManager etc. The error usually is a "Type Coercion" error, for example:</p>
<blockquote><p><strong>Type Coercion failed:</strong> cannot convert mx.managers::PopUpManagerImpl@ to mx.managers.IPopUpManager</p></blockquote>
<p>The solution is to make these classes part of the main app (so the main app is the owner of these class definitions and both managers can share them). If you simply add the import and variable declaration to the script block of the main app, you can avoid the issue with the classes that cause shared code problem</p>
<blockquote><p><code>import  mx.managers.HistoryManager;<br />
private var managerObj: HistoryManager;</code></p></blockquote>
<p>Alternatively you can also refactor code into a shared module and load the shared module <strong>before</strong> loading any other module.</p>
<h2>Modules and RSLs</h2>
<p>Using both modules are RSLs can significantly reduce swf size and start-up time. But if your app is using RSL (say for framework.swc) then your module swf does not need to use framework.swc as RSL. In such cases the library that will be loaded as RSL by the main app should just be in the external-library-path of the module. </p>
<p>This way since the RSL will already be loaded by the main app and available for consumption, for the module, the module will further save on load time because it won't even try to load the RSL (btw since the RSL is already loaded by the main app, the module will get it quickly get it from the cache so the saving per module is small but can add up if you are using lots of modules)</p>
<p>There is a bug (see <a href="http://bugs.adobe.com/jira/browse/FB-15470">FB-15470</a>) related to this in Flex Builder 3. The fix for the bug will be available in Flash Builder 4</p>
<p>So if you are using FB 3, here is how you can do this manually:<br />
You should move the modules into a seperate Flex Builder project and not list the libraries as rsls (instead just as -external-library-path for the libraries). That way the modules won't try to reload the rsls (because they would have already been loaded by the main app (for the project containing the main app you can continue to have the libraries listed as rsls)) </p>
]]></content:encoded>
			<wfw:commentRss>http://gauravj.com/blog/2009/06/creating-smaller-swfs-part-ii/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
