Ahmad Flex

Flex Ideas

Search for component by name in a flex application

Posted by ahmadflex on June 15, 2009


Bookmark and Share

Hi,

You can use below code to search through the entire flex application for a certain UIComponent object by name or ID. It is a recursive function call. (you can use getComponent function which will search the application, or call findComponent function and pass it an array of  components to search in.)

Here is how you call the function:

ComponentFinder.getComponentByName(“addCommentButton”);

 

Here is the code:

////////////////////////////////////////////////////////////////////////////////
//
//  Ahmad Khudairy
//  Copyright 2009 Ahmad Flex
//  All Rights Reserved.
//
//  NOTICE: You can use, and modify this file. Just please
//          Keep this comment undeleted.
//
////////////////////////////////////////////////////////////////////////////////

public class ObjectFinder
{
/**
*
* This will search the application for the component that has a
* certain name or id. and returns the first
* one it finds. otherwise it returns null.
*
* @param componentName String defining component name or id.
*
* @return A refrence to the component found. returns null if not
* found.
*
*/
public static function getComponentByName(componentName:String):UIComponent
{
	//quick return
	if (componentName == "")
	{
		return null;
	}

	var mainApp:Container = Application.application as Container;
	var appChildren:Array = mainApp.getChildren();
	return findComponent(componentName, appChildren);
}

/**
* recursive function. That will loop through an array of components and
* their children components. and it stops if it finds a component with
* name or id equals to parameter componentName.
*
* @param componentName String defining component name or id.
* @param compsArr Array of components to be searched.
*
* @return A refrence to the component found. returns null if not
* found.
*/

public static function findComponent(componentName:String, compsArr:Array):UIComponent
{
	var obj:Object;
	var found:UIComponent;
	var i:int;
	var length:int = compsArr.length;
	var component:Object;

	for (i = 0; i < length ; i++)
	{
		component = compsArr[i];
		if (component.hasOwnProperty("name") && component.name == componentName)
		{
			return component as UIComponent;
		}
		else if (component.hasOwnProperty("id") && component.id == componentName)
		{
			return component as UIComponent;
		}
		else
		{
			if (component.hasOwnProperty("getChildren") && component.getChildren().length > 0)
			{
				found = findComponent(componentName, component.getChildren());
				if (found && (found.name == componentName || found.id == componentName))
					{
						return found;
					}
			}
		}
	}

	return found;
}

}//class ends


Bookmark and Share

Posted in ActionScript, Algorithms, Flex | Tagged: | Leave a Comment »

Flex Scrolling Label (Marquee)

Posted by ahmadflex on May 28, 2009

var x;


Bookmark and Share

Hi,

This Label control, is specially created to marquee when text is truncated and mouse is over the label as a cool way instead of the tooltip thing in flex that appears when text is trucated. you can also set scrollAlways property to true to get this to always scroll.

change property “scrollSeperationText” to change the “…” that appears before text gets repeated again, and property “scrollFramesPerMove” can be changed to change the marquee effect speed.

Please note this does not support the htmlText.

example , in the window that will popup click on the file icon file Icon to save and see the example swf

Here is the class code:

////////////////////////////////////////////////////////////////////////////////
//
//  Ahmad Khudairy
//  Copyright 2009 Ahmad Flex
//  All Rights Reserved.
//
//  NOTICE: You can use, and modify this file. Just please
//          Keep this comment undeleted.
//
////////////////////////////////////////////////////////////////////////////////

import flash.events.Event;
import flash.events.MouseEvent;

import mx.controls.Label;

public class ScrollLabel extends Label
{
//————————————————————————–
//
//  Constructor
//
//————————————————————————–

public function ScrollLabel()
{
super();

scrollWhenTruncated = true;

truncateToFit = true;
}

//————————————————————————–
//
//  Variables
//
//————————————————————————–

/**
* @private
* Holds reference of scrolling frame
*/
private var currentFrame:int=0;

/**
* @private
* Holds reference of scrolling index
*/
private var currentStartIndex:int = 0;

/**
* @private
* Holds the text value
*/
private var originalText:String = “”;

/**
* @private
* True if truncation happened to text
*/
protected var truncated:Boolean;

/**
* If text is scrolling this will be true, and fasle otherwise
*/
public var isScrolling:Boolean;

//————————————————————————–
//
//  Properties
//
//————————————————————————–

/**
* Number of frames to wait before scrolling text each time. <br>
* A value of 1 is the fastest
*/
public var scrollFramesPerMove:int = 5;

/**
* Text that will seperate scroll rounds
*/
public var scrollSeperationText:String = “       …         “;

/**
* @private
*/
private var _scrollWhenTruncated:Boolean = false;

/**
* If set to true, then text will scroll when mouse is over the label
* and text was truncated
*/
public function get scrollWhenTruncated():Boolean
{
return _scrollWhenTruncated;
}

/**
* @private
*/
public function set scrollWhenTruncated(value:Boolean):void
{
if (_scrollWhenTruncated == value)
{
return;
}

_scrollWhenTruncated = value;

if (_scrollWhenTruncated)
{
scrollAlways = false;
}

addMouseOverHandlers(_scrollWhenTruncated);
}

/**
* @private
*/
var _scrollAlways:Boolean;

/**
* If set to true, then text will always scroll
*/
public function get scrollAlways():Boolean
{
return _scrollAlways;
}

/**
* @private
*/
public function set scrollAlways(value:Boolean):void
{
if (_scrollAlways == value)
{
return;
}

_scrollAlways = value;

if (_scrollAlways)
{
scrollWhenTruncated = false;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
else
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}

//————————————————————————–
//
//  Overriden Methods
//
//————————————————————————–

override public function set text(value:String):void
{
super.text = value;
originalText = text;
}

override public function set htmlText(value:String):void
{
trace(“Scrolling is not supported for htmlText”);
super.htmlText = value;
}

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
textField.text = originalText;
truncated = textField.truncateToFit();
}

//————————————————————————–
//
//  Methods
//
//————————————————————————–

/**
* @private
* Adds/Removes event listeners to <code>MouseEvent.ROLL_OVER</code> and
* <code>MouseEvent.ROLL_OUT</code> events
*
* @param listenToMouseOver if true then it adds the event listeners and
* removes them otherwise
*/
private function addMouseOverHandlers(listenToMouseOver:Boolean):void
{
if (listenToMouseOver)
{
addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
}
else
{
removeEventListener(MouseEvent.ROLL_OVER, onMouseOver);
removeEventListener(MouseEvent.ROLL_OUT, onMouseOut);
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}

/**
* @private
* Handles <code>MouseEvent.ROLL_OVER</code> event. It add an event
* listener to <code>Event.ENTER_FRAME</code> if text was truncated.
*
* @param event MouseEvent object
*/
private function onMouseOver(event:MouseEvent):void
{
if (truncated)
{
addEventListener(Event.ENTER_FRAME, onEnterFrame);
isScrolling = true;
super.text = text + ” “;
}
}

/**
* @private
* Handles <code>MouseEvent.ROLL_OUT</code> event. It removes event
* listener to <code>Event.ENTER_FRAME</code>, and reset <code>text</code>
*
* @param event MouseEvent object
*/
private function onMouseOut(event:MouseEvent):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
super.text = originalText;
isScrolling = false;
currentStartIndex = 0;
currentFrame = 0;
}

/**
* @private
* Handles <code>Event.ENTER_FRAME</code> event. It calls the
* <code>scrollText</code> function
*
* @param event Event object
*/
private function onEnterFrame(event:Event):void
{
scrollText();
}

/**
* @private
* Scrolls the text
*/
private function scrollText():void
{
currentFrame++;
if (currentFrame == scrollFramesPerMove)
{
if (currentStartIndex < originalText.length)
{
textField.text = originalText.substring(currentStartIndex, originalText.length ) + scrollSeperationText + originalText;
currentStartIndex++;
}
else if (currentStartIndex >= originalText.length)
{
textField.text = scrollSeperationText.substring(currentStartIndex – originalText.length, scrollSeperationText.length ) + originalText;
currentStartIndex++;

if (currentStartIndex > originalText.length + scrollSeperationText.length)
{
currentStartIndex = 1;
}
}

if (!truncated)
{
var i:int;
var scrollSeperationTextLength:int = scrollSeperationText.length;
var fullLength:int = scrollSeperationTextLength + originalText.length;
var tempText:String = textField.text;
var textIsFit:Boolean = false;

while (!textIsFit)
{
for (i = 0; i < fullLength; i ++)
{
if (i < scrollSeperationTextLength)
{
tempText = tempText + scrollSeperationText.charAt(i);
}
else
{
tempText = tempText + originalText.charAt(i – scrollSeperationTextLength);
}

textField.text = tempText;
if (textField.truncateToFit())
{
textIsFit = true;
textField.text = tempText;
break;
}
}
}
}

currentFrame = 0;
}
}
}

Bookmark and Share

Posted in ActionScript, components, Flex | Tagged: , , , , , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.