Flex 4 Cookbook

Book description

With this collection of proven recipes, you have the ideal problem-solving guide for developing interactive Rich Internet Applications on the Adobe Flash Platform. You'll find answers to hundreds of common problems you may encounter when using Adobe Flex, Flex 4 Framework, or Flash Builder, Adobe's GUI-based development tool.

Flex 4 Cookbook has hands-on recipes for everything from Flex basics to solutions for working with visual components and data access, as well as tips on application development, unit testing, and Adobe AIR. Each recipe provides an explanation of how and why it works, and includes sample code that you can use immediately. You'll get results fast, whether you're a committed Flex developer or still evaluating the technology. It's a great way to jumpstart your next web application.

Topics include:

  • Using Spark Component
  • Text Layout Framework
  • Groups and Layout
  • Spark List and ItemRenderer
  • Images, bitmaps, videos, and sounds
  • CSS, styling, and skinning
  • States and Effects
  • Working with Collections
  • Using DataBinding
  • Validation, formatting, and regular expressions
  • Using Charts
  • Services and Data Access
  • Using RSLs and Modules
  • Working with Adobe AIR 2.0

Publisher resources

View/Submit Errata

Table of contents

  1. A Note Regarding Supplemental Files
  2. Preface
    1. Who This Book Is For
    2. Who This Book Is Not For
    3. How This Book Is Organized
    4. Conventions Used in This Book
    5. Using Code Examples
    6. How to Use This Book
    7. O’Reilly Cookbooks
    8. Safari® Books Online
    9. How to Contact Us
    10. Acknowledgments
  3. 1. Flex and ActionScript Basics
    1. 1.1. Create a Flex Project in Flash Builder
    2. 1.2. Create a Flex Library Project in Flash Builder
    3. 1.3. Set Compiler Options in Flash Builder
    4. 1.4. Compile a Flex Project Without Flash Builder
    5. 1.5. Add an Event Listener in MXML
    6. 1.6. Create Typed Vectors
    7. 1.7. Use Event Bubbling
    8. 1.8. Use Custom Events and Dispatch Data with Events
    9. 1.9. Listen for a Keyboard Event
    10. 1.10. Define Optional Parameters for Methods
    11. 1.11. Define and Implement an Interface
    12. 1.12. Access the Parent of a Flex Component
  4. 2. Containers
    1. 2.1. Position Children Within a Container
    2. 2.2. Dynamically Add and Remove Children
    3. 2.3. Reorder Child Elements of a Container
    4. 2.4. Display Children Using Data Items
    5. 2.5. Use a Custom Item Renderer in a DataGroup
    6. 2.6. Use Multiple Item Renderers in a DataGroup
    7. 2.7. Enable Scrolling in a Container
    8. 2.8. Scale Children of a Container
    9. 2.9. Apply Skins to a Container
    10. 2.10. Set the Background Image of a BorderContainer
    11. 2.11. Use a Control Bar
    12. 2.12. Modify Layout of Content Elements in a Panel
    13. 2.13. Track Mouse Position Within a Container
    14. 2.14. Drag and Drop Between Visual Containers
    15. 2.15. Drag and Drop Between Data Containers
    16. 2.16. Add a Spark Layout Container to a MX Navigation Container
    17. 2.17. Create a Spark-Based ViewStack
  5. 3. Layout
    1. 3.1. Position Children Linearly
    2. 3.2. Switch Layout Management at Runtime
    3. 3.3. Align and Size Children Within a Layout
    4. 3.4. Lay Out Children Using Rows and Columns
    5. 3.5. Size Children Uniformly
    6. 3.6. Lazily Create and Recycle Children
    7. 3.7. Create a Custom Layout
    8. 3.8. Measure and Alter the Container Size
    9. 3.9. Dynamically Change the Child Depth in the Layout
    10. 3.10. Use Matrix3D to Apply Transformations Within a Layout
    11. 3.11. Use TransformOffsets to Apply Transformations Within a Layout
    12. 3.12. Create a Custom 3D Layout
    13. 3.13. Programmatically Scroll Within a Layout
    14. 3.14. Determine the Visibility of Elements in a Sequence-Based Layout
  6. 4. Graphics
    1. 4.1. Size and Position a Graphic Element
    2. 4.2. Use Path to Draw a Shape with Stroke and Fill
    3. 4.3. Display Text in a Graphic Element
    4. 4.4. Display Bitmap Data in a Graphic Element
    5. 4.5. Display Gradient Text
    6. 4.6. Apply Bitmap Data to a Graphic Element as a Mask
    7. 4.7. Create a Custom Shape Element
    8. 4.8. Create a Custom Standalone Graphic Component
    9. 4.9. Define and Reuse Graphic Symbols
  7. 5. Components
    1. 5.1. Handle a Button’s Click Event
    2. 5.2. Create a Button Bar
    3. 5.3. Load a External SWF
    4. 5.4. Use a Calendar Date Input
    5. 5.5. Create Event Handlers for Menu-Based Controls
    6. 5.6. Display an Alert in an Application
    7. 5.7. Display a Custom Pop Up in a Custom Component
    8. 5.8. Detect a Mouse Click Outside a Pop Up to Close It
    9. 5.9. Using s:Scroller to Create a Scrollable Container
    10. 5.10. Handle focusIn and focusOut Events
    11. 5.11. Open a DropDownList with a Keyboard Shortcut
    12. 5.12. Grouping Radio Buttons
    13. 5.13. Submit a Flex Form to a Server-Side Script
  8. 6. Skinning and Styles
    1. 6.1. Create a Skin for s:Button
    2. 6.2. Apply a Repeating Background Image to an Application
    3. 6.3. Create a Skin for s:ButtonBar and s:ButtonBarButton
    4. 6.4. Skin an s:DropDownList
    5. 6.5. Skin a Spark Container
    6. 6.6. Change the Appearance of Components Using Styles
    7. 6.7. Apply Skins and Properties to Spark and MX Components with CSS
    8. 6.8. Create a Button Component with an Icon
    9. 6.9. Add Custom Style Properties
    10. 6.10. Partially Embed Fonts with CSS
  9. 7. Text and TextFlows
    1. 7.1. Create a TextFlow Object
    2. 7.2. Generate a TextFlow Object from Another Source
    3. 7.3. Create Links in a TextFlow
    4. 7.4. Add Graphic Elements to a TextFlow
    5. 7.5. Bind a Value to a s:TextInput Control
    6. 7.6. Create a Custom Selection Style
    7. 7.7. Style Links Within a TextFlow
    8. 7.8. Locate Elements Within a TextFlow
    9. 7.9. Determine All Fonts Installed on a User’s Computer
    10. 7.10. Display Vertical Text in a TextArea
    11. 7.11. Set the Selection in a TextArea
    12. 7.12. Control the Appearance of the Selected Text
    13. 7.13. Copy a Character as a Bitmap
    14. 7.14. Create Linked Containers in a TextFlow
    15. 7.15. Use a Custom Format Resolver
    16. 7.16. Skin the TextArea Control
    17. 7.17. Create Multiple Text Columns
    18. 7.18. Highlight the Last Character in a TextFlow
  10. 8. Lists and ItemRenderers
    1. 8.1. Create an Item Renderer for a Spark List
    2. 8.2. Create an Editable List
    3. 8.3. Scroll to an Item in a Spark List
    4. 8.4. Change the Layout of a Spark List
    5. 8.5. Create a Nested List
    6. 8.6. Set XML Data for a Spark List
    7. 8.7. Allow Only Certain Items in a Spark List to Be Selectable
    8. 8.8. Format and Validate Data Added in a Spark List Item Editor
    9. 8.9. Create a Right-Click Menu for a Spark List
    10. 8.10. Enable Dragging in a Spark List
    11. 8.11. Customize the Drop Indicator of a Spark List
    12. 8.12. Display Asynchronously Loaded Data in a Spark List
  11. 9. DataGrid
    1. 9.1. Create Custom Columns for a DataGrid
    2. 9.2. Specify Sort Functions for DataGrid Columns
    3. 9.3. Filter Items in a DataGrid
    4. 9.4. Create Custom Headers for a DataGrid
    5. 9.5. Handle Events from a DataGrid
    6. 9.6. Enable Drag and Drop in a DataGrid
    7. 9.7. Edit Items in a DataGrid
    8. 9.8. Search Within a DataGrid and Autoscroll to the Match
    9. 9.9. Generate a Summary for Flat Data by Using a Grouping Collection
    10. 9.10. Create an Async Refresh for a Grouping Collection
  12. 10. Video
    1. 10.1. Create a Basic Video Player
    2. 10.2. Display Video Playback Progress
    3. 10.3. Create a Skinned Video Player
    4. 10.4. Display Streaming Video
    5. 10.5. Display the Bytes Loaded of a Video
    6. 10.6. Create a Basic Video Player Using the Open Source Media Framework
    7. 10.7. Access and Display Cue Points Embedded in a Video File
    8. 10.8. Create a Wrapper for the Open Source Media Framework
    9. 10.9. Display Captions with the Open Source Media Framework
  13. 11. Animations and Effects
    1. 11.1. Dynamically Set a Filter for a Component
    2. 11.2. Call an Animation in MXML and in ActionScript
    3. 11.3. Create Show and Hide Effects for a Component
    4. 11.4. Define Keyframes for an Animation
    5. 11.5. Create Parallel Series or Sequences of Effects
    6. 11.6. Pause, Reverse, and Restart an Effect
    7. 11.7. Set Effects for Adding a Component to or Removing One from a Parent Component
    8. 11.8. Create Custom Animation Effects
    9. 11.9. Use the DisplacementMapFilter Filter in a Flex Effect
    10. 11.10. Use the Convolution Filter to Create an Animation
    11. 11.11. Use Pixel Bender to Create a Transition
  14. 12. Collections
    1. 12.1. Add, Remove, or Retrieve Data from an ArrayList
    2. 12.2. Retrieve and Sort Data from an ArrayCollection
    3. 12.3. Filter an ArrayCollection
    4. 12.4. Determine When an Item Within an ArrayCollection Is Modified
    5. 12.5. Create a GroupingCollection
    6. 12.6. Create a Hierarchical Data Provider for a Control
    7. 12.7. Navigate a Collection Object and Save Your Position
    8. 12.8. Create a HierarchicalViewCollection Object
    9. 12.9. Filter and Sort an XMLListCollection
    10. 12.10. Sort on Multiple Fields in a Collection
    11. 12.11. Sort on Dates in a Collection
    12. 12.12. Create a Deep Copy of an ArrayCollection
    13. 12.13. Use Data Objects with Unique IDs
  15. 13. Data Binding
    1. 13.1. Bind to a Property
    2. 13.2. Bind to a Function
    3. 13.3. Create a Bidirectional Binding
    4. 13.4. Bind to Properties by Using ActionScript
    5. 13.5. Use Bindable Property Chains
    6. 13.6. Bind to Properties on a XML Source by Using E4X
    7. 13.7. Create Customized Bindable Properties
    8. 13.8. Bind to a Generic Object
    9. 13.9. Bind to Properties on a Dynamic Class
  16. 14. Validation, Formatting, and Regular Expressions
    1. 14.1. Use Validators and Formatters with TextInput Controls
    2. 14.2. Create a Custom Formatter
    3. 14.3. Use Regular Expressions to Create an International Zip Code Validator
    4. 14.4. Validate Combo Boxes and Groups of Radio Buttons
    5. 14.5. Show Validation Errors by Using ToolTips in a Form
    6. 14.6. Use Regular Expressions for Locating Email Addresses
    7. 14.7. Use Regular Expressions for Matching Credit Card Numbers
    8. 14.8. Use Regular Expressions for Validating ISBNs
    9. 14.9. Create Regular Expressions by Using Explicit Character Classes
    10. 14.10. Use Character Types in Regular Expressions
    11. 14.11. Match Valid IP Addresses by Using Subexpressions
    12. 14.12. Use Regular Expressions for Different Types of Matches
    13. 14.13. Match Ends or Beginnings of Lines with Regular Expressions
    14. 14.14. Use Back-References
    15. 14.15. Use a Look-Ahead or Look-Behind
  17. 15. Working with Services and Server-Side Communication
    1. 15.1. Configure a HTTPService
    2. 15.2. Use RESTful Communication Between Flex Applications
    3. 15.3. Communicate with a Service That Returns JSON-Formatted Data
    4. 15.4. Configure Services for an Application Using BlazeDS
    5. 15.5. Configure and Connect to a RemoteObject
    6. 15.6. Use Publish/Subscribe Messaging for Chat Applications
    7. 15.7. Use the IExternalizable Interface for Custom Serialization
    8. 15.8. Track Results from Multiple Simultaneous Service Calls
    9. 15.9. Register a Server-Side Data Type Within a Flex Application
    10. 15.10. Communicate with a WebService
    11. 15.11. Add a SOAP Header to a Request to a WebService
    12. 15.12. Parse a SOAP Response from a WebService
    13. 15.13. Communicate Securely with AMF by Using SecureAMFChannel
    14. 15.14. Send and Receive Binary Data via a Binary Socket
    15. 15.15. Communicate Using a XMLSocket
    16. 15.16. Navigate a XML Document in E4X
    17. 15.17. Use Regular Expressions in E4X Queries
    18. 15.18. Add a XMLList to a XML Object
    19. 15.19. Handle Namespaces in XML Returned by a Service
    20. 15.20. Encode an ActionScript Data Object as XML
    21. 15.21. Decode XML from a Web Service into Strongly Typed Objects
  18. 16. Browser Communication
    1. 16.1. Link to an External URL
    2. 16.2. Work with FlashVars
    3. 16.3. Invoke JavaScript Functions from Flex
    4. 16.4. Invoke ActionScript Functions from JavaScript
    5. 16.5. Change the HTML Page Title via BrowserManager
    6. 16.6. Parse the URL via BrowserManager
    7. 16.7. Deep-Link to Data via BrowserManager
    8. 16.8. Deep-Link Containers via BrowserManager
  19. 17. Modules and Runtime Shared Libraries
    1. 17.1. Create a Runtime Shared Library
    2. 17.2. Use Cross-Domain Runtime Shared Libraries
    3. 17.3. Optimize a Runtime Shared Library
    4. 17.4. Create a MXML-Based Module
    5. 17.5. Create an ActionScript-Based Module
    6. 17.6. Use ModuleLoader to Load Modules
    7. 17.7. Use ModuleManager to Load Modules
    8. 17.8. Load Modules from Different Servers
    9. 17.9. Communicate with a Module
    10. 17.10. Use Query Strings to Pass Data to Modules
    11. 17.11. Use Linker Reports to Optimize Modules
  20. 18. AIR Basics
    1. 18.1. Create and Run an AIR Application with Flash Builder 4
    2. 18.2. Sign and Export an AIR Application
    3. 18.3. Sign an AIR File with a Trusted Certificate
    4. 18.4. Targeting a Specific Version of AIR
    5. 18.5. Set the Application ID
    6. 18.6. Set the Application Name and Filename
    7. 18.7. Set the Application Version
    8. 18.8. Edit the Application Description and Copyright Information
    9. 18.9. Edit the Initial Window Settings
    10. 18.10. Set the Installation Folder for an Application
    11. 18.11. Set the Default Programs Menu Folder
    12. 18.12. Set a Custom Application Icon
    13. 18.13. Allow an AIR Application to Interact with the Browser
    14. 18.14. Set the Application to Handle All Updates
    15. 18.15. Determine the Application Version at Runtime
    16. 18.16. Create Multilingual AIR Installations
    17. 18.17. Create Applications with Update Capabilities
    18. 18.18. Create Applications with Update Capabilities with a Custom Interface
    19. 18.19. Package an Application in a Native Installer (.exe, .dmg, .rpm)
    20. 18.20. Include Native Code Within Your AIR Application
  21. 19. Working with Data in AIR
    1. 19.1. Safeguard Files with the Encrypted Local Store
    2. 19.2. Migrate Serialization Changes
    3. 19.3. Create an In-Memory Database
    4. 19.4. Encrypt a Database with a Password
    5. 19.5. Use Parameters in Queries
    6. 19.6. Include a Database in an Application
    7. 19.7. Store Simple Relationships with an Object Relational Mapping
  22. 20. Operating System Integration with AIR
    1. 20.1. Close All Open Windows at Once
    2. 20.2. Add a Drop Shadow for a Custom Chrome Window
    3. 20.3. Use Deferred Rendering with Clipboard Data
    4. 20.4. Create Custom Clipboard Data Formats
    5. 20.5. Assign Keyboard Shortcuts to Menu Items
    6. 20.6. Notify the User Through the Dock (Mac) and the Taskbar (Windows)
    7. 20.7. Register Custom File Types
    8. 20.8. Open a File with Its Default Application
    9. 20.9. Check for Mounted and Unmounted Drives
    10. 20.10. Obtain a List of Available External Drives
    11. 20.11. Tell the Operating System That a File Has Been Downloaded from the Web
    12. 20.12. Deploy an AIR Application as a Native Installer
    13. 20.13. Create a HTTP Proxy Using the ServerSocket Class
  23. 21. Charting
    1. 21.1. Create a Chart
    2. 21.2. Add Effects to Charts
    3. 21.3. Select Regions of a Chart
    4. 21.4. Format Tick Marks for a Chart
    5. 21.5. Create a Custom Label for a Chart
    6. 21.6. Create a Drill-Down Effect for a Column Chart
    7. 21.7. Skin Chart Items
    8. 21.8. Use ActionScript to Dynamically Add Columns to and Remove Columns from a Chart
    9. 21.9. Overlap Multiple ChartSeries
    10. 21.10. Drag and Drop Items in a Chart
    11. 21.11. Create an Editable Line Chart
  24. 22. Unit Testing with FlexUnit
    1. 22.1. Create an Application That Uses the FlexUnit Framework
    2. 22.2. Create an Application to Run FlexUnit Tests
    3. 22.3. Create a FlexUnit Test Case
    4. 22.4. Run Code Before and After Every Test
    5. 22.5. Share Test Data Between Test Cases
    6. 22.6. Handle Events in a Test Case
    7. 22.7. Test Visual Components with FlexUnit
    8. 22.8. Create Mock Objects for Testing
    9. 22.9. Use Complex Assertions in a Test Case
  25. 23. Compiling, Debugging, and Deploying
    1. 23.1. Use trace Statements Without Flash Builder
    2. 23.2. Use the Component Compiler
    3. 23.3. Install the Flex Ant Tasks
    4. 23.4. Use mxmlc and Ant to Compile Flex Applications
    5. 23.5. Use Ant to Compile and Deploy Flex Applications That Use RSLs
    6. 23.6. Use Rake to Compile Flex Applications
    7. 23.7. Create and Monitor Expressions in the Flash Builder Debugger
    8. 23.8. Install the Ant View in the Standalone Version of Flash Builder
    9. 23.9. Use ASDoc and Ant to Generate Documentation
    10. 23.10. Use Express Install for Your Application
    11. 23.11. Use Memory Profiling with Flash Builder to View Memory Snapshots
    12. 23.12. Check the Performance of Specific Methods
  26. 24. Internationalization, Accessibility, and Printing
    1. 24.1. Add an International Character Set to an Application
    2. 24.2. Use a Resource Bundle to Localize an Application
    3. 24.3. Use the ResourceManager for Localization
    4. 24.4. Use Resource Modules for Localization
    5. 24.5. Support Input Method Editor (IME) Devices
    6. 24.6. Detect a Screen Reader
    7. 24.7. Create a Tabbing Reading Order for Accessibility
    8. 24.8. Print Selected Items in an Application
    9. 24.9. Format Application Content for Printing
    10. 24.10. Control Printing of Unknown-Length Content over Multiple Pages
    11. 24.11. Add a Header and a Footer When Printing
  27. Index
  28. About the Authors
  29. Colophon
  30. Copyright

Product information

  • Title: Flex 4 Cookbook
  • Author(s): Rich Tretola, Marco Casario, Todd Anderson, Joshua Noble, Garth Braithwaite
  • Release date: May 2010
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596805616