package com.ericfeminella.display
{
import flash.display.DisplayObjectContainer;
import flash.display.DisplayObject;
import mx.core.Container;
/**
*
* All static class which provides an API in which child objects
* of a particular DisplayObjectContainer can be reparented to a
* new DisplayObjectContainer within the application.
*
*/
public final class DisplayObjectReparenter
{
/**
*
* Reparents a child DisplayObject to a new container and retains the
* current state of the DisplayObject once relocated
*
* @param Specifies the DisplayObject which is to be reparented
* @param Specifies the current container of the DisplayObject
* @param Specifies the new DisplayObjectContainer to add the
* DisplayObject
* @throws An exception specifying that the DisplayObject is
* not a child of the container
* @throws An exception specifying that the DisplayObject could
* not be reparented
*
*/
public static function reparentChild(child:DisplayObject, parent:DisplayObjectContainer, newParent:DisplayObjectContainer) : void
{
if (parent.contains(child))
{
try {
parent.removeChild( child );
newParent.addChild( child );
}
catch(err:Error)
{
throw new Error("Unable to Reparent: DisplayObject instance is not a child of specified container");
}
}
else
{
throw new Error("Unable to Reparent: DisplayObject instance is not a child of specified container");
}
}
/**
*
* Reparents an array of DisplayObjects to a new container and retains the
* current state of the child DisplayObjects once reparented
*
* @param Specifies an Array of type DisplayObject in which to reparent
* @param Specifies the current container of the Array of DisplayObjects
* @param Specifies the new DisplayObjectContainer to add the children
* @throws An exception specifying that an item in the array is not of
* type DisplayObject
* @throws An exception specifying that a DisplayObject could not be
* reparented
*
*/
public static function reparentChildren(displayObjectArray:Array, parent:DisplayObjectContainer, newParent:DisplayObjectContaine) : void
{
var n:int = displayObjectArray.length;
for (var i:int = 0; i < n; i++) {
if ( displayObjectArray[i] is DisplayObject )
{
reparentChild( displayObjectArray[i], parent, newParent );
}
else
{
throw new Error("Unable to reparent: object must be of type DisplayObject");
}
}
}
}
}