<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>lowpitch.com</title>
	
	<link>http://lowpitch.com/blog</link>
	<description>Flex and flex ramblings.</description>
	<lastBuildDate>Sun, 24 Aug 2008 22:57:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/lowpitch" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="lowpitch" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>IFlexModuleFactory – create and examine loaded Flex Modules</title>
		<link>http://lowpitch.com/blog/iflexmodulefactory-create-and-examine-loaded-flex-modules/</link>
		<comments>http://lowpitch.com/blog/iflexmodulefactory-create-and-examine-loaded-flex-modules/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 16:45:50 +0000</pubDate>
		<dc:creator>lowpitch</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Modules]]></category>

		<guid isPermaLink="false">http://lowpitch.com/blog/?p=20</guid>
		<description><![CDATA[In an earlier post I described how you can load in Flex modules using the ModuleManager class and the IModuleInfo interface, but I didn&#8217;t really mention IFlexModuleFactory which is pretty fundamental to the whole process. 
The factory is responsible for knowing how to create an instance of the module, and also provides access to certain [...]]]></description>
			<content:encoded><![CDATA[<p>In an earlier post I described how you can <a href="/blog/2008/08/17/modulemanager-and-imoduleinfo-loading-flex-modules-dynamically/">load in Flex modules using the <em>ModuleManager</em> class</a> and the <em>IModuleInfo</em> interface, but I didn&#8217;t really mention <em>IFlexModuleFactory</em> which is pretty fundamental to the whole process. </p>
<p>The factory is responsible for knowing how to create an instance of the module, and also provides access to certain properties of the module. It can get a little more complicated as the methods exposed by the factory become available at different stages of the load process, so you have to be careful not to try and retrieve information before it is available.</p>
<p>The factory is retrieved used by accessing the <em>factory</em> property of the IModuleInfo object, returned by <em>ModuleManager.getModule ()</em>. For more information on this, see the <a href="/blog/2008/08/17/modulemanager-and-imoduleinfo-loading-flex-modules-dynamically/">previous post</a>. The factory will be of type <em>IFlexModuleFactory</em>, which behind the scenes is implemented by <em>mx.managers.SystemManager</em>. You don&#8217;t really need to worry about this detail though, it&#8217;s enough to know about the two public methods exposed by the factory. </p>
<p>They are:</p>
<ul>
<li><strong>IFlexModuleFactory.create</strong> &#8211; This is really the most important method, and provides the mechanism for creating an instance of your loaded module. When enough of the module has been downloaded for this method to be called, the <em>IModuleInfo</em> object will dispatch a <em>ModuleEvent.READY</em> event, and the public property <em>IModuleInfo.ready</em> will return <em>true</em>. The method allows you to optionally pass in one or more parameters, using the &#8220;&#8230;rest&#8221; mechanism. These parameters are passed to your module&#8217;s constructor. Calling this method will create a new instance of your module, and the new instace of the method will be returned. The return type is specified as <em>Object</em>, so you will need to manually cast the returned object to the correct type in order to work with it.</li>
<li><strong>IFlexModuleFactory.info</strong> &#8211; This module allows you to find out a bit of information about the module which is being loaded. When enough of the module has been downloaded for this method to be called, the <em>IModuleInfo</em> object will dispatch a <em>ModuleEvent.SETUP</em> event, and the public property <em>IModuleInfo.setup</em> will return <em>true</em>. The method returns an <em>Object</em>, containing a variety of name/value pairs providing bits of information about the module. Examples of the kind of information that can be contained in the object includes width, height, a list of mixins used by the module, a list of RSLs used by the modules, a list of fonts embedded within the module, and information about the modules Main class name. The method can be useful for finding out the module&#8217;s layout information before you create an instance of it and add it to the display list.</li>
<p>Just to mention a quick &#8216;gotcha&#8217; that I encountered when first working with Modules&#8230; If you find that the call to <em>IModuleData.factory.create</em> for some reason returns null, or at some point in the process you get an Error #1009, most likely your module is using some classes which for some reason are not accessible once the SWFs have been compiled and optimised. A simple fix for this can be to reference the offending classes in some way in the main application to force them to be compiled. Common offenders are the &#8216;manager&#8217; classes, for example <em>DragManager</em>, <em>PopupManager</em> and <em>SystemManager</em>. </p>
<img src="http://feeds.feedburner.com/~r/lowpitch/~4/Dg86G_gV4bU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lowpitch.com/blog/iflexmodulefactory-create-and-examine-loaded-flex-modules/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ModuleManager and IModuleInfo – loading Flex Modules dynamically</title>
		<link>http://lowpitch.com/blog/modulemanager-and-imoduleinfo-loading-flex-modules-dynamically/</link>
		<comments>http://lowpitch.com/blog/modulemanager-and-imoduleinfo-loading-flex-modules-dynamically/#comments</comments>
		<pubDate>Sat, 16 Aug 2008 23:51:12 +0000</pubDate>
		<dc:creator>lowpitch</dc:creator>
				<category><![CDATA[Actionscript 2.0]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Modules]]></category>

		<guid isPermaLink="false">http://lowpitch.com/blog/?p=19</guid>
		<description><![CDATA[When you&#8217;re working with Flex Modules, most of the time the ModuleLoader component will be enough to get you up and running. It will quickly and easily let you load your Modules and add them to the display list, it&#8217;ll even dispatch an event to let you know when the Module is ready. Lovely stuff. [...]]]></description>
			<content:encoded><![CDATA[<p>When you&#8217;re working with Flex Modules, most of the time the <em><a target="_blank" href="http://livedocs.adobe.com/flex/201/langref/mx/modules/ModuleLoader.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/livedocs.adobe.com');">ModuleLoader</a></em> component will be enough to get you up and running. It will quickly and easily let you load your Modules and add them to the display list, it&#8217;ll even dispatch an event to let you know when the Module is ready. Lovely stuff. It&#8217;s very similar to the <em>SWFLoader</em> component with some added Module-related goodness thrown in.</p>
<p>However, there may be times when the <em>ModuleLoader</em> component may not be appropriate for your needs, and you&#8217;ll have to get your hands dirty and get involved with the lower-level <em><a href="http://livedocs.adobe.com/flex/201/langref/mx/modules/ModuleManager.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/livedocs.adobe.com');" target="_blank">ModuleManager</a></em> class. Some examples of when <em>ModuleLoader</em> might not be suitable include:</p>
<ul>
<li>Your Module is entirely non-visual, and therefore shouldn&#8217;t be added to the display list</li>
<li>Performance is really important to your application, and the extra overhead of the <em>ModuleLoader</em> container is something you need to remove to try and speed things up. (<em>ModuleLoader</em> essentially wraps your Module&#8217;s content in an extra container, incurring a slight performance hit).</li>
<li>You need greater control over how and when your modules are loaded and unloaded.</li>
</ul>
<h3>How does <em>ModuleManager</em> work?</h3>
<p><em>ModuleManager</em> maintains a map of modules, indexed by each module&#8217;s URL. Information about the module is stored as an object implementing <em><a href="http://livedocs.adobe.com/flex/201/langref/mx/modules/IModuleInfo.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/livedocs.adobe.com');" target="_blank">IModuleInfo</a></em>, and is retrieved by calling the <em>ModuleManager.getModule </em>method, passing in the module&#8217;s URL as the method&#8217;s only parameter. Once you&#8217;ve retrieved the<em> IModuleInfo </em>object, you can call its <em>load </em>method to starting loading in the module. While the module is loading, you can monitor the load progress using a variety of different events (of type <em>ModuleEvent</em>). Finally, when the module is loaded and available, you can create an instance of that module using <em>someModuleInfo.factory.create ()</em>.</p>
<p><span id="more-19"></span></p>
<h3><em>IModuleInfo</em> in more detail</h3>
<p>As mentioned above, <em>ModuleManager</em> maintains a mapping of URLs to modules. When you call <em>ModuleManager</em>&#8217;s static <em>getModule</em> method, it returns an object of type <em>IModuleInfo</em>. If you dig around within the <em>ModuleManager</em> class, you&#8217;ll see that this interface is implemented internally. It&#8217;s useful to know a little bit more about how you can interact with the <em>ModuleInfo</em>, what methods it exposes, and what events it dispatches.</p>
<p>There are four read-only public Boolean properties which offer information about the current state of the module. They can briefly be described:</p>
<ul>
<li><strong>IModuleInfo.error</strong> &#8211; This&#8217;ll be <em>true</em> if there was an error during loaded. You&#8217;re also notified of such an error by the <em>ModuleEvent.ERROR</em> event.</li>
<li><strong>IModuleInfo.loaded</strong> &#8211; This will be <em>true</em> if you&#8217;ve previously called the <em>IModuleInfo.load</em> method. Note: It does not necessarily signify that the module has been loaded, just that the module load has begun.</li>
<li><strong>IModuleInfo.setup</strong> &#8211; When enough of the module has loaded to be able to call the <em>factory.info ()</em> method, this flag will be <em>true</em>. When this point in the load has been reached, a<em> ModuleEvent.SETUP</em> event will be dispatched.</li>
<li><strong>IModuleInfo.ready</strong> &#8211; When enough of the module has been loaded for you to be able to instantiate an instance of the module using the <em>factory.create ()</em> method, this flag will be <em>true</em>. At this stage, a <em>ModuleEvent.READY</em> event will be dispatched. This property will typically become <em>true</em> at some point after the<em> IModuleInfo.setup</em> becomes true.</li>
</ul>
<p>There are some additional properties which can also be useful:</p>
<ul>
<li><strong>IModuleInfo.url </strong>fairly obviously returns the URL associated with the <em>IModuleInfo </em>object. This could be useful if you&#8217;re using one event handler to listen out for the events from multiple modules &#8211; the <em>event.currentTarget.url</em> property could be used to identify which module had sent out the event.</li>
<li><strong>IModuleInfo.data</strong> is an object which you can use to associate data with a particular module. The data will be shared across all instances of a particular module, so this property comes in handy if you want to share state, or user information between multiple instances of the same dynamically loaded module.</li>
<li><strong>IModuleInfo.factory</strong> is pretty important. Once the the module is &#8216;ready&#8217; (after the <em>ModuleEvent.READY</em> event has been dispatched, or when <em>IModuleInfo.ready</em> returns <em>true</em>), you can create an instance of your module by calling the <em>IFlexModuleFactory.create</em> method. Additionally, once the module has been &#8217;setup&#8217; (after <em>ModuleEvent.SETUP</em> has fired, or when <em>IModuleInfo.setup</em> returns <em>true</em>), you can query the <em>IFlexModuleFactory.info</em> method to find out a little more about the module that has been loaded.</li>
</ul>
<p>Next we should look at some of the methods exposed by an <em>IModuleInfo</em>:</p>
<ul>
<li><strong>IModuleInfo.load</strong> &#8211; This is the method you&#8217;ll call to start loading a module. You can optionally pass in information about the current ApplicationDomain and SecurityDomain if necessary. When the module is loading, events will be dispatched along the way as detailed below. Quite a nice feature here is that if the module has already been loaded, and you attempt to reload it, it&#8217;ll still dispatch the &#8220;setup&#8221; and &#8220;ready&#8221; events, meaning that you can use one set of event listener methods without needing to worry about whether the module is loading for the first time, or if it already exists in local memory. Snazzy&#8230;</li>
<li><strong>IModuleInfo.release</strong> &#8211; This method will release the current reference to the module. Internally, when this method is called, a reference counter will be decremented. If no references to the module exist after this method has been called, it will automatically be unloaded.</li>
<li><strong>IModuleInfo.unload</strong> &#8211; This triggers the unload of the module. It&#8217;s worth noting that if any references to the module exist, it will not be garbage collected. Also, calling this method won&#8217;t remove your module from the display list, you&#8217;ll have to do that too.</li>
</ul>
<p>Lastly, let&#8217;s look at the events dispatched&#8230; All the events listed here are instances of <em>ModuleEvent</em>, and for some of the events you&#8217;ll be able to access information about the load progress through the event&#8217;s <em>bytesLoaded</em> and <em>bytesTotal </em>properties.</p>
<ul>
<li><strong>ModuleEvent.ERROR</strong> &#8211; Rather obviously, this is dispatched when an error occurs during loading. This will also be indicated by the <em>IModuleInfo.error</em> property returning <em>true</em>.</li>
<li><strong>ModuleEvent.PROGRESS</strong> &#8211; This event is dispatched periodically during the load, giving you the chance to display a visual indication of load progress if you want. This is one of the events where the <em>bytesLoaded</em> and <em>bytesTotal</em> properties will be set.</li>
<li><strong>ModuleEvent.SETUP</strong> &#8211; This event is dispatched when enough of the module has downloaded for you to call the <em>IModuleInfo.factory.info</em> method. At this point, <em>IModuleInfo.setup</em> will also return <em>true</em>. This event does not allow you to access load progress through the <em>bytesLoaded</em> and <em>bytesTotal</em> properties.</li>
<li><strong>ModuleEvent.READY</strong> &#8211; This event is dispatched when enough of the module has downloaded for you to create an instance of the module through the<em> IModuleInfo.factory.create</em> method. From then on, <em>IModuleInfo.ready</em> will also return <em>true</em>. Apparently, this event is one where the <em>bytesLoaded</em> and <em>bytesTotal</em> properties should be set, although I&#8217;ve noticed that this isn&#8217;t always the case. I reckon it&#8217;s safer just to use the <em>ModuleEvent.PROGRESS</em> information for displaying progress information.</li>
<li><strong>ModuleEvent.UNLOAD</strong> &#8211; This event is dispatched when the module has been unloaded. No progress information is available through the event object with this event.</li>
</ul>
<h3>A simple example</h3>
<p>First, a basic module, <em>SimpleModule</em>, which does nothing more than display a label within a panel.</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;">SimpleModule.<span class="me1">mxml</span><br />
&lt;?<span class="kw3">xml</span> <span class="kw3">version</span>=<span class="st0">&quot;1.0&quot;</span> encoding=<span class="st0">&quot;utf-8&quot;</span>?&gt;<br />
&lt;mx:Module xmlns:mx=<span class="st0">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span class="st0">&quot;absolute&quot;</span>&gt;<br />
&nbsp; &nbsp; &lt;mx:TitleWindow x=<span class="st0">&quot;10&quot;</span> y=<span class="st0">&quot;10&quot;</span> <span class="kw3">width</span>=<span class="st0">&quot;120&quot;</span> <span class="kw3">height</span>=<span class="st0">&quot;100&quot;</span> layout=<span class="st0">&quot;absolute&quot;</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:<span class="kw3">Text</span> <span class="kw3">text</span>=<span class="st0">&quot;SimpleModule!&quot;</span> /&gt; <br />
&nbsp; &nbsp; &lt;/mx:TitleWindow&gt;<br />
&lt;/mx:Module&gt;</div></div>
<p>The the main application, which will load in this module and attach instances to a Tile control.</p>
<div class="codecolorer-container actionscript" style="height:280px;"><div class="codecolorer" style="font-family: monospace;">Shell.<span class="me1">mxml</span><br />
&lt;?<span class="kw3">xml</span> <span class="kw3">version</span>=<span class="st0">&quot;1.0&quot;</span> encoding=<span class="st0">&quot;utf-8&quot;</span>?&gt;<br />
&lt;mx:Application xmlns:mx=<span class="st0">&quot;http://www.adobe.com/2006/mxml&quot;</span> <br />
&nbsp; &nbsp; applicationComplete=<span class="st0">&quot;initApp()&quot;</span>&gt; <br />
&nbsp; &nbsp; &lt;mx:Script&gt; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;!<span class="br0">&#91;</span>CDATA<span class="br0">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> mx.<span class="me1">modules</span>.<span class="me1">Module</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> mx.<span class="me1">events</span>.<span class="me1">ModuleEvent</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> mx.<span class="me1">modules</span>.<span class="me1">ModuleManager</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> mx.<span class="me1">modules</span>.<span class="me1">IModuleInfo</span>; <br />
&nbsp;<br />
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;protected <span class="kw2">var</span> _moduleInfo:IModuleInfo;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">private</span> <span class="kw2">function</span> initApp<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span> <br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addToLog <span class="br0">&#40;</span><span class="st0">&quot;Application initialised&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// create the module - note, we're not loading it yet</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_moduleInfo = ModuleManager.<span class="me1">getModule</span><span class="br0">&#40;</span><span class="st0">&quot;SimpleModule.swf&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// add some listeners</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _moduleInfo.<span class="me1">addEventListener</span><span class="br0">&#40;</span>ModuleEvent.<span class="me1">READY</span>, onModuleReady<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _moduleInfo.<span class="me1">addEventListener</span><span class="br0">&#40;</span>ModuleEvent.<span class="me1">SETUP</span>, onModuleSetup<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _moduleInfo.<span class="me1">addEventListener</span><span class="br0">&#40;</span>ModuleEvent.<span class="me1">UNLOAD</span>, onModuleUnload<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _moduleInfo.<span class="me1">addEventListener</span><span class="br0">&#40;</span>ModuleEvent.<span class="me1">PROGRESS</span>, onModuleProgress<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">function</span> getModuleInfo <span class="br0">&#40;</span><span class="br0">&#41;</span> : IModuleInfo <span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// return the module info</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> _moduleInfo;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Adds output to the log<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; **/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">function</span> addToLog <span class="br0">&#40;</span>msg:<span class="kw3">String</span><span class="br0">&#41;</span> : <span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">log</span>.<span class="kw3">text</span> += msg + <span class="st0">&quot;<span class="es0">\n</span>&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// scroll to the bottom on the next frame</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callLater<span class="br0">&#40;</span>scrollToBottom<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">function</span> scrollToBottom <span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// scroll to the bottom</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">log</span>.<span class="me1">verticalScrollPosition</span> = <span class="kw3">log</span>.<span class="me1">maxVerticalScrollPosition</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Called when the &quot;load&quot; button is pressed<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Starts loading the module - when the module has been <br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* loaded, an instance of the module will be created<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* and added to the tile control<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;**/</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">private</span> <span class="kw2">function</span> loadModule <span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; addToLog <span class="br0">&#40;</span><span class="st0">&quot;Loading module&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// load the module</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;getModuleInfo<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="kw3">load</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Called when the &quot;unload&quot; button is pressed<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Removes all the instances of the module from the<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * tile control, then unloads the module<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">private</span> <span class="kw2">function</span> unloadModule <span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; addToLog <span class="br0">&#40;</span><span class="st0">&quot;Unloading module&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="co1">// clear out all the the instances </span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="co1">// of the module from the tile</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; tile.<span class="me1">removeAllChildren</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="co1">// unload the module</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; getModuleInfo<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">release</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="co1">//getModuleInfo().un</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="coMULTI">/**<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Handler for the ModuleEvent.PROGRESS event<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;**/</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;protected <span class="kw2">function</span> onModuleProgress <span class="br0">&#40;</span><span class="kw3">e</span>:ModuleEvent<span class="br0">&#41;</span> : <span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; addToLog <span class="br0">&#40;</span><span class="st0">&quot;ModuleEvent.PROGRESS received: &quot;</span> + <span class="kw3">e</span>.<span class="kw3">bytesLoaded</span> + <span class="st0">&quot; of &quot;</span> + <span class="kw3">e</span>.<span class="kw3">bytesTotal</span> + <span class="st0">&quot; loaded.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="coMULTI">/**<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Handler for the ModuleEvent.SETUP event<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;**/</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">private</span> <span class="kw2">function</span> onModuleSetup <span class="br0">&#40;</span><span class="kw3">e</span>:ModuleEvent<span class="br0">&#41;</span> : <span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; addToLog <span class="br0">&#40;</span><span class="st0">&quot;ModuleEvent.SETUP received&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="co1">// cast the currentTarget</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="kw2">var</span> moduleInfo:IModuleInfo = <span class="kw3">e</span>.<span class="me1">currentTarget</span> as IModuleInfo;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; addToLog <span class="br0">&#40;</span><span class="st0">&quot;Calling IModuleInfo.factory.info ()&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="co1">// grab the info and display information about it</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="kw2">var</span> info:<span class="kw3">Object</span> = moduleInfo.<span class="me1">factory</span>.<span class="me1">info</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> each:<span class="kw3">String</span> <span class="kw1">in</span> info<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; addToLog <span class="br0">&#40;</span><span class="st0">&quot;&nbsp; &nbsp; &nbsp;&quot;</span> + each + <span class="st0">&quot; = &quot;</span> + info<span class="br0">&#91;</span>each<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="coMULTI">/**<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Handler for the ModuleEvent.READY event<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;**/</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">private</span> <span class="kw2">function</span> onModuleReady <span class="br0">&#40;</span><span class="kw3">e</span>:ModuleEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span> <br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; addToLog <span class="br0">&#40;</span><span class="st0">&quot;ModuleEvent.READY received&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="co1">// cast the currentTarget</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="kw2">var</span> moduleInfo:IModuleInfo = <span class="kw3">e</span>.<span class="me1">currentTarget</span> as IModuleInfo;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; <span class="co1">// Add an instance of the module's class to the </span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// display list. </span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;addToLog <span class="br0">&#40;</span><span class="st0">&quot;Calling IModuleInfo.factory.create ()&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tile.<span class="me1">addChild</span><span class="br0">&#40;</span> moduleInfo.<span class="me1">factory</span>.<span class="me1">create</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> as SomeModule<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;addToLog <span class="br0">&#40;</span><span class="st0">&quot;SomeModule instance created and added to Display List&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="coMULTI">/**<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Handler for the ModuleEvent.UNLOAD event<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;**/</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw3">public</span> <span class="kw2">function</span> onModuleUnload <span class="br0">&#40;</span><span class="kw3">e</span>:ModuleEvent<span class="br0">&#41;</span> : <span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; addToLog <span class="br0">&#40;</span><span class="st0">&quot;ModuleEvent.UNLOAD received&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span> <br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#93;</span><span class="br0">&#93;</span>&gt; <br />
&nbsp; &nbsp; &lt;/mx:Script&gt; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &lt;mx:HBox <span class="kw3">width</span>=<span class="st0">&quot;100%&quot;</span> <span class="kw3">height</span>=<span class="st0">&quot;100%&quot;</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;mx:Tile id=<span class="st0">&quot;tile&quot;</span> <span class="kw3">width</span>=<span class="st0">&quot;100%&quot;</span> <span class="kw3">height</span>=<span class="st0">&quot;100%&quot;</span> /&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;mx:TextArea id=<span class="st0">&quot;log&quot;</span> <span class="kw3">width</span>=<span class="st0">&quot;100%&quot;</span> <span class="kw3">height</span>=<span class="st0">&quot;100%&quot;</span>/&gt;<br />
&nbsp; &nbsp; &lt;/mx:HBox&gt;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &lt;mx:ApplicationControlBar&gt;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&lt;mx:<span class="kw3">Button</span> label=<span class="st0">&quot;Load&quot;</span> click=<span class="st0">&quot;loadModule ()&quot;</span> /&gt;<br />
&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&lt;mx:<span class="kw3">Button</span> label=<span class="st0">&quot;Unload&quot;</span> click=<span class="st0">&quot;unloadModule ()&quot;</span>/&gt;<br />
&nbsp; &nbsp; &lt;/mx:ApplicationControlBar&gt;<br />
&nbsp; &nbsp; <br />
&lt;/mx:Application&gt;</div></div>
<p>What&#8217;s going on here? Well&#8230; when the app is created, <em>initApp</em> is called and the <em>IModuleInfo</em> object is created, and various event listeners are setup. When the user clicks the <strong>load</strong> button, the module is loaded. It doesn&#8217;t matter whether this is the first time the module is loaded, or if the module is already loaded and ready, the <em>ModuleEvent.READY</em> event will be dispatched to let us know that the module is ready, and that we can now create an instance of it. Within the handler for the <em>ModuleEvent.READY</em> event, we call <em>IModuleInfo.factory.create ()</em> and add the instance of the module to our Tile control.</p>
<p>When the user clicks the <strong>unload</strong> button, all instances of the Module that we&#8217;ve added to the Tile control are removed, and we call <em>IModuleInfo.unload</em> to unload the module. A log message from within the handler for <em>ModuleEvent.UNLOAD</em> demonstrates that this is happening.</p>
<p>Additionally, within the event handler for <em>ModuleEvent.SETUP</em> we show an example of the <em>IModuleInfo.factory.info</em> method &#8211; this&#8217;ll give you an example of the kind of information you can retrieve about the module, and from looking at the log messages you&#8217;ll be able to see the order in which these events are dispatched.</p>
<p>Lastly, here&#8217;s the finished example:</p>
<p>[kml_flashembed movie="/blog/bin/modulemanager/Shell.swf" height="400" width="600" /]</p>
<p>&nbsp;</p>
<p><em>(Update: I&#8217;ve <a href='/blog/2008/08/17/iflexmodulefactory-create-and-examine-loaded-flex-modules/'>explained the IFlexModuleFactory class in a little more detail here</a>.)</em></p>
<img src="http://feeds.feedburner.com/~r/lowpitch/~4/UU2i6H89L5s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lowpitch.com/blog/modulemanager-and-imoduleinfo-loading-flex-modules-dynamically/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>NativeWindow – using AIR windows with Actionscript (part 3 of 3)</title>
		<link>http://lowpitch.com/blog/nativewindow-using-air-windows-with-actionscript-part-3-of-3/</link>
		<comments>http://lowpitch.com/blog/nativewindow-using-air-windows-with-actionscript-part-3-of-3/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 14:58:35 +0000</pubDate>
		<dc:creator>lowpitch</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Actionscript 3.0]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[AS 3.0]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://lowpitch.com/blog/?p=17</guid>
		<description><![CDATA[In the first part of this post I covered each public property exposed by an instance of NativeWindow, before continuing to look at each public method in part two. In this final part of the series I&#8217;ll go through each of the different events dispatched by a NativeWindow during its lifetime.
Event.ACTIVATE
This event is sent out [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="/blog/2008/08/09/nativewindow-working-with-air-windows-with-actionscript-part-1-of-3/">first part of this post I covered each public property exposed by an instance of <em>NativeWindow</em></a>, before <a href="/blog/2008/08/10/nativewindow-using-air-windows-with-actionscript-part-2-of-3/">continuing to look at each public method in part two</a>. In this final part of the series I&#8217;ll go through each of the different events dispatched by a <em>NativeWindow</em> during its lifetime.</p>
<h3>Event.ACTIVATE</h3>
<p>This event is sent out when the window is activated. The event doesn&#8217;t go through a capture phase, and it won&#8217;t bubble up the Display List. Some examples of when your window may dispatch this event include:</p>
<ul>
<li>When the <em>NativeWindow </em>is first created and activated</li>
<li>When your application contains multiple <em>NativeWindows</em> and the user clicks on a window which previously was not the active window, giving it focus and bringing it to the front, the window will dispatch<em> Event.ACTIVATE</em></li>
<li>When you switch to another running application elsewhere on the system (an AIR application, or otherwise) and then switch back to this window, the event will be dispatched</li>
</ul>
<p><span id="more-17"></span></p>
<h3>Event.DEACTIVATE</h3>
<p>Similarly, this event is sent out when the window is deactivated. Again, the event doesn&#8217;t go through a capture phase, and it won&#8217;t bubble up the Display List. Some examples of when your window may dispatch this event include:</p>
<ul>
<li>When your application contains multiple <em>NativeWindows</em> and the user clicks on a window which previously was not the active window, giving it focus and bringing it to the front, the window which previously was active and had the focus will dispatch<em> Event.DEACTIVATE</em></li>
<li>When you switch to another running application elsewhere on the system (an AIR application, or otherwise), the previously active window will dispatch this event</li>
</ul>
<h3>Event.CLOSING<br />
Event.CLOSE</h3>
<p>I have grouped these two events as they are rather obviously connected. If the user clicks the exit button in the system chrome, for example, the sequence of events will be:</p>
<ul>
<li>An<em> Event.CLOSING</em> event is dispatched.</li>
<li>If this event is not cancelled (by calling the event&#8217;s <em>preventDefault</em> method) then the window will be closed, using <em>NativeWindow.close ()</em> which was discussed in the previous part of this post</li>
<li>Once the window has been closed, an <em>Event.CLOSE </em>event will be dispatched</li>
</ul>
<p>As mentioned in the previous part of this series, if you want to implement your own &#8220;close&#8221; functionality, perhaps by creating your own application chrome, you should follow this same logic to give other objects elsewhere in your application a chance to cancel the closing operation if necessary.</p>
<h3>NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING<br />
NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGE</h3>
<p>Again, these events are related in a similar way to <em>Event.CLOSING </em>and <em>Event.CLOSE</em>. The events are dispatched as part of one of the following operations:</p>
<ul>
<li>Maximizing a window which was previously un-maximized</li>
<li>Minimizing a window which was previously un-minimized</li>
<li>Restoring a window from a maximized or minimized state</li>
</ul>
<p>Unlike <em>Event.CLOSING</em> / <em>Event.CLOSE</em>, these events are defined within a subclass of <em>Event</em>, suggesting that there may be some added goodies within the event object that we can examine. As expected, there are a couple of properties available to tell us a little more about what is happening &#8211; to be more precise, the event object will tell us the value of the current state, and the value of the state we are moving to.</p>
<p>This is information is available by examining the <em>NativeWindowDisplayStateEvent.beforeDisplayState</em> and <em>NativeWindowDisplayStateEvent.afterDisplayState</em> properties. Both properties will be a string, matching one of the constants defined in the <em>NativeWindowDisplayState</em> class. These are:</p>
<ul>
<li><em>NativeWindowDisplayState.MAXIMIZED</em></li>
<li><em>NativeWindowDisplayState.MINIMIZED</em></li>
<li><em>NativeWindowDisplayState.NORMAL</em></li>
</ul>
<p>So, for example, when a user clicks the maximize button in the system chrome, the sequence of events will be:</p>
<ul>
<li>A<em> </em><em>NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING </em> event is dispatched.</li>
<li>If this event is not cancelled (by calling the event&#8217;s <em>preventDefault</em> method) then the state change will continue</li>
<li>Once the display state has updated, a <em>NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGE </em><em></em>event will be dispatched</li>
</ul>
<h3>NativeWindowBoundsEvent.MOVING<br />
NativeWindowBoundsEvent.MOVE</h3>
<p>These events are dispatched whenever the position of the window within the system desktop is moved. This could be for one of a number of reasons:</p>
<ul>
<li>A system-controlled move operation, for example the user dragging the window around with the system chrome</li>
<li>If the window is maximized</li>
<li>If the window is minimized</li>
<li>If the window is restored from maximized, or minimized state</li>
<li>If the window is moved programmatically, by setting the <em>x</em>, <em>y</em>, or <em>bounds </em>properties</li>
</ul>
<p>Again, the events here are defined in a custom subclass of <em>Event</em>, and this time the hidden treasures available to use are the <em>beforeBounds </em>and <em>afterBounds </em>properties, which obviously tell the us the &#8220;old&#8221; and &#8220;new&#8221; positions (and sizes, if we&#8217;re interested) of the window.</p>
<p>The sequence of events which would happen as part of a move operation would be:</p>
<ul>
<li>A<em> </em><em>NativeWindowBoundsEvent.MOVING</em> event is dispatched.</li>
<li>If this event is not cancelled (by calling the event&#8217;s <em>preventDefault</em> method) then the move operation will continue</li>
<li>Once the window has been moved, a <em>NativeWindowBoundsEvent.MOVE </em>event will be dispatched</li>
</ul>
<p>It&#8217;s worth noting that when a window is being dragged around by the user, it will repeatedly go through this cycle, moving the window in small steps. If at any stage, one of the <em>NativeWindowBoundsEvent.MOVING </em>events is cancelled, the move operation will terminate. This includes moves triggered by the <em>NativeWindow.startMove () </em>method discussed in the previous part of the post.</p>
<h3>NativeWindowBoundsEvent.RESIZING<br />
NativeWindowBoundsEvent.RESIZE</h3>
<p>The events listed here work similarly to those dispatched as part of the move operation, although this time they apply to resize operations. Resize operations may occur for one of a number of reasons:</p>
<ul>
<li>A system-controlled resize operation, for example the user resizing the window via the system resize handles</li>
<li>If the window is maximized</li>
<li>If the window is minimised</li>
<li>If the window is restored from maximized, or minimized state</li>
<li>If the window is resized programmatically, by setting the <em>width</em>, <em>height</em> or <em>bounds </em>properties</li>
</ul>
<p>The event type is the same as with the move operation, giving us access to the &#8220;old&#8221; and &#8220;new&#8221; sizes of the window. The sequence of events works in the same way too:</p>
<ul>
<li>A<em> </em><em>NativeWindowBoundsEvent.RESIZING</em> event is dispatched.</li>
<li>If this event is not cancelled (by calling the event&#8217;s <em>preventDefault</em> method) then the resize operation will continue</li>
<li>Once the window has been resized, a <em>NativeWindowBoundsEvent.RESIZE </em>event will be dispatched</li>
</ul>
<p>Lastly, as with the move operation, when a window is being resized by the user, it will repeatedly go through this cycle, resizing the window in small steps. If at any stage, one of the <em>NativeWindowBoundsEvent.RESIZING </em>events is cancelled, the resize operation will terminate. This includes resizes triggered by the <em>NativeWindow.startResize () </em>method discussed in the previous part of the post.</p>
<h3>That&#8217;s all of &#8216;em</h3>
<p>So that&#8217;s all of the events covered now. You can see how to handle various different operations that will happen during the lifetime of your window. You can react to (and cancel, if you wish) moves, resizes and the closing of your window. Most importantly, if creating your own application chrome, or implementing your own close/resize/move functionality, you should try and keep these operations cancelable. It&#8217;ll make things much easier down the line if, for example, you want to stop the user closing the window before the save their work.</p>
<h3>Conclusion</h3>
<p>I&#8217;ve now gone through all of the properties, methods and events exposed by a <em>NativeWindow</em>. As you can see, the API is fairly thorough, but extremely simple to use. It makes it easy to create, move, resize, hide, and reorder your windows, and to control how your windows will look and behave. I will try and carry this all through into an example at some point, but for now I hope this explanation has been fairly useful. Gimme a shout if there&#8217;s anything I&#8217;ve missed&#8230;&#8230;</p>
<img src="http://feeds.feedburner.com/~r/lowpitch/~4/twqLuygpcFE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lowpitch.com/blog/nativewindow-using-air-windows-with-actionscript-part-3-of-3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>NativeWindow – using AIR windows with Actionscript (part 2 of 3)</title>
		<link>http://lowpitch.com/blog/nativewindow-using-air-windows-with-actionscript-part-2-of-3/</link>
		<comments>http://lowpitch.com/blog/nativewindow-using-air-windows-with-actionscript-part-2-of-3/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 00:10:13 +0000</pubDate>
		<dc:creator>lowpitch</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Actionscript 3.0]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[AS 3.0]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://lowpitch.com/blog/?p=16</guid>
		<description><![CDATA[In the previous post I listed the public properties exposed by an instance of NativeWindow. In this post, I&#8217;ll be going through NativeWindow&#8217;s public methods. The third and final part of this series will explain the Events dispatched by a NativeWindow during its lifetime.
Once a NativeWindow has been created, you can call methods to move, [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous post I listed the <a href="/blog/2008/08/09/nativewindow-working-with-air-windows-with-actionscript-part-1-of-3/">public properties exposed by an instance of <em>NativeWindow</em></a>. In this post, I&#8217;ll be going through <em>NativeWindow&#8217;</em>s public methods. The third and final part of this series will explain the Events dispatched by a <em>NativeWindow </em>during its lifetime.</p>
<p>Once a <em>NativeWindow</em> has been created, you can call methods to move, resize, reposition, activate and close the window. All these methods are fairly simple, with the odd gotcha thrown in for good measure. So, here&#8217;s a description of each of these methods&#8230;</p>
<p><span id="more-16"></span></p>
<h3><strong>NativeWindow.activate ()</strong></h3>
<p>Calling <em>NativeWindow.activate () </em>firstly sets the <em>visible </em>of the window to <em>true</em>. Next, the window is brought to the front. Lastly, the window is given keyboard and mouse focus. Typically, you&#8217;d call the <em>activate </em>method shortly after creating it.</p>
<h3>NativeWindow.close ()</h3>
<p>This method, rather obviously, closes the window. This has several implications&#8230; First of all, once a window has been closed, it cannot be re-opened. References to a window that has been closed will exist, but some methods and properties of that window will no longer be available, and if accessed an exception will be thrown. Any DisplayObjects that were within the window, and not referenced anywhere else, will be available for Garbage Collection.</p>
<p>If you just want to close a window temporarily, consider setting its <em>visible </em>to false.</p>
<p>One other thing to mention here&#8230; when you call <em>NativeWindow.close () </em>no event is dispatched to signify the window is about to close. A Close event is sent out after the window has been closed, but if you want the action of closing the window to be preventable, you&#8217;ll need to send out a Closing event (take a look at <em>air.Event.CLOSING</em> which is more than suitable) giving interested parties a chance to set <em>preventDefault </em>on that event. If , and only if, the isDefaultPrevented () method of your custom Closing event returns <em>false</em>, then you can proceed to close your window.</p>
<h3>NativeWindow.globalToScreen (globalPoint:Point)</h3>
<p>This method is kind of like MovieClip.localToGlobal from days gone by, but takes things one step further. The method takes a Point object, representing co-ordinates relative to the origin of the <em>NativeWindow</em>&#8217;s stage and converts it to a Point object representing the same position relative to the top-left position of the system&#8217;s desktop.</p>
<h3>NativeWindow.maximize ()<br />
NativeWindow.minimize ()<br />
NativeWindow.restore ()</h3>
<p>This three methods are kind of similar, so I&#8217;ve grouped them here. The methods will attempt to maximize, minimize or restore the <em>NativeWindow</em> programmatically.</p>
<p>All three operations happen asynchronously, and you can detect the completion of these operations by listening out for a <em>NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGE </em>event.</p>
<p>Something important to note here, when the user tries to perform one of these three actions from the system chrome, a <em>NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING</em> event is dispatched, giving interested parties a chance to cancel the operation (by calling <em>preventDefault</em> <em>() </em>). To ensure this behaviour is also preventable when performing these options with code, you&#8217;ll need to manually dispatch a suitable <em>DISPLAY_STATE_CHANGING </em>event, and then investigate the event&#8217;s <em>isDefaultPrevented () </em>method before continuing the maximize / minimize / restore operation.</p>
<h3>NativeWindow.notifyUser (type:String)</h3>
<p>Assuming that <em>NativeWindow.supportsNotification </em>is <em>true </em>(see the previous post for more details), calling this method will trigger a visual cue to the user that something has happened. There are currently two different types of cue that you can trigger.<em> NotificationType.INFORMATIONAL</em> will trigger the cue for a short period of time, whereas <em>NotificationType.CRITICAL</em> will ensure the visial cue continues until the user brings focus to the window.</p>
<h3>NativeWindow.orderInBackOf (window:NativeWindow)<br />
NativeWindow.orderInFrontOf (window:NativeWindow)</h3>
<p>These methods will move the window directly behind, or in front of, the window passed as the parameter. As always, some things to note.</p>
<ul>
<li>Attempting to re-order minimized or hidden windows is not possible.</li>
<li>The methods will return true if the window was moved successfully, and false otherwise.</li>
<li>If the window that you are moving behind, or in front of, has a different <em>alwaysInFront</em> property than the current window (<a href="/blog/2008/08/09/nativewindow-working-with-air-windows-with-actionscript-part-1-of-3/">see the previous post for more information</a>), the window you are moving will have its <em>alwaysInFront </em>property changed to match this other window.</li>
<li>When a window is reordered, it is not activated, and is not given the focus.</li>
</ul>
<h3>NativeWindow.orderToBack ()<br />
NativeWindow.orderToFront ()</h3>
<p>These methods will move the window behind, or in front of, all other visible windows. Things to note here include:</p>
<ul>
<li>Attempting to re-order minimized or hidden windows is not possible.</li>
<li>The methods will return true if the window was moved successfully, and false otherwise.</li>
<li>If the window&#8217;s <em>alwaysInFront </em>property is set to <em>false, </em>then calling <em>orderToFront</em> () will not move the window in front of any windows which have <em>alwaysInFront </em>set to <em>true.</em></li>
<li>Likewise, if the window&#8217;s <em>alwaysInFront </em>property is set to <em>true </em>then calling <em>orderToBack () </em>will not move the window behind any windows which have <em>alwaysInFront </em>set to <em>false</em>.</li>
</ul>
<h3>NativeWindow.startMove ()</h3>
<p>Calling this method will trigger the start of a system-controlled move of the window. What this means is that you can create your own custom drag bar, listen to <em>MouseEvent.MOUSE_DOWN</em> events from that drag bar and then, in the event handler, call <em>myNativeWindow.startMove () </em>to begin the move process.</p>
<p>While the window is being moved, the window dispatches various events. This will be covered in more detail in the next part of this discussion. However, it&#8217;s worth mentioning here that a Move sequence can be terminated by handling, and then cancelling, the NativeWindowBoundsEvent.MOVING event.</p>
<p>The method returns <em>true</em> if the move process was successfully initiated, or <em>false</em> otherwise (for example, if the window was already maximized).</p>
<h3>NativeWindow.startResize (edgeOrCorner:String)</h3>
<p>This method is very similar to <em>NativeWindow.startMove (). </em>It allows you to create your own resize handles, listen to MouseMove.MOUSE_DOWN events from that resize handle, and then call myNativeWindow.startResize () from the event handler to begin the resize.</p>
<p>Again, while the window is being resized, it dispatches various events which will be covered in the next part of this discussion. As with moving a window, you can terminate the resize sequence by handling, then cancelling, the NativeWindowBoundsEvent.RESIZING event.</p>
<p>The method returns <em>true</em> if the resize process was successfully initiated, or <em>false</em> otherwise (for example, if the window was already maximized).</p>
<p>Unlike the <em>startMove </em>method, this method accepts a parameter. This parameter specifies which corner or edge of the window the resize should stem from. The possible values of this parameter are specified as constants in the <em>NativeWindowResize</em> class. The default is <em>NativeWindowResize.BOTTOM_RIGHT.</em></p>
<h3>That&#8217;s it!</h3>
<p>That&#8217;s all of the public methods exposed by the <em>NativeWindow</em> class. You can see how to move, resize and re-order windows within the operating system, how to visually alert the user that the application needs their attention, how to activate the window, and how to close it. In the final part of this topic I&#8217;ll describe the various different Events that are dispatched by a <em>NativeWindow</em> during it&#8217;s lifetime.</p>
<p>Update: Part 3 of this post <a href="/blog/2008/08/10/nativewindow-using-air-windows-with-actionscript-part-3-of-3/">can be found here</a>.</p>
<img src="http://feeds.feedburner.com/~r/lowpitch/~4/BAZUAhls7VI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lowpitch.com/blog/nativewindow-using-air-windows-with-actionscript-part-2-of-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NativeWindow – using AIR windows with Actionscript (part 1 of 3)</title>
		<link>http://lowpitch.com/blog/nativewindow-working-with-air-windows-with-actionscript-part-1-of-3/</link>
		<comments>http://lowpitch.com/blog/nativewindow-working-with-air-windows-with-actionscript-part-1-of-3/#comments</comments>
		<pubDate>Sat, 09 Aug 2008 18:06:56 +0000</pubDate>
		<dc:creator>lowpitch</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Actionscript 3.0]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[AS 3.0]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://lowpitch.com/blog/?p=12</guid>
		<description><![CDATA[In a previous post I described how to create and activate an application window when creating an Actionscript AIR project in Flex Builder 3. I then went on to describe in a little more detail the various properties of the NativeWindowInitOptions class, and how setting some or all of these properties can affect the style [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post I described how to <a href="/blog/2008/08/04/creating-air-projects-with-actionscript/">create and activate an application window when creating an Actionscript AIR project in Flex Builder 3</a>. I then went on to describe in a little more detail the various <a href="/blog/2008/08/06/nativewindowinitoptions-configure-your-air-nativewindow/">properties of the NativeWindowInitOptions class</a>, and how setting some or all of these properties can affect the style and behaviour of the window that will be created. I guess the final step is to describe how to work with the window once it has been created, starting with an explanation of the <em>flash.display.NativeWindow</em> class. There&#8217;s quite a bit of functionality contained within the class, so I&#8217;ll break this post up into three. Part 1 will describe the properties of a NativeWindow, part 2 will list the public methods and finally part 3 will discuss the events dispatched by the Window throughout its lifetime.</p>
<p>As previously mentioned, an application window is created and displayed in Adobe AIR by creating a new instance of the <em>NativeWindow </em>class, and then calling this instance&#8217;s <em>activate</em> method. Content is added to the window by adding <em>DisplayObject</em>s to the <em>stage</em> property of the <em>NativeWindow</em> instance.</p>
<p>Here are descriptions of each of the <em>NativeWindow</em>&#8217;s public properties&#8230;</p>
<p><span id="more-12"></span></p>
<h3>NativeWindow.active</h3>
<p>Indicates whether or not a <em>NativeWindow</em> is the active application window or not. So for example, if your application has opened two windows, depending on which of those windows is currently being interacted with, one window&#8217;s <em>active </em>property will return <em>true, </em>and the other will return <em>false. </em> To activate a NativeWindow programmatically, you can call <em>someNativeWindow.activate ()</em>.</p>
<h3>NativeWindow.alwaysInFront</h3>
<p>Rather obviously, this Boolean property will specify whether or not the window will always be on top of other windows (even those from other applications). For most situations, this property should be left as <em>false </em>unless the window needs to stay on top of other applications for a particular reason.</p>
<p>It&#8217;s possible to trigger certain behaviours by changing the value of this property. Setting this value from <em>false </em>to <em>true </em>will bring the window in front of all other application windows. Changing the value from <em>true </em>to <em>false </em>will send the window behind any other windows on the system with <em>alwaysInFront </em>set to <em>true</em>, but still on top of all other windows.</p>
<h3>NativeWindow.bounds</h3>
<p style="text-align: left;">This property, of type <em>flash.geom.Rectangle, </em>contains information about the position and size of the window <strong>including any system chrome</strong>. The fact that these dimensions include the system chrome is important. The size of the stage of the window (the area which is used for any content added to the stage), is equal to the size of the window minus the size of the chrome.</p>
<p style="text-align: left;">Setting the <em>bounds </em>property of a window is the same as setting it&#8217;s <em>x</em>, <em>y</em>, <em>width</em>, and <em>height</em> properties all in one step.</p>
<p style="text-align: left;">If you attempt to set the size of the window to an illegal size (for example, smaller than the <em>minSize</em><strong> </strong>property, larger than the <em>maxSize</em> property, or larger or smaller than the limits imposed by the OS) then the window will be sized to the nearest acceptable value. This applies when setting the size of the window through the <em>bounds</em> property, and also when setting the <em>width </em>or <em>height </em>directly.</p>
<p style="text-align: left;">This property will be revisited in part 3 of this post when Events dispatched by the window will be discussed.</p>
<h3>NativeWindow.closed</h3>
<p style="text-align: left;">This Boolean indicates whether or not the window has been closed. Attempting to access certain properties or methods of a window which has been closed will cause an<em> IllegalOperationError</em> to be the thrown.</p>
<h3>NativeWindow.displayState</h3>
<p style="text-align: left;">This property is a string, and describes the window&#8217;s current display state. The possible values of this property are fairly self explanatory:</p>
<ul>
<li><em>NativeWindowDisplayState.NORMAL</em></li>
<li><em>NativeWindowDisplayState.MINIMIZED</em></li>
<li><em>NativeWindowDisplayState.MAXIMIZED</em></li>
</ul>
<h3>NativeWindow.height</h3>
<p>The height of the window <strong>including </strong>any system chrome. To access the height of the usable area within the system chrome, use <em>stage.stageHeight</em>.</p>
<h3>NativeWindow.maximizable</h3>
<p>This Boolean tells us whether or not the window is maximizable. The value is set using the NativeWindowInitOptions instance, passed to the constructor, and cannot be modified once the window has been created.</p>
<h3>NativeWindow.maxSize</h3>
<p>This property, of type <em>flash.geom.Point</em> (<em>Point.x</em> corresponds to the <em>width</em>, <em>Point.y</em> corresponds to the <em>height</em>), allows us to specify the maximum size of the window. This maximum size is enforced when resizing is triggered both by Actionscript, or by the user clicking the OS maximize button.</p>
<h3>NativeWindow.menu</h3>
<p>This property allows us to create a <em>flash.display.NativeMenu </em>to be displayed for the Window. Note, for the <em>menu </em>to be displayed, <em>NativeWindow.supportsMenu </em>must be set to <em>true</em>, and <em>NativeWindow.systemChrome</em> must not be set to <em>NativeWindowSystemChrome.NONE</em>.</p>
<h3>NativeWindow.minimizable</h3>
<p>This Boolean tells us whether or not the window is minimizable. The value is set using the NativeWindowInitOptions instance, passed to the constructor, and cannot be modified once the window has been created.</p>
<h3>NativeWindow.minSize</h3>
<p>This property, of type <em>flash.geom.Point</em>, allows us to specify the minimum size of the window. This minimum size is enforced when resizing is triggered either by Actionscript, or by the user clicking the OS maximize button.</p>
<h3>NativeWindow.resizable</h3>
<p>This Boolean tells us whether or not the window is resizable. The value is set using the NativeWindowInitOptions instance, passed to the constructor, and cannot be modified once the window has been created.</p>
<h3>NativeWindow.stage</h3>
<p>This property gives us <strong>read-only </strong>access to the <em>flash.display.Stage </em>object at the root of this window&#8217;s display list. For content to be displayed in the window, it must be added to the <em>stage</em> (or to a descendant of a <em>DisplayObject </em>already added to the stage).</p>
<h3>NativeWindow.supportsMenu</h3>
<p>Setting this Boolean to <em>true</em> allows us to display a <em>NativeMenu </em>for this window by setting the <em>menu </em>property (see above). For the menu to be displayed, the <em>systemChrome </em>property must not be set to <em>NativeWindowSystemChrome.NONE</em>.</p>
<h3>NativeWindow.supportsNotification</h3>
<p>You know how sometimes an application can flash in some way to show you that something has happened? On Windows, the window&#8217;s entry in the task bar might flash, or on OS X the icon in the dock might jump around a bit. This property tells you whether or not this functionality is supported on the system on which your application is running. If this property is <em>true</em>, you can trigger this visual cue with the window&#8217;s <em>notifyUser</em> method.</p>
<h3>NativeWindow.supportsTransparency</h3>
<p>This Boolean property tells us whether or not transparency of windows is supported in the current environment. If the <em>transparent </em>property of the window is set to <em>true</em>, and <em>supportsTransparency </em>is also <em>true</em>, then your transparent window will work as expected. If, however, <em>supportsTransparency </em>is <em>false</em>, the your transparent window will not render correctly. Any pixels intended to have an <em>alpha </em>of 0 will be rendered as black. It&#8217;s also worth noting that this value may change during the lifetime of your window, for example if a user updates a preference setting.</p>
<h3>NativeWindow.systemChrome</h3>
<p>This read-only property tells us what system chrome is employed by the window. The property will match one of the constants defined in the<em> NativeWindowSystemChrome</em> class. The <em>systemChrome</em> is originally specified in the <em>NativeWindowInitOptions</em> class which is passed to the <em>NativeWindow</em> constructor and <strong>cannot </strong>be altered once the window has been created.</p>
<h3>NativeWindow.systemMaxSize</h3>
<p>This read-only property lets us find out the maximum size of a window allowed by the operating system. It&#8217;s a <em>flash.geom.Point</em> object with <em>Point.x </em>representing the <em>width</em>, and <em>Point.y</em> representing the height.</p>
<h3>NativeWindow.systemMinSize</h3>
<p>Similarly, thisy property lets us find out the minimum size of a window allowed by the operating system. Again, it&#8217;s a <em>flash.geom.Point</em> object with <em>Point.x </em>representing the maximum <em>width</em>, and <em>Point.y</em> representing the maximum <em>height</em>.</p>
<h3>NativeWindow.title</h3>
<p>This property lets us set the window title. Assuming we are using system chrome, the title will be displayed in the title bar, and also in other OS-specific locations such as the Windows taskbar.</p>
<h3>NativeWindow.transparent</h3>
<p>This Boolean tells us whether or not the window supports transparency. The value is set using the NativeWindowInitOptions instance, passed to the constructor, and cannot be modified once the window has been created. This property also relies upon the <em>supportsTransparency </em>property (see above) being equal to <em>true</em>.</p>
<h3>NativeWindow.type</h3>
<p>This read-only property tells us what window type setting was chosen when the window was created. The property will match one of the string constants defined in the<em> NativeWindowType</em> class. The <em>type </em>is originally specified in the <em>NativeWindowInitOptions</em> class which is passed to the <em>NativeWindow</em> constructor and <strong>cannot </strong>be altered once the window has been created.</p>
<h3>NativeWindow.visible</h3>
<p>This boolean allows us to control whether or not the window is visible. A window which is invisible will still be accessible, and all its methods and properties will still exist, it just will be hidden.</p>
<p>When a window is first created, it&#8217;s <em>visible </em>property will default to <em>false</em>. To show a window, you have to either call the <em>activate </em>method or set the <em>visible </em>property to true.</p>
<h3>NativeWindow.width</h3>
<p>The width of the window <strong>including </strong>any system chrome. To access the width of the usable area within the system chrome, use <em>stage.stageWidth</em>.</p>
<h3>NativeWindow.x</h3>
<p>This property is the horizontal co-ordinate of the window&#8217;s top left corner relative to the operating system&#8217;s desktop.</p>
<h3>NativeWindow.y</h3>
<p>This property is the vertical co-ordinate of the window&#8217;s top left corner relative to the operating system&#8217;s desktop. There are two ways to programmatically reposition a <em>NativeWindow</em>. Either set it&#8217;s <em>x </em>and <em>y </em>properties to new values, or set the <em>bounds</em> property as described above.</p>
<h3>That&#8217;s it!</h3>
<p>So that&#8217;s all the properties of the <em>NativeWindow</em> class &#8211; there&#8217;s quite a few of them, but there&#8217;s nothing too complicated in there. In the second part of this post I&#8217;ll list the public methods exposed by <em>NativeWindow</em>.</p>
<p>Update: Part 2 of this post <a href="/blog/2008/08/10/nativewindow-using-air-windows-with-actionscript-part-2-of-3/">can be found here</a>.</p>
<img src="http://feeds.feedburner.com/~r/lowpitch/~4/qnfy9SFZGZE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lowpitch.com/blog/nativewindow-working-with-air-windows-with-actionscript-part-1-of-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NativeWindowInitOptions – configure your AIR NativeWindow</title>
		<link>http://lowpitch.com/blog/nativewindowinitoptions-configure-your-air-nativewindow/</link>
		<comments>http://lowpitch.com/blog/nativewindowinitoptions-configure-your-air-nativewindow/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 21:45:35 +0000</pubDate>
		<dc:creator>lowpitch</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Actionscript 3.0]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[AS 3.0]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://lowpitch.com/blog/?p=13</guid>
		<description><![CDATA[In a previous post I listed some code to create and activate a NativeWindow in an Actionscript AIR project without really explaining what was going on behind the scenes. In this post, I&#8217;ll explain a little more about the NativeWindowInitOptions class, and how you can control the appearance and behaviour of the NativeWindow that you [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a title="_blank" href="http://lowpitch.com/blog/2008/08/06/nativewindow-nativewindowinitoptions-creating-air-projects-with-actionscript/" >previous post</a> I listed some code to create and activate a <em>NativeWindow</em> in an Actionscript AIR project without really explaining what was going on behind the scenes. In this post, I&#8217;ll explain a little more about the <em>NativeWindowInitOptions</em> class, and how you can control the appearance and behaviour of the <em>NativeWindow</em> that you create.</p>
<p>The code I used in the previous post was this:</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;"><span class="kw2">var</span> windowOptions:NativeWindowInitOptions = <span class="kw2">new</span> NativeWindowInitOptions<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> mainWindow:NativeWindow = <span class="kw2">new</span> NativeWindow<span class="br0">&#40;</span>windowOptions<span class="br0">&#41;</span>;</div></div>
<p>Fairly obviously, the <em>NativeWindow</em> constructor accepts an instance of <em>NativeWindowInitOptions</em> as its first parameter. In the previous example, I&#8217;m not doing anything fancy with the <em>NativeWindowInitOptions</em> instance that I create, I just create a new instance of the class and pass it to the <em>NativeWindow</em> constructor. As a result, all the various properties will have their default values, and the <em>NativeWindow</em> will be totally default. How terribly dull. Here&#8217;s a list of properties of <em>NativeWindowInitOptions</em> that you can play with to control how the <em>NativeWindow</em> will look and behave when it&#8217;s created&#8230;.</p>
<p><span id="more-13"></span></p>
<h3>NativeWindowInitOptions.minimizable</h3>
<p>This is pretty obvious, but if the <em>minimizable</em> property is set to <em>true</em> then the <em>NativeWindow</em> will be minimizable. Otherwise, it won&#8217;t. This can have knock-on consequences on the appearance of the window. For example, when running on Windows, the value of this property can affect the window menu, and the window minimize button. If you don&#8217;t manually set this property, it will default to <em>true</em>.</p>
<h3>NativeWindowInitOptions.maximizable</h3>
<p>Again, if the <em>maximizable</em> property is set to <em>true</em> then the NativeWindow will be <em>maximizable</em>. This property may also affect the appearance of the window on some operating systems. This property also defaults to <em>true</em>.</p>
<h3>NativeWindowInitOptions.resizable</h3>
<p>The <em>resizable</em> property controls whether or not the window can be resized. When set to <em>false</em>, the little resize handles won&#8217;t appear in the corner of the window. Again, this property defaults to <em>true</em>.</p>
<p>(<em>Note: When running on OS X, resizable and maximizable must both be set to false to prevent resizing. If you do one and not the other, it won&#8217;t work as expected</em>).</p>
<h3>NativeWindowInitOptions.systemChrome</h3>
<p>The <em>systemChrome</em> property controls how the chrome of your <em>NativeWindow</em> will look. The default value is <em>NativeWindowSystemChrome.STANDARD</em>, and with this value the AIR application will adopt the look of a typical window in the operating system on which it&#8217;s running. So, when your AIR app runs on Windows, the chrome / menu bar / etc. will look like a typical Windows app. When the same app runs on OS X, it&#8217;ll look like a Mac app. The other possible value of this property is <em>NativeWindowSystemChrome.NONE</em>, which essentially hides the default system chrome, enabling you to create your own chrome for your <em>NativeWindow</em>.</p>
<h3>NativeWindowInitOptions.transparent</h3>
<p>This property, which defaults to <em>false</em>, determines whether or not the window supports transparency. If set to <em>true</em>, your window will be blended with the desktop beneath it. In other words, if you have content with an <em>alpha</em> of 0.5, your desktop will be visible through your window.</p>
<p>When this property is set to true, any areas of a window which do not contain a<em> DisplayObject</em>, or that only contain a <em>DisplayObject </em>with an alpha value under a certain threshold, will no longer react to mouse events. If a user clicks one of these &#8220;empty&#8221; areas, the mouse click will be handled by whatever happens to be sitting beneath your window, even though the click may have occurred within your application&#8217;s rectangle. The &#8216;threshold&#8217; mentioned above varies between different operating systems but tends to fall between 0.05 and 0.01.</p>
<p>For those of you who may wish to create an application shaped like a clown&#8217;s face, this property is for you.<br />
(Note: You can only set this property to <em>false</em> if you have also specified a value of <em>NativeWindowSystemChrome.NONE</em> for the <em>systemChrome</em>.)</p>
<h3>NativeWindowInitOptions.type</h3>
<p>There are currently three possible values for this property:</p>
<ul>
<li><strong>NativeWindowType.NORMAL</strong><br />
This is the default value for this property, and a <em>NativeWindow</em> of this type will use standard-sized chrome, and will also show up in the Windows taskbar or OS X window menu. Here is a screen shot of a very basic AIR window of this type running on OS X<br />
<br/><br />
<img class="alignnone size-full wp-image-15" title="NativeWindowType.NORMAL" src="http://lowpitch.com/blog/wp-content/uploads/2008/08/normalchrome.jpg" alt="Screenshot of NativeWindow with type NativeWindowType.NORMAL" /></li>
<li><strong>NativeWindowType.UTILITY</strong><br />
A <em>NativeWindow</em> of this type still uses the system chrome, but in slimmed down form. Additionally, this type of window does not show up in the Windows taskbar or OS X window menu. Here is a screen shot of an AIR window of this type as a comparison.<br />
<br/><br />
<img title="NativeWindowType.UTILITY" src="http://lowpitch.com/blog/wp-content/uploads/2008/08/utilitychrome.jpg" alt="Screenshot of NativeWindow with type NativeWindowType.UTILITY" /></p>
<p><em>(Note: One thing I have noticed about windows of this type is that, on OS X at least, they are not minimizable by default. They are maximizable and resizable, but the minimize button is disabled)</em></li>
<li><strong>NativeWindowType.LIGHTWEIGHT</strong><br />
A <em>NativeWindow</em> of this type has no system chrome, and in fact the value of the <em>systemChrome</em> mentioned above must be set to <em>NativeWindowSystemChrome.NONE</em> for this to work. An example of this type of window might be a notification message, similar to the one that pops up when you recieve a new IM in MSN Messenger, or a new email in Gmail Notifier.</li>
</ul>
<h3>These properties cannot be altered after the NativeWindow has been created</h3>
<p>It is important to mention that <strong>each of these properties must be set on the <em>NativeWindowInitOptions</em> object before you attempt to create your <em>NativeWindow</em></strong>. If you create your <em>NativeWindow</em>, passing the <em>NativeWindowInitOptions</em> object in as the parameter, and then subsequently attempt to alter the value of any of these properties, it will have no effect. Once the <em>NativeWindow</em> has been created, the properties mentioned above are set in stone.</p>
<h3>Putting it all together</h3>
<p>Finally, a code sample. The following snippet will create and activate a NativeWindow that supports transparency, has no chrome, is not maximizable, resizable or minimizable, and is of type <em>NativeWindowType.UTILITY</em>, meaning it will not be displayed in the Windows taskbar or OS X window menu. It then adds itself to the stage of the newly created window.</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;"><span class="kw2">var</span> windowOptions:NativeWindowInitOptions = <span class="kw2">new</span> NativeWindowInitOptions <span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
windowOptions.<span class="me1">minimizable</span> = <span class="kw2">false</span>;<br />
windowOptions.<span class="me1">maximizable</span> = <span class="kw2">false</span>;<br />
windowOptions.<span class="me1">resizable</span> = <span class="kw2">false</span>;<br />
windowOptions.<span class="kw3">type</span> = NativeWindowType.<span class="me1">UTILITY</span>;<br />
windowOptions.<span class="me1">systemChrome</span> = NativeWindowSystemChrome.<span class="me1">NONE</span>;<br />
windowOptions.<span class="me1">transparent</span> = <span class="kw2">true</span>;<br />
<span class="kw2">var</span> nativeWindow:NativeWindow = <span class="kw2">new</span> NativeWindow <span class="br0">&#40;</span>windowOptions<span class="br0">&#41;</span>;<br />
nativeWindow.<span class="me1">activate</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
nativeWindow.<span class="kw3">stage</span>.<span class="me1">addChild</span><span class="br0">&#40;</span><span class="kw3">this</span><span class="br0">&#41;</span>;</div></div>
<p>At some point soon I will try to write up the <em>NativeWindow</em> class in more detail, including listing the properties it offers, and the events that it dispatches.</p>
<img src="http://feeds.feedburner.com/~r/lowpitch/~4/TXurFNWAwuU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lowpitch.com/blog/nativewindowinitoptions-configure-your-air-nativewindow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating AIR projects with Actionscript</title>
		<link>http://lowpitch.com/blog/creating-air-projects-with-actionscript/</link>
		<comments>http://lowpitch.com/blog/creating-air-projects-with-actionscript/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 18:54:53 +0000</pubDate>
		<dc:creator>lowpitch</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Actionscript 3.0]]></category>
		<category><![CDATA[Flex Builder]]></category>
		<category><![CDATA[AS 3.0]]></category>

		<guid isPermaLink="false">http://lowpitch.com/blog/?p=7</guid>
		<description><![CDATA[If you&#8217;re using Flex Builder 3, and you want to create an Actionscript project which will eventually deploy as an AIR application, you&#8217;ll quickly find that this option isn&#8217;t available when using the Actionscript Project Wizard. There are plenty of posts out there detailing a workaround (for example, see here).
I followed the workaround steps and [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re using Flex Builder 3, and you want to create an Actionscript project which will eventually deploy as an AIR application, you&#8217;ll quickly find that this option isn&#8217;t available when using the Actionscript Project Wizard. There are plenty of posts out there detailing a workaround (for example, see <a href="http://www.digitalflipbook.com/archives/2007/10/air_application.php" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.digitalflipbook.com');" target="_blank">here</a>).</p>
<p>I followed the workaround steps and it seemed to work OK, but when I first published my application&#8230; nothing happened. A little icon appeared in my dock, mysteriously titled <em>adl </em>(which I&#8217;ve since discovered stands for the <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=CommandLineTools_4.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/livedocs.adobe.com');" target="_blank">AIR debug launcher</a>), but no windows of Flash-based goodness appeared anywhere. After much digging around, I came across <a href="http://bugs.adobe.com/jira/browse/FB-9303" onclick="javascript:pageTracker._trackPageview('/outbound/article/bugs.adobe.com');" target="_blank">this entry in the Adobe Bug System</a> and found the answer buried away in the comments.</p>
<p>When I&#8217;d previously created Flex-based AIR projects, I hadn&#8217;t really appreciated that the Flex framework was handling all of the window-creating shenanigans on my behalf. Just by making my root MXML tag a <em>WindowedApplication</em>, instead of a &#8220;normal&#8221; <em>Application</em>, the main AIR window was being created and activated automatically. When you&#8217;re not using the Flex framework, you have to do this stuff yourself.</p>
<p>As it turns out, the two most important classes involved in this process are <a href="http://livedocs.adobe.com/flex/3/langref/flash/display/NativeWindow.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/livedocs.adobe.com');" target="_blank"><em>flash.display.NativeWindow</em></a> and <a href="http://livedocs.adobe.com/flex/3/langref/flash/display/NativeWindowInitOptions.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/livedocs.adobe.com');" target="_blank"><em>flash.display.NativeWindowInitOptions</em></a>. A <em>NativeWindow </em>is essentially the window you see on your desktop when you run an AIR application. This includes the menu bar (if any) and the system chrome (if any).</p>
<p>To successfully create and launch a window in AIR, you&#8217;ll need to create and activate a NativeWindow object, and then add your Document class (or some Sprite, at least) to that Window&#8217;s stage, accessible through the NativeWindow&#8217;s <em>stage </em>property. The code will look something like&#8230;</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;"><span class="co1">// create an instance of NativeWindowInitOptions to pass to the NativeWindow constructor</span><br />
<span class="kw2">var</span> windowOptions:NativeWindowInitOptions = <span class="kw2">new</span> NativeWindowInitOptions<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="co1">// create the window</span><br />
<span class="kw2">var</span> mainWindow:NativeWindow = <span class="kw2">new</span> NativeWindow<span class="br0">&#40;</span>windowOptions<span class="br0">&#41;</span>;<br />
<span class="co1">// activate the window</span><br />
mainWindow.<span class="me1">activate</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="co1">// adds &quot;this&quot; to the stage of the Window we have created</span><br />
mainWindow.<span class="kw3">stage</span>.<span class="me1">addChild</span><span class="br0">&#40;</span><span class="kw3">this</span><span class="br0">&#41;</span>;</div></div>
<p>This code will create and activate an AIR window with default options, and add <em>someSprite</em> to the stage of that Window. From then on, you can continue with your code just as you would in a traditional Flash application and all will work as expected. </p>
<p>I&#8217;ll post more about the <em>NativeWindow</em> and <em>NativeWindowInitOptions</em> classes at some point soon&#8230;.</p>
<p><em>(Update: <a href="http://lowpitch.com/blog/2008/08/07/nativewindowinitoptions-configure-your-air-nativewindow/" >Check out this post for more information about the NativeWindowInitOptions class</a>)</em></p>
<img src="http://feeds.feedburner.com/~r/lowpitch/~4/fng82Nry438" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lowpitch.com/blog/creating-air-projects-with-actionscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash plays certain MP3s back at double speed – why?</title>
		<link>http://lowpitch.com/blog/flash-plays-mp3s-back-at-double-speed-why/</link>
		<comments>http://lowpitch.com/blog/flash-plays-mp3s-back-at-double-speed-why/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 10:06:59 +0000</pubDate>
		<dc:creator>lowpitch</dc:creator>
				<category><![CDATA[Actionscript 2.0]]></category>
		<category><![CDATA[AS 2.0]]></category>
		<category><![CDATA[Audio]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://lowpitch.com/blog/?p=11</guid>
		<description><![CDATA[A colleague and I recently found some odd things happening with dynamically loaded audio in an elearning application we have been working on. The external MP3s seemed to be playing back at double speed, and as a result the audio was a hideously high-pitched mess. The weird thing was, this wasn&#8217;t happening on every machine. [...]]]></description>
			<content:encoded><![CDATA[<p>A colleague and I recently found some odd things happening with dynamically loaded audio in an elearning application we have been working on. The external MP3s seemed to be playing back at double speed, and as a result the audio was a hideously high-pitched mess. The weird thing was, this wasn&#8217;t happening on every machine. Some computers, including mine, would play the audio back fine but a couple of other people were reporting the same problem so we had to look into it. Bah.</p>
<p>After much fiddling, we reached the following conclusions:</p>
<ul>
<li>Flash seems unable to handle MP3s encoded with a <a href="http://en.wikipedia.org/wiki/Variable_bit_rate" onclick="javascript:pageTracker._trackPageview('/outbound/article/en.wikipedia.org');">variable bit rate</a> (VBR)</li>
<li>Flash seems unable to reliably handle MP3s which have a <a href="http://en.wikipedia.org/wiki/Sampling_rate" onclick="javascript:pageTracker._trackPageview('/outbound/article/en.wikipedia.org');">sample rate</a> which is <strong>not</strong> 44,100 <em>Hz</em>, 22,050 <em>Hz</em> or 11,025 <em>Hz</em>.</li>
<li>Additionally, it seems possible that there are certain bit rates which are also not handled well by Flash. We&#8217;ve tested files successfully at 40kbps, 80kps, 120kbps (and many other multiples of 40).</li>
</ul>
<p><em>(Note: This was happening in an application built with Actionscript 2.0 for Flash Player 8. I haven&#8217;t had a chance as yet to see if the same problem occurs in SWFs running in AVM2)</em></p>
<p>While the problem of the audio playing at double speed did seem fairly hard to recreate, we did find another way of highlighting the problem. The <a href="http://www.jeroenwijering.com/?item=JW_FLV_Player" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.jeroenwijering.com');">JW Media Player</a> is a popular, open-source MP3/FLV Player and can see be seen in-use all over the internet. From our tests, &#8216;incorrectly&#8217; encoded MP3 files can also create a problem when played as part of a playlist in the JW Media Player. If the user is listening to one of these MP3s, and moves the seek bar somewhere between half-way and the end of the file, the Media Player incorrectly decides that the MP3 has finished, and automatically moves to the next item in the playlist. Without looking into the code in more detail, this kind of assumes that the information Flash is broadcasting about the MP3 (for example sample length, or current position.) is incorrect, which is causes the MP3 to finish prematurely. Re-encoding the audio to match the criteria listed above will always fix this problem. </p>
<p><em>(Note: Current versions of the free Audio editing software <a href="http://audacity.sourceforge.net/" onclick="javascript:pageTracker._trackPageview('/outbound/article/audacity.sourceforge.net');" target="_blank">Audacity</a>, used in tandem with the <a href="http://lame.sourceforge.net/index.php" onclick="javascript:pageTracker._trackPageview('/outbound/article/lame.sourceforge.net');" target="_blank">LAME MP3 encoder</a>, do not allow you to choose the bitrate of the exported MP3. However, the Beta version of Audacity 1.3 does offer that functionality via the Options button in the Export Dialog. The Beta version of this software can currently be downloaded from <a href="http://audacity.sourceforge.net/download/" onclick="javascript:pageTracker._trackPageview('/outbound/article/audacity.sourceforge.net');" target="_blank">http://audacity.sourceforge.net/download/</a>.)</em></p>
<img src="http://feeds.feedburner.com/~r/lowpitch/~4/V6fWTbqFUVg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lowpitch.com/blog/flash-plays-mp3s-back-at-double-speed-why/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XML-RPC in Actionscript 3.0 – couple of gotchas</title>
		<link>http://lowpitch.com/blog/xml-rpc-in-actionscript-30-couple-of-gotchas/</link>
		<comments>http://lowpitch.com/blog/xml-rpc-in-actionscript-30-couple-of-gotchas/#comments</comments>
		<pubDate>Fri, 25 Jul 2008 21:09:35 +0000</pubDate>
		<dc:creator>lowpitch</dc:creator>
				<category><![CDATA[Actionscript 3.0]]></category>
		<category><![CDATA[AS 3.0]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[XML-RPC]]></category>

		<guid isPermaLink="false">http://lowpitch.com/blog/?p=6</guid>
		<description><![CDATA[As part a recent project, I&#8217;ve needed to integrate a Flex 3 application with an existing XML-RPC webservice. Over the years I&#8217;ve not done a huge amount of webservice integration with Flash. In the past I would typically choose to send data back and forth from the server using the built in XML or LoadVars [...]]]></description>
			<content:encoded><![CDATA[<p>As part a recent project, I&#8217;ve needed to integrate a Flex 3 application with an existing <a href="http://www.xmlrpc.com" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.xmlrpc.com');" target="_blank">XML-RPC</a> webservice. Over the years I&#8217;ve not done a huge amount of webservice integration with Flash. In the past I would typically choose to send data back and forth from the server using the built in <em>XML </em>or <em>LoadVars </em>objects available in Actionscript 2.0. On a couple of projects, I ended up using the <a href="http://www.adobe.com/products/flashremoting/" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.adobe.com');" target="_blank">Flash Remoting API</a> and <a href="http://www.amfphp.org/" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.amfphp.org');" target="_blank">AMFPHP</a> to achieve what I wanted and everything turned out pretty well, and  once I integrated an application with some .NET <a href="http://en.wikipedia.org/wiki/SOAP" onclick="javascript:pageTracker._trackPageview('/outbound/article/en.wikipedia.org');" target="_blank">SOAP</a> webservices. But XML-RPC was a new one on me.</p>
<p>Anyway, after a bit of research, I was a little surprised to discover that XML-RPC hasn&#8217;t already been supported by the Flex framework, but after looking around I came across <a href="http://code.google.com/p/as3-rpclib/" onclick="javascript:pageTracker._trackPageview('/outbound/article/code.google.com');" target="_blank">as3-rpclib</a>, an open-source library that not only supports XML-RPC, but also AMF0 and JSON-RPC (I&#8217;ve currently only used these classes with XML-RPC, so can&#8217;t comment on the other formats implements).</p>
<p>So far, the classes have worked really well for me. However I did hit a couple of stumbling blocks along the way which I&#8217;ll attempt to detail here.</p>
<p><span id="more-6"></span></p>
<p><strong>Information stored in AsyncToken is lost when onResult is sent out</strong><br />
When a service method is invoked using <em>myXMLObject.call ()</em>, an instance of <em>AysncToken</em> is returned. A responder can then added to the token, so that when the result is returned from the webservice, the appropriate result or fault hander is called. AsyncToken is a dynamic class, meaning that new properties can be assigned to the instance at runtime without an Exception being thrown. Because of the dynamic nature of the class, it&#8217;s fairly common to set a dynamic property of the token which identifies which particular method call was invoked. Something like:</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;">myToken = xmlrpc.<span class="kw3">call</span> <span class="br0">&#40;</span><span class="st0">&quot;someMethod&quot;</span><span class="br0">&#41;</span>;<br />
myToken.<span class="me1">methodName</span> = <span class="st0">&quot;someMethod&quot;</span>;</div></div>
<p>This lets us use one result handler for multiple method calls. Within our result handler we can then examine the token associated with the event, and &#8220;switch&#8221; based on the methodName property of the token. The code would look something like:</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;">protected <span class="kw2">function</span> onResult <span class="br0">&#40;</span>event: ResultEvent<span class="br0">&#41;</span> : <span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw2">var</span> token:AsyncToken = event.<span class="me1">token</span>;<br />
&nbsp; &nbsp; <span class="kw1">switch</span> <span class="br0">&#40;</span>token.<span class="me1">methodName</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&quot;someMethodName&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doSomething <span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">default</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doSomethingElse <span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div></div>
<p>I&#8217;ve since refactored my code not to use this particular way of handling webservice results, but initially I was using something very similar to the above. However, when I tried this with the as3-xmlrpc library, it didn&#8217;t work. <em>token.methodName</em> was always null. After much digging around, I discovered it was a bug in the library code. I&#8217;ve <a href="http://code.google.com/p/as3-rpclib/issues/detail?id=13" onclick="javascript:pageTracker._trackPageview('/outbound/article/code.google.com');">raised it as an issue</a> on the Google Code site, but until this issue is patched, here&#8217;s a fix. The changes need to be made in the file found at <em>com.ak33m.rpc.xmlrpc.XMLRPCObject</em>, in the <em>onResult</em> method, beginning at line #139.</p>
<p>Existing code:</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;"><br />
<span class="kw2">var</span> token:AsyncToken = evt.<span class="kw3">target</span>.<span class="me1">token</span>;<br />
<span class="kw2">var</span> resultevent:ResultEvent = ResultEvent<span class="br0">&#40;</span>evt.<span class="kw3">data</span><span class="br0">&#41;</span>;</div></div>
<p>A new <em>ResultEvent</em> is created, which is dispatched later on in the method. However, the token is never set on this new <em>ResultEvent</em>, and so when we examine it in our <em>onResult</em> handler elsewhere, it doesn&#8217;t contain the data that we expect. We can fix this by changing the code to:</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;"><br />
<span class="kw2">var</span> token:AsyncToken = evt.<span class="kw3">target</span>.<span class="me1">token</span>;<br />
<span class="kw2">var</span> currentRE:ResultEvent = ResultEvent <span class="br0">&#40;</span>evt.<span class="kw3">data</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> resultevent:ResultEvent = <span class="kw2">new</span> ResultEvent <span class="br0">&#40;</span>currentRE.<span class="kw3">type</span>,currentRE.<span class="me1">bubbles</span>, currentRE.<span class="me1">cancelable</span>, currentRE.<span class="me1">result</span>, token<span class="br0">&#41;</span>;</div></div>
<p>This will create a new <em>ResultEvent</em>, preserving our token, and our code will now work as expected.</p>
<p><strong>How to send parameters using the &#8217;struct&#8217; datatype</strong><br />
Some of the methods exposed by the webservice I was using required a parameter of type &#8217;struct&#8217;. Initially, I couldn&#8217;t work out how the hell I could force the Value Objects I was passing to my method calls to be serialised as structs, and as a result any calls I made to the webservice were constantly refused.  Eventually I came across the interface com.ak33m.rpc.xmlrpc.IXMLRPCStruct.</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;"><br />
package com.<span class="me1">ak33m</span>.<span class="me1">rpc</span>.<span class="me1">xmlrpc</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw3">interface</span> IXMLRPCStruct<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span> getPropertyData <span class="br0">&#40;</span><span class="br0">&#41;</span>:*;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div></div>
<p>When I changed my Value Object to implement this Interface, and correctly implemented the getPropertyData method, the calls to my webservice methods began to work.</p>
<p>(<em>Note: I have since discovered that the getting the latest version of the library from SVN renders this particular issue irrelevant. In the SVN version of the code, the XMLRPCSerializer has been updated and will automatically convert Objects to structs. With this change, my webservice calls would have worked from the start. It&#8217;s definitely worth checking out the latest version of the code, as there are a couple of other issues which have been fixed too.</em>)</p>
<img src="http://feeds.feedburner.com/~r/lowpitch/~4/37LmmahXRrA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lowpitch.com/blog/xml-rpc-in-actionscript-30-couple-of-gotchas/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PureMVC – Multicore vs Standard / Singlecore</title>
		<link>http://lowpitch.com/blog/puremvc-multicore-vs-standard-singlecore/</link>
		<comments>http://lowpitch.com/blog/puremvc-multicore-vs-standard-singlecore/#comments</comments>
		<pubDate>Wed, 23 Jul 2008 18:48:57 +0000</pubDate>
		<dc:creator>lowpitch</dc:creator>
				<category><![CDATA[PureMVC]]></category>
		<category><![CDATA[AS 3.0]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://lowpitch.com/blog/?p=5</guid>
		<description><![CDATA[I&#8217;ve recently started using the Actionscript 3.0 Multicore implementation of PureMVC in order to use the framework within a modular Flex application. At the time of writing, most of the PureMVC examples and tutorials online are relevant to the Standard, or Singlecore,  version of the framework.
In general, the methodology and syntax is indentical across both [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently started using the <a title="PureMVC Multicore implentation" href="http://puremvc.org/component/option,com_wrapper/Itemid,170/" onclick="javascript:pageTracker._trackPageview('/outbound/article/puremvc.org');" target="_blank">Actionscript 3.0 Multicore implementation of PureMVC</a> in order to use the framework within a modular Flex application. At the time of writing, most of the PureMVC examples and tutorials online are relevant to the Standard, or Singlecore,  version of the framework.</p>
<p>In general, the methodology and syntax is indentical across both versions of the framework. There are, however, a couple of minor differences which are pretty important, as without understanding these little quirks, many of the sample applications built using the Singlecore implementation will break when ported to Multicore.</p>
<p>The Standard version of PureMVC uses Singletons to provide access to the framework&#8217;s core actors. Within the framework, access to the Facade, Model, View and Controller are all provided using a standard <em>getInstance</em> method. To allow multiple instances of the framework to co-exist, the Multicore version of the framework uses <a title="Wikipedia definition of the Multiton pattern" href="http://en.wikipedia.org/wiki/Multiton" onclick="javascript:pageTracker._trackPageview('/outbound/article/en.wikipedia.org');" target="_blank">Multitons</a>, and then access to the various actors is provided within the framework by passing a unique instance key to the getInstance methods. The idea is simple &#8211; each separate instance of the framework has its own unique key, and so multiple instances of the actors may co-exist without interfering with each other.</p>
<p><span id="more-5"></span></p>
<p>In both versions of the framework, all instances of subclasses of <em>Notifier</em> (this includes <em>Mediator</em>, <em>Proxy</em>, <em>SimpleCommand</em> and <em>MacroCommand</em>) provide an easy way of accessing the facade using the protected property, <em>facade</em>.</p>
<p>The Standard implementation achieves this by creating an instance variable, defined as follows:</p>
<div class="codecolorer-container actionscript" style="height:35px;"><div class="codecolorer" style="font-family: monospace;">protected <span class="kw2">var</span> facade:IFacade = Facade.<span class="me1">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div></div>
<p>The Multicore implementation achieves this by providing an implicit getter, defined as follows:</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;">protected <span class="kw2">function</span> <span class="kw3">get</span> facade<span class="br0">&#40;</span><span class="br0">&#41;</span>:IFacade<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> multitonKey == <span class="kw2">null</span> <span class="br0">&#41;</span> <span class="kw3">throw</span> <span class="kw3">Error</span><span class="br0">&#40;</span> MULTITON_MSG <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">return</span> Facade.<span class="me1">getInstance</span><span class="br0">&#40;</span> multitonKey <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div></div>
<p>Obviously, if you are using the Multicore instance and your code tries to access the facade (for example, to register a new <em>Proxy</em>, or to send a <em>Notification</em>) before the multitonKey has been set, it will not work as expected, and an exception will be thrown. In particular, if you try and access the facade within the constructor of your <em>Mediator</em> or <em>Proxy</em>, <strong>it will fail</strong>, and you&#8217;ll see an Exception with the message &#8220;<strong>Error: multitonKey for this Notifier not yet initialized!</strong>&#8220;. How are you supposed to know when the value of the multitonKey has been set?</p>
<p>Digging around in the framework, it becomes clear that when a <em>Mediator</em> is registered with the <em>View</em>, or when a <em>Proxy</em> is registered with the <em>Model</em>, a couple of things happen. Firstly, <em>Notifier</em>&#8217;s <em>initializeNotifier</em> method is called, with the multitonKey passed as the only parameter. Secondly, a reference to the <em>Proxy</em> or <em>Mediator</em> is stored in the relevant proxyMap or mediatorMap. Finally, a call to the <em>View</em> or the <em>Model</em>&#8217;s <em>onRegister</em> is made.</p>
<p>As a result, it looks like there are a couple of prime candidates when it comes to deciding where to place any interactions with the facade that must occur when the Proxy or Mediator is first created. You can override the initializeNotifier method, using code similar to:</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;">override <span class="kw3">public</span> <span class="kw2">function</span> initializeNotifier<span class="br0">&#40;</span> <span class="kw3">key</span>:<span class="kw3">String</span> <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">super</span>.<span class="me1">initializeNotifier</span> <span class="br0">&#40;</span><span class="kw3">key</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="co1">// from here on, you can happily interact with the facade</span><br />
&nbsp; &nbsp; <span class="co1">// your code can be placed here</span><br />
<span class="br0">&#125;</span></div></div>
<p>or you can override the onRegister method, using code similar to:</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;">override <span class="kw3">public</span> <span class="kw2">function</span> onRegister<span class="br0">&#40;</span> <span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// from here on, you can happily interact with the facade</span><br />
&nbsp; &nbsp; <span class="co1">// your code can be placed here</span><br />
<span class="br0">&#125;</span></div></div>
<p>Personally, I prefer overriding onRegister, because it involves less typing (and therefore I&#8217;m less likely to bugger something up&#8230;).</p>
<p>So, now when looking at one of the popular PureMVC examples, originally written for the Standard version of the framework, it&#8217;s easy to see what would need to change if porting the application to the Multicore version. As an example, the <a href="http://puremvc.org/component/option,com_wrapper/Itemid,170/" onclick="javascript:pageTracker._trackPageview('/outbound/article/puremvc.org');" target="_blank">Flex Login</a> example contains a <em>Mediator</em> named <em>LoginPanelMediator</em>. Its constructor looks like:</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;"><span class="kw3">public</span> <span class="kw2">function</span> LoginPanelMediator<span class="br0">&#40;</span>viewComponent: LoginPanel<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">super</span><span class="br0">&#40;</span><span class="kw3">NAME</span>, viewComponent<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">// local reference to the LoginProxy</span><br />
&nbsp; &nbsp; _loginProxy = facade.<span class="me1">retrieveProxy</span><span class="br0">&#40;</span> LoginProxy.<span class="kw3">NAME</span> <span class="br0">&#41;</span> as LoginProxy;<br />
&nbsp; &nbsp; <span class="co1">//</span><br />
&nbsp; &nbsp; <span class="co1">// listen to events dispatched by its view component</span><br />
&nbsp; &nbsp; loginPanel.<span class="me1">addEventListener</span><span class="br0">&#40;</span> LoginPanel.<span class="me1">TRY_LOGIN</span>, login <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div></div>
<p>We can see straight away that this would fail when ported to the Multicore version of the framework. During the constructor, the Mediator is trying to retrieve a proxy from the facade, but at this point the multitonKey will not have been set. We could refactor this class, resulting in a change to the constructor, and a newly created override of the onRegister function:</p>
<div class="codecolorer-container actionscript"><div class="codecolorer" style="font-family: monospace;"><span class="kw3">public</span> <span class="kw2">function</span> LoginPanelMediator<span class="br0">&#40;</span>viewComponent: LoginPanel<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">super</span><span class="br0">&#40;</span><span class="kw3">NAME</span>, viewComponent<span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span class="co1">// listen to events dispatched by its view component</span><br />
&nbsp; &nbsp; loginPanel.<span class="me1">addEventListener</span><span class="br0">&#40;</span> LoginPanel.<span class="me1">TRY_LOGIN</span>, login <span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span class="co1">// constructor no longer contains any references to the facade</span><br />
<span class="br0">&#125;</span><br />
<br />
override <span class="kw3">public</span> <span class="kw2">function</span> onRegister <span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// when this method is called, the multitonKey has been set, </span><br />
&nbsp; &nbsp; <span class="co1">// so we can safely access the facade</span><br />
&nbsp; &nbsp; <span class="co1">// local reference to the LoginProxy</span><br />
&nbsp; &nbsp; _loginProxy = facade.<span class="me1">retrieveProxy</span><span class="br0">&#40;</span> LoginProxy.<span class="kw3">NAME</span> <span class="br0">&#41;</span> as LoginProxy;<br />
<span class="br0">&#125;</span></div></div>
<p>The differences between the Multicore and Singlecore/Standard versions of the framework are really very minor, but it&#8217;s worth remembering to always move any interaction with the facade out of a Proxy or Mediator&#8217;s constructor.</p>
<img src="http://feeds.feedburner.com/~r/lowpitch/~4/AGybGl7Nuh0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://lowpitch.com/blog/puremvc-multicore-vs-standard-singlecore/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

