Get Professional BlazeDS Creating Rich Internet Applications with Flex and Java 1st Edition Shashank Tiwari free all chapters
Get Professional BlazeDS Creating Rich Internet Applications with Flex and Java 1st Edition Shashank Tiwari free all chapters
https://ebookfinal.com/download/professional-nosql-1st-edition-
shashank-tiwari/
https://ebookfinal.com/download/learning-flex-3-getting-up-to-speed-
with-rich-internet-applications-1st-ed-edition-alaric-cole/
https://ebookfinal.com/download/javafx-developing-rich-internet-
applications-1st-edition-jim-clarke/
https://ebookfinal.com/download/professional-webobjects-with-java-1st-
edition-thomas-termini/
Java and Flex Integration Bible 1st ed Edition Matthew
Keefe
https://ebookfinal.com/download/java-and-flex-integration-bible-1st-
ed-edition-matthew-keefe/
https://ebookfinal.com/download/ajax-rich-internet-applications-and-
web-development-for-programmers-1st-edition-paul-j-deitel/
https://ebookfinal.com/download/frameworks-methodologies-and-tools-
for-developing-rich-internet-applications-1st-edition-giner-alor-
hernandez/
https://ebookfinal.com/download/graphene-materials-fundamentals-and-
emerging-applications-1st-edition-ashutosh-tiwari/
Professional
BlazeDS
Creating Rich Internet Applications with Flex® and Java®
Shashank Tiwari
Programmer to Programmer™
Contact Us.
We always like to get feedback from our readers. Have a book idea?
Need community support? Let us know by e-mailing [email protected]
Download from Library of Wow! eBook <www.wowebook.com>
Professional BlazeDS
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Chapter 1: Flex for Java Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 2: Introducing BlazeDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Chapter 3: Using BlazeDS as a Server-Side Proxy . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Chapter 4: Remoting between Flex and POJOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Chapter 5: Accessing Spring Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Chapter 6: Communicating Real-Time via Messages . . . . . . . . . . . . . . . . . . . . . . 137
Chapter 7: Leveraging JPA and Hibernate with Flex . . . . . . . . . . . . . . . . . . . . . . . . 169
Chapter 8: Testing and Debugging Flex and BlazeDS Applications . . . . . . . . . 215
Chapter 9: Extending BlazeDS with Custom Adapters . . . . . . . . . . . . . . . . . . . . . 253
Chapter 10: Making BlazeDS Applications Scalable . . . . . . . . . . . . . . . . . . . . . . . 271
Chapter 11: CRUD Applications and More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Appendix A: Introducing Flex 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Shashank Tiwari
ISBN: 978-0-470-46489-2
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections
107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or
authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood
Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be
addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201)
748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including
without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or
promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work
is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional
services. If professional assistance is required, the services of a competent professional person should be sought.
Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or
Web site is referred to in this work as a citation and/or a potential source of further information does not mean that
the author or the publisher endorses the information the organization or Web site may provide or recommendations
it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or
disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be
available in electronic books.
Library of Congress Control Number: 2009932709
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress
are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and
other countries, and may not be used without written permission. Flex is a registered trademark of Adobe Systems
Incorporated. Java is a registered trademark of Sun Microsystems, Inc. All other trademarks are the property of their
respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
This book represents the efforts of many people and I sincerely thank them for their contribution.
Many thanks to the entire team at Wrox/Wiley. You made this book possible! Scott Meyers took this
book from a proposal to a real project. Tom Dinse diligently and patiently reviewed the manuscript and
managed the schedule. Many others worked seamlessly behind the scenes to make things happen.
Thanks much to Yakov Fain for meticulously reviewing and validating the manuscript for its technical
correctness. He deserves special praise for his attention to detail and valuable insight.
I sincerely appreciate all of you (everyone at Wiley and Yakov) for working extra hard against tight
timelines to make things happen on schedule.
Special thanks to the folks at Adobe. Thanks to Jeff Vroom for his early encouragement and support.
Thanks to James Ward for the encouraging words he wrote in the forward to this book. Many thanks to
Jeremy Grelle, the lead of the Spring BlazeDS project at SpringSource, for his help.
Above all, thanks to my friends and family for their assistance and encouragement. My wife, Caren,
and sons, Ayaan and Ezra, were extremely loving and accommodating while I took out time on many
evenings, early mornings, and nights in the last many months to finish the book. They were very involved
in the project. My five year old, Ayaan, continuously knew how I was progressing through the book,
often encouraging me to finish a chapter sooner than I would otherwise have. My three year old, Ezra,
was quieter than his normal naughty self when I was working hard typing away on my keyboard. My
wife, Caren, planned the last many weekdays and weekends solely around my writing schedule. She fed
me with delicious food as she always does and helped me stay focused all along, even when the task
looked too difficult. Without their affection, I would have never been able to write this book.
Thanks also to my parents (Mandakini and Suresh), my in-laws (Cissy and Frank), and all other near and
dear ones for their continuing support and encouragement.
My sincere thanks and gratitude to all of you who contributed towards this book.
Foreword xxi
Introduction xxiii
xviii
Download from Library of Wow! eBook <www.wowebook.com>
Contents
Rules Centralization 283
State Repository 283
Caching 283
Signed Framework RSL 285
Resource Pooling 287
JDBC Connection Pooling 287
JMS Resource Pooling 289
Bind Messaging to the Beans Using Camel 290
Using Spring’s Support for Message Listener Containers 291
Workload Distribution 291
Summary 292
Index 337
xix
Download from Library of Wow! eBook <www.wowebook.com>
Download from Library of Wow! eBook <www.wowebook.com>
Foreword
As with many interactions between developers these days, I first met Shashank Tiwari online via his
blog. He was writing thoughtfully about Java and Flex back in the days when this was mostly uncharted
territory where only a handful of brave developers had ventured. Through those interactions we began
making plans to lead more Java developers to the blissful world of Java and Flex.
Having been a professional software developer for more than ten years, I’ve met developers from a
wide variety of backgrounds, including hackers, scientists, teachers, plumbers, and athletes. There is
no better way to know what a developer is like than to write code and articles with them. Over the
past few years Shashank and I have co-authored a number of articles and code samples together. From
these experiences, I know that Shashank is a top notch Java developer and a Flex master. What makes
Shashank’s books stand out, however, is that he is not only a superb developer but also an excellent
teacher.
I was very excited when I heard that Shashank was working on a new book focused on integrating Java
and Flex through BlazeDS. So many Java developers I interact with want to build better software but
are reluctant to learn a new technology stack. Flex and BlazeDS provide a great path for Java developers
to build better software that users love to use. The wealth of out-of-the-box and community-built Flex
components enables developers to rapidly assemble great applications. But there is a learning curve
for the typical Java developer who must understand and navigate the intricacies of a thick-client UI
framework, data marshalling mechanisms, and a new tool chain. Shashank makes that learning curve
smooth and fun.
This book provides much needed practical knowledge for assembling production applications using
Java and Flex. The extensions Shashank has built to BlazeDS, his examples of real-world Spring inte-
gration, and his detailed explanations regarding Hibernate data access provide readers with end-to-end
instructions for building great UIs on Java back-ends.
I’ve had the great privilege of working with and learning from Shashank. He has helped me to be a better
developer and to gain a more complete understanding of new technologies. I’m sure that Professional
BlazeDS will do the same for you.
James Ward
www.jamesward.com
Co-author of First Steps in Flex with Bruce Eckel
www.firststepsinflex.com
Java easily ranks among the most popular enterprise computing platforms. Adobe Flex is a leading Rich
Internet Application (RIA) development framework. The combination of Java and Flex is one of the most
compelling propositions for enterprises to create rich and engaging applications to enhance productivity
and increase customer satisfaction.
An effective integration of Java and Flex implies that you can leverage robust scalable Java server side
applications in conjunction with the superior rendering capabilities of the Flash player. It also means
that your investments in Java server back-ends can be reused and leveraged in your new generation
applications.
Looking at the integration from the other side, an effective combination of Java and Flex means that your
clunky old web 1.0 interfaces and old-style interaction models can be replaced with their more effective
new-generation counterparts.
In addition to all the benefits listed above, an effective plumbing between Java and Flex promises Flex’s
reach to all enterprise resources, including databases, enterprise resource planning systems, authentica-
tion systems and organization-wide messaging systems.
Java and Flex integration is achieved with the help of a number of products and tools, including LifeCycle
Data Services (LCDS) and BlazeDS from Adobe. LCDS is a commercial product and BlazeDS is an open
source alternative to LCDS.
This book is all about combining Java and Flex. The book covers the integration story with BlazeDS as
the protagonist. It explores all aspects of BlazeDS and explains its extension points that allow it to be
stretched beyond its off-the-shelf capabilities.
This is one of the first few books that addresses the topic of Java and Flex integration using BlazeDS.
From the very first draft proposal I have seen a lot of excitement around this topic and this book (at least
among all involved in its creation). As I was writing this book over the last few months, BlazeDS and
Flex saw a lot of rapid changes and inclusion of newer features and capabilities. It was also the time
Spring BlazeDS emerged as a SpringSource and Adobe supported initiatives to integrate Spring and Flex
using BlazeDS. I have made every attempt to keep the content up-to-date and relevant. However, some
of the pieces of software are in beta phase and will continue to evolve as we work hard to make this book
available in your hands. Even though this makes the book susceptible to being outdated on a few rare
instances, I am confident the content remains useful and relevant through the next versions of the Flex
framework, Java, and BlazeDS.
The book at all times attempts to balance between theory and practice, giving you enough visibility into
the underlying concepts and providing you with the best practices and practical advice that you can
apply at your workplace right away.
For those who are Java experts the book teaches the details of BlazeDS and its applicability in using it to
plumb Java and Flex applications together. I understand that some such experts may not have sufficient
knowledge of the Flex framework. Therefore I explicitly introduce Flex 3 in Chapter 1 and provide a peek
into the upcoming Flex 4 version in the appendix.
For those who are Flex developers and architects the book provides explanations and examples of how
they can effectively include BlazeDS in their application stack. I assume much of the conceptual content
would be accessible to such developers. It’s possible though that some advanced topics on persistence,
Spring integration and custom extensions could be difficult for a developer who has little or no knowl-
edge of Java. This book does not attempt to teach Java fundamentals, which is outside its scope. Tools for
learning the essentials of Java are widely available and I encourage you take advantage of them to get
the most out of this book.
I advise that you read the book from start to finish for an initial understanding and then return to specific
chapters to apply detailed and specific recommendations.
Version 1.0 of Spring BlazeDS and FlexMonkey, JMS 1.1 and Hibernate 3.3.2.GA are covered.
As mentioned previously, I recommend that you read the book sequentially from the first to the last
chapter. That way your learning curve will be smooth and gradual.
❑ Flex for Java Developers (Chapter 1) — A brief primer to Flex for Java developers. The chapter
teaches Flex fundamentals, providing a few examples in context.
❑ Introducing BlazeDS (Chapter 2) — This chapter gently introduces BlazeDS. It explains the
BlazeDS architecture, its core features, configuration files and the deployment infrastructure.
In addition, the chapter explains the installation processes and peeks into the underlying core.
xxiv
Download from Library of Wow! eBook <www.wowebook.com>
Introduction
❑ Using BlazeDS as a Server-side Proxy (Chapter 3) — Flex applications can access data and
web services from remote destinations if the remote destinations define an appropriate security
definition, in a file called crossdomain.xml. The Flash player debars access from sources that
lack such a security definition. However, BlazeDS provides server side proxy capabilities to
access these sources. The focus here is on the role of BlazeDS as a server-side proxy.
❑ Remoting between Flex and POJOs (Chapter 4) — This chapter illustrates the details of remote
procedure calls between Flex and Java objects and explains how objects can be streamed between
the two using a binary protocol called AMF (Action Message Format).
❑ Accessing Spring Beans (Chapter 5) — SpringSource, the makers of the Spring Framework, and
Adobe, makers of Flex, combined efforts to create Spring BlazeDS that helps integrate Spring and
Flex applications using BlazeDS effectively. This chapter covers the features of this new integra-
tion project.
❑ Communicating Real-time via Messages (Chapter 6) — BlazeDS supports real-time messaging
and data push. In this chapter I start with the essentials of messaging between Flex and Java and
highlight the important aspects like AMF long polling and the JMS adapter.
❑ Leveraging JPA and Hibernate with Flex (Chapter 7) — Hibernate is the most popular Java per-
sistence framework and JPA is the standard that defines Java EE persistence architecture. Both
the product (Hibernate) and the associated specification (JPA) bring benefits of Object relational
mapping to the world of Java. This chapter explains how the same features can be extended to
Flex applications using BlazeDS.
❑ Testing and Debugging Flex and BlazeDS Applications (Chapter 8) — This chapter explains
the nuts and bolts of logging, testing, and debugging Flex and BlazeDS applications.
❑ Extending BlazeDS with Custom Adapters (Chapter 9) — BlazeDS is a great product off-the-
shelf, but the most valuable aspect of its design is that it can be extended easily to adapt to varied
server side resources. This chapter will explain the available API and the approach to creating,
deploying and using custom adapters.
❑ Making BlazeDS Applications Scalable (Chapter 10) — This chapter explains how you could
scale BlazeDS using the concepts that involve clustering, data compression, data format opti-
mization, robust connection, service orientation, caching, resource pooling, and workload distri-
bution.
❑ CRUD Applications and More (Chapter 11) — This chapter illustrates CRUD application gener-
ation using Flash Builder 4 beta. It also illustrates the use of Gas3 for automatic translation from
Java to AS3 and auto-population of AS3 objects based on XML content that support XML schema
definitions.
❑ Introducing Flex 4 (Appendix) — The appendix introduces the fundamentals of Flex 4, the next
version of the Flex framework. Currently, Flex 4 is in beta. It will be released only in early 2010.
The appendix provides a sneak peek into what’s brewing.
xxv
Download from Library of Wow! eBook <www.wowebook.com>
Introduction
❑ Flex Builder version 3 (not a necessity but convenient)
❑ Flash Builder 4 beta (especially to follow along the directions in Chapter 11)
❑ JBoss AS or Tomcat (when using Tomcat, please install an external JMS provider like ActiveMQ)
❑ Spring BlazeDS
❑ FlexMonkey
❑ JMS provider (like ActiveMQ)
❑ Hibernate
❑ JGroups
❑ Ruby and FunFX
Conventions
To help you get the most from the text and keep track of what’s happening, we’ve used a number of
conventions throughout the book.
Boxes like this one hold important, not-to-be forgotten information that is directly
relevant to the surrounding text.
Notes, tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.
Source Code
As you work through the examples in this book, you may choose either to type in all the code manually or
to use the source code files that accompany the book. All of the source code used in this book is available
for download at www.wrox.com. Once at the site, simply locate the book’s title (either by using the Search
box or by using one of the title lists) and click the Download Code link on the book’s detail page to obtain
all the source code for the book.
xxvi
Download from Library of Wow! eBook <www.wowebook.com>
Introduction
Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is
978-0-470-46489-2.
Once you download the code, just decompress it with your favorite compression tool. Alternately, you
can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see
the code available for this book and all other Wrox books.
Errata
We make every effort to ensure that there are no errors in the text or in the code. However, no one is
perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty
piece of code, we would be very grateful for your feedback. By sending in errata you may save another
reader hours of frustration and at the same time you will be helping us provide even higher quality
information.
To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one
of the title lists. Then, on the book details page, click the Book Errata link. On this page you can view all
errata that has been submitted for this book and posted by Wrox editors. A complete book list including
links to each book’s errata is also available at www.wrox.com/misc-pages/booklist.shtml.
If you don’t spot ‘‘your’’ error on the Book Errata page, go to www.wrox.com/contact/techsupport.
shtml and complete the form there to send us the error you have found. We’ll check the information and,
if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of
the book.
p2p.wrox.com
For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based
system for you to post messages relating to Wrox books and related technologies and interact with other
readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of
your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts,
and your fellow readers are present on these forums.
At http://p2p.wrox.com you will find a number of different forums that will help you not only as you
read this book, but also as you develop your own applications. To join the forums, just follow these steps:
You can read messages in the forums without joining P2P but in order to post your own messages, you
must join.
xxvii
Download from Library of Wow! eBook <www.wowebook.com>
Introduction
Once you join, you can post new messages and respond to messages other users post. You can read
messages at any time on the Web. If you would like to have new messages from a particular forum
e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to
questions about how the forum software works as well as many common questions specific to P2P and
Wrox books. To read the FAQs, click the FAQ link on any P2P page.
xxviii
Download from Library of Wow! eBook <www.wowebook.com>
Flex for Java Developers
This book is a comprehensive resource on leveraging Flex and Java together to create robust
enterprise-grade Rich Internet Applications (RIAs). The book chooses the popular open source
data services product from Adobe, BlazeDS, as the critical integration layer that makes Flex and
server-side Java work together. BlazeDS is a piece of Java code and works within a Java virtual
machine (JVM). More precisely, it’s a web application that can be successfully deployed in a
Java application server or even in a minimalist Servlet container like Apache Tomcat. The book,
therefore, talks more Java than Flex and focuses on how BlazeDS can be configured, customized,
and extended to integrate with all things Java. However, it’s not practical that integration be
studied without looking at both sides of the puzzle. Therefore, in this chapter, we start by talking
about Flex, Java’s counterpart in the context of this book. It’s not a comprehensive treatment of the
subject. It can’t be in mere 40-odd pages. It’s just enough information to help you understand the
basics and to present to you the interfaces and integration points. The illustration and discussion
is biased toward addressing the Java developer more than anybody else, although nothing in this
chapter really requires knowledge of Java itself.
AS3 is an object-oriented programming language that has a close resemblance to the Java and
JavaScript programming languages. MXML is an XML-based declarative language, which makes
programming intuitive and easy. All MXML code is translated automatically to AS3 before it’s com-
piled. This intermediate AS3 representation of the MXML code is often deleted unless a developer
Figure 1-1 shows the essential parts of the Flex framework in a diagram. A Flex application, on com-
pilation, can output a swf file that is ready to run in a Flash Player, or it can output a swc file that can
be consumed as a library and made part of a swf file. A swf file can be dropped into a web server and
accessed over the web using the standard HTTP protocol. When accessed by a user, the swf is down-
loaded locally to the user’s machine and played in the local browser that hosts the Flash Player. Figure 1-2
depicts the accessing of a swf file in a diagram.
Flex SDK
Flex Library
(Controls, Services & Helpers)
MXML
ActionScript 3
(AS3)
AS3
SWC
SWF
Figure 1-1
This book is written for the Flex 3.2 software development kit (SDK) and the Flex 4 SDK. Every attempt
is made to cover the entire set of features in Flex 3.2 and Flex 4 relevant to the integration of Flex and
Java. However, at the time of writing Flex 4 is still in the oven and its final form is still unknown. I will
try and keep close pace with the development of Flex 4 as I write this book. Even then, it’s possible that a
few things may change by the time you get hold of this book. Despite this possible small caveat, I am still
certain that almost all the content of this book will be useful and relevant.
Web Browser
Flash
Player
Web Server
“swf”
file
Figure 1-2
2
Download from Library of Wow! eBook <www.wowebook.com>
Chapter 1: Flex for Java Developers
At this point, let’s write a simple example application to reinforce the basics you have learned so far.
The sample application has two visual interface components: a text input and a text display area. Each of
these components has a label attached to it. The labels help the user understand what to input and what
sense to make of the display. The source for the visual controls alone is:
Adobe likes to refer to Flex user interface components as controls. In addition to user interface com-
ponents, Flex contains classes that act as services, utilities, and helpers. These classes help with the
following:
❑ Maintain data collections
❑ Bind data from source to destinations
❑ Control event propagation and handling
❑ Connect to external sources of data and services
❑ Actualize effects and transitions
❑ Affect the appearance of the controls
❑ Validate and format data
❑ Facilitate data visualization
❑ Support accessibility requirements
❑ Test, log, tune, and structure applications
The visual controls code suggests the presence of an event handler that is invoked on the click of the
button labeled ‘‘Submit.’’ The code for the click handler is:
[Bindable]
public var yourName:String;
Next, the source needs to be compiled. For now, I compile this code simply by pressing the Run button in
Flex Builder (which is now called Flash Builder), the official Eclipse based Flex integrated development
environment (IDE) from Adobe. Unlike the Flex SDK, which is an open source and freely available tool
set, Flash Builder is a purchased, but inexpensive, product. IntelliJ IDEA is an emerging alternative to
Flash Builder, as it now supports Flex development. IntelliJ IDEA does not support the Flash Builder
style ‘‘design’’ mode though. No IDE at all is also an option. The Flex SDK can be used without any IDE
to compile the code successfully. The compiled output using any of the preceding methods produces the
same program in a swf file format.
Tip to Remember
When not using Flash Builder or IntelliJ IDEA, leverage the Flex ANT Tasks, which are bundled with
the free Flex 3.2 SDK, to compile Flex applications. When using ANT Tasks, you can avoid tediously
specifying all necessary parameters on the command line.
3
Download from Library of Wow! eBook <www.wowebook.com>
Chapter 1: Flex for Java Developers
When this compiled swf is accessed via a web browser (which hosts a Flash Player plug-in of version 9
and above), the initial screen shows up, as in Figure 1-3.
Figure 1-3
In the screen, I put in my name and click the Submit button, and I see the application greeting me by my
name, as was expected. Figure 1-4 depicts this output.
Figure 1-4
Next, we refactor this simple application a bit just to emphasize the flexible data-binding aspects of Flex.
The Submit button and the click handler are actually superfluous code and can be gotten rid of. The
TextInput control can directly be bound to the Label that displays the output. The refactored code for
the visual control takes the following form:
Actually, this code does not just represent the visual control but is pretty much the entire code. The
only thing missing is the enclosing Application tags, which form the essential container that houses all
Flex applications. The Application class also works as the entry point for Flex applications in a manner
analogous to the main method in an executable Java or C++ program.
In the refactored code, none of the click handlers or the extra variables is required. This was possible
because the Flex framework under the hood is implicitly triggering and handling events when we bind
the text property of the TextInput control to the text property of the display Label.
On the contrary, an intermediate variable was used earlier to pass the value from the TextInput control
to the display Label. This intermediate variable was declared Bindable to make sure that its value was
propagated through whenever it changed. A variable, when annotated as Bindable, implicitly creates
an underlying set of events and event handlers, which facilitate the data update from the source of the
data to the destination at which it is consumed. Destination variables are wrapped in curly braces that
represent data binding.
Before we move forward to surveying the list of available controls, let’s see the new output of the refac-
tored application in Figure 1-5.
4
Download from Library of Wow! eBook <www.wowebook.com>
Chapter 1: Flex for Java Developers
Figure 1-5
I guess the simple example has whetted your appetite and gotten you curious to learn more. Next, we
dive into the set of controls that come off the shelf with Flex.
In the following subsections, we dig in a bit deeper into each of these controls and see them in use.
The seven different prebuilt button and clickable link controls are:
❑ Button
❑ LinkButton
❑ ButtonBar
❑ ToggleButtonBar
❑ LinkBar
❑ TabBar
❑ PopUpButton
5
Download from Library of Wow! eBook <www.wowebook.com>
Chapter 1: Flex for Java Developers
To demonstrate the capabilities of these controls and understand their usage pattern, let’s build an appli-
cation showcase that puts them together in one place. It’s aptly called the ButtonShowcase and its code
corresponds to an MXML file by the same name. You will find this and all other code in the downloadable
bundle of code associated with this book.
The first of the seven button types is a Button. A Button is a clickable control that all of us are familiar
with. Creating a Button using Flex is simple and involves only a line or two of code, as follows:
Clicking a Button triggers a click event. A function could be defined as an event handler for this click
event. This function is invoked every time the click event is triggered. In the button showcase, the click
handler function merely pops up an alert on-screen, but you are likely to do more useful things within
this function in a real application. The event handler for the simple button is:
A traditional button could be replaced with a link, which could demonstrate the same set of features as
that of a button. Using the LinkButton class, you can quickly create a link that behaves like a button.
Here is the code for it:
Once you have two or more buttons, you may desire to group them together, especially if they are related.
The ButtonBar class comes handy in grouping a bunch of buttons. For our button showcase, we will
create four buttons labeled Flex, AIR, Flash, and Acrobat, respectively, and add them together to a bar of
buttons. The process of creating this aggregation is quite simple. In our code, we don’t really create each
of these four buttons individually and add them one by one. Instead, we use the dataProvider property
of the ButtonBar. An Array of strings is defined and bound to a bar of buttons by assigning it as the
value of the bar’s data provider. Here is the code for your reference:
The dataProvider property allows for a loosely coupled architecture that facilitates the separation of
data and its view in the context of a control.
Data providers further the purpose of separating sources of data from the ways they are manipulated and
consumed within user interface controls.
6
Download from Library of Wow! eBook <www.wowebook.com>
Chapter 1: Flex for Java Developers
Buttons in a ButtonBar are laid out horizontally by default. Setting the direction property of the
ButtonBar control to vertical lays out the constituent buttons vertically. On clicking any of the but-
tons in the bar, an itemClick event is dispatched. An item click handler function can be defined and
bound as the event handler for this event. Our example application itemClick handler is programmed
to pop up an alert dialog box, showing the selected index and the associated label. Here is the source
code for it:
The ButtonBar stacks constituent buttons together and acts as a convenient navigation aid. It does not
maintain state though. This means it does not remember the current selection. However, another control
called ToggleButtonBar comes to your rescue in situations where you need to remember the last selec-
tion. A ToggleButtonBar allows only one button from the group to be selected at a time. Making a new
selection deselects the last choice and sets the new choice as the selected one. Setting the toggleOnClick
property to true deselects a selected item on clicking it.
Like buttons, link buttons can be grouped in a LinkBar. Doing it is easy. Here is some code that demon-
strates how to do it:
Like buttons and links, tabs are a common artifact used for navigation. Typically, different independent
views of an application are navigated to using tabs. A TabBar control lets you define a set of tabs, where
each tab is effectively an element, like a button or a link. The TabBar control has a data provider property,
which helps attach data elements to the control.
So far, you have seen buttons, links, and the bars that can contain buttons and links. In addition to these
Flex also includes a unique button, which has two parts, the first of which is the standard button and
the second of which can be used to pop up any UIComponent. Such a button is called a PopUpButton. The
typical use case is to pop up a list or menu and have the selection in the list or menu update the first
button label. For our example, though, we don’t show this use case. We pop up a date chooser instead.
Figure 1-6 shows the date chooser popping up on clicking the second button.
Figure 1-6
7
Download from Library of Wow! eBook <www.wowebook.com>
Chapter 1: Flex for Java Developers
The PopUpButton can be created in a line or two of code, as follows:
The click handler for the first button just brings up an alert component, while that of the second button
pops the date chooser component. Here is the code for it:
To reinforce what has been stated about the seven button controls so far, they are put together in one
place in Figure 1-7.
Button
LinkButton
ButtonBar
(horizontal)
ButtonBar
(vertical)
ToggleButtonBar
LinkBar
TabBar
PopUpButton
Figure 1-7
A pictorial representation always helps when talking about user interface components.
Next, we look at menu controls, which are another useful set of controls for navigation and triggering
actions.
Menus
A Menu navigation control, in Flex, pops up in response to a user’s action. It has a set of selectable units
or items within it. A menu may have any number of nested submenus inside it.
8
Download from Library of Wow! eBook <www.wowebook.com>
Chapter 1: Flex for Java Developers
Once a user selects a menu item, the Menu, by default, becomes invisible. In addition a Menu also becomes
invisible when the Menu.hide method is explicitly called or a user clicks outside of the Menu component
or selects another component in the application. Figure 1-8 shows what a Menu looks like.
Figure 1-8
The menu can include other controls such as radio buttons, as shown in the example in Figure 1-8. Here
is the data that defines the menu items:
Remember that this menu pops when you click a button. It’s standard practice not to paint the menu
upfront but to bring it up on some user-initiated action. This practice is one of the main reasons why you
will not find an MXML component for a menu. The only way to create a menu is to do so programmati-
cally using AS3.
Figure 1-9
To reuse the data from the Menu control in a MenuBar control you need to collate the menu items as an
XMLList, which is a list of XML objects or elements. The Menu data exists as an XML document or object.
9
Download from Library of Wow! eBook <www.wowebook.com>
Chapter 1: Flex for Java Developers
In an XML document there is a single element at the root node and that does not accommodate multiple
members at the main level. A MenuBar can have multiple members at the primary level. All top-level
elements in an XMLList are added as primary members on the menu bar.
Menus can also appear within a PopUpMenuButton. A PopUpMenuButton is a PopUpButton that pops up
a menu when its secondary button is clicked. When a menu item is selected, the primary button label
is updated with the label of the selected item. The popped-up menu cannot contain submenus in it.
Listing 1-1 shows the code for a PopUpMenuButton.
<mx:PopUpMenuButton id="popUpMenuButton"
dataProvider="{menuData}"
labelField="@label"
showRoot="false"
creationComplete="init();"/>
</mx:Application>
Lists
There are three types of lists available as a part of the Flex controls. These are List, HorizontalList, and
TileList. A List is a vertical display of a list of items. A user can select one or more of these items. The
allowMultipleSelection property value governs if multiple selections are allowed or not. A List could
be created as follows:
<mx:Model id="listItems">
<technologies>
<technology label="Flex" data="1"/>
<technology label="AIR" data="2"/>
10
Download from Library of Wow! eBook <www.wowebook.com>
Exploring the Variety of Random
Documents with Different Content
pienellä kaupalla, metsästyksellä, kalastuksella ja huoneitten
salvamisella, — kulkee ohi.
"Kas vaan, kun olet rehellinen. Mutta sillähän siitä pääset, kun
tulet katsomaan."
"Ei kehtaa."
"Näin sen pitää tapahtua", sanoo hän. "Ja muista nyt sitten."
"Yötä."
"No mikä se niin juoksutti?"
Siinä nyt istui Uula Mellan pirtin penkillä. Kahvi kuohuu yli,
sekoittaen lemullaan ummehtuneen hien hajun, ja aurinko paistaa
korkealla.
SUURTEN JOKIEN VARSILTA
Jokihulluutta.
*****
— Tarvitaan!
*****
*****
Körkön Heikki ei kuule isänsä sanoja, tai jos kuuleekin, niin ei ole
kuulevinaan. Kyllä kai sitä juttua riittäisi, loppumattomiin, ja aina
uusia metkuja, koukkuja ja käänteitä. Kun on mennäkseen, niin
menee, sittenpähän näkee mitä tulee.
*****
Kohtaus joella.
"Hei! Soutakaa tämä herra yli!" — Mutta eipä ollut metsäherra nyt
näkevinäänkään ja kuitenkin taitaisi kuipotella siellä vieläkin, ellei
hän olisi antanut — hm — määräystä miehilleen, että soutakaa tämä
herra yli.
"Älä kysykään!"
"Paina nappia vain, niin kyllä tulee!" neuvoo Lomsteri. "Ja morjes
nyt vain ja kiitoksia. Kylläpähän joskus kostan."
"Päivää!"
Nyt Heikin sisu alkaa kiehua, naama käy kravunpunaiseksi, hän vie
kätensä torveksi suunsa eteen ja huutaa silmät pullollaan:
Mikko Puuperä.
"Joo", sanoo hän, "se on niin, että kaipa hänenkin oli hyvä olla
puulaakissa ja hyvä kai hänellä oli palkkakin, mutta enemmän teki
mieli. Nähkääs kun sitä pitää pelata herraa. Ja sitten tuli Mikko
Puuperä ja hän sai lähteä kävelemään. Eivät viitsineet nostaa juttua,
summa kun oli niin pieni…"
Tiltu.
Hän ei ole mikään "Tukkijoen" Maija, hänen virkansa on aivan
toinen. Hän on ollut kaksi kertaa lasareetissa, mutta hänellä
kerrotaan olevan rahoja säästössä sen Huuhkajan ukon takana, —
tiedättehän, — joka on jollakin tavalla Tiltuun kiintynyt. Ukko on
lestadiolainen ja itkee usein hänen kovaa kohtaansa, mutta tallettaa
kuitenkin hänen synnillä ansaitut rahansa ja antaa hänelle tarpeen
tullen ruokaa ja yösijan. Pappi on siitä nuhdellut Huuhkajan ukkoa,
mutta ukko on vastannut, ettei hänellä ole sydäntä ajaa Tiltua
lankeemuksen tielle, sillä voihan tapahtua, että hän ottaa sanasta,
nuhteesta ja varoituksesta ojentuakseen. "Minkä te teette yhdelle
näistä pienimmistä…"
Eikä Tiltu myöskään ole ruma. Eihän hän tietysti ole kauniskaan,
mutta hänen olentonsa aavistuttaa, että hän voisi olla hyvinkin
hyvän näköinen. Hänellä on sopusuhtainen vartalo, mutta hän ei
koskaan löydä sille oikein sopivia vaatteita. Hänellä on säännölliset
kasvonpiirteet, mutta silmien ympärillä on siniset renkaat ja huulet
ovat aina kosteat, niinkuin imisi hän karamellia. Ja käsistä näkee,
ettei hän koskaan ole tehnyt raskasta työtä, minkä joskus soutanut
itsensä työmaalta toiselle, kun muut ilkeydessään eivät ole ottaneet
häntä veneeseensä.
Entisaikaan, viisi, kymmenen vuotta sitten, Tiltu vielä oli ylpeä.
Eivät hänelle kelvanneet muut kuin herrat ja nuoret ja pulskat. Taisi
joskus ajatella rakkautta ja antautuakin rakkaudesta. Mutta sitten se
alkoi mennä alaspäin, piru tiesi, kuinka, — kenties se oli
ensimmäisen lasareetireisun jälkeen. Hän otti itselleen seuralaiseksi
ja "jarrumieheksi" Sieppi-Amalian, ikivanhan tukkilaislutkan, mutta
riitaantui hänen kanssaan ja liittyi Jokipekan Lauriin,
viinakauppiaaseen. Kunnes huomasi, että edullisinta oli sittenkin
kulkea yksin, pieni puukko rinnalla.
"Raha ensin!"
"Ei tässä reistailemaan ruveta. Kun ei kelpaa, niin ole ilman sitten."
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com