Listing 1: GSActivities.xml

<messages>
 <message msgType="sale">
   <transID>1234</transID>
   <octane>87</octane>
   <price>2.50</price>
   <gallons>10.2</gallons>   
   <paidby>MC</paidby>
 </message>
 <message msgType="sale">
   <transID>1035</transID>  
   <octane>89</octane>
   <price>2.69</price>
   <gallons>14.5</gallons>      
   <paidby>Cash</paidby>
 </message>
 <message msgType="spill">
   <transID>2301</transID> 
   <octane>93</octane>
   <price>2.99</price>
   <paidby></paidby>
   <gallons>17.3</gallons>   
 </message>
</messages>


Listing 2: GasStation1.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="#e0e0FF">

<mx:XML id="activities" source="GSactivity.xml" />
	<mx:Canvas x="10" y="10" width="100%" height="100%">
 	   <mx:HBox x="10" y="20" width="100%" height="30">
			<mx:CheckBox id="cbx93" label="93"/>
			<mx:CheckBox id="cbx89" label="89"/>
			<mx:CheckBox id="cbx87" label="87"/>
			<mx:Label text="Msg.Type" />
			<mx:ComboBox id="cbMsgTypes" width="117" ?
			dataProvider="{messageType}"></mx:ComboBox>
		</mx:HBox>
		<mx:VBox x="10" y="64" height="100%" width="100%">
			<mx:Label text="Activity" width="100%" fontSize="15"/>
			<mx:DataGrid  id="messageBook" dataProvider="{activities.message}" width="100%"
			height="100%">
				<mx:columns>
					<mx:DataGridColumn headerText="Message Type" dataField="@msgType"/>
					<mx:DataGridColumn headerText="Transaction ID" dataField="transID"/>
					<mx:DataGridColumn headerText="Octane" dataField="octane"/>
					<mx:DataGridColumn headerText="Price per gal." dataField="price"/>
					<mx:DataGridColumn headerText="Amount(gal.)" dataField="gallons"/>
					<mx:DataGridColumn headerText="Paid" labelFunction="paid"/>
				</mx:columns>
			</mx:DataGrid>
			<mx:Label text="Required actions" fontSize="15" />
			<mx:TextArea id="txtAction" width="100%"/>
		</mx:VBox>
	</mx:Canvas>

    <!--Defining USD formatting -->
    <mx:CurrencyFormatter id="usdFormatter" precision="2"
	currencySymbol="$" useThousandsSeparator="false" alignSymbol="left" />

		<mx:Script>
	 <![CDATA[
	     //Data for the Message type  combo
        [Bindable]
        private var messageType: Array = ["all","sale", "spill", "purchase"];

		private function paid(item:Object, column:DataGridColumn):String {
			// calculate total gain/loss
		     var total:Number=Number(item.gallons)* Number(item.price);
		     if (item.@msgType!="sale"){
		     	total*=-1;
		     }
	 	     return ""+usdFormatter.format(total); //USD formatting
		}
	 ]]>
	</mx:Script>
</mx:Application>


Listing 3: MessageTypes.xml

<MessageTypes>
  <message type="sale">


<description>Sale of gasoline products</description>
    		<actions>Sale is good news. No action required
    </actions>
  </message>
  <message type="purchase">
    <description>Purchase of gasoline products from suppliers</description>
    <actions>If the gas station owner is not on premises, please call him at 212-?
    123-4567. Otherwise no actions is required
    </actions>
  </message>
  <message type="spill">
    <description>Spill of gasoline products on the ground</description>
    <actions> Get a bucket of sand and cover the mess. Expect to receive smaller ?
    pay check this week.
    </actions>
  </message>
</MessageTypes>


Listing 4: GasStation3.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundColor="#e0e0FF" applicationComplete="init()">

<mx:XML id="msgTypes" source="MessageTypes.xml" />
<mx:XML id="activities" source="GSactivity.xml" />
<mx:XMLListCollection id="msgList" source="{activities.message}" />
	<mx:Canvas x="10" y="10" width="100%" height="100%">
 	   <mx:HBox x="10" y="20" width="100%" height="30">
			<mx:CheckBox id="cbx93" label="93" selected="true" click="refreshData()"/>
			<mx:CheckBox id="cbx89" label="89" selected="true" click="refreshData()"/>
			<mx:CheckBox id="cbx87" label="87" selected="true" click="refreshData()"/>
			<mx:Label text="Msg.Type" />
			<mx:ComboBox id="cbMsgTypes" width="117" dataProvider="{messageType}">
	            <mx:change>refreshData()</mx:change>
			</mx:ComboBox>
		</mx:HBox>
		<mx:VBox x="10" y="64" height="100%" width="100%">
			<mx:Label text="Activity" width="100%" fontSize="15"/>
			<mx:DataGrid  id="messageBook" dataProvider="{msgList}" width="100%" ?
			height="100%" change="getAction()">
				<mx:columns>

	<mx:DataGridColumn headerText="Message Type" dataField="@msgType"/>
					<mx:DataGridColumn headerText="Transaction ID" dataField="transID"/>
					<mx:DataGridColumn headerText="Octane" dataField="octane"/>
					<mx:DataGridColumn headerText="Price per gal." dataField="price"/>
					<mx:DataGridColumn headerText="Amount(gal.)" dataField="gallons"/>
					<mx:DataGridColumn headerText="Paid" labelFunction="paid"/>
					<mx:DataGridColumn headerText="Paid by" dataField="paidby"/>
				</mx:columns>
			</mx:DataGrid>
			<mx:Label text="Required actions" fontSize="15" />
			<mx:TextArea id="txtAction" width="100%"/>
		</mx:VBox>
	</mx:Canvas>

    <!--Defining USD formatting -->
    <mx:CurrencyFormatter id="usdFormatter" precision="2"
	currencySymbol="$" useThousandsSeparator="false" alignSymbol="left" />
 
    <!-- Gallons Amount formating with 2 digits after dec.point -->
    <mx:NumberFormatter id="numberFormatter" precision="2"/>

	<mx:Script>
	 <![CDATA[

         //Message type  combo data
        [Bindable]
        private var messageType: Array = ["all","sale", "spill", "purchase"];

        import mx.collections.*;
        private var sortMessages:Sort;

        [Bindable]
        private var grandTotalSale:Number=0;

		private function init():void {
			 // assign the filter function
             msgList.filterFunction=filterMessages;
             // perform filtering
             refreshData();

   	        // emulating message feed in specified intervals
            var myTimer:Timer = new Timer(5000, 0); // every 5 sec
            myTimer.addEventListener("timer", addMessage);
            myTimer.start();

		}

        private function filterMessages(item:Object):Boolean{

            // filter by message types
   	        if (cbMsgTypes.selectedLabel !="all" && ?
			item.@msgType!=cbMsgTypes.selectedLabel ){
               return false;
  	        }

            // Check every checkbox and the combobox and
            // populate the datagrid with rows that match
            // selected criteria
        	if (item.octane=="87"  && this.cbx87.selected)
        	   return true;
        	if (item.octane=="89"  && this.cbx89.selected)
        	   return true;
        	if (item.octane=="93"  && this.cbx93.selected)
        	   return true;

            return false;
        }

		private function paid(item:Object, column:DataGridColumn):String {
			// calculate total gain/loss. Label function is not
			// the best place for calculations as it's being called
			// on each change of the underlying collection
		     var total:Number=Number(item.gallons)* Number(item.price);
		     if (item.@msgType!="sale"){
		     	total*=-1;
		     }

	 	     return ""+usdFormatter.format(total); //USD formatting
		}

		private function getAction():void {
		txtAction.text=msgTypes.message.(@type==messageBook.selectedItem.@msgType).actions;
		}

		private function refreshData():void{
  		      msgList.refresh();
  		      txtAction.text="";
		}

        private function addMessage(event:TimerEvent):void{
        // create and add one message with randomly-generated
        // values to the collection
           var newNode:XML = new XML();
           var transID:String=Math.round(Math.random()*5000).toString();
           var octanes: Array = ["87", "89", "93" ];
           var octaneIndex:Number=Math.round(Math.random()*2);
           var octane:String=octanes[octaneIndex];
           var prices: Array = [2.49, 2.69, 2.99 ];
           var price:Number=prices[octaneIndex];

           var msgTypes: Array = ["sale", "purchase", "spill"];
           var msgType:String=msgTypes[Math.round(Math.random()*2)];


           var payTypes: Array = ["MC", "Visa", "Cash" ];
           var payType:String=msgType=="sale"?payTypes[Math.round(Math.random()*2)]:"";

           var gals:String=(numberFormatter.format(Math.random()*50).toString());

          newNode=<message msgType={msgType}>
                    <transID>{transID}</transID>
                    <octane>{octane}</octane>
                    <price>{price}</price>
                    <gallons>{gals}</gallons>   
                    <paidby>{payType}</paidby>
                  </message>;

         // adding new messages always on top
         activities.insertChildBefore( activities.message[0], newNode);
        }
	 ]]>
	</mx:Script>
</mx:Application>