home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 2010 Software/Programs / PCGuia_programas.iso / Software / Utils / Livebrush / Install-LivebrushLite.air / livebrush.swf / scripts / com / livebrush / utils / Selection.as < prev    next >
Encoding:
Text File  |  2009-10-26  |  2.9 KB  |  130 lines

  1. package com.livebrush.utils
  2. {
  3.    import flash.events.EventDispatcher;
  4.    
  5.    public class Selection extends EventDispatcher
  6.    {
  7.       private var _items:Array;
  8.       
  9.       public function Selection()
  10.       {
  11.          super();
  12.          this.init();
  13.       }
  14.       
  15.       public function get items() : Array
  16.       {
  17.          return this._items;
  18.       }
  19.       
  20.       public function isSelected(o:Object) : Boolean
  21.       {
  22.          return this.selectionIndex(o) > -1;
  23.       }
  24.       
  25.       public function addAndRemoveItems(items:Array) : void
  26.       {
  27.          for(var i:int = 0; i < items.length; i++)
  28.          {
  29.             this.addAndRemoveItem(items[i]);
  30.          }
  31.       }
  32.       
  33.       public function getItem(i:int) : Object
  34.       {
  35.          return this._items[i];
  36.       }
  37.       
  38.       public function addSelection(s:Selection, removeDuplicates:Boolean = false) : void
  39.       {
  40.          var i:int = 0;
  41.          if(removeDuplicates)
  42.          {
  43.             while(i < s.items.length)
  44.             {
  45.                this.addAndRemoveItem(s.items[i]);
  46.                i++;
  47.             }
  48.          }
  49.          else
  50.          {
  51.             while(i < s.items.length)
  52.             {
  53.                this.addItem(s.items[i]);
  54.                i++;
  55.             }
  56.          }
  57.       }
  58.       
  59.       public function get length() : int
  60.       {
  61.          return this._items.length;
  62.       }
  63.       
  64.       public function removeItemNum(i:int) : Object
  65.       {
  66.          return this._items.splice(i,1)[0];
  67.       }
  68.       
  69.       private function init() : void
  70.       {
  71.          this._items = [];
  72.       }
  73.       
  74.       public function clear() : void
  75.       {
  76.          this._items = [];
  77.       }
  78.       
  79.       public function addItem(o:Object) : void
  80.       {
  81.          var alreadySelected:Boolean = this.isSelected(o);
  82.          if(!alreadySelected)
  83.          {
  84.             this._items.push(o);
  85.          }
  86.       }
  87.       
  88.       private function selectionIndex(o:Object) : int
  89.       {
  90.          return this._items.indexOf(o);
  91.       }
  92.       
  93.       public function push(o:Object) : void
  94.       {
  95.          this.addItem(o);
  96.       }
  97.       
  98.       public function addAndRemoveItem(o:Object) : Boolean
  99.       {
  100.          var index:int = this.selectionIndex(o);
  101.          var alreadySelected:* = index > -1;
  102.          if(alreadySelected)
  103.          {
  104.             this.removeItemNum(index);
  105.          }
  106.          else
  107.          {
  108.             this._items.push(o);
  109.          }
  110.          return !alreadySelected;
  111.       }
  112.       
  113.       public function clone() : Selection
  114.       {
  115.          var newSelection:Selection = new Selection();
  116.          newSelection.addItems(this.items);
  117.          return newSelection;
  118.       }
  119.       
  120.       public function addItems(items:Array) : void
  121.       {
  122.          for(var i:int = 0; i < items.length; i++)
  123.          {
  124.             this.addItem(items[i]);
  125.          }
  126.       }
  127.    }
  128. }
  129.  
  130.