package
{
import com.exanimo.controls.ProgressBar;
import com.exanimo.net.LoadQueue;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
/**
*
* ..
*
* @langversion ActionScript 3
* @playerversion Flash 9.0.0
*
* @author Matthew Tretter (matthew@exanimo.com)
* @since 2007.10.03
*
*/
public class MultiItemProgressBarExample1 extends Sprite
{
private var _masterQueue:LoadQueue;
/**
*
* Sets up the example.
*
*/
public function MultiItemProgressBarExample1()
{
this._masterQueue = this._createLoadQueue();
var progressBar:ProgressBar = this.getChildByName('multiItemProgressBar') as ProgressBar;
progressBar.source = this._masterQueue;
}
/**
*
* Populates the LoadQueue with random items so that you can see it work.
*
* @param loadQueue:LoadQueue
* the queue to add the items to
* @param numItems:uint
* the number of items to add to the queue
*
*/
public function addItemsToQueue(loadQueue:LoadQueue, numItems:uint):void
{
for (var i:uint = 0; i < numItems; i++)
{
var ldr:URLLoader = new URLLoader();
loadQueue.addItem(ldr);
loadQueue.setLoadArguments(ldr, new URLRequest('http://helpexamples.com/flash/video/water.flv?cachebuster=' + (Math.random() * 999) + new Date().getTime().toString()));
}
}
/**
*
* Returns an XML representation of the LoadQueue so that you can
* monitor what's going on.
*
* @param loadQueue:LoadQueue
* the LoadQueue you want to see
* @return XML
* an XML representation of the supplied LoadQueue
*
*/
public function getXMLRepresentationOfQueue(loadQueue:LoadQueue):XML
{
var out:XML = ;
var item:*;
for (var i:uint = 0; i < loadQueue.numItems; i++)
{
item = loadQueue.getItemAt(i);
if (item is LoadQueue)
{
out.appendChild(this.getXMLRepresentationOfQueue(item));
}
else
{
out.appendChild();
}
}
return out;
}
/**
*
* Updates the text field to show the status of the LoadQueue.
*
* @param e:Event
* the event that triggered the handler
*
*/
public function update(e:Event):void
{
this.outputField.text = this.getXMLRepresentationOfQueue(this._masterQueue).toXMLString();
}
/**
*
* Start loading the queue. Called when a user clicks on the "Load"
* button.
*
* @param e:MouseEvent
* the event that triggered the handler.
*
*/
public function loadButtonClickHandler(e:MouseEvent):void
{
this._masterQueue.load();
}
/**
*
* Pause loading. Called when a user clicks the "Pause" button.
*
* @param e:MouseEvent
* the event that triggered the handler.
*
*/
public function pauseButtonClickHandler(e:MouseEvent):void
{
this._masterQueue.close();
}
/**
*
* Sets the number of maximum simultaneous connections the LoadQueue is
* allowed. Called when the user selects an item in the ComboBox.
*
* @param e:Event (optional)
* the event that triggered the function
*
*/
public function setMaxConnections(e:Event = null):void
{
this._masterQueue.maxConnections = parseInt(maxConnectionsComboBox.value);
}
private function _createLoadQueue():LoadQueue
{
// Create a tree of LoadQueues
var lq:LoadQueue = new LoadQueue();
var a:LoadQueue = new LoadQueue();
var b:LoadQueue = new LoadQueue();
var c:LoadQueue = new LoadQueue();
lq.addItem(a);
lq.addItem(b);
a.addItem(c);
// Add some items to the queues.
this.addItemsToQueue(b, 7);
this.addItemsToQueue(a, 4);
this.addItemsToQueue(c, 3);
// Add event listeners for the controls and the text field.
this.addEventListener(Event.ENTER_FRAME, this.update);
this.loadButton.addEventListener(MouseEvent.CLICK, this.loadButtonClickHandler);
this.pauseButton.addEventListener(MouseEvent.CLICK, this.pauseButtonClickHandler);
this.maxConnectionsComboBox.addEventListener(Event.CHANGE, setMaxConnections);
return lq;
}
}
}