topical media & game development
mashup-rmx-07-Chapter7-Exercise3-Chapter7-Exercise3.mx
mashup-rmx-07-Chapter7-Exercise3-Chapter7-Exercise3.mx
(swf
)
[ flash
]
flex
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:components="components.*"
layout="absolute"
initialize="init()">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import mx.controls.Alert;
import mx.controls.Button;
import mx.collections.ArrayCollection;
import mx.utils.*;
import mx.managers.*;
import com.almerblank.utils.ServiceUtility;
private var amfphp:ServiceUtility = ServiceUtility.getInstance();
private const GATEWAY:String = 'http://amfphp:8888/gateway.php';
private const POLICY:String = 'http://amfphp:8888/crossdomain.xml';
[Bindable] private var productsDP:ArrayCollection;
[Bindable] private var offset:uint = 0;
[Bindable] private var pageSize:uint;
[Bindable] private var totalProducts:uint;
[Bindable] private var previousVisible:Boolean = false;
[Bindable] private var nextVisible:Boolean = true;
[Bindable] private var keywords:String = '';
[Bindable] private var lowLimit:uint;
[Bindable] private var highLimit:uint;
[Bindable] private var pageText:String;
[Bindable] public var selectedProduct:Object;
private function init():void{
amfphp.init(GATEWAY, false, null);
getProducts(0);
}
private function getProducts(startID:uint):void{
amfphp.call('com.almerblank.ISBN8962.Ch7.Products.browse', resultHandler, faultHandler, startID);
CursorManager.setBusyCursor();
}
private function resultHandler(result:Array):void{
try {
productsDP = new ArrayCollection(result);
pageSize = productsDP.getItemAt(5).pageSize;
offset = productsDP.getItemAt(5).offset;
totalProducts = productsDP.getItemAt(5).totalProducts;
setPageText();
setButtonVisibility();
trace('offset = '+offset+'\npageSize = '+pageSize+'\ntotalProducts = '+totalProducts);
} catch(error:*) {
trace('get products error: '+error.message);
} finally {
CursorManager.removeBusyCursor();
}
}
private function faultHandler(fault:Object):void{
Alert.show(fault.description, 'Service Error:');
CursorManager.removeBusyCursor();
}
private function setButtonVisibility():void{
previousVisible = (offset > 0) ? true : false;
nextVisible = (totalProducts > offset + pageSize) ? true : false;
}
public function searchClickHandler(event:MouseEvent):void{
var button:Button = event.target as Button;
var widget:Object = button.parentDocument;
if(button.label == 'Search'){
keywords = widget.keywords.text;
if(keywords != ''){
searchProducts(0);
button.label = 'Cancel';
widget.keywords.enabled = false;
} else {
Alert.show('Please enter something to search for.', 'Search Error:');
}
} else {
keywords = '';
widget.keywords.text = '';
getProducts(0);
button.label = 'Search';
widget.keywords.enabled = true;
}
}
private function searchProducts(startID:uint):void{
trace('searching products for '+keywords);
amfphp.call('com.almerblank.ISBN8962.Ch7.Products.search', searchResultHandler, faultHandler, keywords, startID);
CursorManager.setBusyCursor();
}
private function searchResultHandler(result:Array):void{
try {
productsDP = new ArrayCollection(result);
var total:uint = productsDP.length - 1;
pageSize = productsDP.getItemAt(total).pageSize;
offset = productsDP.getItemAt(total).offset;
totalProducts = productsDP.getItemAt(total).totalProducts;
setPageText();
setButtonVisibility();
trace('offset = '+offset+'\npageSize = '+pageSize+'\ntotalProducts = '+totalProducts);
} catch(error:*) {
trace('search products error: '+error.message);
} finally {
CursorManager.removeBusyCursor();
}
}
private function fetchPage(startID:uint):void{
if(keywords != ''){
searchProducts(startID);
} else {
getProducts(startID);
}
}
private function setPageText():void{
lowLimit = offset+1;
highLimit = (totalProducts > 5) ? lowLimit+4 : totalProducts;
pageText = 'Displaying items '+lowLimit+' - '+highLimit+' of '+totalProducts;
}
private function fetchProductDetails(event:ListEvent):void{
selectedProduct = event.target.selectedItem;
productViews.selectedIndex = 1;
}
]]>
</mx:Script>
<mx:ViewStack width="100%" height="100%" id="productViews" historyManagementEnabled="true">
<mx:Canvas id="productCatalog">
<mx:Label x="141" fontSize="18" fontWeight="bold" text="Product Catalog" top="30"/>
<mx:Text x="141" text="Select a product to view more details" top="50"/>
<components:SearchBox id="searchBox" x="{dgProducts.width-100}" top="40"/>
<mx:DataGrid
x="141"
y="70"
id="dgProducts"
dataProvider="{productsDP}"
itemClick="fetchProductDetails(event)"
width="900">
<mx:columns>
<mx:DataGridColumn
headerText="Prod. ID"
dataField="product_id"
width="60"/>
<mx:DataGridColumn
headerText="Product"
dataField="product_name"/>
<mx:DataGridColumn
headerText="Description"
dataField="product_desc"/>
<mx:DataGridColumn
headerText="Price"
dataField="product_price" width="80"/>
</mx:columns>
</mx:DataGrid>
<mx:Button
x="141"
y="234"
id="previousPage"
label="Previous"
visible="{previousVisible}"
click="fetchPage(offset-pageSize)"/>
<mx:Text x="500" y="240" text="{pageText}" color="0xffffff"/>
<mx:Button
x="{dgProducts.width+90}"
y="234"
id="nextPage"
label="Next"
visible="{nextVisible}"
click="fetchPage(offset+pageSize)"/>
</mx:Canvas>
<components:ProductDetails id="productDetails"/>
</mx:ViewStack>
</mx:Application>
(C) Æliens
18/6/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.