You are previewing Programming Flex 2.
O'Reilly logo
Programming Flex 2

Book Description

Is there an easier way to build and deliver rich internet applications (RIAs) other than the Flash IDE or Ajax? Absolutely. With Adobe Flex 2, the Flex 2 SDK, and this book, you have all you need to build RIAs. Programming Flex 2 offers you plenty of practical and useful examples that reveal how and why to use a particular feature of Flex 2, and when and when not to.

As part of the Adobe Developer Library, Programming Flex 2 is the authoritative guide to this new Adobe framework. You learn to use a markup language called MXML and a vast library of off-the-shelf and highly-configurable components to build Flash-based applications that combine the immediacy of the Web with the functionality and responsiveness of desktop applications. You also discover why -- with the Flash Player runtime environment and the powerful ActionScript 3.0 programming language -- the possibilities with Flex 2 are nearly limitless.

Topics include:

  • Managing Layout

  • Working with Components

  • Working with Media

  • Managing State

  • Using Transitions and Effects

  • Working with Data

  • Customizing Application Appearance

  • Client Data Communication and Remote Data Communication

  • Debugging Flex Framework Applications

  • Creating Custom Components

Flex may be easier to learn than the Flash IDE, but you still need a reliable guide to the framework. Programming Flex 2 not only serves as a reference, but provides valuable and practical insight into this new technology. As you learn how to build Flex applications, you'll also discover how Flex works. This book supplies all the information you need in one convenient place.

Adobe Developer Library is a co-publishing partnership between O'Reilly Media and Adobe Systems, Inc. and is designed to produce the number one information resources for developers who use Adobe technologies. Created in 2006, the Adobe Developer Library is the official source for comprehensive learning solutions to help developers create expressive and interactive web applications that can reach virtually anyone on any platform. With top-notch books and innovative online resources covering the latest in rich Internet application development, the Adobe Developer Library offers expert training and in-depth resources, straight from the source.

Table of Contents

  1. Programming Flex™ 2
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Foreword
      1. This Shouldn't Be Too Hard
      2. Maybe We Need a Different Approach
      3. My First Meeting with Flex
      4. Flex, Take 2
        1. The Flex 2 Framework
        2. Flex Builder 2
        3. ActionScript 3
        4. Flex Data Services
      5. Taking Another Look at Flash
      6. Programming Flex 2
      7. Looking Ahead
    4. Preface
      1. Who This Book Is For
      2. How This Book Is Organized
      3. What You Need to Use This Book
      4. Conventions Used in This Book
      5. Using Code Examples
      6. Comments and Questions
      7. Acknowledgments
        1. From Chafic
        2. From Joey
    5. 1. Introducing Flex
      1. 1.1. Understanding Flex Application Technologies
        1. 1.1.1. Flash Player
        2. 1.1.2. The Flex Framework
        3. 1.1.3. Flex Builder 2
        4. 1.1.4. Integrating with Data Services
        5. 1.1.5. Integrating with Media Servers
        6. 1.1.6. Additional Flex Libraries and Components
      2. 1.2. Using Flex Elements
        1. 1.2.1. MXML
        2. 1.2.2. ActionScript
      3. 1.3. Working with Data Services (Loading Data at Runtime)
      4. 1.4. Understanding the Differences Between Traditional (HTML) and Flex Web Applications
      5. 1.5. Understanding How Flex Applications Work
      6. 1.6. Understanding Flex and Flash Authoring
      7. 1.7. Understanding Flex 1.5 and Flex 2
      8. 1.8. Summary
    6. 2. Building Applications with the Flex Framework
      1. 2.1. Using Flex Tool Sets
      2. 2.2. Creating Projects
        1. 2.2.1. Setting Up a New Project
          1. 2.2.1.1. Creating an SDK project
          2. 2.2.1.2. Creating a Flex Builder 2 project
      3. 2.3. Building Applications
        1. 2.3.1. Compiling Using mxmlc
          1. 2.3.1.1. Specifying an output location
          2. 2.3.1.2. Specifying source paths
          3. 2.3.1.3. Customizing application background color
          4. 2.3.1.4. Changing script execution settings
          5. 2.3.1.5. Setting metadata
          6. 2.3.1.6. Using incremental builds
          7. 2.3.1.7. Storing compiler settings in configuration files
        2. 2.3.2. Using Ant
        3. 2.3.3. Compiling Using Flex Builder
        4. 2.3.4. Publishing Source Code
      4. 2.4. Deploying Applications
      5. 2.5. Flash Player Security
      6. 2.6. Understanding HTML Wrappers
      7. 2.7. Using SWFObject
      8. 2.8. Using Runtime Shared Libraries
        1. 2.8.1. Creating Runtime Shared Libraries with the Command-Line Compilers
          1. 2.8.1.1. Using compc
          2. 2.8.1.2. Compiling an application using a runtime shared library
          3. 2.8.1.3. Using Ant to build runtime shared library applications
        2. 2.8.2. Using Flex Builder to Build Runtime Shared Libraries
          1. 2.8.2.1. Creating a Flex Library Project
          2. 2.8.2.2. Linking an application to a library
        3. 2.8.3. Adding Nonclass Assets to Runtime Shared Libraries
      9. 2.9. Summary
    7. 3. MXML
      1. 3.1. Understanding MXML Syntax and Structure
        1. 3.1.1. Creating MXML Documents
          1. 3.1.1.1. XML encoding
          2. 3.1.1.2. Applications and components
          3. 3.1.1.3. Understanding namespaces
        2. 3.1.2. Components
          1. 3.1.2.1. Containers
          2. 3.1.2.2. UI controls
          3. 3.1.2.3. Setting component properties
          4. 3.1.2.4. Nonvisual components
      2. 3.2. Making MXML Interactive
        1. 3.2.1. Handling Events
        2. 3.2.2. Using Data Binding
      3. 3.3. Summary
    8. 4. ActionScript
      1. 4.1. Using ActionScript
        1. 4.1.1. Inline ActionScript
        2. 4.1.2. Nested ActionScript
        3. 4.1.3. MXML Scripts
        4. 4.1.4. Classes
      2. 4.2. MXML and ActionScript Correlations
      3. 4.3. Understanding ActionScript Syntax
        1. 4.3.1. Understanding Packages
        2. 4.3.2. Declaring Classes
          1. 4.3.2.1. Creating class files
          2. 4.3.2.2. Package declarations
          3. 4.3.2.3. Import statements
          4. 4.3.2.4. Class declaration
      4. 4.4. Variables and Properties
        1. 4.4.1. Methods
        2. 4.4.2. Expressions
        3. 4.4.3. Statements
        4. 4.4.4. Arrays
        5. 4.4.5. Objects
      5. 4.5. Inheritance
      6. 4.6. Interfaces
      7. 4.7. Handling Events
      8. 4.8. Error Handling
        1. 4.8.1. Handling Synchronous Errors
        2. 4.8.2. Handling Asynchronous Errors
      9. 4.9. Using XML
        1. 4.9.1. Creating XML Objects
        2. 4.9.2. Reading XML Data
        3. 4.9.3. Writing to and Editing XML Objects
      10. 4.10. Reflection
        1. 4.10.1. Getting the Class Name
        2. 4.10.2. Getting the Class by Name
        3. 4.10.3. Class Introspection
      11. 4.11. Summary
    9. 5. Framework Fundamentals
      1. 5.1. Understanding the Flex Application Life Cycle
      2. 5.2. Differentiating Between Flash Player and Framework
      3. 5.3. Bootstrapping Flex Applications
        1. 5.3.1. Loading One Flex Application into Another Flex Application
      4. 5.4. Understanding Application Domains
      5. 5.5. Understanding the Preloader
      6. 5.6. Summary
    10. 6. Managing Layout
      1. 6.1. Flex Layout Overview
        1. 6.1.1. Working with Children
        2. 6.1.2. Container Types
        3. 6.1.3. Layout Rules
          1. 6.1.3.1. Box-based layout
          2. 6.1.3.2. Canvas-based layout
            1. 6.1.3.2.1. Constraint-based layout
          3. 6.1.3.3. Hybrid layout containers
          4. 6.1.3.4. Additional layout rules
            1. 6.1.3.4.1. Tile layout rule
            2. 6.1.3.4.2. Grid layout rule
            3. 6.1.3.4.3. Form layout rule
        4. 6.1.4. Padding, Borders, and Gaps
        5. 6.1.5. Nesting Containers
        6. 6.1.6. Handling Scrolling and Clipping
        7. 6.1.7. The Spacer Component
      2. 6.2. Making Fluid Interfaces
      3. 6.3. Putting It All Together
      4. 6.4. Summary
    11. 7. Working with UI Components
      1. 7.1. Understanding UI Components
        1. 7.1.1. Creating Component Instances
        2. 7.1.2. Common UI Component Properties
        3. 7.1.3. Handling Events
          1. 7.1.3.1. Handling events with MXML
          2. 7.1.3.2. Handling events with ActionScript
          3. 7.1.3.3. Event objects
          4. 7.1.3.4. Standard Flex component events
      2. 7.2. Buttons
      3. 7.3. Value Selectors
      4. 7.4. Text Components
      5. 7.5. List-Based Controls
        1. 7.5.1. Data Models
        2. 7.5.2. Creating a Collection Object
        3. 7.5.3. Setting the Data Provider
        4. 7.5.4. Using Data Grids
          1. 7.5.4.1. Using data providers
          2. 7.5.4.2. Working with data grid columns
        5. 7.5.5. Using Tree Controls
        6. 7.5.6. Working with Selected Values and Items
      6. 7.6. Pop-Up Controls
        1. 7.6.1. Understanding Menus
        2. 7.6.2. Using PopUpButton
        3. 7.6.3. Using PopUpMenuButton
        4. 7.6.4. Listening to Menu Events
      7. 7.7. Navigators
        1. 7.7.1. Accordion Controls
        2. 7.7.2. Option Bars
        3. 7.7.3. View Stacks
      8. 7.8. Control Bars
      9. 7.9. Summary
    12. 8. Framework Utilities and Advanced Component Concepts
      1. 8.1. Tool Tips
        1. 8.1.1. Adding Tool Tips
        2. 8.1.2. Controlling Tool Tip Width and Line Formatting
        3. 8.1.3. Applying Styles to Tool Tips
        4. 8.1.4. Customizing Tool-Tip Settings
        5. 8.1.5. Applying Effects
      2. 8.2. Pop-Ups
        1. 8.2.1. Using Alerts
        2. 8.2.2. Creating a Pop-Up Window
        3. 8.2.3. Removing a Pop-Up Window
        4. 8.2.4. Custom Pop-Up Component Types
        5. 8.2.5. Adding Modality
      3. 8.3. Cursor Management
      4. 8.4. Drag and Drop
        1. 8.4.1. Using Built-In Drag and Drop Features
        2. 8.4.2. Understanding Drag and Drop Events
        3. 8.4.3. Custom Drag and Drop Operations
      5. 8.5. Customizing List-Based Controls
        1. 8.5.1. Drop-In Item Renderers
        2. 8.5.2. Inline Item Renderers
        3. 8.5.3. Custom Components as Item Renderers
        4. 8.5.4. Creating Item Editors
      6. 8.6. Focus Management and Keyboard Control
        1. 8.6.1. Controlling Tab Order
        2. 8.6.2. Programmatically Controlling Focus
        3. 8.6.3. Responding to Keyboard Events
      7. 8.7. Summary
    13. 9. Working with Media
      1. 9.1. Overview
        1. 9.1.1. Supported Media Types in Flex
      2. 9.2. Adding Media
        1. 9.2.1. Loading Media
        2. 9.2.2. Embedding Media
          1. 9.2.2.1. Embedding media with MXML
          2. 9.2.2.2. Embedding media with ActionScript
          3. 9.2.2.3. Embedding media with CSS
      3. 9.3. Working with the Different Media Types
        1. 9.3.1. Working with Graphics
          1. 9.3.1.1. Adding graphics
          2. 9.3.1.2. Scaling graphics using a scaling grid (a.k.a. Scale-9)
          3. 9.3.1.3. Working with SWF libraries
          4. 9.3.1.4. Building an asset library using a static class in Flex
          5. 9.3.1.5. Working with SWF animations
        2. 9.3.2. Working with Audio and Video
          1. 9.3.2.1. Adding a sound effect
          2. 9.3.2.2. Advanced sound control
          3. 9.3.2.3. Working with video
          4. 9.3.2.4. Streaming media
      4. 9.4. Summary
    14. 10. Managing State
      1. 10.1. Creating States
      2. 10.2. Applying States
      3. 10.3. Defining States Based on Existing States
      4. 10.4. Adding and Removing Components
      5. 10.5. Setting Properties
      6. 10.6. Setting Styles
      7. 10.7. Setting Event Handlers
      8. 10.8. Using ActionScript to Define States
        1. 10.8.1. Defining States
        2. 10.8.2. Adding States
        3. 10.8.3. Adding Overrides
        4. 10.8.4. Adding and Removing Child Elements
        5. 10.8.5. Setting Properties and Styles
        6. 10.8.6. Setting Event Handlers
        7. 10.8.7. Using Dynamic States
      9. 10.9. Managing Object Creation Policies (Preloading Objects)
      10. 10.10. Handling State Events
      11. 10.11. When to Use States
      12. 10.12. Summary
    15. 11. Using Effects and Transitions
      1. 11.1. Using Effects
        1. 11.1.1. Working with Effects
        2. 11.1.2. Playing Effects
          1. 11.1.2.1. Manually playing effects
          2. 11.1.2.2. Using triggers
        3. 11.1.3. Effect Events
        4. 11.1.4. Composite Effects
        5. 11.1.5. Pausing, Resuming, and Reversing Effects
        6. 11.1.6. Delaying and Repeating Effects
        7. 11.1.7. Customizing Animation Easing
        8. 11.1.8. Using Effects and Fonts
      2. 11.2. Creating Custom Effects
        1. 11.2.1. Defining an Effect Instance Class
        2. 11.2.2. Defining an Effect Factory Class
        3. 11.2.3. Using Custom Effects
        4. 11.2.4. Creating Tween Effects
      3. 11.3. Using Transitions
        1. 11.3.1. Creating Transitions with MXML
        2. 11.3.2. Creating Transitions with ActionScript
        3. 11.3.3. Using Transition Filters
      4. 11.4. Creating Custom Transitions
      5. 11.5. Summary
    16. 12. Working with Data
      1. 12.1. Using Data Models
        1. 12.1.1. Using the Model Tag
          1. 12.1.1.1. Creating tag-based model structures
          2. 12.1.1.2. Populating a model from a file
          3. 12.1.1.3. Referencing model data
        2. 12.1.2. Using XML
          1. 12.1.2.1. Specifying XML structure with tags
          2. 12.1.2.2. Loading XML from a file
          3. 12.1.2.3. Referencing XML data
        3. 12.1.3. Using ActionScript Classes
      2. 12.2. Data Binding
        1. 12.2.1. Understanding Data Binding Syntax
          1. 12.2.1.1. Curly braces
          2. 12.2.1.2. <mx:Binding>
          3. 12.2.1.3. BindingUtils
      3. 12.3. Enabling Data Binding for Custom Classes
        1. 12.3.1. Customizing Data Binding
      4. 12.4. Data Binding Examples
        1. 12.4.1. Controlling Images
        2. 12.4.2. Working with Web Services
      5. 12.5. Building Data Binding Proxies
        1. 12.5.1. Using Data Binding with a Shared Object
      6. 12.6. Summary
    17. 13. Validating and Formatting Data
      1. 13.1. Validating User Input
        1. 13.1.1. Using Validators
          1. 13.1.1.1. Validator basics
          2. 13.1.1.2. Customizing validator messages
          3. 13.1.1.3. Handling validator events
          4. 13.1.1.4. Triggering validators
        2. 13.1.2. Using Standard Framework Validators
          1. 13.1.2.1. StringValidator
          2. 13.1.2.2. NumberValidator
          3. 13.1.2.3. DateValidator
          4. 13.1.2.4. EmailValidator
          5. 13.1.2.5. PhoneNumberValidator
          6. 13.1.2.6. ZipCodeValidator
          7. 13.1.2.7. CreditCardValidator
          8. 13.1.2.8. CurrencyValidator
          9. 13.1.2.9. SocialSecurityValidator
          10. 13.1.2.10. RegExpValidator
        3. 13.1.3. Writing Custom Validators
      2. 13.2. Formatting Data
        1. 13.2.1. NumberFormatter
        2. 13.2.2. DateFormatter
        3. 13.2.3. CurrencyFormatter
        4. 13.2.4. PhoneFormatter
        5. 13.2.5. ZipCodeFormatter
        6. 13.2.6. Writing Custom Formatters
      3. 13.3. Summary
    18. 14. Customizing Application Appearance
      1. 14.1. Using Styles
        1. 14.1.1. Style Value Formats
        2. 14.1.2. Instance Styles
        3. 14.1.3. Using CSS
        4. 14.1.4. Style Properties
        5. 14.1.5. Using StyleManager
        6. 14.1.6. Global Styles
        7. 14.1.7. Reviewing Style Precedence
        8. 14.1.8. Working with Fonts
          1. 14.1.8.1. System fonts
          2. 14.1.8.2. Device fonts
          3. 14.1.8.3. Embedded fonts
          4. 14.1.8.4. Embedding font subsets
          5. 14.1.8.5. Using FlashType
      2. 14.2. Skinning Components
        1. 14.2.1. Applying Skins
        2. 14.2.2. Graphical Skinning
          1. 14.2.2.1. Inline graphical skins
          2. 14.2.2.2. Setting graphical skins with setStyle
          3. 14.2.2.3. Using CSS to set graphical skins
          4. 14.2.2.4. Using Scale-9
          5. 14.2.2.5. Using Flash Library symbols
        3. 14.2.3. Programmatic Skinning
        4. 14.2.4. Skinning Application Backgrounds
        5. 14.2.5. Skinning Tool Tips
      3. 14.3. Customizing the Preloader
      4. 14.4. Themes
        1. 14.4.1. Setting a Theme
        2. 14.4.2. Creating a Theme
      5. 14.5. Runtime CSS
      6. 14.6. Summary
    19. 15. Client Data Communication
      1. 15.1. Local Connections
        1. 15.1.1. Basic Local Connection Communication
        2. 15.1.2. Legacy Communication
        3. 15.1.3. Cross-Domain Communication
      2. 15.2. Persistent Data
        1. 15.2.1. Creating Shared Objects
        2. 15.2.2. Reading and Writing to Shared Objects
        3. 15.2.3. Controlling Scope
        4. 15.2.4. Using Local Shared Objects
        5. 15.2.5. Customizing Serialization
      3. 15.3. Communicating with the Host Application
        1. 15.3.1. Working with ExternalInterface
        2. 15.3.2. Setting the Web Browser Status
        3. 15.3.3. Integrating HTML and Flex Forms
      4. 15.4. Summary
    20. 16. Remote Data Communication
      1. 16.1. Understanding Strategies for Data Communication
      2. 16.2. Working with Request/Response Data Communication
        1. 16.2.1. Simple HTTP Services
        2. 16.2.2. HTTPService
          1. 16.2.2.1. Sending requests
          2. 16.2.2.2. Handling results
          3. 16.2.2.3. Sending parameters
          4. 16.2.2.4. Using HTTPService with ActionScript
        3. 16.2.3. URLLoader
          1. 16.2.3.1. Sending requests
          2. 16.2.3.2. Handling responses
          3. 16.2.3.3. Sending parameters
          4. 16.2.3.4. Using URLLoader in a remote proxy
      3. 16.3. Web Services
        1. 16.3.1. Using WebService Components with MXML
          1. 16.3.1.1. Calling web service methods
          2. 16.3.1.2. Handling results
          3. 16.3.1.3. Sending parameters
        2. 16.3.2. Using WebService Components with ActionScript
          1. 16.3.2.1. Sending requests
          2. 16.3.2.2. Sending parameters
          3. 16.3.2.3. Handling results
          4. 16.3.2.4. Using WebService with a remote proxy
        3. 16.3.3. Data Type Conversion
        4. 16.3.4. Flash Remoting
          1. 16.3.4.1. Creating Flash Remoting connections
          2. 16.3.4.2. Calling Flash Remoting methods
          3. 16.3.4.3. Passing parameters to Flash Remoting methods
          4. 16.3.4.4. Using Flash Remoting remote proxies
      4. 16.4. Real-Time/Socket Connection
      5. 16.5. File Upload/Download
        1. 16.5.1. Downloading Files
        2. 16.5.2. Uploading Files
      6. 16.6. Summary
    21. 17. Application Debugging
      1. 17.1. The Flash Debug Player
        1. 17.1.1. Runtime Errors
        2. 17.1.2. The Debugging API
        3. 17.1.3. Using Show Redraw Regions
      2. 17.2. Using FDB
      3. 17.3. Debugging with Flex Builder 2
      4. 17.4. Remote Debugging
        1. 17.4.1. Establishing a Remote Debugging Session with the Flex Builder Debugger
      5. 17.5. Logging Using trace( ) Within an Application
      6. 17.6. The Logging Framework
        1. 17.6.1. Specifying the Logging Options
        2. 17.6.2. Defining a Custom Target
      7. 17.7. Debugging Remote Data
        1. 17.7.1. Debugging with the Flex Logging Framework
        2. 17.7.2. Debugging Using a Data Inspector
      8. 17.8. Summary
    22. 18. Application Components
      1. 18.1. The Importance of Application Components
      2. 18.2. MXML Component Basics
        1. 18.2.1. Creating and Using a Component
        2. 18.2.2. Adding and Laying Out Controls
        3. 18.2.3. Understanding Interaction
          1. 18.2.3.1. Defining component properties
          2. 18.2.3.2. Defining component methods
          3. 18.2.3.3. Defining component events
      3. 18.3. Component Styles
        1. 18.3.1. Internal States
      4. 18.4. Summary
    23. 19. Building Custom Components
      1. 19.1. Component Framework Overview
      2. 19.2. Component Life Cycle
        1. 19.2.1. Construction
        2. 19.2.2. Configuration
        3. 19.2.3. Attachment
        4. 19.2.4. Initialization
        5. 19.2.5. Invalidation
        6. 19.2.6. Validation
        7. 19.2.7. Destruction
      3. 19.3. Component Implementation
        1. 19.3.1. Implementing the Constructor
        2. 19.3.2. Implementing createChildren( )
        3. 19.3.3. Understanding Invalidation
        4. 19.3.4. Implementing commitProperties( )
        5. 19.3.5. Implementing measure( )
        6. 19.3.6. Implementing updateDisplayList( )
      4. 19.4. Adding Custom Properties and Events
      5. 19.5. Adding Styling Support
      6. 19.6. Summary
    24. Index
    25. About the Authors
    26. Colophon
    27. SPECIAL OFFER: Upgrade this ebook with O’Reilly