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>
	 * &nbsp;&nbsp;//intv is 2 which was passed by above code</br>
	 *	&nbsp;&nbsp;if(e.detail==Alert.NO){</br>
	 *	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;</br>
	 *	&nbsp;&nbsp;&nbsp;}</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));
			}
		}
	}
}

좋은 웹페이지 즐겨찾기