A typical Java developer knows that when you need to develop a GUI for a Java application, Swing is the tool. Eclipse SWT also has a number of followers, but the majority of people use Java Swing. For the past 10 years, it was a given that Swing development wouldn't be easy; you have to master working with the event-dispatch thread, GridBaglayout, and the like. Recently, the NetBeans team created a nice GUI designer called Matisse, which was also ported to MyEclipse. Prior to Matisse, JBuilder had the best Swing designer, but it was too expensive. Now a good designer comes with NetBeans for free.
Why even consider Flex for developing Rich Internet Applications (RIA)? First, we'll give the short answer. Just look at the code in Listing 1. This code compiles and runs in the Flash player and produces the output shown in Figure 1. Yes, it's a tree control with several nodes that know how to expand, collapse, and highlight the user's selection. Imagine the amount of Java code you'd need to write to achieve the same functionality.
The code is nice and clean; the GUI looks rich and appealing. The Flex compiler automatically converts the MXML code in Listing 1 into an object-oriented language called ActionScript, and then compiles it into an SWF file, the format that the Flash player understands.
To add the business processing to this example, we'd need to write event handlers in the ActionScript 3.0 language, which is very similar to Java, but we wouldn't have to worry about routing all events to the event-dispatch queue. Below are some of the other reasons that the Flex/Flash combination is a very promising technology for RIA development:
- The Flash player, a powerful virtual machine with a high-performing byte code/JIT compiler and rich UI API, is available on most of the platforms.
- The size of the VM is small.
- It easily integrates with Web browsers.
- Flash applications can run outside of the Web browser.
- Flex offers component-based programming, which eliminates most of the low-level coding.
- It offers simple integration with all kinds of media (video and audio). Java Swing is behind in this field.
- It has a quick adoption rate as all other Adobe products.
- Most important, Flex 2 easily integrates with Java on the server side.
Flex client applications are compiled SWF files that can be delivered to the client and run by the Flash player, which is installed as a plug-in to your browser. On the client side, Flex consists of a Flash player, a framework of predefined components, a couple of command-line compilers, and an Eclipse-based Flex Builder IDE. On the server side, Flex is a Web application that includes Flex Data Services and a Flex Charting component and can be deployed in any JEE server.
To see Flex in action, let's write the functional specification, then develop and deploy a sample application.
Designing the Stock Portfolio Application
We'll create a Web application that will receive and display a feed containing security prices and the latest news as in Figure 2 and 3.
We'll create a Web application that will receive and display a feed containing security prices and the latest news as in Figure 2 and 3.
The top portion of the screen must be populated by the stocks included in the user's portfolio. For simplicity, store the user's portfolio in the XML file as inListing 2.
When the user clicks on a row with a particular stock (i.e., ADBE as in Figure 2), populate the lower data grid with the headlines related to the selected stock. The news should be coming fromhttp://finance.yahoo.com/rss/headline. The column link has to contain the URLs of the news, and when the user clicks on the link, a new browser's window should pop up displaying the selected news article.
The top of the screen should contain the toggle buttons Show Grid/Show Chart. When the Show Grid option is selected, the user will see the screen as in Figure 2, and when Show Chart is selected, the top data grid has to be replaced with the pie chart (see Figure 3), preserving the same functionality (clicking on the pie slice repopulates the news headlines).The market data has to be refreshed on the screen every second or so.
Since we are limited by the size of this article, for the server side we'll just write a simple POJO that will generate random numbers and push them back to the Flash client. However, in the next article we'll add the data feed from an external Java application via JMS.
Sounds like an ambitious task for a short magazine article, doesn't it? This article is not a tutorial on Flex MXML or ActionScript, but as a Java developer, you should be able to understand how the code is put together with minimal explanations. After deploying and running this application you can study Flex more formally. So let's roll up our sleeves...
Installing Flex Builder and Flex Data Services
At the time of this writing, Flex 2 is still in beta (http://labs.macromedia.com/flexproductline/ ) and its production release is expected this summer. The burning question is, "Is it free?" The answer is, "It depends." There is a free lunch (or rather a complimentary appetizer from the chef), but it'll just whet your appetite, and pretty soon your hand will start slowly reaching for the wallet. Flex Data Services (FDS) that provide seamless integration of Flex into enterprise data and messaging have deployment fees. For high-performance enterprise applications supporting client authentication, messaging, and the like, you may want to purchase the FDS license. For smaller applications, you might use integration via HTTPService, Web Services, and Remote Java invocation using standard and open source technologies.
At the time of this writing, Flex 2 is still in beta (http://labs.macromedia.com/flexproductline/ ) and its production release is expected this summer. The burning question is, "Is it free?" The answer is, "It depends." There is a free lunch (or rather a complimentary appetizer from the chef), but it'll just whet your appetite, and pretty soon your hand will start slowly reaching for the wallet. Flex Data Services (FDS) that provide seamless integration of Flex into enterprise data and messaging have deployment fees. For high-performance enterprise applications supporting client authentication, messaging, and the like, you may want to purchase the FDS license. For smaller applications, you might use integration via HTTPService, Web Services, and Remote Java invocation using standard and open source technologies.
The free part is Flex command-line compilers and the Flex framework that includes all these libraries of rich components. This means you can use any plain text editor for writing Flex code, compile it into the SWF file, and run it in a free Flash 9 player. As of today though, Adobe says that the Eclipse-based Flex Builder IDE, which makes development a lot more productive, will not be free. Flex Charting components that offer client-side interactive charts are not free either.
The Flex Builder IDE comes in two flavors: a standalone version and the Eclipse plug-in. You'll have to choose one of these versions during the installation process. Let's install Eclipse 3.1 first fromwww.eclipse.org/downloads/ (unless you have it already), and then the plug-in version of Flex Builder. During the installation, you'll have to specify the directory where your Eclipse resides (the one that has a subdirectory called plug-ins). After Flex Builder is installed, start the Eclipse IDE, select the menus Window | Open Perspective | Other and add the Flex Development perspective (the Flex Debugging perspective will be added automatically to the Eclipse IDE). The Flex Builder comes with great Help content, which in the plug-in version is hidden under the menu Help | Help Contents | Adobe Help.
To run client-only Flex applications, we need to install one more program: Flash Player 9. However, for our stock portfolio application, we'll need to install and deploy FDS, which is a Web application that can be deployed in any JEE compliant container. Since FDS is a Web application, you can deploy it in the application server of your choice. First, we'll download and install Apache Tomcat 5.5 from http://tomcat.apache.org/. By default, on Windows machines Tomcat is installed in "C:\Program Files\Apache Software Foundation\Tomcat 5.5" and runs on port 8080. At the end of the installation, the Apache Tomcat server will be started as a Windows service. Now download from and run the FDS executable installer (default directory ID C:\fds2), picking the optional Flex Data Services J2EE Application. After the installation is finished, unzip the content of the flex.war into Tomcat's webaps\ROOT directory and restart the Apache Tomcat service. Enter the URL http://127.0.0.1:8080/ in your browser, and you should see the FDS Welcome screen. Flex Data Services are deployed now under Tomcat.
Developing Stock Portfolio Application with Flex
What Runs Where
First, let's see which components will run on the client and what belongs to the Web server. The code shown in Listings 3-6 will be compiled into portfolio.swf, which will run on the client either independently in the Flash 8.5 player, or with the help of portfolio.html (auto-generated by Flex Builder) in the Web browser with the Flash plug-in. Tomcat is our server, which will host compiled Java classes shown in Listings 7 and 8. We'll also make small additions in the server configuration files (see Listings 9 and 10) to specify where to find the Java classes and to allow access to the external Yahoo! service. While Flex provides different ways of client/server communication (RemoteObjects, HTTPService, WebService), in this article our client will use the RPC by means of the <mx:RemoteObject> component that will find the matching Java classes located and configured on the server as described at the end of this article.
What Runs Where
First, let's see which components will run on the client and what belongs to the Web server. The code shown in Listings 3-6 will be compiled into portfolio.swf, which will run on the client either independently in the Flash 8.5 player, or with the help of portfolio.html (auto-generated by Flex Builder) in the Web browser with the Flash plug-in. Tomcat is our server, which will host compiled Java classes shown in Listings 7 and 8. We'll also make small additions in the server configuration files (see Listings 9 and 10) to specify where to find the Java classes and to allow access to the external Yahoo! service. While Flex provides different ways of client/server communication (RemoteObjects, HTTPService, WebService), in this article our client will use the RPC by means of the <mx:RemoteObject> component that will find the matching Java classes located and configured on the server as described at the end of this article.
Developing the GUI Part
Open the Flex perspective in Eclipse and create a new project (e.g., Portfolio_RCP) containing files from Listings 3-7, and then compile it into one file called portfolio.swf. To simplify deployment, set the project's output directory to Tomcat's \ROOT\portfolio.
Open the Flex perspective in Eclipse and create a new project (e.g., Portfolio_RCP) containing files from Listings 3-7, and then compile it into one file called portfolio.swf. To simplify deployment, set the project's output directory to Tomcat's \ROOT\portfolio.
Our main file portfolio.mxml (see Listing 3) divides the screen with the vertical Flex box-type layout (<mx:VDividedBox>) with an adjustable divider between its children (think of Swing's split panes), and it includes two separate code fragments. The bottom child contains a data grid programmed in <FinancialNews> (see Listing 6). The top child contains <PortfolioView> (see Listing 4).
Java developers know that some Swing components store the data and GUI parts separately, for example, the JTable gets its feed from a data model class that can store its data in a Java collection. On a similar note, some Flex controls (e.g., <mx:DataGrid>) can also use ActionScript collection classes as data providers. Our data model is the file portfolio.xml depicted in Listing 2.
This code represents a typical master-detail relationship, where a change in the selected security in the master component <PortfolioView> repopulates the detail component <FinancialNews>.
Getting the Price Quotes
Let's go over some interesting programming techniques that we've applied while coding the top panel, which contains several Flex framework components wrapped into a Canvas container (see Listing 4). After learning that the user's portfolio is represented by portfolio.xml, you may expect at least some number of lines performing XML parsing.
Let's go over some interesting programming techniques that we've applied while coding the top panel, which contains several Flex framework components wrapped into a Canvas container (see Listing 4). After learning that the user's portfolio is represented by portfolio.xml, you may expect at least some number of lines performing XML parsing.
What do you think of this line:
<mx:XML format="e4x" id="portfolioModel" source="portfolio.xml" />
The XML parsing is complete! After this line you can traverse the entire XML document using a simple dot notation, e.g., portfolioModel.security. This magic is possible because Flex supports the e4x format that was introduced by W3C as a simple alternative for reading and processing XML documents.
Another interesting feature to comment is data binding, which is a way to link the data in one object to another. In particular, it's a convenient way to move the data between the GUI and non-visual components. Data binding can be specified using the curly braces syntax. The following lines from Listing 4 provide an immediate refresh of the top data grid and chart as soon as the data in the underlying security changes.
<mx:DataGrid id="portfolioGrid" width="100%" height="100%"
dataProvider="{portfolioModel.security}"
<mx:PieChart id="portfolioPie" dataProvider="{portfolioModel.security}"
dataProvider="{portfolioModel.security}"
<mx:PieChart id="portfolioPie" dataProvider="{portfolioModel.security}"
The variable portfolioModel.security represents the data source, and the destination will change as soon as any property of the source changes. Note that we have one data source mapped to two destinations (the data grid and pie chart), so we don't need to do any additional programming to refresh the data grid and the chart when the incoming market data changes the properties of the security.
No comments:
Post a Comment