`
jxncyun
  • 浏览: 31917 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论
  • aniyo: 哥们,你这是什么设计模式哦,还吸引了我,我的个晕
    设计模式

自定义Alert

 
阅读更多
手动阀爱上爱师傅是阿斯顿阿方索

////////////////////////////////////////////////////////////////////////////////
//
//  仿造版本3的Alert修改成版本4的
// 
//  created By 程平
//  2011-11-9
// 
//
////////////////////////////////////////////////////////////////////////////////
package com.pingan.tools.components
{
import com.pingan.tools.components.skin.CustomAlertSkin;

import flash.display.DisplayObject;
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventPhase;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.ui.Keyboard;
import flash.ui.Mouse;

import mx.core.FlexGlobals;
import mx.core.IFlexDisplayObject;
import mx.core.IFlexModule;
import mx.core.IFlexModuleFactory;
import mx.core.IFontContextComponent;
import mx.core.IUITextField;
import mx.core.IVisualElement;
import mx.core.UIComponent;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.events.SandboxMouseEvent;
import mx.managers.ISystemManager;
import mx.managers.PopUpManager;
import mx.resources.IResourceManager;
import mx.resources.ResourceManager;

import spark.components.Button;
import spark.components.HGroup;
import spark.components.Image;
import spark.components.Label;
import spark.components.SkinnableContainer;
import spark.components.supportClasses.Skin;
import spark.core.IDisplayText;
import spark.primitives.BitmapImage;


public class CustomAlert extends SkinnableContainer implements IFontContextComponent
{

[SkinPart(required="false")]
/**标题栏小图标,在其皮肤中可以控制是否显示 **/
public var iconDisplay:BitmapImage;


[SkinPart(required="false")]
/**标题栏标题文字 同样在皮肤中可控制 */
public var titleDisplay:IDisplayText;


[SkinPart(required="false")]

/**
*  仿造titlewindow的拖拽,定义一个拖拽区域,用于拖拽该组件
*/
public var moveArea:InteractiveObject;

/**
*  @private
*/
override public function get baselinePosition():Number
{
return getBaselinePositionForPart(titleDisplay as  IVisualElement);
}

//接口中的两个方法
public function get fontContext():IFlexModuleFactory
{
return moduleFactory;
}


public function set fontContext(moduleFactory:IFlexModuleFactory):void
{
this.moduleFactory = moduleFactory;
}


/**自定义Alert中的几个按钮的值 **/

public static const YES:uint = 0x0001;
public static const NO:uint = 0x0002;
public static const OK:uint = 0x0004;
public static const CANCEL:uint= 0x0008;
public static const NONMODAL:uint = 0x8000;

/** 按钮的默认尺寸 **/
public static var buttonHeight:Number = 22;
public static var buttonWidth:Number = 65;


/** 显示按钮的标志及默认显示的按钮的标志**/
public var buttonFlags:uint = OK;
public var defaultButtonFlag:uint = OK;


/** 按钮显示的名称 **/
private static var _cancelLabel:String = "取消";

public static function get cancelLabel():String
{
return _cancelLabel;
}
public static function set cancelLabel(value:String):void
{
if(value!=_cancelLabel&&value!=""){
_cancelLabel = value;
}
}

public function CustomAlert()
{
super();
//mouseEnabled = false;
}


private static var _noLabel:String  = "否";

public static function get noLabel():String
{
return _noLabel;
}
public static function set noLabel(value:String):void
{
if(value!=_noLabel&&value!=""){
_noLabel = value;
}
}


private static var _okLabel:String ="确定";

public static function get okLabel():String
{
return _okLabel;
}
public static function set okLabel(value:String):void
{
if(value!=_okLabel&&value!=""){
_okLabel = value;
}
}


private static var _yesLabel:String = "是";

public static function get yesLabel():String
{
return _yesLabel;
}
public static function set yesLabel(value:String):void
{
if(value!= _yesLabel&&value!=""){
_yesLabel = value ;
}

}



//----------------------------------
//  设置标题的值
//----------------------------------


private var _title:String = "";

private var titleChanged:Boolean;

[Bindable]
[Inspectable(category="General", defaultValue="")]


public function get title():String
{
return _title;
}


public function set title(value:String):void
{
_title = value;

if (titleDisplay)
titleDisplay.text = title;

}



override protected function partAdded(partName:String, instance:Object):void
{
super.partAdded(partName, instance);
if (instance == moveArea)
{
moveArea.addEventListener(MouseEvent.MOUSE_DOWN, moveArea_mouseDownHandler);
}
if (instance == titleDisplay)
{
titleDisplay.text = title;
}
}

protected function moveArea_mouseDownHandler(event:MouseEvent):void
{

var sbRoot:DisplayObject = systemManager.getSandboxRoot();
sbRoot.addEventListener(
MouseEvent.MOUSE_UP, moveArea_mouseUpHandler, true);


this.startDrag();

}



protected function moveArea_mouseUpHandler(event:Event):void
{
this.stopDrag();

var sbRoot:DisplayObject = systemManager.getSandboxRoot();

sbRoot.removeEventListener(
MouseEvent.MOUSE_UP, moveArea_mouseUpHandler, true);

}



public var text:String ;

public var imageObject:Object ;




// private static var tempAlert:CustomAlert;






public static function show(text:String = "", title:String = "",
flags:uint = 0x4 /* Alert.OK */,
parent:Sprite = null,
closeHandler:Function = null,
imageSource:Object = null,
defaultButtonFlag:uint = 0x4 /* Alert.OK */,
moduleFactory:IFlexModuleFactory = null):CustomAlert
{
//trace("执行了: show()");
var modal:Boolean = (flags & CustomAlert.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:CustomAlert  = new CustomAlert();
trace("执行了: new CustomAlert()");
// tempAlert = alert;
//trace("静态 "+tempAlert);
alert.setStyle("skinClass", com.pingan.tools.components.skin.CustomAlertSkin);

alert.text = text;
alert.title = title;
alert.imageObject = imageSource;





//按钮标志及默认按钮标志



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

if (defaultButtonFlag == CustomAlert.OK ||
defaultButtonFlag == CustomAlert.CANCEL ||
defaultButtonFlag == CustomAlert.YES ||
defaultButtonFlag == CustomAlert.NO)
{
alert.defaultButtonFlag = defaultButtonFlag;
}
if (closeHandler != null)
alert.addEventListener(CloseEvent.CLOSE, closeHandler);

// 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;
}

trace("执行前: PopUpManager");
alert.addEventListener(FlexEvent.CREATION_COMPLETE, static_creationCompleteHandler);
alert.isPopUp = false;
PopUpManager.addPopUp(alert, parent, modal);
trace("执行后: PopUpManager");
return alert;
}

/** 弹出窗口居中**/
private static function static_creationCompleteHandler(event:FlexEvent):void

{
//trace("执行了: static_creationCompleteHandler()");
if (event.target is IFlexDisplayObject && event.eventPhase == EventPhase.AT_TARGET)
{
var alert:CustomAlert = CustomAlert(event.target);
alert.removeEventListener(FlexEvent.CREATION_COMPLETE, static_creationCompleteHandler);

alert.setActualSize(alert.getExplicitOrMeasuredWidth(),
alert.getExplicitOrMeasuredHeight());
PopUpManager.centerPopUp(IFlexDisplayObject(alert));

}
}

private var defaultButtonChanged:Boolean = false;

override protected function createChildren():void{
super.createChildren();

//trace("执行了: createChildren()");

/** 放置图片及正文的容器 **/
var imageAndLabelHGroup:HGroup = new HGroup() ;

imageAndLabelHGroup.verticalAlign = "middle";
addElement(imageAndLabelHGroup);

/** 显示图片组件 ,如果有图片传进来则创建BitmapImage**/
var imageDisplay:BitmapImage;
if(imageObject!= null){
imageDisplay = new BitmapImage();
imageDisplay.source = imageObject;
imageAndLabelHGroup.addElement(imageDisplay);
}

/** 放置正文的组件Label 如果没有文本则也要有此组件**/
var textLabel:Label = new Label();
//与CustomAlert的皮肤最大宽度一致
textLabel.maxWidth = 320 ;
textLabel.text = text ;
imageAndLabelHGroup.addElement(textLabel);

/** 放置按钮的容器 **/
var btnHGroup:HGroup  = new HGroup();

/** 创建按钮 **/
var buttonFlags:uint = this.buttonFlags;
var defaultButtonFlag:uint = this.defaultButtonFlag;

var label:String;
var button:Button;

if (buttonFlags & CustomAlert.OK)
{
label = String(CustomAlert.okLabel);
button = createButton(label, "OK",btnHGroup);
if (defaultButtonFlag == CustomAlert.OK)
defaultButton = button;
}

if (buttonFlags & CustomAlert.YES)
{
label = String(CustomAlert.yesLabel);
button = createButton(label, "YES",btnHGroup);
if (defaultButtonFlag == CustomAlert.YES)
defaultButton = button;
}

if (buttonFlags & CustomAlert.NO)
{
label = String(CustomAlert.noLabel);
button = createButton(label, "NO",btnHGroup);
if (defaultButtonFlag == CustomAlert.NO)
defaultButton = button;
}

if (buttonFlags & CustomAlert.CANCEL)
{
label = String(CustomAlert.cancelLabel);
button = createButton(label, "CANCEL",btnHGroup);
if (defaultButtonFlag == CustomAlert.CANCEL)
defaultButton = button;
}

if (!defaultButton && buttons.length)
defaultButton = buttons[0];

// Set the default button to have focus.
if (defaultButton)
{
defaultButtonChanged = true;
invalidateProperties();
}
addElement(btnHGroup);






}

// 还不知道有啥用处
private var buttons:Array = [];

/**
*  创建按钮
*/
private function createButton(label:String, name:String,container:HGroup):Button
{
//trace("执行了: createButton(): |name = "+name+" |label = "+label);
var button:Button = new Button();

button.label = label;
button.name = name;

button.buttonMode = true;
button.addEventListener(MouseEvent.CLICK, clickHandler);
button.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
container.addElement(button);
button.setActualSize(CustomAlert.buttonWidth, CustomAlert.buttonHeight);

buttons.push(button);

return button;
}



private static function clickHandler(event:MouseEvent):void
{
var name:String = Button(event.currentTarget).name;
trace(event.currentTarget+"||"+((event.target as  UIComponent ).parentDocument as Skin ).owner);
//获取CustomAlert对象
var tempAlert:CustomAlert = ((event.target as  UIComponent ).parentDocument as Skin ).owner as CustomAlert;
removeAlert(name,tempAlert);
}

/**相应键盘事件 **/

override protected function keyDownHandler(event:KeyboardEvent):void
{
var tempAlert:CustomAlert = ((event.target as  UIComponent ).parentDocument as Skin ).owner as CustomAlert;
var buttonFlags:uint = tempAlert.buttonFlags;

if (event.keyCode == Keyboard.ESCAPE)
{
if ((buttonFlags & CustomAlert.CANCEL) || !(buttonFlags & CustomAlert.NO))
removeAlert("CANCEL",tempAlert);
else if (buttonFlags & CustomAlert.NO)
removeAlert("NO",tempAlert);
}
}


/**发送点击按钮的事件,并且关闭CustomAlert **/

private static function removeAlert(buttonPressed:String,alert:CustomAlert):void
{
var tempAlert:CustomAlert = alert ;
tempAlert.visible = false;
var closeEvent:CloseEvent = new CloseEvent(CloseEvent.CLOSE);
if (buttonPressed == "YES")
closeEvent.detail = CustomAlert.YES;
else if (buttonPressed == "NO")
closeEvent.detail = CustomAlert.NO;
else if (buttonPressed == "OK")
closeEvent.detail = CustomAlert.OK;
else if (buttonPressed == "CANCEL")
closeEvent.detail = CustomAlert.CANCEL;
tempAlert.dispatchEvent(closeEvent);

mx.managers.PopUpManager.removePopUp(tempAlert);

}
}
}































<?xml version="1.0" encoding="utf-8"?>

<!--

    ADOBE SYSTEMS INCORPORATED
    Copyright 2008 Adobe Systems Incorporated
    All Rights Reserved.

    NOTICE: Adobe permits you to use, modify, and distribute this file
    in accordance with the terms of the license agreement accompanying it.

-->

<!--- The default skin class for a Spark SkinnableContainer container. 

     @see spark.components.SkinnableContainer
       
      @langversion 3.0
      @playerversion Flash 10
      @playerversion AIR 1.5
      @productversion Flex 4
-->
<s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:fb="http://ns.adobe.com/flashbuilder/2009" minWidth="200" minHeight="80"
alpha.disabled="0.5"
maxWidth="350">
<fx:Metadata>[HostComponent("com.pingan.tools.components.CustomAlert")]</fx:Metadata>

<fx:Script fb:purpose="styling">
<![CDATA[
import mx.core.FlexVersion;

/*
override protected function initializationComplete():void
{

super.initializationComplete();
}

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
if (getStyle("borderVisible") == true)
{
border.visible = true;
background.left = background.top = background.right = background.bottom = 1;
contents.left = contents.top = contents.right = contents.bottom = 1;
}
else
{
border.visible = false;
background.left = background.top = background.right = background.bottom = 0;
contents.left = contents.top = contents.right = contents.bottom = 0;
}

dropShadow.visible = getStyle("dropShadowVisible");

var cr:Number = getStyle("cornerRadius");


if (cornerRadius != cr)
{
cornerRadius = cr;

dropShadow.tlRadius = cornerRadius;
dropShadow.trRadius = cornerRadius;


setPartCornerRadii(topMaskRect);
setPartCornerRadii(border);
setPartCornerRadii(background);               
}



borderStroke.color = getStyle("borderColor");
borderStroke.alpha = getStyle("borderAlpha");
backgroundFill.color = getStyle("backgroundColor");
backgroundFill.alpha = getStyle("backgroundAlpha");

super.updateDisplayList(unscaledWidth, unscaledHeight);
}


private function setPartCornerRadii(target:Rect):void
{           
target.topLeftRadiusX = cornerRadius;
target.topRightRadiusX = cornerRadius;

}

private var cornerRadius:Number; */
]]>       
</fx:Script>

<s:states>
<s:State name="normal" />
<s:State name="disabled" />
</s:states>

<!-- drop shadow can't be hittable so it stays sibling of other graphics -->
<!--- @private -->
<s:RectangularDropShadow id="dropShadow" blurX="20" blurY="20" alpha="0.32" distance="11"
angle="90" color="#000000" left="0" top="0" right="0" bottom="0"/>


<!-- drop shadow can't be hittable so all other graphics go in this group -->
<s:Group left="0" right="0" top="0" bottom="0">

<!-- top group mask -->
<!--- @private -->
<s:Group left="1" top="1" right="1" bottom="1" id="topGroupMask" >
<!--- @private -->
<s:Rect id="topMaskRect" left="0" top="0" right="0" bottom="0">
<s:fill>
<s:SolidColor alpha="0"/>
</s:fill>
</s:Rect>
</s:Group>



<!-- layer 1: border -->
<!--- @private -->
<s:Rect id="border" left="0" right="0" top="0" bottom="0" >
<s:stroke>
<!--- @private -->
<s:SolidColorStroke id="borderStroke" weight="1"  color="0xD9D9D9"/>
</s:stroke>
</s:Rect>

<!-- layer 2: background fill -->
<!--- Defines the appearance of the PanelSkin class's background. -->
<s:Rect id="background" left="1" top="1" right="1" bottom="1">
<s:fill>
<!--- @private
Defines the  PanelSkin class's background fill. The default color is 0xFFFFFF. -->
<s:SolidColor id="backgroundFill" color="0xFFFFFF"/>
</s:fill>
</s:Rect>

<!-- layer 3: contents -->
<!--- Contains the vertical stack of titlebar content and controlbar. -->
<s:Group left="1" right="1" top="1" bottom="1" id="contents">
<s:layout>
<s:VerticalLayout gap="0" horizontalAlign="justify" />
</s:layout>

<!--- @private -->
<s:Group id="topGroup" mask="{topGroupMask}">

<!-- layer 0: title bar fill -->
<!--- @private -->
<s:Rect id="tbFill" left="0" right="0" top="0" bottom="1">
<s:fill>
<s:LinearGradient rotation="90">
<s:GradientEntry color="0xE2E2E2" />
<s:GradientEntry color="0xD9D9D9" />
</s:LinearGradient>
</s:fill>
</s:Rect>

<!-- layer 1: title bar highlight -->
<!--- @private -->
<s:Rect id="tbHilite" left="0" right="0" top="0" bottom="0">
<s:stroke>
<s:LinearGradientStroke rotation="90" weight="1">
<s:GradientEntry color="0xEAEAEA" />
<s:GradientEntry color="0xD9D9D9" />
</s:LinearGradientStroke>
</s:stroke>
</s:Rect>

<!-- layer 2: title bar divider -->
<!--- @private -->
<s:Rect id="tbDiv" left="0" right="0" height="1" bottom="0">
<s:fill>
<s:SolidColor color="0xC0C0C0" />
</s:fill>
</s:Rect>
<!-- layer 3: text -->
<s:HGroup left="9" right="3" top="1" bottom="0" minHeight="20"
  verticalAlign="middle">
<!--if you want your CustomAlert has a title icon  you can  give it a BitmapImage like this
if you don't , you can delete it
-->
<s:BitmapImage  id="iconDisplay"  />

<!--- @copy spark.components.Panel#titleDisplay -->
<s:Label id="titleDisplay" maxDisplayedLines="1"

verticalAlign="middle" textAlign="start" fontWeight="bold">
</s:Label>
<!-- layer : moveArea -->
<!--- @copy spark.components.TitleWindow#moveArea -->

</s:HGroup>
<s:Group id="moveArea" left="0" right="0" top="0" bottom="0" />
</s:Group>

<!--
Note: setting the minimum size to 0 here so that changes to the host component's
size will not be thwarted by this skin part's minimum size.   This is a compromise,
more about it here: http://bugs.adobe.com/jira/browse/SDK-21143
-->
<!--- @copy spark.components.SkinnableContainer#contentGroup -->
<s:Group id="contentGroup" width="100%" height="100%" minWidth="0" minHeight="0">
<s:layout.normal>
<s:VerticalLayout horizontalAlign="center" paddingBottom="10" paddingLeft="10"
  paddingRight="10" paddingTop="10"/>
</s:layout.normal>

</s:Group>


</s:Group>
</s:Group>

</s:Skin>
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics