2009년 2월 27일 금요일

[FLEX] 하이라키(Hierarchical) 데이터그리드 헤더 CheckBox 렌더러

/**
 * DataGrid의 아이템 렌더러들을 관리하는 패키지
 **/

package dataGrid.renderers
{
 import flash.events.*;
 
 import mx.collections.ArrayCollection;
 import mx.collections.HierarchicalCollectionView;
 import mx.collections.HierarchicalData;
 import mx.collections.XMLListCollection;
 import mx.controls.CheckBox;
 import mx.controls.Label;
 import mx.controls.dataGridClasses.DataGridListData;
 import mx.core.ClassFactory;
 
 
 /**
  * DataGrid의 헤더에 사용되는 체크박스 아이템 렌더러 클래스
  *
  *  @includeExample CheckBox_example.mxml
  **/
 public class HierarchicalDataGridHeaderTextAndCheckBox {
  private var cbx:CheckBox;
  private var dgr:*;
  private var lbl:Label;
  private var _headerLabel:String;
  private var fieldName:String;
 
  public function HierarchicalDataGridHeaderTextAndCheckBox()
  {
   super();
   this.horizontalScrollPolicy = this.verticalScrollPolicy = "off";
   this.setStyle("horizontalAlign","center");
   this.setStyle("verticalAlign","middle");
   this.setStyle("verticalGap", -3);
   this.setStyle("horizontalGap", 0);
   this.percentHeight = this.percentWidth = 100;
   
   lbl = new Label();
   lbl.setStyle("textAlign", "center");
   this.addChild(lbl);
   
   cbx = new CheckBox();
   cbx.addEventListener(MouseEvent.CLICK, clickHandler);
   cbx.width = 14;
   this.addChild(cbx);
  }
      
     /**
   * 데이터 그리드 데이터 프로바이더 총 갯수를 지정하는 속성
   * @default 0
   */
     private var _dataProviderTotalCount:uint = 0;
     public function set dataProviderTotalCount(value:uint):void
  {
   _dataProviderTotalCount = value;
  }
 
  /**
   * 데이터 그리드 데이터 프로바이더 총 갯수를 구하는 속성
   * @default 0
   */
  public function get dataProviderTotalCount():uint
  {
   return _dataProviderTotalCount;
  }
 
  /**
   * 데이터 그리드 데이터 프로바이더 중 체크된 갯수를 지정하는 속성
   * @default 0
   */
  private var _trueCount:int = 0;
     public function set trueCount(value:int):void
     {
      _trueCount = value;
     }
    
     /**
   * 데이터 그리드 데이터 프로바이더 중 체크된 갯수를 구하는 속성
   * @default 0
   */
     public function get trueCount():int
     {
      return _trueCount;
     }
 
   /**
   * 데이터 그리드 체크박스와 headerText가 같이 들어갈 경우 headerText를 지정하는 속성
   *
   * @param String
   *
   * @default null
   */
     public function set headerLabel(value:String):void
     {
      if (value)
      {
       _headerLabel = value;
       lbl.text = _headerLabel;
      }
     }
    
     public function get headerLabel():String
     {
      return _headerLabel;
     }
 
   /**
   * 데이터그리드 헤더 체크박스의 클릭핸들러로서, 헤더 체크박스의 특성상 전체 선택/선택 해지 두 가지 기능을 지원한다.
   *
   * @param event 체크박스의 MouseClick 이벤트
   *
   */
        private function clickHandler(event:MouseEvent):void
        {
      dgr = DataGridListData(this.listData).owner;
   fieldName = DataGridListData(this.listData).dataField;
   var dgrColumIdx:int = DataGridListData(this.listData).columnIndex;
   
            if (cbx.selected)
            {
          ClassFactory(dgr.columns[dgrColumIdx].headerRenderer).properties["trueCount"] = _dataProviderTotalCount;
          dgr.columns[dgrColumIdx].headerText = "true";
          
          checkAll();
         }
         else
         {
          ClassFactory(dgr.columns[dgrColumIdx].headerRenderer).properties["trueCount"] = 0;
          dgr.columns[dgrColumIdx].headerText = "false";
          
          uncheckAll();
     }
     super.invalidateDisplayList();
    }
    
     /**
   * 데이터그리드의 모든 체크 박스의 체크 상태를 check 한다.
   */
       private function checkAll():void
  {
        if (dgr.dataProvider is HierarchicalCollectionView)
        {
         var dgrDp:XMLListCollection = XMLListCollection(HierarchicalData(HierarchicalCollectionView(dgr.dataProvider).source).source);
         var dpLength:int = dgrDp.length;
         
         for (var k:int = 0; k < dpLength; k++)
         {
     var rowData:XMLList = new XMLList(dgrDp.getItemAt(k) as XML);
     checkChild(rowData, true);
    }
        }
        }
       
      /**
   * 데이터그리드의 모든 체크 박스의 체크 상태를 uncheck 한다.
   */
        private function uncheckAll():void
        {
        if (dgr.dataProvider is HierarchicalCollectionView)
        {
         var dgrDp:XMLListCollection = XMLListCollection(HierarchicalData(HierarchicalCollectionView(dgr.dataProvider).source).source);
         var dpLength:int = dgrDp.length;
         
         for (var k:int = 0; k < dpLength; k++)
         {
     var rowData:XMLList = new XMLList(dgrDp.getItemAt(k) as XML);
     checkChild(rowData, false);
    }
        }
        }
       
        /**
         * @private
         */       
        private function checkChild(value:XMLList, isCheck:Boolean):void
        {
   var length:int = value.length();
   
   for (var x:int = 0; x < length; x++)
   {
    var xml:XML = value[x] as XML;
    var child:XMLList = xml.children();
    if (xml.hasOwnProperty("node"))
    {
     xml[fieldName] = isCheck;    
     checkChild(xml.child("node").child("row"), isCheck);
    }
    else
    {
     xml[fieldName] = isCheck;
    }
   }
  }
        
  /**
   * RiaBaseRenderer 의 data 속성을 오버라이드한 것으로 data에 체크 값을 true 또는 false로 셋팅하기 위한 setter 메소드
   *
   * @param value 컨테이너의 경우 DataGridColumn, UIComponent의 경우 각 row의 row 데이터
   */
        override public function set data(value:Object):void
        {
         super.data = value;
     
      if (value)
       cbx.selected = super.data.headerText == "true" ? true : false;
        }
 }
}


댓글 없음: