tags:

views:

162

answers:

2

Hi All,

I am trying to create an API utility using Adobe AIR where given a component name it should list all it methods, variables, and other properties of that component.

But I could not find an API to list all the UIComponents of Flex/Flash.

is there an API to lookup all the UIComponent or if there is no such API, can anyone tell me how do i lookup the components?

Note: I have tried using flash.utils.describeType() but it is not able to suit my requirement as the input to the method is passed dynamically using TextInput.

~Jegan

A: 

Do you need to do anything with the data, other than displaying it? If not, it might be easiest to pull up the right page from Adobe's online API docs.

Simon
+1  A: 

It sounds like you are asking two things:

  1. How to list all of the properties/methods of a UIComponent.
  2. How to list all of the UIComponents in the Flex SDK.

The first one can be done with describeType(). Given that the method is passed dynamically using TextInput, you need to just include all of the UIComponents into the SWF. You can do that with the following include statement for each component in the SDK (this example is with the Button):

include mx.controls.Button; Button;.

Then you can do something like this:

var className:String = myTextArea.text; // mx.controls::Button or mx.controls.Button
var clazz:Class = flash.utils.getDefinitionByName(className);
var methods:Array = getMethods(clazz);
var properties:Array = getProperties(clazz); // write a similar method for accessors and variables

/**
 *  Returns a list of methods for the class.
 * Pass in the superclass depth for how many classes
 * you want this to return (class tree).  If it's -1, it will return the whole method list
 */
public static function getMethods(target:Object, superclassDepth:int = -1, keepGeneratedMethods:Boolean = false):Array
{
 var description:XML = DescribeTypeCache.describeType(target).typeDescription;
 var hierarchy:Array = getHierarchy(target);
 var methodList:XMLList = description..method;
 methodList += description.factory..method; // factory is required if target is Class
 var methodName:String
 var methods:Array = [];
 var declaredBy:String;
 var methodXML:XML;
 var i:int = 0;
 var n:int = methodList.length();
 for (i; i < n; i++)
 {
  methodXML = methodList[i];
  declaredBy = methodXML.@declaredBy;
  methodName = methodXML.@name;
  // we break because the xml list is orded hierarchically by class!
  if (superclassDepth != -1 && hierarchy.indexOf(declaredBy) >= superclassDepth)
   break;
  // ignore methods that start with underscore:
  if (methodName.charAt(0) == "_" && !keepGeneratedMethods)
   continue;
  methods.push(methodName);
 }
 // sort the method list, so there's some kind of order to the report:
 methods.sort(Array.CASEINSENSITIVE);
 return methods;
}

The second one, listing the UIComponents in the SDK, is a bit more complex because there's no stable list of components in the SDK (depends on version, etc.). What you can do, however, is get a list of every class included in the swf. You do this by generating a Link Report (which is used for helping out modules), which is just a compiler argument:

-link-report=my_report.xml

Then you can figure out a way to find the unique xml nodes in that file, and which ones extend UIComponent. I use ruby and the nokogiri xml parser for ruby to do that. There is also a neat AIR app to visualize the Link Report.

If you're doing the first case, that method should get you started on listing all properties/methods of a UIComponent. If you're doing the second, that will give you a list of all UIComponents included in the swf.

Let me know if that helps, Lance

viatropos
The above code is not working for me when i input "Button" in the TextArea control. The error message that i am getting is "ReferenceError: Error #1065: Variable Button is not defined." Please let me know if there is anything wrong that i did.
Jegan
Save yourself some imports: `import mx.controls.*`
invertedSpear
that reference error is because the Button is not included in the swf. importing it alone with `import mx.controls.*;` won't include it in the swf. you need to either create a variable for it: `public var uselessButtonVariable:Button;`, or do that import thing I showed above: `import mx.controls.Button; Button;`. Try that and it should work.
viatropos
here is an example of an "imports" class the flex sdk uses: http://opensource.adobe.com/svn/opensource/flex/sdk/trunk/frameworks/projects/framework/src/FrameworkClasses.as
viatropos