home *** CD-ROM | disk | FTP | other *** search
- package com.livebrush.utils
- {
- import flash.events.EventDispatcher;
-
- public class Selection extends EventDispatcher
- {
- private var _items:Array;
-
- public function Selection()
- {
- super();
- this.init();
- }
-
- public function get items() : Array
- {
- return this._items;
- }
-
- public function isSelected(o:Object) : Boolean
- {
- return this.selectionIndex(o) > -1;
- }
-
- public function addAndRemoveItems(items:Array) : void
- {
- for(var i:int = 0; i < items.length; i++)
- {
- this.addAndRemoveItem(items[i]);
- }
- }
-
- public function getItem(i:int) : Object
- {
- return this._items[i];
- }
-
- public function addSelection(s:Selection, removeDuplicates:Boolean = false) : void
- {
- var i:int = 0;
- if(removeDuplicates)
- {
- while(i < s.items.length)
- {
- this.addAndRemoveItem(s.items[i]);
- i++;
- }
- }
- else
- {
- while(i < s.items.length)
- {
- this.addItem(s.items[i]);
- i++;
- }
- }
- }
-
- public function get length() : int
- {
- return this._items.length;
- }
-
- public function removeItemNum(i:int) : Object
- {
- return this._items.splice(i,1)[0];
- }
-
- private function init() : void
- {
- this._items = [];
- }
-
- public function clear() : void
- {
- this._items = [];
- }
-
- public function addItem(o:Object) : void
- {
- var alreadySelected:Boolean = this.isSelected(o);
- if(!alreadySelected)
- {
- this._items.push(o);
- }
- }
-
- private function selectionIndex(o:Object) : int
- {
- return this._items.indexOf(o);
- }
-
- public function push(o:Object) : void
- {
- this.addItem(o);
- }
-
- public function addAndRemoveItem(o:Object) : Boolean
- {
- var index:int = this.selectionIndex(o);
- var alreadySelected:* = index > -1;
- if(alreadySelected)
- {
- this.removeItemNum(index);
- }
- else
- {
- this._items.push(o);
- }
- return !alreadySelected;
- }
-
- public function clone() : Selection
- {
- var newSelection:Selection = new Selection();
- newSelection.addItems(this.items);
- return newSelection;
- }
-
- public function addItems(items:Array) : void
- {
- for(var i:int = 0; i < items.length; i++)
- {
- this.addItem(items[i]);
- }
- }
- }
- }
-
-