In this post I’m going to show an utility function that I implemented some time ago. Its purpose is to filter the children of a Flex container. So, for example, let’s say that you want to have all the children of a specific container going deep throughout the entire children hierarchy, but you want to exclude some classes or instances from the search, then this function is for you. I use it to selectively enable or disable some components or to get the value of a specific property for them, but the function is generic enough to be used in many different situations.
The function is called getContainerChildren. Here is its source code:
package com.devahead.utils
{
import mx.collections.ArrayCollection;
import mx.core.Container;
public class MiscUtils
{
/**
* Filters all the children of a container.
*
* @param container
* Container of the children that have to be filtered.
* @param childrenClasses
* Array of the classes that have to be kept while
* filtering the children. If not specified, the type
* of the children is not considered while filtering.
* @param excludedClasses
* Array of the classes that have to be excluded.
* The children must not belong to these types.
* @param excludedInstances
* Array of the instances that have to be excluded.
* The children must not be one of these instances.
* @param deepSearch
* If true, specifies that the children must be
* searched throughout the entire hierarchy util
* there are no more children available for a
* component. If false, only the children of the
* container are considered and not their children.
* @param forceDeepSearchOnExcludedClasses
* If true and also deepSearch is true, then the children
* are searched also within the components that have been
* excluded from the results because their type is among
* the excludedClasses. If false, the children are never
* searched within the excluded components. If a component
* has been excluded because it's in excludedInstances, then
* a deep search is never executed on it, even if it belongs
* to an excluded class and forceDeepSearchOnExcludedClasses
* is true.
* @param forceDeepSearchOnExcludedInstances
* If true and also deepSearch is true, then the children
* are searched also within the components that have been
* excluded from the results because they are among
* the excludedInstances. If false, the children are never
* searched within the excluded components.
* @param resultArray
* Array used to store the children found in the recursive
* calls. It should be left null when the function is called
* externally (not recursively).
* @return Array of the container's children filtered according
* to the specified parameters.
*/
public static function getContainerChildren(container: Container,
childrenClasses: Array = null, excludedClasses: Array = null,
excludedInstances: Array = null, deepSearch: Boolean = false,
forceDeepSearchOnExcludedClasses: Boolean = false,
forceDeepSearchOnExcludedInstances: Boolean = false,
resultArray: ArrayCollection = null): ArrayCollection
{
var componentsArray: ArrayCollection = (resultArray != null ?
resultArray : new ArrayCollection());
if (container != null)
{
// Check all the container's children
for (var i: int = 0; i < container.numChildren; i++)
{
var currChild: Object = container.getChildAt(i);
var isCorrectComponent: Boolean = false;
// Check if the type of the current child is correct
if (childrenClasses == null || childrenClasses.length == 0)
{
isCorrectComponent = true;
}
else
{
for each (var currClass: Class in childrenClasses)
{
if (currChild is currClass)
{
isCorrectComponent = true;
break;
}
}
}
// Check if the current child is an instance that has to be excluded
var excludedAsInstance: Boolean = false;
if (excludedInstances != null && excludedInstances.length > 0)
{
for each (var currExcInst: Object in excludedInstances)
{
if (currChild == currExcInst)
{
isCorrectComponent = false;
excludedAsInstance = true;
break;
}
}
}
// Check if the current child's type has to be excluded
var excludedAsClass: Boolean = false;
if (excludedClasses != null && excludedClasses.length > 0)
{
for each (var currExcClass: Class in excludedClasses)
{
if (currChild is currExcClass)
{
isCorrectComponent = false;
excludedAsClass = true;
break;
}
}
}
// Check if I must use a deep search
var mustGoDeeper: Boolean = (
(excludedAsInstance && forceDeepSearchOnExcludedInstances) ||
(!excludedAsInstance && excludedAsClass && forceDeepSearchOnExcludedClasses));
if (deepSearch && (currChild is Container) &&
((!excludedAsInstance && !excludedAsClass) || mustGoDeeper))
{
// Get all the children of the current child through a recursive call.
//
// NOTE: I don't need to care about the result value of the recursive
// call because the children found are directly inserted into the
// componentsArray variable.
getContainerChildren(currChild as Container, childrenClasses,
excludedClasses, excludedInstances, deepSearch,
forceDeepSearchOnExcludedClasses, forceDeepSearchOnExcludedInstances,
componentsArray);
}
if (isCorrectComponent)
{
// Add the child to the result list
componentsArray.addItem(currChild);
}
}
}
return componentsArray;
}
}
}
The function is recursive, so during every recursion all the children of a container are discovered and they are filtered according to the specified exclusion classes or instances. If we must go deeper to find other components, then another recursion step is executed. Read the rest of this entry »

Recent Comments