<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fresh Click Media &#187; Flex</title>
	<atom:link href="http://www.freshclickmedia.com/blog/category/flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.freshclickmedia.com</link>
	<description></description>
	<lastBuildDate>Mon, 23 Aug 2010 20:00:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Adobe Flex Gravatar Control</title>
		<link>http://www.freshclickmedia.com/blog/2009/05/adobe-flex-gravatar-control/</link>
		<comments>http://www.freshclickmedia.com/blog/2009/05/adobe-flex-gravatar-control/#comments</comments>
		<pubDate>Sun, 31 May 2009 19:05:48 +0000</pubDate>
		<dc:creator>Shane</dc:creator>
				<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.freshclickmedia.com/?p=146</guid>
		<description><![CDATA[<img src="http://farm4.static.flickr.com/3396/3481155738_214aaceb19_o.jpg" alt="Flex Gravatar Control" width="590" height-"100" />

I have recently been getting to grips with Adobe Flex for a project that I've been working on, and thought a fun little bit of work would be to write a Flex Gravatar Control.  I'd done the <a href="http://www.freshclickmedia.com/blog/2008/06/aspnet-gravatar-control-update-full-source-included/">equivalent for ASP.NET</a>, so why not have another go?

Incidentally, I don't know whether there's much call for a Flex Gravatar control - if you're reading this and find a use for it - great, but I hope that there may be something in this post that helps a budding Flex developer too.

The general idea of component development is to package up a reusable bit of code for use in applications.  Since a Gravatar is just an image, it seems natural that the Gravatar component extends (otherwise known as inheriting) the Flex <code>mx.controls.Image</code> class.  The benefit of inheritance here is that we get a bunch of functionality for free that we can use as a basis for our control.  We'll be adding our specific Gravatar code so that the image shown is a particular user's Gravatar.]]></description>
			<content:encoded><![CDATA[<p><img src="http://farm4.static.flickr.com/3396/3481155738_214aaceb19_o.jpg" alt="Flex Gravatar Control" width="590" height-"100" /></p>
<p>I have recently been getting to grips with Adobe Flex for a project that I&#8217;ve been working on, and thought a fun little bit of work would be to write a Flex Gravatar Control.  I&#8217;d done the <a href="http://www.freshclickmedia.com/blog/2008/06/aspnet-gravatar-control-update-full-source-included/">equivalent for ASP.NET</a>, so why not have another go?</p>
<p>Incidentally, I don&#8217;t know whether there&#8217;s much call for a Flex Gravatar control &#8211; if you&#8217;re reading this and find a use for it &#8211; great, but I hope that there may be something in this post that helps a budding Flex developer too.</p>
<p>The general idea of component development is to package up a reusable bit of code for use in applications.  Since a Gravatar is just an image, it seems natural that the Gravatar component extends (otherwise known as inheriting) the Flex <code>mx.controls.Image</code> class.  The benefit of inheritance here is that we get a bunch of functionality for free that we can use as a basis for our control.  We&#8217;ll be adding our specific Gravatar code so that the image shown is a particular user&#8217;s Gravatar.</p>
<p>Let&#8217;s take a look at the Flex Navigator for the Gravatar Project.</p>
<div id="attachment_153" class="wp-caption alignnone" style="width: 313px"><a href="http://www.freshclickmedia.com/wp-content/uploads/2009/06/gravatarnavigator.gif"><img src="http://www.freshclickmedia.com/wp-content/uploads/2009/06/gravatarnavigator.gif" alt="Flex Navigator for the Gravatar Project" title="gravatarnavigator" width="303" height="351" class="size-full wp-image-153" /></a><p class="wp-caption-text">Flex Navigator for the Gravatar Project</p></div>
<p>Aside from the standard stuff, as3corelib.swc is referenced in the libs folder.  If you&#8217;re not aware of it, as3corelib is described as follows over at the <a href="http://code.google.com/p/as3corelib/">project page hosted on Google Code</a>.</p>
<blockquote><p>The corelib project is an ActionScript 3 Library that contains a number of classes and utilities for working with ActionScript 3. These include classes for MD5 and SHA 1 hashing, Image encoders, and JSON serialization as well as general String, Number and Date APIs.</p></blockquote>
<p>Since Gravatars use hashing for their URLs, we&#8217;ll be using the MD5 aspect of corelib.  </p>
<p>The components directory contains a single file, GravatarImage.as, which contains the code for the Gravatar component, and Gravatar.mxml is the demo application&#8217;s MXML that uses the Gravatar component.</p>
<p>Let&#8217;s dive straight in and take a look at the component code:</p>
<pre class="code">
package components
{
	// import the MD5 hashing code:
	import com.adobe.crypto.MD5;

	import mx.controls.Image;

	public class GravatarImage extends Image
	{
		public function GravatarImage()
		{
			super();
		}

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			this.width = _size;
			this.height = _size;

			this.source = &quot;http://www.gravatar.com/avatar/&quot; +
							MD5.hash(_email).toString() + &quot;?&quot; +
							&quot;size=&quot; + _size +
							&quot;&amp;amp;rating=&quot; + _rating +
							&quot;&amp;amp;d=&quot; + _defaultImage;
		}

		private var _validRatings:Array = new Array('g', 'pg', 'r', 'x');
		private var _validDefaultImageTypes:Array = new Array('default', 'identicon', 'monsterid', 'wavatar');

		[Bindable]
		private var _email:String = new String();

		[Bindable]
		private var _size:Number = 80;		// default size of 80 pixels

		[Bindable]
		private var _rating:String = &quot;G&quot;;	// default rating of G

		[Bindable]
		private var _defaultImage:String = &quot;default&quot;; // default to the blue 'G'				

		public function set email(value:String):void
		{
			// if the email is invalid, a default image will be returned:
			_email = value;
		}

		public function set size(value:Number):void
		{
			// sanity check on incoming value, must be between 1 and 512:
			if( value &gt;= 1 &amp;&amp; value &lt; 512)
				_size = value;
			else
				_size = 80;
		}

		public function set rating(value:String):void
		{
			// do a sanity check on the rating, allowing values
			// only in the _validRatings array (defined above):
			if( _validRatings.indexOf( value.toLowerCase()) != -1)
				_rating = value;
		}

		public function set defaultImage(value:String):void
		{
			_defaultImage = value;
		}
	}
}
</pre>
<p>The code is pretty simple.  The main function, updateDisplayList is called when the image should draw itself.  It simply constructs a URL based on the properties that have been set, and makes a request to the gravatar service for the image.  Note that the corelib&#8217;s MD5 hash is used to obfuscate the email address passed in.</p>
<p>The properties relate to those of the URL generated and are as follows:</p>
<h3>email</h3>
<p>The email address associated with the gravatar image.</p>
<h3>size</h3>
<p>The size property of the control can be in the range 1 to 512. If it is outside this range, a default of 80 will be used.</p>
<h3>rating</h3>
<p>The &#8216;highest&#8217; allowed rating of image.</p>
<ul>
<li>A G rated gravatar is suitable for display on all websites with any audience type.</li>
<li>PG rated gravatars contain may contain rude gestures, provocatively dressed individuals, the lesser swear words, or mild violence.</li>
<li>R rated gravatars may contain such things as harsh profanity, intense violence, nudity, or hard drug use.</li>
<li>X rated gravatars may contain hardcore sexual imagery or extremely disturbing violence.</li>
</ul>
<h3>defaultImage</h3>
<p>URL encoded URL, protocol included, of a GIF, JPEG, or PNG image that should be returned if either the requested email address has no associated gravatar, or that gravatar has a rating higher than is allowed by the &#8216;MaxAllowedRating&#8217; property.  The default image type also supports &#8216;default&#8217;, &#8216;identicon&#8217;, &#8216;monsterid&#8217;, and &#8216;wavatar&#8217; strings for alternative Gravatar default images.</p>
<p>So, how do we use the control?  Again, it&#8217;s pretty simple.  Let&#8217;s have a look at the application MXML:</p>
<pre class="code">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; layout=&quot;absolute&quot;
	xmlns:components=&quot;components.*&quot;&gt;

	&lt;mx:Script&gt;
		&lt;![CDATA[

			[Bindable]
			private var postComments:XML =
                &lt;comments&gt;
                    &lt;comment name=&quot;Shane Porter&quot; email=&quot;anemail@somedomain&quot; /&gt;
					&lt;comment name=&quot;Kate&quot; email=&quot;someotheremail@somedomain&quot; /&gt;
					&lt;comment name=&quot;Mr Unknown&quot; email=&quot;xxx@xxxyyy.com&quot; /&gt;
                &lt;/comments&gt;;

		]]&gt;
	&lt;/mx:Script&gt;

	&lt;mx:DataGrid id=&quot;myDataGrid&quot; dataProvider=&quot;{postComments.comment}&quot; rowHeight=&quot;88&quot; rowCount=&quot;3&quot;&gt;
		&lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField=&quot;@name&quot; headerText=&quot;Name&quot;/&gt;
            &lt;mx:DataGridColumn dataField=&quot;@email&quot; headerText=&quot;Gravatar&quot;&gt;
              	&lt;mx:itemRenderer&gt;
              		&lt;mx:Component&gt;
              			&lt;components:GravatarImage email=&quot;{data.@email}&quot; size=&quot;80&quot; rating=&quot;G&quot; defaultImage=&quot;monsterid&quot; /&gt;
              		&lt;/mx:Component&gt;
              	&lt;/mx:itemRenderer&gt;
            &lt;/mx:DataGridColumn&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;
&lt;/mx:Application&gt;
</pre>
<p>There&#8217;s a component namespace reference on the outer Application element, and for illustrative purposes, some XML defined that is used as the data provider for a Data Grid control.  The GravatarImage component is used as an item renderer for the &#8216;Gravatar&#8217; column.  The email property is set to the value of the email XML attribute, and the size, rating and defaultImage is also set.  </p>
<div id="attachment_155" class="wp-caption alignnone" style="width: 223px"><a href="http://www.freshclickmedia.com/wp-content/uploads/2009/06/appdesignview.gif"><img src="http://www.freshclickmedia.com/wp-content/uploads/2009/06/appdesignview.gif" alt="Application in Design View" title="Application in Design View" width="213" height="300" class="size-full wp-image-155" /></a><p class="wp-caption-text">Application in Design View</p></div>
<p>When the app is run, the datagrid uses the XML as its data provider, and the Gravatars are rendered.  Note that in this screenshot, I provided known email addresses associated with Gravatars.  The bottom row is the same as the code shown previously, and since it is not associated with a Gravatar, the specified &#8216;monsterid&#8217; default image is used.</p>
<div id="attachment_156" class="wp-caption alignnone" style="width: 510px"><a href="http://www.freshclickmedia.com/wp-content/uploads/2009/06/gravatarinbrowser.gif"><img src="http://www.freshclickmedia.com/wp-content/uploads/2009/06/gravatarinbrowser.gif" alt="The page shown in the browser" title="The page shown in the browser" width="500" height="278" class="size-full wp-image-156" /></a><p class="wp-caption-text">The page shown in the browser</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.freshclickmedia.com/blog/2009/05/adobe-flex-gravatar-control/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
