Welcome!

Adobe Flex Authors: Liz McMillan, RealWire News Distribution, Maureen O'Gara, Yakov Fain, Keith Swenson

Related Topics: ColdFusion

ColdFusion: Article

Multi-Tier Application Development with Adobe Flex

A Complete Application with RPC Communications and JMS

The [RemoteClass...] metadata tag above the class definition tells the Flex de-serialization method to use this particular class for de-serialization whenever the server sends com.theriabook.jms.dto.StockQuoteDTO object down.

When the client successfully gets the quotes, it processes them and asks for new ones, as shown in Listing 9.

The E4X provides a very elegant solution for navigating an XML object here. The applyQuotes function iterates though the quotes from our portfolio and gets the XMLList based on the matching Symbol attribute via evaluation of the portfolioModel.security expression. (Symbol==quote.symbol.) This looks similar to XPath, but it's easier, isn't it?

Since there's a chance that the E4X expression above will return an empty XMLList, it's better to check for zero-length to avoid exceptions.

We are modifying the same XMLList that has been set as the data provider of our grid. In fact for data grids, Flex maintains an internal XMLListCollection with this XMLList as a source. When the program changes the data in the XMLList, these changes are automatically reflected on the screen. Error reporting is often done by calling Alert(), which brings up a pop-up window. But we suggest a less obtrusive way, whereby as an error condition disappears, the normal display restores without user interaction. Let's put a simple red Label control right above the data grid. Later we'll embed it in the Panel's title. An error, if any, will be displayed in this Label control, and to make it a bit fancier, the detailed error description will be displayed as a tooltip whenever the user moves the mouse over this field:

<mx:Label color="red" toolTip="{errorTip}" text="{errorText}" width="100%"/>

To implement this functionality, in the scripting section we will create two bindable variables:

[Bindable]
private var errorText:String;
[Bindable]
private var errorTip:String;

When an error occurs, the onFault function sets the values of the variables errorText and errorTip, and their bindable nature will immediately display these values in the <mx:Label>. But most important, we will attempt to recover by pulling the new quotes set. (see Listing 10). Do not forget to clean the errorText and errorTip variables in the function onResult, if the next attempt to pull the quotes will be successful.

Let's spend some time discussing the process of initiating the quote request. The function pullQuotes() gets initially invoked upon creation of the Panel:

<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" title="Portfolio" . . .
creationComplete="pullQuotes();">

To generate a remote call of some anonymous function every second, we'll use the setTimeout mechanism. The anonymous function initiates the call of the Java object proxied by the remoting destination freshQuotes:

private function pullQuotes():void{
setTimeout(function ():void {freshQuotes.getQuotes();},1000);
}

Listing 11 has the complete code of the first version of PortfolioView1.mxml, which contains just a data grid.

Adding the Charting Component
The population of the data grid is complete, and we are ready to work on adding a Flex charting component (see Figure 4).

Let's create a simple application that adds the pie chart below the data grid and gives the grid and the chart 50% of the screen height each:

<?xml version="1.0" encoding="utf-8"?>
<!--portfolio2.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" >
<PortfolioView2 id="pv"/>
</mx:Application>

The fragment of the PortfolioView2.mxml is shown in Listing 12.

Flex Panel containers have a layout property (horizontal, vertical, and absolute). Since the vertical layout is the default, our chart is positioned right under the grid. Please note that chart's dataProvider is based on the same XMLList portfolioModel.security as is the dataProvider of the portfolioGrid. In other words, we have two views of the same data model. The data binding feature results in immediate updates of both controls on each change of the model. Java Swing developers will appreciate the benefits of this feature as opposed to the JavaBean and property listeners hassle (see Listing 13).

For the pie chart, we've selected a callout type of label positioning, specified so the size of the pie is proportional to the attribute Value, and we've added a 3D depth to the chart by setting explode Radius to 0.02. Note how we've assigned the function name showPosition to the labelFunction attribute of the pie chart. The signature of the labelFunction assumes that the first argument brings the element of the dataProvider corresponding to the current wedge in the series.

Chart/DataGrid Toggling
Imagine a deck of playing cards: only the top card is visible. Hide the top card and you'll see the next one. We'll use the deck of two "cards": one card will display the grid, and another one - the chart. To Java Swing developers this should look like the CardLayout. In Flex jargon it's called <mx:ViewStack>. A fragment of PortfolioView3.mxml introduces MXML components <mx:ViewStack>, <mx:ToggleButtonBar>, and <mx:Canvas> (see Listing 14).

The most suitable Flex containers for toggling the views are ViewStack or its direct descendant TabNavigator. (See Figure 5) The latter uses more screen real estate to paint the tabs. So we'd rather put the view toggling controls on the unused area of the Panel's title bar. The ViewStack component provides programmatic indexed access to the child containers and shows them one at a time. The simplest Flex container is called Canvas, so we wrap up the DataGrid and the PieChart separately inside it. A Canvas is a descendant of the Container ActionScript class and has the properties label and icon. There are two "non-programmatic" ways to use these properties. First, certain containers use them implicitly, for instance, TabNavigator automatically arranges its tabs to display labels and show icons from the nested child containers. The second way is to explicitly use the ViewStack as a data provider for the descendants of the NavBar control such as ButtonBar, LinkBar, and, in our case, ToggleButtonBar. When you use a ViewStack as a data provider, the label and icon properties of the ViewStack container's children are used to populate the navigation items. The ViewStack feeds ToggleButtonbar and ToggleButtonbar controls the ViewStack in return (see Listing 15).

The toggle buttons Show Grid/Show Chart use images, and it's a good idea to embed these resources right into the SWF file. This way the browser can download the entire client in one HTTP request (but the size of the SWF file becomes larger). For the same reason, the multi-file Java applets are packaged into a single JAR.

In Listing 15, we use canvases to wrap, but now we're going wrap up the entire Portfolio Panel. Yes, the Panel will become a child of the Canvas. Here is why we need it. The Canvas is the simplest container, and it's also the absolute positioning container. In other words, if you place A and B as children of the Canvas, they overlap unless each of them has specific x and y coordinate settings. So we're planning on overlapping the ToggleButtonBar and the PortfolioPanel in a single Canvas. As an additional measure, to make the ToggleButtonBar appear on the far right, we put the ToggleButtonBar inside the HBox container. Make sure that the RemoteObject and the XML are moved out to become children of the Canvas; they have to be at the outermost level - this is an MXML requirement (see Listing 16).

Dealing with Financial News
Let's set up the scene for the financial news first, and then we'll add them to the screen that we've developed so far. The data you see here are provided by an RSS feed. RSS stands for Real Simple Syndication and is used for presenting such data as news, blogs, or other Web content in a form of XML that contains summaries of the articles as well as links to the full version of the content. We use it simply to illustrate the client/server communication via the Flex component called <mx:HTTPService>, which facilitates HTTP POST and GET requests from the Flash player to a remote URL with automatic embedding of the parameters. In particular, we'll be using the RSS news generator offered by Yahoo! Finance. Just enter the following URL in your browser: http://biz.yahoo.com/rss.html.
You should see a screen similar to the one depicted in Figure 6. This Web site lets you enter a stock symbol, for example, ADBE.

You'll see a little orange XML button, with a new URL that looks like http://finance.yahoo.com/rss/headline?s=adbe. Just follow this link, which will bring you to the RSS XML feed with the latest news about the symbol ADBE.

In Part 2 of this excerpt we'll be done with programming master-detail relationships; our users will click on the row in the portfolio grid and see the news, then click on one of the lines in the column Link, which opens a new Web browser's window with the full news content.

•   •   •

•   •   •

 

This article is an excerpt from Chapter 5 of Rich Internet Applications with Adobe Flex & Java by Yakov Fain, Dr. Victor Rasputnis, and Anatole Tartakovsky, published by SYS-CON Books/2007.

More Stories By Victor Rasputnis

Dr. Victor Rasputnis is a Managing Principal of Farata Systems. He's responsible for providing architectural design, implementation management and mentoring to companies migrating to XML Internet technologies. He holds a PhD in computer science from the Moscow Institute of Robotics. You can reach him at vrasputnis@faratasystems.com

More Stories By Yakov Fain

Yakov Fain is a Managing Director of Farata Systems, consulting, training and product company. He has authored several Java books, dozens of technical articles. SYS-CON Books released his latest co-authored book , Rich Internet Applications with Adobe Flex and Java: Secrets of the Masters in Spring 2007. Sun Microsystems has nominated and awarded Yakov with the title Java Champion. He leads the Princeton Java Users Group. He is an Adobe Certified Flex Instructor. Currently Yakov works on the book for O'Reilly "Enterprise Application Development with Flex". He twits at twitter.com/yfain.

More Stories By Anatole Tartakovsky

Anatole Tartakovsky is a Managing Principal of Farata Systems. He's responsible for creation of frameworks and reusable components. Anatole authored number of books and articles on AJAX, XML, Internet and client-server technologies. He holds an MS in mathematics. You can reach him at atartakovsky@faratasystems.com

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.