Flex可携带数据的Aler组件(DataCarriableAlert)


package component.alert
{
import flash.display.Sprite;
import flash.events.EventPhase;
import mx.controls.Alert;
import mx.core.FlexGlobals;
import mx.core.IFlexDisplayObject;
import mx.core.IFlexModule;
import mx.core.IFlexModuleFactory;
import mx.core.UIComponent;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.managers.ISystemManager;
import mx.managers.PopUpManager;

/**
* Alert control provided by flex sdk is not supportted to carry data in close handler, only a predefined closeEvent object,</br>
* you can use DataCarriableAlert to resolve this problem, you can obtain both the closeEvent object and your parameters.
* Usage example:</br>
* DataCarriableAlert.show("Are you sure to delete the selected trades?","Message",Alert.YES|Alert.NO,this,deletionConfirm,[2]);</br>
*
* </br>private function deletionConfirm(e:CloseEvent,intv:int):void{</br>
*   //intv is 2 which was passed by above code</br>
*   if(e.detail==Alert.NO){</br>
*      return;</br>
*    }</br>
* }</br>
*/
public class DataCarriableAlert extends Alert
{
public function DataCarriableAlert()
{
super();
}

public static function show(text:String = "", title:String = "",
flags:uint = 0x4 /* Alert.OK */,
parent:Sprite = null,
closeHandler:Function = null,
closeHandlerParameters:Array=null,
iconClass:Class = null,
defaultButtonFlag:uint = 0x4 /* Alert.OK */,
moduleFactory:IFlexModuleFactory = null):Alert
{
var modal:Boolean = (flags & Alert.NONMODAL) ? false : true;

if (!parent)
{
var sm:ISystemManager = ISystemManager(FlexGlobals.topLevelApplication.systemManager);
// no types so no dependencies
var mp:Object = sm.getImplementation("mx.managers.IMarshallPlanSystemManager");
if (mp && mp.useSWFBridge())
parent = Sprite(sm.getSandboxRoot());
else
parent = Sprite(FlexGlobals.topLevelApplication);
}

var alert:Alert = new Alert();

if (flags & Alert.OK||
flags & Alert.CANCEL ||
flags & Alert.YES ||
flags & Alert.NO)
{
alert.buttonFlags = flags;
}

if (defaultButtonFlag == Alert.OK ||
defaultButtonFlag == Alert.CANCEL ||
defaultButtonFlag == Alert.YES ||
defaultButtonFlag == Alert.NO)
{
alert.defaultButtonFlag = defaultButtonFlag;
}

alert.text = text;
alert.title = title;
alert.iconClass = iconClass;

/********************************override part start****************************************************************/
if (closeHandler != null){
alert.addEventListener(CloseEvent.CLOSE,
function(closeEvent:CloseEvent):void{
var parameters:Array=[];
parameters.push(closeEvent);
if(closeHandlerParameters!=null && closeHandlerParameters.length>0){
for each(var p:Object in closeHandlerParameters){
parameters.push(p);
}
}
closeHandler.apply(alert,parameters);
}
)
}
/********************************override part end*******************************************************************/


// Setting a module factory allows the correct embedded font to be found.
if (moduleFactory)
alert.moduleFactory = moduleFactory;
else if (parent is IFlexModule)
alert.moduleFactory = IFlexModule(parent).moduleFactory;
else
{
if (parent is IFlexModuleFactory)
alert.moduleFactory = IFlexModuleFactory(parent);
else
alert.moduleFactory = FlexGlobals.topLevelApplication.moduleFactory;

// also set document if parent isn't a UIComponent
if (!parent is UIComponent)
alert.document = FlexGlobals.topLevelApplication.document;
}

alert.addEventListener(FlexEvent.CREATION_COMPLETE, static_creationCompleteHandler);
PopUpManager.addPopUp(alert, parent, modal);

return alert;
}

private static function static_creationCompleteHandler(event:FlexEvent):void
{
if (event.target is IFlexDisplayObject && event.eventPhase == EventPhase.AT_TARGET)
{
var alert:Alert = Alert(event.target);
alert.removeEventListener(FlexEvent.CREATION_COMPLETE, static_creationCompleteHandler);

alert.setActualSize(alert.getExplicitOrMeasuredWidth(),
alert.getExplicitOrMeasuredHeight());
PopUpManager.centerPopUp(IFlexDisplayObject(alert));
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值