Listing 1: HelloTree.mxml

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

<mx:Label text="Rich Internet Applications with Adobe Flex and Java"/>
<mx:Tree id="tree1" labelField="@label" showRoot="false" width="400">
        <mx:dataProvider>
            <mx:XML format="e4x">
                <folder label="TOC">
                 <folder label="Chapter 1"/>
                 <folder label="Chapter 2"/>
                 <folder label="Chapter 3" >
                    <Sample label="HelloWorld" />
                    <Sample label="HelloBasil" />
                    <Sample label="HelloBasilAS" />
                    <Sample label="HelloTree" />
                 </folder>
                 <folder label="Chapter 4" />
                </folder>
            </mx:XML>
        </mx:dataProvider>
</mx:Tree>

</mx:Application>


Listing 2: A fragment of  portfolio.xml

<portfolio>
      <security>
            <Symbol>MSFT</Symbol>
            <Quantity>10000</Quantity>
            <Price>20.56</Price>
            <Value>1</Value>
       </security>
       <security>
           <Symbol>IBM</Symbol>
           <Quantity>3000</Quantity>
           <Price>80.21</Price>
           <Value>1</Value>
       </security>
    ...
</portfolio>


Listing 3: Portfolio.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	 xmlns="*" layout="vertical">
 	<mx:VDividedBox width="100%" height="100%">
  	  <PortfolioView id="pv"/>
	  <FinancialNews id="fn"  security="{pv.selectedSecurity}"/>
 	</mx:VDividedBox>
</mx:Application>


Listing 4: The top panel's content: PortfolioView.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"  
    width="100%" height="100%"  creationComplete="startQuotes();"    >
<mx:XML  format="e4x" id="portfolioModel" source="portfolio.xml" />
<mx:Panel width="100%" height="100%" title="Portfolio">
	<mx:ViewStack id="vs" width="100%" height="100%">
	<mx:VBox label="Show Grid"  icon="iconGrid" >
	<mx:DataGrid id="portfolioGrid" width="100%" height="100%"
        dataProvider="{portfolioModel.security}"
	 change="selectedSecurity = portfolioGrid.selectedItem.Symbol;">
	<mx:columns><mx:Array>
			<mx:DataGridColumn dataField="Symbol"/>
			<mx:DataGridColumn dataField="Quantity" textAlign="right"/>
			<mx:DataGridColumn dataField="Price" textAlign="right"/>
			<mx:DataGridColumn dataField="Value" textAlign="right"/>
		</mx:Array></mx:columns>
	</mx:DataGrid>

	</mx:VBox>
	<mx:HBox label="Show Chart"  icon="iconChart"   horizontalAlign="center"
	verticalAlign="middle">
	<mx:PieChart id="portfolioPie" dataProvider="{portfolioModel.security}" showDataTips="true"
	itemClick="selectedSecurity=event.hitData.item.Symbol"  height="90%">
		<mx:series>
            <mx:Array>
            <mx:PieSeries labelPosition="callout" field="Value"   labelFunction="showPosition" nameField="Symbol"
			explodeRadius="2"/></mx:Array>
	             </mx:series>
	</mx:PieChart>
	<mx:Legend  verticalAlign="middle" dataProvider="{portfolioPie}" label="{data.Symbol}"/>
	</mx:HBox>
	</mx:ViewStack>
</mx:Panel>
<mx:HBox horizontalAlign="right"  width="98%" >
	    <mx:ToggleButtonBar   dataProvider="{vs}" paddingTop="4" />
</mx:HBox>
<mx:RemoteObject  destination="Portfolio" id="freshQuotes"  >
      <mx:method name="getQuotes" concurrency="last"      result="applyQuotes(event.result as Array)"/>
</mx:RemoteObject>
<!-- mx:Consumer id="consumer" destination="chat-topic-jms"
message="applyQuotes(event.message.body.quotes)"    / -->

   <mx:Script><![CDATA[
        import mx.controls.Alert;
        [Bindable] public var selectedSecurity:String;
         private function showPosition(data:Object, field:String, index:Number, percentValue:Number):String {
            return data.Symbol +  "\n" + "Shares:" + data.Quantity + "\n" + "Price:" + data.Price + "\n" +
			"Value:" + data.Value ;
        }
        [Embed(source="images/icon_chart.png")] public var iconChart : Class;
        [Embed(source="images/icon_grid.png")]  public var iconGrid : Class;

        import com.theriabook.jms.dto.StockQuoteDTO;
        private function applyQuotes(quotes: Array):void {
            for (var i:int=0; i<quotes.length; i++) { 
				quote = StockQuoteDTO(quotes[i]);  //casting
				var row:* = portfolioModel.security.(Symbol==quote.symbol);
			row.Price = Math.round(100*quote.last)/100;
			row.Value = Math.round(row.Price * row.Quantity);
            }
        }
		internal var quote:StockQuoteDTO = null;
		private function startQuotes():void{
			setInterval(function ()  { freshQuotes.getQuotes(); }, 1000);
			//consumer.subscribe();
		}
    ]]></mx:Script>
</mx:Canvas>


Listing 5: The ActionScript on the client:  StockQuoteDTO.as

package com.theriabook.jms.dto  {

[Managed]
[RemoteClass(alias="com.theriabook.jms.dto.StockQuoteDTO")]
public dynamic class StockQuoteDTO 
{
	public var symbol:String;
	public var last:Number;
}


Listing 6: The bottom panel's content: FinancialNews.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" width="100%" height="100%">
	<mx:DataGrid id="newsGrid" width="100%" height="100%"
		dataProvider="{newsList}"  variableRowHeight="true"> 
	<mx:columns> 
		<mx:Array>
			<mx:DataGridColumn headerText="Date" dataField="pubDate" width="200"/>
			<mx:DataGridColumn headerText="Title" dataField="title"   wordWrap="true" />
			<mx:DataGridColumn headerText="Description" dataField="description"  wordWrap="true"   />
			<mx:DataGridColumn headerText="Link" width="130">
				<mx:itemRenderer>
 				  <mx:Component>
			          <mx:LinkButton label="{data.link}"

          click="navigateToURL(new URLRequest(data.link), '_blank')"/>
				  </mx:Component>
				</mx:itemRenderer>
			</mx:DataGridColumn>
		</mx:Array>
	</mx:columns>
	</mx:DataGrid>

	<mx:XMLListCollection id="newsList" source="{newsFeed.result.channel.item}" />
	<mx:HTTPService id="newsFeed"  useProxy="true"
	 destination="YahooFinancialNews"   concurrency="last"
		resultFormat="e4x" >
	</mx:HTTPService>
	<mx:Script>
		<![CDATA[
			public function set security(value:	String):void {
				newsFeed.send({s:value});
			}
		]]>
	</mx:Script>
</mx:Canvas>


Listing 7: The Java counterpart: StockQuoteDTO.java

package com.theriabook.jms.dto;
import java.io.Serializable;
public class StockQuoteDTO implements Serializable {
	private static final long serialVersionUID = 4672447577075475117L;
	public String symbol; 
	public double last;
	public StockQuoteDTO(String sym, double newPrice){
		symbol = sym;
		last = newPrice; 
	}
}


Listing 8: A simple stock quotes generator: Portfolio.java

package com.theriabook.ro;
import java.util.Random;
import com.theriabook.jms.dto.StockQuoteDTO;

public class Portfolio {

	static Random random = new Random();
	static StockQuoteDTO[] quotes = {
			new StockQuoteDTO("IBM", 82.0),
			new StockQuoteDTO("MSFT", 27.0),
			new StockQuoteDTO("ADBE", 38.0),
			new StockQuoteDTO("ORCL", 13.0)};
	double volatility=.05;
	public  StockQuoteDTO[] getQuotes() {
		for (int i = 0; i < quotes.length;i++){
			quotes[i].last +=   random.nextGaussian()* volatility;
		}
    	return quotes;
	}
 
}


Listing 9: Configuring proxy to allow access to an external server

<destination id="YahooFinancialNews">
        <properties>
            <url>http://finance.yahoo.com/rss/headline</url>
        </properties>
    </destination>


Listing 10: Configuring Flex remoting service

<destination id="Portfolio">
        <properties>
            <source>com.theriabook.ro.Portfolio</source>
        </properties>
    </destination>