IconUtility.as
package
{
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.geom.Matrix;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.utils.Dictionary;
import mx.containers.accordionClasses.AccordionHeader;
import mx.controls.tabBarClasses.Tab;
import mx.core.BitmapAsset;
import mx.core.UIComponent;
public class IconUtility extends BitmapAsset
{
private static var dictionary:Dictionary;
public static function getClass( target:UIComponent, source:String, width:Number = NaN, height:Number = NaN ):Class
{
if(!dictionary)
{
dictionary = new Dictionary(false);
}
//if(source is String)
//{
var loader:Loader = new Loader();
loader.load(new URLRequest(source as String), new LoaderContext(true));
//source = loader;
//}
dictionary[target] = { source:loader, width:width, height:height };
return IconUtility;
}
public function IconUtility():void
{
addEventListener(Event.ADDED, addedHandler, false, 0, true)
}
private function addedHandler(event:Event):void
{
if(parent)
{
if(parent is AccordionHeader)
{
var header:AccordionHeader = parent as AccordionHeader;
getData(header.data);
}
else if(parent is Tab)
{
var tab:Tab = parent as Tab;
getData(tab.data);
}
else
{
getData(parent);
}
}
}
private function getData(object:Object):void
{
var data:Object = dictionary[object];
if(data)
{
var source:Object = data.source;
if(data.width > 0 && data.height > 0)
{
bitmapData = new BitmapData(data.width, data.height, true, 0x00FFFFFF);
}
if(source is Loader)
{
var loader:Loader = source as Loader;
if(!loader.content)
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
}
else
{
displayLoader(loader);
}
}
}
}
private function displayLoader( loader:Loader ):void
{
if(!bitmapData)
{
bitmapData = new BitmapData(loader.content.width, loader.content.height, true, 0x00FFFFFF);
}
bitmapData.draw(loader, new Matrix(bitmapData.width/loader.width, 0, 0, bitmapData.height/loader.height, 0, 0));
if(parent is UIComponent)
{
var component:UIComponent = parent as UIComponent;
component.invalidateSize();
}
}
private function completeHandler(event:Event):void
{
if(event && event.target && event.target is LoaderInfo)
{
displayLoader(event.target.loader as Loader);
}
}
}
}
main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import com.IconUtility;
public function open():void
{
navigateToURL(new URLRequest("http://hi.baidu.com/woaidelphi/"), "_blank");
}
]]>
</mx:Script>
<mx:Button id="button" label="Button"
icon="{IconUtility.getClass(button, 'http://hiphotos.baidu.com/woaidelphi/abpic/item/ace9cd3836b88206b9998fe9.jpg')}" x="488" y="233" width="278" height="107"
click="open()"/>
</mx:Application>