Archive for February, 2010

Posted by admin at 17 February 2010

Category: programming

Logging to the firebug console from a Flex App is easy. A quick google search turns up loads of goodies… but you don’t really need a full fledged logging API for simple Flex Apps. Additionally, the example in the Adobe Flex Cookbook pisses off Internet Explorer as there is no check to see if window.console is even available.

At any rate, I wrote a small static class for logging to firebug from a Flex app. Use it however you wish:

package ctz.util
{
	import flash.external.ExternalInterface;

	public final class ConsoleLog
	{
		public function ConsoleLog()
		{
			throw new Error("static methods only");
		}

		public static function log(o:Object):void
		{
			if (o is Array)
				ConsoleLog.logArray(o as Array);
			else if (o is String)
				ConsoleLog.logString(o as String);
			else
				ConsoleLog.logObject(o);
		}

		public static function logString(msg:String):void
		{
			if (ExternalInterface.available)
				ExternalInterface.call("function log(msg){ if (window.console) { console.log(msg); } }", msg);
		}

		public static function logObject(obj:Object):void
		{
			var s:String = "";
			for (var k:String in obj)
				s += k + ": " + obj[k];
			ConsoleLog.logString(s);
		}

		public static function logArray(arr:Array):void
		{
			var s:String = "Item Count: " + arr.length + " || ";
			for (var i:int = 0; i < arr.length; i++)
				s += i + ": " + arr[i] + " ";
			ConsoleLog.logString(s);
		}
	}
}

File can be downloaded here.

Posted by admin at 4 February 2010

Category: programming

Tags: ,

Accomplishing a programming task with as few lines of code as possible is typically a good thing. At the same time, keeping the readability of your code at a reasonable level is also a good thing – especially if other developers will be maintaining your code in the future.

When writing custom components in Flex (and more or less writing anything to do with DisplayObjects in Flex) you find yourself writing a lot of the same, boring code. Basically, you instantiate a new DisplayObject, set some common properties on it, and then add it to a container. For example, take the following piece of code:

private function createUI():void
{
    var vbox:VBox = new VBox();
    vbox.percentWidth = 100;
    vbox.percentHeight = 100;
    vbox.name = 'My name is vbox';

    var txt:TextInput = new TextInput();
    txt.text = 'I am a TextInput';
    txt.setStyle('borderStyle', 'solid');
    txt.setStyle('borderThickness', 1);

    vbox.addChild(txt);
}

Pretty boring stuff, right?

Enter ctz.util.FlexUtils. With FlexUtils, you can cut down on writing all that boring code – like the snippet above. While there is nothing too complex or ground-breaking in this class, I find it to be quite helpful… and you may too:

package ctz.util
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.utils.*;

	import mx.collections.ArrayCollection;

	public final class FlexUtils
	{
		public function FlexUtils()
		{
			throw new Error('static methods only');
		}

		/**
		 * Create a Display Object and add some atts
		 */
		public static function createDisplayObject(typeName:Class, atts:Object=null):DisplayObject
		{
			try {
				var o:DisplayObject = new typeName();
				if (atts != null) {
					for (var k:String in atts) {
						if (o.hasOwnProperty(k)) {
							o[k] = atts[k];
						} else {
							typeName(o).setStyle(k, atts[k]);
						}
					}
				}
			} catch (e:Error) {
				trace('Error in FlexUtils.createDisplayObject: ' + e.message);
			}
			return o;
		}

		/**
		 * Given a root display object, get all children of the same type, recursively
		 */
		public static function getChildrenByType(collection:ArrayCollection, typeName:Class, rootObject:DisplayObjectContainer):void
		{
			var dobj:DisplayObject;
			for (var i:int = 0; i < rootObject.numChildren; i++) {
				dobj = rootObject.getChildAt(i);
				if (dobj is typeName)
					collection.addItem(dobj);
				if (dobj is DisplayObjectContainer)
					FlexUtils.getChildrenByType(collection, typeName, dobj as DisplayObjectContainer);
			}
		}

		public static function objDump(o:Object):String
		{
			var s:String = '';
			for (var key:String in o)
				s += key + ' => ' + o[key] + '\n';
			return s;
		}
	}
}

Note, I should probably update the two custom components I have published to use this class, but I really don’t have the time these days.

The createUI() method from the snippet above is magically transformed into the snippet below using FlexUtils.createDisplayObject()…

private function createUI():void
{
    var atts:Object = {percentWidth: 100, percentHeight: 100, name: 'My name is vbox'};
    var vbox:VBox = FlexUtils.createDisplayObject(VBox, atts) as VBox;

    atts = {text: 'I am a TextInput', borderStyle: 'solid', borderThickness: 1};
    var txt:TextInput = FlexUtils.createDisplayObject(TextInput, atts) as TextInput;

    vbox.addChild(txt);
}

I hope someone finds this remotely useful.

You can download the file here.