disabling few rows in spark datagrid - actionscript-3

Is there a way in spark datagrid to disable some rows programmatically, in flex 3, it could be done using the function mouseEventToItemRenderer this way:
override protected function mouseEventToItemRenderer (
event: MouseEvent): IListItemRenderer {
var listItem: IListItemRenderer;// = super.mouseEventToItemRenderer(event);
if (_disableFlag)
{
if (listItem)
{
if (listItem.data)
{
if (disabledRow(listItem.data))
{
return null;
}
}
}
}
return listItem;
}
And then I implement the function disabledRow to return true or false depending on some condition, the condition that will specify if the selected item will be renderered or not. Is there a way in spark datagrid to do the same?

I think the spark datagrid supports the gridrollover event. under that we can get the
itemrender i think this might be suitable for you
for further reference please refer the Documentation

Ok, so here's how I did
First Step
I created the classes SelectionIndicator, HoverIndicator and CaretIndicator like the ones we find as skin parts in the datagrid skin, so the skin part caretIndicator:
<!--- #private -->
<fx:Component id="caretIndicator">
<s:Rect implements="spark.components.gridClasses.IGridVisualElement">
<fx:Script>
<![CDATA[
import spark.components.DataGrid;
import spark.components.Grid;
/**
* #private
*/
public function prepareGridVisualElement(grid:Grid, rowIndex:int, columnIndex:int):void
{
const dataGrid:DataGrid = grid.dataGrid;
if (!dataGrid)
return;
const color:uint = dataGrid.getStyle("caretColor");
caretIndicatorFill.color = color;
}
]]>
</fx:Script>
<s:stroke>
<!--- #private -->
<s:SolidColorStroke id="caretIndicatorFill" color="0x0167FF" weight="1"/>
</s:stroke>
</s:Rect>
</fx:Component>
becomes
package com.tn.zuro.components
{
import mx.graphics.SolidColorStroke;
import spark.components.DataGrid;
import spark.components.Grid;
import spark.components.gridClasses.IGridVisualElement;
import spark.primitives.Rect;
public class CaretIndicator extends Rect implements IGridVisualElement
{
private var caretIndicatorFill:SolidColorStroke ;
public function CaretIndicator()
{
super();
caretIndicatorFill = new SolidColorStroke();
this.stroke = caretIndicatorFill;
}
public function prepareGridVisualElement(grid:Grid, rowIndex:int, columnIndex:int):void
{
const dataGrid:DataGrid = grid.dataGrid;
if (!dataGrid)
return;
const color:uint = dataGrid.getStyle("caretColor");
caretIndicatorFill.color = color;
}
}
}
The same goes for SelectionIndicator and HoverIndicator, just don't forget to use SolidColor instead of solidColorStroke and the property fill or Rect instead of the property stroke.
Second Step
In the personalized datagrid, I added event listeners for the events gridClick, gridRollOver and gridMouseDown:
this.addEventListener(GridEvent.GRID_CLICK, gridClickHandler);
this.addEventListener(GridEvent.GRID_ROLL_OVER, gridEventHandler);
this.addEventListener(GridEvent.GRID_MOUSE_DOWN, mouse_down_handler);
And here are the event listeners:
protected function mouse_down_handler(event:GridEvent):void
{
if (_disableFlag)
{
if (event.item)
{
if (disabledRow(event.item))
{
event.grid.caretIndicator = null;
event.grid.selectionIndicator = null;
}
}
}
}
protected function gridClickHandler(event:GridEvent):void
{
if (_disableFlag)
{
if (event.item)
{
if (!disabledRow(event.item))
{
event.grid.selectionIndicator = new ClassFactory(SelectionIndicator);
event.grid.caretIndicator = new ClassFactory(CaretIndicator);
}
}
}
}
protected function gridEventHandler(event:GridEvent):void
{
if (_disableFlag)
{
if (event.item)
{
if(disabledRow(event.item))
{
event.grid.hoverIndicator = null;
}
else
{
event.grid.hoverIndicator = new ClassFactory(HoverIndicator);
}
}
}
}
You probably already guessed that _disableFlag and disabledRow are respectively a Boolean and a Function. Now the last step is the easiest:
Third Step
When you use the personalized dataGrid, if you want to disable the columns, you set _disableFlag to true and implement the disabledRow which returns true if a condition is met, and false if not
<custom:NinjaGrid id="ninja"
_disableFlag=true
creationComplete="ninja_trainingCompleteHandler(event)"/>
protected function ninja_trainingCompleteHandler(event:FlexEvent):void
{
ninja.disabledRow = beNinja;
}
private function beNinja(ninja:Object):Boolean
{
if (ninja.knowHowToWalkOnWater == true)
return true;
return false;
}
This solution doesn't work for the selection mode multiple rows, I found another solution for the multiple rows selection mode.
Second Solution
In this solution, I use only the HoverIndicator component, the event listener for the rollover event will remain the same. I don't use the event listener for the click event anymore, and in the mouse_down_handler function, I have now this code:
protected function mouse_down_handler(event:GridEvent):void
{
if(_disableFlag)
{
if (event.item)
{
if (!disabledRow(event.item))
{
if (!event.ctrlKey)
{
tempItem = event.item;
tempSelecteditems = new Vector.<Object>();
tempSelecteditems.push(tempItem);
}
else
{
if (tempSelecteditems.indexOf(event.item) < 0)
{
tempSelecteditems.push(event.item);
}
else
{
tempSelecteditems[tempSelecteditems.indexOf(event.item)] = null;
}
}
}
else
{
if (!event.ctrlKey)
{
selectedItem = tempItem;
tempSelecteditems = new Vector.<Object>();
}
else
{
if (tempSelecteditems.length)
{
selectedItems = tempSelecteditems;
}
else
{
selectedItem = tempItem;
}
}
}
}
}
}

Related

Calling methods from another class Action Script 3

I have two classes called 'main' and 'TimerCountDown'. I was try to call a single function 'reset' from 'TimerCountDown' in 'main' class.
This is my TimerCountDown class:
public class TimerCountDown extends MovieClip
{
public function TimerCountDown(t:TextField, timeType:String, timeValue:Number, es:String, _documentclass):void
{
this.documentclass = _documentclass;
this.tfTimeDisplay = t;
if (timeType == "seconds")
{
this.timeInSeconds = timeValue;
}
if (timeType == "minutes")
{
this.timeInSeconds = timeValue * 60;
}
this.tfEndDisplayString = es;
this.startTimer();
}
public function reset():void{
clockCounter.reset();
}
}
How can I create a reference in main class use the reset function in functions of main class? I can only do sth like
var myTimerObject:TimerCountDown = new TimerCountDown(timer, "seconds", 40, "0!", this);
but have no idea on calling reset function.
You can call it like this :
myTimerObject.reset();
You may keep a reference of myTimerObject in main class
public class Main {
private var _targetTimerObject:TimerCountDown;
public function set targetTimerObject(value:TimerCountDown):void {
_targetTimerObject = value;
}
public function someFunction():void {
if (_targetTimerObject) {
_targetTimerObject.reset();
}
}
}

How to signal DataGroup about data change in IList Dataprovider?

I Implement IList interface like
[Bindable]
public class MemList implements IList
{
public function MemList()
{
}
public function get length():int
{
var size:int = 5000;
return size;
}
public function addItem(item:Object):void
{
}
public function addItemAt(item:Object, index:int):void
{
}
public function getItemAt(index:int, prefetch:int=0):Object
{
var obj:Object = new Object();
obj.value = ByteStr(); // this function return a string
return obj;
}
public function getItemIndex(item:Object):int
{
return 0;
}
public function itemUpdated(item:Object, property:Object=null, oldValue:Object=null, newValue:Object=null):void
{
}
public function removeAll():void
{
}
public function removeItemAt(index:int):Object
{
return null;
}
public function setItemAt(item:Object, index:int):Object
{
return null;
}
public function toArray():Array
{
return null;
}
public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
{
}
public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
{
}
public function dispatchEvent(event:Event):Boolean
{
return false;
}
public function hasEventListener(type:String):Boolean
{
return false;
}
public function willTrigger(type:String):Boolean
{
return false;
}
}
and provide its object to DataGroup as data provider, In Mxml file I am doing this
<fx:Script><![CDATA[
[Bindable]private var list:MemList = new MemList();
protected function onBtnShow(event:MouseEvent):void
{
send(somenumber, MAX_SIZE, onShowResp);//get data from network
}
private function onShowResp(retVal:uint, ba:ByteArray):void
{
//this function called when ba bytearray is filled with data.
}
]]></fx:Script>
<s:Button id="btnShow" label="Show" click="onBtnShow(event)"/>
<s:Scroller verticalScrollPolicy="auto" id="aScroller"
liveScrolling="false">
<s:DataGroup id="dgMemView" width="100%" height="350"
dataProvider="{list}"
clipAndEnableScrolling="true"
itemRenderer="ItemRenderer"
>
<s:layout>
<s:VerticalLayout useVirtualLayout="true"/>
</s:layout>
</s:DataGroup>
</s:Scroller>
public function ByteStr(loc:uint):String
{
//this function can access ba bytearray filled with data. do some processing
on that data and return a string.
return string;
}
My question is when application run data group does not show anything because there is no data in the ba (bytearray) that must be filled with network data. when I press show button it makes a request and fills ba (bytearray) with data. how can I notify Datagroup that data has been changed. I am using flash builder 4.5. Thanks
You need to dispatch appropriate collectionChange event. See docs at http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/collections/IList.html#event:collectionChange

as3 Textfield placeholder

I have a quick question on textfield place holder on as3.
All the modern browser or mobile apps support a place holder for the text input field. once you click on it the focus move to the beginning of the text and the place holder remain until you start typing.
Just wondering if this is archieveable on as3?
Cheers
Bill
Sure, but you need to do something with that TextField manually. The general routine is this: Have a TextField descendant class, that will have a field that will hold placeholder text.
An example:
public class PlaceholderTF extends TextField
{
private var placeholderText:String;
private var currentText:String;
public function PlaceholderTF(prompt:String="Input text here") {
super(); // making a correct TextField out of ourselves
placeholderText = prompt;
currentText = "";
AWUTA = false;
text = "";
}
public override function set text(value:String):void {
if (currentText != value) {
currentText = value;
// calling built-in setter to correctly update text
if (currentText == null || currentText == "") {
super.text = placeholderText;
} else {
super.text = currentText;
}
}
}
}
This solution is pretty crude, but might work.
var placeholder_text:String = "Search";
myTextField.addEventListener(FocusEvent.FOCUS_IN, focusIn);
myTextField.addEventListener(FocusEvent.FOCUS_OUT, focusOut);
// fire when click to edit
function focusIn(event:Event):void
{
if(myTextField.text == placeholder_text)
{
myTextField.text = "";
}
}
// fire when you clicked out
function focusOut(event:Event):void
{
if(myTextField.text == "")
{
myTextField.text = placeholder_text;
}
}
Here is an example that works for me with form validation:
package components {
import flash.events.Event;
import flash.events.FocusEvent;
import flash.text.TextField;
public class TextFieldPlaceholder extends TextField {
private var _placeholderText:String;
private var _currentText:String;
public function TextFieldPlaceholder(placeholderText:String = 'Input text here') {
_placeholderText = placeholderText;
_currentText = '';
this.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
this.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
super();
}
override public function get text():String {
if (_currentText == _placeholderText) {
return '';
}
return _currentText;
}
override public function set text(value:String):void {
_currentText = value;
if (!_currentText) {
super.text = _placeholderText;
} else {
super.text = _currentText;
}
}
private function onFocusIn(e:Event):void {
super.text = _currentText;
}
private function onFocusOut(e:Event):void {
this.text = super.text;
}
}
}

removing previous image

Whats a better way to remove an old image or an image that is not there at all?
I am working with this XML slideshow and I have tweaked it the way I want, except the first image that loads doesn't disappear. How can I make this statement better? I've tried if (previous || imgHolder.numChildren > 0 ) as well and didn't work. Any help?
package
{
import flash.display.MovieClip;
import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.display.*;
public class Main extends MovieClip
{
var xml:XMLLoader;
var images:Array;
var current:int = 0;
var previous:Number;
public function Main()
{
init();
}
private function init()
{
LoaderMax.activate([ImageLoader]);
xml = new XMLLoader("xml/gallery.xml",{name:"loader",onComplete:onXmlLoaded});
xml.load();
}
function onXmlLoaded(e:LoaderEvent)
{
images = LoaderMax.getContent("loader");
nextImage(); //nextImage can use image var
}
private function nextImage()
{
TweenLite.from(images[current],1,{alpha:0, onStart:currentImage, onComplete: updateImage});
}
private function currentImage():void
{
imgHolder.addChild(images[current]);
}
private function updateImage()
{
if (previous)
{
imgHolder.removeChild(images[previous]);
}
previous = current;
if (current < images.length - 1)
{
current++;
} else
{
current = 0;
}
TweenLite.delayedCall(2, nextImage);
}
}
}
You could manipulate the contents of the Array to keep track of your current and previous slide index. The first in the Array is your current slide and the last is the previous slide. Like you suggested a numChildren check on your imgHolder is a good way to check if there is something to remove.
private function nextImage() : void
{
TweenLite.from(images[0], 1 , {alpha:0, onStart:currentImage, onComplete:updateImage});
}
private function currentImage():void
{
imgHolder.addChild(images[0]);
}
private function updateImage() : void
{
if(imgHolder.numChildren > 1)
{
imgHolder.removeChild(images[images.length-1]);
}
images.push(images.shift());
TweenLite.delayedCall(2, nextImage);
}

ActionScript - Global Custom Events?

up until now, the way i've been needing to handle my own custom events is by adding an event listener to the object that was dispatching the custom event. while this method of event handling works just fine, i've come to the point where i would like my custom events to be globally accessible, where the listening object does not need to be the same object that is dispatching the event.
in this example, my main Controller class is instantiating and adding to the display list 2 sprite classes: Square and Triangle. the 4th and final class is a custom event called ColorChangeEvent.
i'm attempting to dispatch a new ColorChangeEvent from the Square class, which uses a timer to dispatch a new random color once every second, while Triangle will listen for the dispatched event and change its fill color to the color that was dispatched by Square.
Controller.as:
package
{
import flash.display.Sprite;
public class Controller extends Sprite
{
public function Controller()
{
var sq:Square = new Square();
sq.x = sq.y = 100;
var tr:Triangle = new Triangle();
tr.x = tr.y = 250;
addChild(sq);
addChild(tr);
}
}
}
Square.as:
package
{
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Square extends Sprite
{
public function Square()
{
graphics.beginFill(0x999999);
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
var myTimer:Timer = new Timer(1000);
myTimer.addEventListener(TimerEvent.TIMER, dispatchNewColor);
myTimer.start();
}
private function dispatchNewColor(evt:TimerEvent):void
{
var randomColor:Number = Math.random() * 0xFFFFFF;
trace("Square Class Dispatched: " + randomColor);
dispatchEvent(new ColorChangeEvent(ColorChangeEvent.CHANGE, randomColor));
}
}
}
Triangle.as:
package
{
import flash.display.Sprite;
import flash.geom.ColorTransform;
public class Triangle extends Sprite
{
public function Triangle()
{
graphics.beginFill(0x999999);
graphics.moveTo(0, 0);
graphics.lineTo(100, 50);
graphics.lineTo(-50, 150);
graphics.endFill();
addEventListener(ColorChangeEvent.CHANGE, changeColor);
}
private function changeColor(evt:ColorChangeEvent):void
{
var ct:ColorTransform = new ColorTransform;
ct.color = evt.color;
transform.colorTransform = ct;
trace("Triangle Class Received: " + evt.color);
}
}
}
ColorChangeEvent.as:
package
{
import flash.events.Event;
public class ColorChangeEvent extends Event
{
public static const CHANGE:String = "change";
public var color:Number;
public function ColorChangeEvent(type:String, color:Number)
{
super(type);
this.color = color;
}
override public function clone():Event
{
return new ColorChangeEvent(type, color);
}
}
}
needless to say, this isn't working.
of course, i could add the event listener to the Square instance in the Controller class, who's event handler could pass that value to Triangle via a public function to change the color, but this is exactly the kind of limitation i'm trying to avoid.
it's not always easy to access and pass a value to a class from where the custom event is dispatched, which is why i'm looking for an actual global solution to handling custom events.
I have been using this class for some time now. To use it you would do this in square:
data.EventManager.instance.publish("someName", randomColor);
and then in your Triangle:
data.EventManager.instance.subscribe("someName", handleColorChange);
private function handleColorChange(color:Number):void {
// implementation here
}
You can even pass the ColorChangeEvent instead of just the color.
data.EventManager.instance.publish(ColorChangeEvent.CHANGE, new ColorChangeEvent(ColorChangeEvent.CHANGE, randomColor);
And then
data.EventManager.instance.subscribe(ColorChangeEvent.CHANGE, handleColorChange);
private function handleColorChange(colorChangeEvent:ColorChangeEvent):void {
// implement here
}
I removed a lot of code that is specific to my projects, so I am not 100% it is usable exactly as-is. But, you should be able to modify it to get it working correctly. If not, let me know and I can try to work it out with you.
This class handles additional things that I will not go into, though you are free to explore. Be aware, however, that anything that subscribes for event notification has a strong reference by the EventManager. That means that if you want to destroy something for garbage collection, you need to call EventManager.instance.cancel(ColorChangeEvent.CHANGE, handleColorChange) before the Triangle instances can be collected.
package data {
import flash.utils.*;
public class EventManager extends Object {
private var _subscribers:Dictionary;
private var _calls:Dictionary;
private var _feeds:Dictionary;
private var _requests:Dictionary;
private var _notify:Dictionary;
private var _services:Dictionary;
private static var __instance:EventManager;
public function EventManager() {
if (__instance) {
trace("EventManager is a Singleton class which should only be accessed via getInstance()");
}
_feeds = new Dictionary(true);
_subscribers = new Dictionary(true);
_requests = new Dictionary(true);
_services = new Dictionary(true);
_notify = new Dictionary(true);
}
public function getFeedData($name:String) {
if (_feeds[$name]) {
return _feeds[$name];
}
return undefined;
}
public function unpublish($name:String) {
var _post:* = _feeds[$name];
delete _feeds[$name];
return _post;
}
public function cancel($name:String, $subscriberFunc:Function, ...args): void {
var _cnt:Number;
var _subscriberArray:Array;
if (_subscribers[$name]) {
for (_cnt = 0; _cnt < _subscribers[$name].length; _cnt++) {
if (_subscribers[$name][_cnt] == $subscriberFunc) {
_subscribers[$name].splice(_cnt, 1);
}
}
}
if (_requests[$name]) {
_subscriberArray = _requests[$name];
_cnt = _subscriberArray.length;
while (_cnt > 0) {
if (_subscriberArray[_cnt] == $subscriberFunc) {
_subscriberArray.splice(_cnt, 1);
}
_cnt--;
}
}
}
public function subscribe($name:String, $subscriber:Function, ...args): void {
var _funcArray:Array;
var _func:Function;
if (_feeds[$name]) {
$subscriber(_feeds[$name]);
}
if (! _subscribers[$name]) {
_subscribers[$name] = new Array();
}
_subscribers[$name].push($subscriber);
if (_notify[$name]) {
_funcArray = _notify[$name];
for each (_func in _funcArray) {
_func();
}
delete _notify[$name];
}
}
public function request($name:String, $feedFunction:Function): void {
var _requestArray:Array;
var _request:Function;
if (! _feeds[$name]) {
if (! _requests[$name]) {
_requests[$name] = new Array();
}
_requests[$name].push($feedFunction);
} else {
$feedFunction(_feeds[$name]);
}
if (_notify[$name]) {
_requestArray = _notify[$name];
for each (_request in _requestArray) {
_request();
}
delete _notify[$name];
}
}
public function publish($name:String, $data:*, $args:Object = null): void {
var _subscriberArray:Array;
var _func:Function;
var cnt:Number = 0;
_feeds[$name] = $data;
if (_subscribers[$name] != undefined) {
_subscriberArray = _subscribers[$name].slice();
_cnt = 0;
while (_cnt < _subscriberArray.length) {
_func = _subscriberArray[_cnt] as Function;
if ($args) {
_func($data, $args);
}else {
_func($data);
}
_cnt++;
}
}
if (_requests[$name]) {
_subscriberArray = _requests[$name].slice();
delete _requests[$name];
_cnt = 0;
while (_cnt < _subscriberArray.length) {
if (_subscriberArray[_cnt] != null) {
_subscriberArray[_cnt]($data);
}
_cnt++;
}
}
}
public function notify($name:String, $subscriber:Function): void {
if (_requests[$name] || _subscribers[$name]) {
$subscriber();
}else {
if (! _notify[$name]) {
_notify[$name] = new Array();
}
_notify[$name].push($subscriber);
}
}
public static function getInstance(): EventManager {
if (! __instance) {
__instance = new EventManager();
}
return __instance;
}
public static function get instance(): EventManager {
return getInstance();
}
}
}
I got this to work by creating a singleton: EventDispatchSingleton that extends EventDispatcher. It's basically an empty singleton that provides the dispatchEvent and add/removeEventListener methods (these are automatically provided by extending EventDispatcher).
Anywhere I want to dispatch an event I import EventDispatchSingleton and then call EventDispatchSingleton.instance.dispatchEvent(<someEvent>);.
Then, wherever I want to listen to that event, I just import EventDispatchSingleton and call EventDispatchSingleton.instance.addEventListener(eventName, callback);
You should look into event bubbling, specificly I think you will find the Capturing phase of the event propagation useful. Take a read of Event propagation from Adobe LiveDocs. It's in the Flex docs, but it is about AS3 Events.
Also Senocular has a good post on Flash Event Bubbling.