import util.Observable import util.IteratorCat /** * Represents the array of photos (i.e., the Model of the MVC triad). */ class main.PhotoModelSql extends Observable { private var photos : Array private var photoNames:Array private var photoCat:Array private var current : Number = 0 private var iterator : IteratorCat private var myData :LoadVars /** * Constructor. */ public function PhotoModelSql () { //build by default an Iterator passing this list as the reference photos=new Array() photoNames=new Array() photoCat=new Array() //Position 0 empty photos.push("") photoNames.push("") photoCat.push("") } /* * sets the Iterator for this model * @param list is an array of images * @param cat is an array of categories * Both are passed from the loadData method */ public function setIterator(list:Array, cat:Array, names:Array):Void{ iterator = new IteratorCat (list, cat, names) currentItem= 1 } /* * Loads data from passed URL * @param URL The url to load data from */ public function loadData (url : String) : Void { var i : Number //for loops myData = new LoadVars () myData.ignoreWhite = true var owner = this myData.onLoad = function (ok) { if (ok) { var cant = this.cant for (var i = 0; i < cant; i ++) { owner.photos.push (this["photo"+i]) owner.photoNames.push (this['name'+i]) owner.photoCat.push(this['cat'+i]) } //set Iterator owner.setIterator(owner.photos, owner.photoCat, owner.photoNames) //Broadcast to listeners owner.o.loaded = true owner.setChanged() owner.notifyObservers({name:"loaded"}) } } myData.load (url) } //Return quantity of items in this Model public function getCantItems(){ return iterator.getCantItems() } /* * Filter items on this Model, based on categories * @param ctg is the category selected */ public function filter(ctg:String){ iterator.filter(ctg) } //Forward positioning to iterator public function first():Void{ iterator.first(); } //Forward positioning to iterator public function last():Void{ iterator.last(); } public function next():Void{ iterator.next(); setChanged() notifyObservers({name:"next", item:currentItem}) } public function previous():Void{ iterator.previous(); setChanged() notifyObservers({name:"previous", item:currentItem}) } //Indicate if the collection have one more elemen public function hasNext():Boolean{ return iterator.hasNext() } /* * Load a Photo and broadcast change * @param nr The position of the image to be load */ public function loadPhoto(nr):Void{ currentItem = nr setChanged() notifyObservers({name:"loadPhoto", item:currentItem}) } /* * Allows to set current item like SomeObject.cursorItem=x instead of SomeObject.setcursorItem(x) * @param pos The position to set as the current item */ public function set currentItem(pos:Number):Void{ iterator.currentItem = pos } //Return current item public function get currentItem():String{ return iterator.currentItem } // Return name of current item public function getCurrentName(){ return iterator.getCurrentName() } //Return current position function getPos():Number{ return iterator.getPos() } /* * Return item at some position * @param pos The position of the returned item */ public function getItemAt(pos:Number):String{ return iterator.getItemAt(pos) } }