组件:
AlarmFlowGroup.mxml
(flex sdk4.1)
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="100%" height="100%"
creationComplete="group_creationCompleteHandler(event)">
<!-- 告警流水窗口组件 -->
<!-- by yinluming in 5.12.2011-->
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.events.ScrollEvent;
[Bindable]
private var _datagridRowCount:int = 10;
public function set datagridRowCount(_datagridRowCount:int):void{
this._datagridRowCount = _datagridRowCount;
}
[Bindable]
private var _datagridRowHeight:int = 20;
public function set datagridRowHeight(_datagridRowHeight:int):void{
this._datagridRowHeight = _datagridRowHeight;
}
[Bindable]
private var baseAc:ArrayCollection = new ArrayCollection([]);
[Bindable]
private var preAc:ArrayCollection = new ArrayCollection([]);
[Bindable]
private var nextAc:ArrayCollection = new ArrayCollection([]);
private var prescrollpst:Number = -1;
/**
*模拟多线程的锁
* */
private var threadLock:int = 0;
private var lockData:ArrayCollection = new ArrayCollection([]);
protected function group_creationCompleteHandler(event:FlexEvent):void
{
testDataCreator(baseAc,1000,"TOM","BOY");
testDataCreator(nextAc,500,"ROSE","GIRL");
testMqData();
}
private function testDataCreator(ac:ArrayCollection,num:int,sname:String,ssex:String):void{
for(var i:int = 0; i < num ; i++){
var sobj:Object = new Object();
sobj['sname'] = sname + i + "Th";
sobj['ssex'] = ssex;
sobj['sage'] = Math.round(Math.random()*100);
sobj['slike'] = sname + i + "Like";
sobj['sheight'] = Math.round(Math.random()*200);
sobj['sweight'] = Math.round(Math.random()*100);
ac.addItem(sobj);
}
}
private function testMqData():void{
var tt:Timer = new Timer(1000);
tt.addEventListener(TimerEvent.TIMER,mqcallback);
tt.start();
}
private function mqcallback(event:TimerEvent):void{
var randomindex:int = int(Math.round(Math.random()*100));
var sobj:Object = new Object();
sobj['sname'] = "MQ" + randomindex + "Th";
sobj['ssex'] = "BOY";
sobj['sage'] = Math.round(Math.random()*100);
sobj['slike'] = "MQ" + randomindex + "Like";
sobj['sheight'] = Math.round(Math.random()*200);
sobj['sweight'] = Math.round(Math.random()*100);
if(threadLock == 0){
if(lockData != null){
for each(var o:Object in lockData){
var obj:Object = lockData.removeItemAt(0);
handlerDataFromMq(obj);
}
}
handlerDataFromMq(sobj);
trace("datagrid dataprovider:" + baseAc.length);
}else{
lockData.addItem(sobj);
trace(sobj['sname'] + "---------------------------" + lockData.length);
}
}
/****
* 暂时简单处理新上告警的排序后插入的位置。
* */
private function handlerDataFromMq(obj:Object):void{
baseAc.addItemAt(obj,0);
var removeObj:Object = baseAc.removeItemAt(baseAc.length - 1);
if(nextAc.length < 500){
nextAc.addItemAt(removeObj,0);
}else{
nextAc.removeItemAt(nextAc.length - 1);
nextAc.addItemAt(removeObj,0);
}
}
private function addDataFromButtom(baseAc:ArrayCollection,nextAc:ArrayCollection,preAc:ArrayCollection,optnum:int):void{
trace("nextAc length" + nextAc.length);
threadLock = 1;
if(nextAc.length == 0){
trace("nextAc num is 0");
doSthAfer();
return;
}
for(var i:int = 0; i < optnum ; i++){
try{
var sobj:Object = nextAc.removeItemAt(0);
}catch(e:Error){
trace("less than optnum");
doSthAfer();
return;
}
baseAc.addItem(sobj);
var removeObj:Object = baseAc.removeItemAt(0);
if(preAc.length < 500){
preAc.addItem(removeObj);
}else{
preAc.removeItemAt(0);
preAc.addItem(removeObj);
}
}
doSthAfer();
}
private function doSthAfer():void{
var tt:Timer = new Timer(1200,1);
tt.addEventListener(TimerEvent.TIMER,timercb);
tt.start();
}
private function timercb(event:TimerEvent):void{
threadLock = 0;
}
private function addDataFromTop(baseAc:ArrayCollection,nextAc:ArrayCollection,preAc:ArrayCollection,optnum:int):void{
threadLock = 1;
if(preAc.length == 0){
trace("preAc num is 0");
doSthAfer();
return;
}
for(var i:int = 0; i < optnum ; i++){
try{
var sobj:Object = preAc.removeItemAt(preAc.length - 1);
}catch(e:Error){
trace("less than optnum");
doSthAfer();
return;
}
baseAc.addItemAt(sobj,0);
var removeObj:Object = baseAc.removeItemAt(baseAc.length - 1);
if(nextAc.length < 500){
nextAc.addItemAt(removeObj,0);
}else{
nextAc.removeItemAt(nextAc.length - 1);
nextAc.addItemAt(removeObj,0);
}
}
doSthAfer();
}
protected function upBtn_clickHandler(event:MouseEvent):void
{
addDataFromTop(baseAc,nextAc,preAc,100);
}
protected function downBtn_clickHandler(event:MouseEvent):void
{
addDataFromButtom(baseAc,nextAc,preAc,100);
event.target.visible = false;
event.target.includeInLayout = false;
}
protected function datagrid_scrollHandler(event:ScrollEvent):void
{
var tempscrollpst:Number = event.position;
if(tempscrollpst == prescrollpst && tempscrollpst != 0){
return;
}else{
this.currentState = "hideBtn";
var datagridrownum:Number = Number(event.currentTarget.dataProvider.length - _datagridRowCount);
if(tempscrollpst == 0 && prescrollpst != -1){
prescrollpst = tempscrollpst;
this.currentState = "showUpBtn";
}else if(tempscrollpst == datagridrownum){
prescrollpst = tempscrollpst + 1;
this.currentState = "showDownBtn";
}
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:states>
<s:State name="hideBtn"/>
<s:State name="showUpBtn"/>
<s:State name="showDownBtn"/>
</s:states>
<mx:DataGrid id="datagrid" x="0" y="0" width="100%" dataProvider="{baseAc}"
rowCount="{_datagridRowCount}" rowHeight="{_datagridRowHeight}"
scroll="datagrid_scrollHandler(event)"
horizontalScrollPolicy="off" verticalScrollPolicy="on">
<mx:columns>
<mx:DataGridColumn headerText="姓名" dataField="sname"/>
<mx:DataGridColumn headerText="性别" dataField="ssex"/>
<mx:DataGridColumn headerText="年龄" dataField="sage"/>
<mx:DataGridColumn headerText="爱好" dataField="slike"/>
<mx:DataGridColumn headerText="身高" dataField="sheight"/>
<mx:DataGridColumn headerText="体重" dataField="sweight"/>
</mx:columns>
</mx:DataGrid>
<s:Button id="upBtn" enabled="true" focusEnabled="false"
x="{datagrid.width - upBtn.width}" y="{datagrid.headerHeight + 3}" buttonMode="true"
visible.hideBtn="false" visible.showUpBtn="true" visible.showDownBtn="false"
includeInLayout.hideBtn="false" includeInLayout.showUpBtn="true" includeInLayout.showDownBtn="false"
skinClass="spark.skins.spark.ScrollBarUpButtonSkin"
click="upBtn_clickHandler(event)"/>
<s:Button id="downBtn" enabled="true" focusEnabled="false"
x="{datagrid.width - downBtn.width}" y="{datagrid.height - downBtn.height}" buttonMode="true"
visible.hideBtn="false" visible.showUpBtn="false" visible.showDownBtn="true"
includeInLayout.hideBtn="false" includeInLayout.showUpBtn="false" includeInLayout.showDownBtn="true"
skinClass="spark.skins.spark.ScrollBarDownButtonSkin"
click="downBtn_clickHandler(event)"/>
</s:Group>
测试app:
TestAlarmFlowGroup.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:alarmflow="*">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<alarmflow:AlarmFlowGroup x="0" y="0" width="900" datagridRowCount="20" datagridRowHeight="25"/>
</s:Application>