<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/profilepic-component.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2009://34.35883-</id>
  <updated>2009-11-16T20:20:43Z</updated>
  <title>Comments for ProfilePic Component (http://www.insideria.com/2009/05/profilepic-component.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2009://34.35883</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/profilepic-component.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=35883" title="ProfilePic Component" />
    <published>2009-05-19T13:00:00Z</published>
    <updated>2009-06-02T13:38:14Z</updated>
    <title>ProfilePic Component</title>
    <summary>In the last article we discussed how components are built with the facebook api in mind. Now it&apos;s the time to do something more with it. We know how things need to be set up in order for the components to work. We learned about sessions, about the tow frame time line structure of the components and we talked about the inheritance of the UIComponent class. The previous example leveraged just a small potion of the methods that UIComponent provides. In this article we will take more advantange of the UIComponent class in order to create a component for the profile pic. </summary>
    <author>
      <name>Mirza Hatipovic</name>
      
    </author>
    
    <category term="Adobe Feed" />
    
    <category term="Features" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<div style="text-align: left;">In the <a href="http://www.insideria.com/2009/05/components-architecture-for-fa.html" target="_blank">last article</a> we discussed how components are built with the facebook api in mind. Now it's the time to do something more with it. We know how things need to be set up in order for the components to work. We learned about sessions, about the tow frame time line structure of the components and we talked about the inheritance of the UIComponent class. The previous example leveraged just a small potion of the methods that UIComponent provides. In this article we will take more advantange of the UIComponent class in order to create a component for the profile pic. </div>

<div class="ap_r"><a href="http://www.insideria.com/series-facebook-dev.html"><img src="http://www.insideria.com/upload/2009/04/fb_app_7.jpg" alt="series badge" style="width: 148px;" /></a><div class="apcaption">Find the rest of this series on the <a href="http://www.insideria.com/series-facebook-dev.html">series page</a>.</div></div>

<div style="text-align: left;">Here is the structure of the UserStatus component from the previous article:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">import</span> fl.core.UIComponent;
 	<span class="category1">import</span> flash.<span class="category2">text</span>.*;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoData;
 	<span class="category1">import</span> com.facebook.utils.FacebookSessionUtil;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.FacebookUser;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoFieldValues;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.friends.*;
 	<span class="category1">import</span> com.facebook.commands.users.*;
 	<span class="category1">import</span> com.facebook.commands.friends.*;
 	<span class="category1">import</span> com.facebook.net.FacebookCall;
 	<span class="category1">import</span> com.facebook.events.FacebookEvent;
 	<span class="category1">import</span> com.facebook.Facebook;
 
 	<span class="category1">public</span> <span class="category1">class</span> UserStatus <span class="category1">extends</span> UIComponent{
  
  		<span class="category1">private</span> <span class="category1">var</span> _status_txt:<span class="category2">TextField</span> = <span class="category1">null</span>;
  
  		<span class="category1">function</span> UserStatus() {
   		}
  
  		override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
   
   			<span class="linecomment">// always call super.configUI() in your implementation</span>
   			<span class="category1">super</span>.configUI();
   
   			_status_txt = <span class="category1">new</span> <span class="category2">TextField</span>();
   			addChild(_status_txt);
   
   		}
  
  		<span class="category1">public</span> <span class="category1">function</span> loadStatus(fbook:Facebook) {
   			
   			<span class="category1">var</span> <span class="category2">call</span>:GetInfo = <span class="category1">new</span> GetInfo([fbook.uid], [GetInfoFieldValues.ALL_VALUES]);
   			<span class="category2">call</span>.addEventListener(FacebookEvent.COMPLETE, onInfo);
   			fbook.post(<span class="category2">call</span>);
   
   		}
  
  		<span class="category1">private</span> <span class="category1">function</span> onInfo(e:FacebookEvent):<span class="category1">void</span>{
   			
   			<span class="category1">var</span> user = (e.<span class="category2">data</span> as GetInfoData).userCollection.<span class="category2">getItemAt</span>(0) as FacebookUser;
   			_status_txt.<span class="category2">text</span> = user.<span class="category2">status</span>.<span class="category2">message</span>;
   		
   		}
  		
  	 
  	}
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Of course, this is the UserStatus class that works purely with text. There is no resizing, no fonts, nothing special. We need to create a new component that will better demonstrate the capabilities of the UIComponent class. Like above mentioned, we will work on the component that handles the profile pictures of the user. We will use the sizing methods to properly re size the image in the component. The big advantage the will be that it can be visually placed and re sized anywhere on the stage which makes the development of the applications more fun.</div>

<div style="text-align: left;">Open the fla file we previously worked on. Remove the code from the first frame of the main timeline, also, remove the UserStatus component from the stage. </div>

<br/>

<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img1.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img1.html','popup','width=552,height=353,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img1-thumb-400x255.png" width="400" height="255" alt="article7_img1.png" class="mt-image-none" style="" /></a></span>

<br/>

<div style="text-align: left;">Next thing we need to do create the MovieClip that will act as the component. Go to insert -> New Symbol and create a MovieClip called <em>ProfilePicDisplay</em>. </div>

<br/>

<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img2.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img2.html','popup','width=448,height=175,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img2-thumb-400x156.png" width="400" height="156" alt="article7_img2.png" class="mt-image-none" style="" /></a></span>

<br/>

<div style="text-align: left;">Cool, remember the facebookUI package we talked about previously? We discussed that the package should be used as a neat collection of FB component classes. In this example we will place the class file in this package too and link the symbol with the class. </div>

<div style="text-align: left;">So, let's create the class file. Go to <em>File -> New -> ActionScript</em> file. </div>

<br/>

<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img3.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img3.html','popup','width=289,height=108,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img3-thumb-289x108.png" width="289" height="108" alt="article7_img3.png" class="mt-image-none" style="" /></a></span>

<br/>

<div style="text-align: left;">Save it as ProfilePicDisplay.as. Here is how the class sceleton of the class is defined:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">import</span> fl.core.UIComponent;
 	
 	<span class="category1">public</span> <span class="category1">class</span> ProfilePicDisplay <span class="category1">extends</span> UIComponent{
  
  		<span class="category1">function</span> ProfilePicDisplay() {
   		}
  
  		override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
   
   			<span class="linecomment">// always call super.configUI() in your implementation</span>
   			<span class="category1">super</span>.configUI();
   
   		}
  
  	}
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">There is nothing strange here. First we import the UIcomponent definition at:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">import</span> fl.core.UIComponent;</pre>
</code>

</div></div> 

<div style="text-align: left;">Then we inherit all the features from the class to the class ProfilePicDisplay:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">public</span> <span class="category1">class</span> ProfilePicDisplay <span class="category1">extends</span> UIComponent</pre>
</code>

</div></div> 

<div style="text-align: left;">And in the rest of the code, we see the empty constructor and the overridden configUI() method, which we will use later to set up the objects on the stage. </div>

<div style="text-align: left;">Go back to the fla file. Now we need to recreate the two frame structure that was discussed in the last article. Here is the image of it:</div>

<br/>

<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img4.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img4.html','popup','width=359,height=222,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img4-thumb-359x222.png" width="359" height="222" alt="article7_img4.png" class="mt-image-none" style="" /></a></span>

<br/>

<div style="text-align: left;">Let's discuss now about the assets that we need to export together with the ProfilePicDisplay component. We will need the UILoader component and like in the last sample, we need the Facebook library SWC from the like in the previous example. Remember that the SWC and UILoader need to be „carried&#8220; together with the component throughout the project. </div>

<div style="text-align: left;">Click on the second frame of the Assets layer and drag the Facebook SWC to the stage:</div>

<br/>

<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img5.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img5.html','popup','width=264,height=136,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img5-thumb-264x136.png" width="264" height="136" alt="article7_img5.png" class="mt-image-none" style="" /></a></span>

<br/>

<div style="text-align: left;">After that, drag one instance of the UILoader component to the stage:</div>

<br/>

<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img6.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img6.html','popup','width=382,height=346,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img6-thumb-382x346.png" width="382" height="346" alt="article7_img6.png" class="mt-image-none" style="" /></a></span>

<br/>

<div style="text-align: left;">Dont forget to uncheck the „Export in first frame&#8220; option for the UIComponent in the library. No need to give any instance names to this objects on the stage. Now let's move on to the first frame of the avatar layer. Again, like in the previous example, use a Hairline stroke to draw a rectangle with no fill on the stage, size 180 x 180: </div>

<br/>

<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img7.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img7.html','popup','width=294,height=285,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img7-thumb-294x285.png" width="294" height="285" alt="article7_img7.png" class="mt-image-none" style="" /></a></span>

<br/>

<div style="text-align: left;">Approximately this is the size of a standard profile pic on facebook. In fact the size does not matter at all here since the component will resize itself accoring to the size of the image that is loaded. </div>

<div style="text-align: left;">Now go to the library and right click the ProfilePicDisplay component. Make sure the following class paths are set:</div>

<br/>

<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img8.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img8.html','popup','width=498,height=276,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img8-thumb-400x221.png" width="400" height="221" alt="article7_img8.png" class="mt-image-none" style="" /></a></span>

<br/>

<div style="text-align: left;">Cool, the component is not finished but we just linked the class with the movieclip and this is a big step forward. To demonstrate that everything works fine place one instance of the ProfilePicDisplay component from the library to the empty stage. When compiled, it should not give any errors. If it does, then please do not read further and try to trace down the errors. </div>

<div style="text-align: left;">Now, since we created a working movieclip and also created the class structure it's time for us to move on. Lets start by adding some code to the existing class. Here we see the basic class:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">import</span> fl.core.UIComponent;
 	
 	<span class="category1">public</span> <span class="category1">class</span> ProfilePicDisplay <span class="category1">extends</span> UIComponent{
  
  		<span class="category1">function</span> ProfilePicDisplay() {
   		}
  
  		override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
   
   			<span class="linecomment">// always call super.configUI() in your implementation</span>
   			<span class="category1">super</span>.configUI();
   
   		}
  
  	}
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Let's start with adding objects to the display list. First we need to import facebook specific classes and the UILoader class:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">import</span> fl.containers.*;
<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoData;
<span class="category1">import</span> com.facebook.utils.FacebookSessionUtil;
<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.FacebookUser;
<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoFieldValues;
<span class="category1">import</span> com.facebook.commands.users.*;
<span class="category1">import</span> com.facebook.net.FacebookCall;
<span class="category1">import</span> com.facebook.events.FacebookEvent;
<span class="category1">import</span> com.facebook.Facebook;</pre>
</code>

</div></div> 

<div style="text-align: left;">This code will be pasted directly below the import fl.core.UIComponent; statement. </div>

<div style="text-align: left;">Next thing we want to do is to declare the variable to hold the UILoader:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">private</span> <span class="category1">var</span> _picLoader:UILoader = <span class="category1">new</span> UILoader();</pre>
</code>

</div></div> 

<div style="text-align: left;">Then we add it to the stage in the configUI loader:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
 
 	<span class="linecomment">// always call super.configUI() in your implementation</span>
 	<span class="category1">super</span>.configUI();
 
 	<span class="category1">this</span>.addChild(_picLoader);
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Ok, when compiled, it does not give any errors but also it does not diplay anything. Before making any calls to the facebook api, we need to make sure that the image loading works. </div>

<div style="text-align: left;">Go to facebook and load your profile page. </div>

<br/>

<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img9.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img9.html','popup','width=789,height=498,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img9-thumb-400x252.png" width="400" height="252" alt="article7_img9.png" class="mt-image-none" style="" /></a></span>

<br/>

<div style="text-align: left;">If you are in FireFox, right click on your profile image and click „Copy Image location&#8220;:</div>

<br/>

<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img10.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img10.html','popup','width=348,height=508,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img10-thumb-348x508.png" width="348" height="508" alt="article7_img10.png" class="mt-image-none" style="" /></a></span>

<br/>

<div style="text-align: left;">We just copied the profile pic image path to the clipboard. Now, we simply need to republish set the source property of the loader:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

_picLoader.source = "<span class="quote">http://profile.ak.facebook.com/v224/120/49/n1110516263_7773.jpg</span>";</pre>
</code>

</div></div> 

<div style="text-align: left;">Publish the swf and you will see the result: </div>

<br/>

<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img11.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img11.html','popup','width=209,height=225,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img11-thumb-209x225.png" width="209" height="225" alt="article7_img11.png" class="mt-image-none" style="" /></a></span>

<br/>

<div style="text-align: left;">As we can see the result is a bit modest. Now we can play a little bit with the properties. Let's say we set the scaleContent property to false:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

_picLoader.scaleContent = <span class="category1">false</span>;</pre>
</code>

</div></div> 

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img12.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img12.html','popup','width=254,height=313,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img12-thumb-254x313.png" width="254" height="313" alt="article7_img12.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Cool, we can now see that the image loads in actual size. You can imagine how many options we have to customize the loader, there are pretty much endless! Try to move the component around the stage and place it in different corners. Try to place multiple ones to see it is working. </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img13.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img13.html','popup','width=589,height=441,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img13-thumb-400x299.png" width="400" height="299" alt="article7_img13.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">We can see now how it is easy to load images from the directly to from facebook. Still we are not finished, we want to make calls to facebook. But it is important how easy it is to work with components once they are properly designed.   </div>

<div style="text-align: left;">Now, finally let's make the facebook call. Until now, our class displays a picture from a constant path and that is something we do not want. We need to add the following method to our class:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">public</span> <span class="category1">function</span> <span class="category2">load</span>(fbook:Facebook):<span class="category1">void</span> {
 			
 			<span class="category1">var</span> <span class="category2">call</span>:GetInfo = <span class="category1">new</span> GetInfo([fbook.uid], [GetInfoFieldValues.PIC_BIG]);
 			<span class="category2">call</span>.addEventListener(FacebookEvent.COMPLETE, onInfo);
 			fbook.post(<span class="category2">call</span>);
 
 		}</pre>
</code>

</div></div> 

<div style="text-align: left;">I will not go much into detail as the function is very similar to the one before. Let's jump directly to the onInfo callback:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">private</span> <span class="category1">function</span> onInfo(e:FacebookEvent):<span class="category1">void</span>{
 			
 			<span class="category1">var</span> user = (e.<span class="category2">data</span> as GetInfoData).userCollection.<span class="category2">getItemAt</span>(0) as FacebookUser;
 			_picLoader.source = user.pic_big;
 		
 		}</pre>
</code>

</div></div> 

<div style="text-align: left;">Here is the overview of the complete class:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">import</span> fl.core.UIComponent;
 	<span class="category1">import</span> fl.containers.*;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoData;
 	<span class="category1">import</span> com.facebook.utils.FacebookSessionUtil;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.FacebookUser;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoFieldValues;
 	<span class="category1">import</span> com.facebook.commands.users.*;
 	<span class="category1">import</span> com.facebook.net.FacebookCall;
 	<span class="category1">import</span> com.facebook.events.FacebookEvent;
 	<span class="category1">import</span> com.facebook.Facebook;
 
 	<span class="category1">public</span> <span class="category1">class</span> ProfilePicDisplay <span class="category1">extends</span> UIComponent{
  
  		<span class="category1">private</span> <span class="category1">var</span> _picLoader:UILoader = <span class="category1">new</span> UILoader();
  
  		<span class="category1">function</span> ProfilePicDisplay() {
   		}
  
  		override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
   
   			<span class="linecomment">// always call super.configUI() in your implementation</span>
   			<span class="category1">super</span>.configUI();
   
   			<span class="category1">this</span>.addChild(_picLoader);
   
   		}
  
  		<span class="category1">public</span> <span class="category1">function</span> <span class="category2">load</span>(fbook:Facebook):<span class="category1">void</span> {
   			
   			<span class="category1">var</span> <span class="category2">call</span>:GetInfo = <span class="category1">new</span> GetInfo([fbook.uid], [GetInfoFieldValues.PIC_BIG]);
   			<span class="category2">call</span>.addEventListener(FacebookEvent.COMPLETE, onInfo);
   			fbook.post(<span class="category2">call</span>);
   
   		}
  
  		<span class="category1">private</span> <span class="category1">function</span> onInfo(e:FacebookEvent):<span class="category1">void</span>{
   			
   			<span class="category1">var</span> user = (e.<span class="category2">data</span> as GetInfoData).userCollection.<span class="category2">getItemAt</span>(0) as FacebookUser;
   			_picLoader.source = user.pic_big;
   		
   		}
  
  
  	}
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">
Now, let's jump to the main timeline. We add the code similar to the one from yesterday:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">import</span> com.facebook.Facebook;
<span class="category1">import</span> com.facebook.utils.FacebookSessionUtil;

<span class="category1">var</span> fbook:Facebook;
<span class="category1">var</span> session:FacebookSessionUtil = <span class="category1">new</span> FacebookSessionUtil("<span class="quote">YOUR_API_KEY</span>", "<span class="quote">YOUR_SECRET</span>", loaderInfo);
fbook = session.facebook;

pic1_mc.<span class="category2">load</span>(fbook);</pre>
</code>

</div></div> 

<div style="text-align: left;">Before we compile the swf, we need to make sure that there is only one component on the stage called pic1_mc. When we run the example, here is the result:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img14.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img14.html','popup','width=309,height=348,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img14-thumb-309x348.png" width="309" height="348" alt="article7_img14.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Cool! The component acts now as „independent&#8220;. Once we created the facebook session, we passed it to the component and the component loads it. There is still penty of stuff that needs to be added to make a real component out of this. Let's start with resizing. </div>

<div style="text-align: left;">
The resizing in the component is not that easy. Technically, it is not correct to set the width and the heigth property of the component, instead we should use the setSize method. setSize is a method implemented by default in the UIComponent framework. In order to create a solid resizing logic, we should extend the setSize method. The setSize method is called anytime even when the component is initiated with the default size. Here is the proof. Place this overridden setSize method inside the class:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

override <span class="category1">public</span> <span class="category1">function</span> <span class="category2">setSize</span>(<span class="category2">width</span>:<span class="category2">Number</span>, <span class="category2">height</span>:<span class="category2">Number</span>):<span class="category1">void</span>{
 		    
 	<span class="category2">trace</span>("<span class="quote">Width: </span>" + <span class="category2">width</span>);
 
 	invalidate();
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Compile the swf and as the result we will see that the output is 177.5, the default size of the component. Now, let's got back to the main timeline and resize it via the Free Transform tool:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img15.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img15.html','popup','width=402,height=306,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img15-thumb-402x306.png" width="402" height="306" alt="article7_img15.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Now let's compile it again, and we will see that there is  a different width displayed. This is a simple proof that the setSize method is called even if the component was not resized in the Flash IDE. So, now it's clear that in order to create a fancy resizing for any component (not just this one) we have to stuck to the setSize method. </div>

<div style="text-align: left;">In the setSize method, we also see the invalidate() call. This call redraws the whole component by calling the draw() method. In the UIComponent architecture it is never recommended to call the draw() method directly, invalidate() should be called instead. So, in order for that to work, we need to override the existing draw method, so place this code into the class file:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

override protected <span class="category1">function</span> draw():<span class="category1">void</span> {
 
 	<span class="category2">trace</span>("<span class="quote">drawing component...</span>");
 
 	<span class="linecomment">// always call super.draw() at the end</span>
 	<span class="category1">super</span>.draw();
 
}	</pre>
</code>

</div></div> 

<div style="text-align: left;">We we run the swf, we see the following in the output window:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img16.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img16.html','popup','width=251,height=154,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img16-thumb-251x154.png" width="251" height="154" alt="article7_img16.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">See the order how the methods are called? First the setSize() method, then after the invalidate() call, the draw() method is called. Now lets remove the trace statements and add some code direcly to the draw method:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

override protected <span class="category1">function</span> draw():<span class="category1">void</span> {
 
 	_picLoader.<span class="category2">width</span> = <span class="category2">width</span>;
 	_picLoader.<span class="category2">height</span> = <span class="category2">height</span>;
 
 	<span class="linecomment">// always call super.draw() at the end</span>
 	<span class="category1">super</span>.draw();
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Also, remove the override setSize method. We dont need is since it was implemented just to demonstrate the order of how the methods are called. The default setSize() method calls the draw() method. So, resize the component randomly on the stage, my sample is much smaller then the original size. </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img18.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img18.html','popup','width=109,height=97,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img18-thumb-109x97.png" width="109" height="97" alt="article7_img18.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Cool! Thats a big step forward. Let's try to work with resizing directly via ActionScript. At the beginning, we mentioned that the component on the stage should be named pic1_mc. If that's the case, we should place the following script to the main timeline:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

pic1_mc.<span class="category2">setSize</span>(10, 10);</pre>
</code>

</div></div> 

<div style="text-align: left;">And the tiny result is:</div>
<div style="text-align: left;">
Wow, we almost have a great small component that will handle all the profile pics. </div>

<div style="text-align: left;">Here is the complete class code for better overview:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">import</span> fl.core.UIComponent;
 	<span class="category1">import</span> fl.containers.*;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoData;
 	<span class="category1">import</span> com.facebook.utils.FacebookSessionUtil;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.FacebookUser;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoFieldValues;
 	<span class="category1">import</span> com.facebook.commands.users.*;
 	<span class="category1">import</span> com.facebook.net.FacebookCall;
 	<span class="category1">import</span> com.facebook.events.FacebookEvent;
 	<span class="category1">import</span> com.facebook.Facebook;
 
 	<span class="category1">public</span> <span class="category1">class</span> ProfilePicDisplay <span class="category1">extends</span> UIComponent{
  
  		<span class="category1">private</span> <span class="category1">var</span> _picLoader:UILoader = <span class="category1">new</span> UILoader();
  
  		<span class="category1">function</span> ProfilePicDisplay() {
   		}
  
  		override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
   
   			<span class="linecomment">// always call super.configUI() in your implementation</span>
   			<span class="category1">super</span>.configUI();
   
   			<span class="category1">this</span>.addChild(_picLoader);
   
   		}
  
  		<span class="category1">public</span> <span class="category1">function</span> <span class="category2">load</span>(fbook:Facebook):<span class="category1">void</span> {
   			
   			<span class="category1">var</span> <span class="category2">call</span>:GetInfo = <span class="category1">new</span> GetInfo([fbook.uid], [GetInfoFieldValues.PIC_BIG]);
   			<span class="category2">call</span>.addEventListener(FacebookEvent.COMPLETE, onInfo);
   			fbook.post(<span class="category2">call</span>);
   
   		}
  
  		<span class="category1">private</span> <span class="category1">function</span> onInfo(e:FacebookEvent):<span class="category1">void</span>{
   			
   			<span class="category1">var</span> user = (e.<span class="category2">data</span> as GetInfoData).userCollection.<span class="category2">getItemAt</span>(0) as FacebookUser;
   			_picLoader.source = user.pic_big;
   		
   		}
  
  		override protected <span class="category1">function</span> draw():<span class="category1">void</span> {
   
   		    _picLoader.<span class="category2">width</span> = <span class="category2">width</span>;
   		    _picLoader.<span class="category2">height</span> = <span class="category2">height</span>;
   
   		    <span class="linecomment">// always call super.draw() at the end</span>
   		    <span class="category1">super</span>.draw();
   
   		}	
  		
  	}
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Now we should move on to make the component capable of loading profile pics of other users. So in that case, we need to provide the user id. We need to implement the method to receive the user id, so here is how it goes.</div>

<div style="text-align: left;">Go to facebook and browse your friends:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img20.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img20.html','popup','width=219,height=253,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img20-thumb-219x253.png" width="219" height="253" alt="article7_img20.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">
Find the id of one of your friends:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img19.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img19.html','popup','width=347,height=103,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img19-thumb-347x103.png" width="347" height="103" alt="article7_img19.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Copy only the uid to the clipboard. Now go to the main timeline and extend the load method like this:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

pic1_mc.<span class="category2">load</span>(fbook, 1110516263);</pre>
</code>

</div></div> 

<div style="text-align: left;">Also, extend the load method inside the class file in order to match the new argument count:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">public</span> <span class="category1">function</span> <span class="category2">load</span>(fbook:Facebook, uid:<span class="category2">Number</span>):<span class="category1">void</span> {
 			
 			<span class="category1">var</span> <span class="category2">call</span>:GetInfo = <span class="category1">new</span> GetInfo([uid], [GetInfoFieldValues.PIC_BIG]);
 			<span class="category2">call</span>.addEventListener(FacebookEvent.COMPLETE, onInfo);
 			fbook.post(<span class="category2">call</span>);
 
 		}</pre>
</code>

</div></div> 

<div style="text-align: left;">Now, when you run the example you will see one friend of you, here my cousin Ajdin:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img21.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img21.html','popup','width=176,height=233,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img21-thumb-176x233.png" width="176" height="233" alt="article7_img21.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">
Cool, now let's place a lot more components with different uids. Here is the fancy result:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article7_img22.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article7_img22.html','popup','width=347,height=408,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article7_img22-thumb-347x408.png" width="347" height="408" alt="article7_img22.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Isn't that cool?  For now we will leave this untouched. Yes I know that it can be further developed but for now this is ok. We can add preloaders, we can add effects etc. Here is the complete class cod so we dont get confused:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">import</span> fl.core.UIComponent;
 	<span class="category1">import</span> fl.containers.*;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoData;
 	<span class="category1">import</span> com.facebook.utils.FacebookSessionUtil;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.FacebookUser;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoFieldValues;
 	<span class="category1">import</span> com.facebook.commands.users.*;
 	<span class="category1">import</span> com.facebook.net.FacebookCall;
 	<span class="category1">import</span> com.facebook.events.FacebookEvent;
 	<span class="category1">import</span> com.facebook.Facebook;
 
 	<span class="category1">public</span> <span class="category1">class</span> ProfilePicDisplay <span class="category1">extends</span> UIComponent{
  
  		<span class="category1">private</span> <span class="category1">var</span> _picLoader:UILoader = <span class="category1">new</span> UILoader();
  
  		<span class="category1">function</span> ProfilePicDisplay() {
   		}
  
  		override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
   
   			<span class="linecomment">// always call super.configUI() in your implementation</span>
   			<span class="category1">super</span>.configUI();
   
   			<span class="category1">this</span>.addChild(_picLoader);
   
   		}
  
  		<span class="category1">public</span> <span class="category1">function</span> <span class="category2">load</span>(fbook:Facebook, uid:<span class="category2">Number</span>):<span class="category1">void</span> {
   			
   			<span class="category1">var</span> <span class="category2">call</span>:GetInfo = <span class="category1">new</span> GetInfo([uid], [GetInfoFieldValues.PIC_BIG]);
   			<span class="category2">call</span>.addEventListener(FacebookEvent.COMPLETE, onInfo);
   			fbook.post(<span class="category2">call</span>);
   
   		}
  
  		<span class="category1">private</span> <span class="category1">function</span> onInfo(e:FacebookEvent):<span class="category1">void</span>{
   			
   			<span class="category1">var</span> user = (e.<span class="category2">data</span> as GetInfoData).userCollection.<span class="category2">getItemAt</span>(0) as FacebookUser;
   			_picLoader.source = user.pic_big;
   		
   		}
  
  		override protected <span class="category1">function</span> draw():<span class="category1">void</span> {
   
   		    _picLoader.<span class="category2">width</span> = <span class="category2">width</span>;
   		    _picLoader.<span class="category2">height</span> = <span class="category2">height</span>;
   
   		    <span class="linecomment">// always call super.draw() at the end</span>
   		    <span class="category1">super</span>.draw();
   
   		}	
  		
  	}
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">I hope that this article was not to confusing. In the next one, we will discuss how to implement effects and other extras. There ideas of how to extend this component are pretty much endless. I can only guarantee that the next article will cover only a portion of possible enhancements tha can be built into the profile pic. But before the next article, make sure you work hard to get this component done. It is in fact not so complicated as it seems. Also, it's important to know that the rest of the components will use the same resizing strategies. Once you successfully mastered the resizing, you will be able to create new upcoming components much easier. So, dear readers, until the next article, take care and stay tuned!</div>

<p><br/>To view the entire series please visit <a href="http://www.insideria.com/series-facebook-dev.html">http://www.insideria.com/series-facebook-dev.html</a></p>
]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35883-comment:2060305</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35883" type="text/html" href="http://www.insideria.com/2009/05/profilepic-component.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/profilepic-component.html#comment-2060305" />
    <title>Comment from vasilios.avtsidis on 2009-05-25</title>
    <author>
        <name>vasilios.avtsidis</name>
        <uri>http://v.avtsidis@optusnet.com.au</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://v.avtsidis@optusnet.com.au">
        <![CDATA[<p>v.avtsidis@picasso.com.au</p>]]>
    </content>
    <published>2009-05-25T10:18:12Z</published>
  </entry>

</feed
