Tải bản đầy đủ (.pdf) (354 trang)

Wrox beginning adobe AIR building applications for the adobe integrated runtime apr 2008 ISBN 0470229047 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (12.69 MB, 354 trang )


29040ffirs.qxd:WroxPro

2/26/08

9:34 AM

Page v

Beginning

Adobe® AIR™
Building Applications for the Adobe Integrated Runtime

Rich Tretola


29040ffirs.qxd:WroxPro

2/26/08

9:34 AM

Page ii


29040ffirs.qxd:WroxPro

2/26/08

9:34 AM



Page i

Beginning Adobe AIR
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

Part I: Getting Started

1

Chapter 1: Introducing AIR (Adobe Integrated Runtime) . . . . . . . . . . . . . . . . . 3
Chapter 2: Rich Internet/Desktop Applications . . . . . . . . . . . . . . . . . . . . . . 15
Chapter 3: The Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Chapter 4: The AIR Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Chapter 5: Building an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Chapter 6: Packaging the AIR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

Part II: Adding Data

87

Chapter 7: Working with Remote Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

Part III: AIR APIs
Chapter 8: Accessing the File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Chapter 9: The Windowing API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Chapter 10: Interacting with the O.S. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Chapter 11: The SQLite Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

Chapter 12: Communication between AIR and the Operating System. . . . . . 197
Chapter 13: Application Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217

Part IV: The AIR Components

233

Chapter 14: The File System Components . . . . . . . . . . . . . . . . . . . . . . . . . 235
Chapter 15: The HTML Component. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Appendix A: Taking a Flex App to the Desktop . . . . . . . . . . . . . . . . . . . . . . 259
Appendix B: Solutions for Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305


29040ffirs.qxd:WroxPro

2/26/08

9:34 AM

Page ii


29040ffirs.qxd:WroxPro

2/26/08

9:34 AM

Page iii


Beginning

Adobe® AIR™


29040ffirs.qxd:WroxPro

2/26/08

9:34 AM

Page iv


29040ffirs.qxd:WroxPro

2/26/08

9:34 AM

Page v

Beginning

Adobe® AIR™
Building Applications for the Adobe Integrated Runtime

Rich Tretola



29040ffirs.qxd:WroxPro

2/26/08

9:34 AM

Page vi

Beginning Adobe® Air™
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com

Copyright © 2008 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-22904-0
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
Library of Congress Cataloging-in-Publication Data is available from the publisher.
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 Legal Department, Wiley Publishing,
Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at
/>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 Website 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 Website may provide or recommendations it may make. Further, readers
should be aware that Internet Websites 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 (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
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. Adobe and AIR are
trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other
countries. 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.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not
be available in electronic books.


29040ffirs.qxd:WroxPro

2/26/08

9:34 AM

Page vii


To the love of my life, Kim, and my beautiful daughters, Skye and Coral.


29040ffirs.qxd:WroxPro

2/26/08

9:34 AM

Page viii


29040ffirs.qxd:WroxPro

2/26/08

9:34 AM

Page ix

About the Author
Rich Tretola is the Rich Applications Technical Lead at Herff Jones, Inc. He is an award-winning Flex
developer and was the lead author of Professional Flex 2 (ISBN 978-0-470-10267-1) from Wrox. He entered
the field of Web development in the late 1990s and has been building applications ever since.
Other than Flex, he builds applications using ColdFusion, Flash, and Java (where he is a Sun-certified
programmer). Rich is highly regarded within the Flex community as an expert in RIA and is also an
Adobe Community Expert. He runs a popular Flex and AIR blog at ,
and is the community manager for .
Rich was also selected to be a speaker at the Adobe MAX 2007 conference in Chicago.


About the Technical Editor
Joe Johnston is an Experience Designer at Knoware, an Interactive Design Studio building applications
from RIAs to embedded devices. Joe has been using AIR/Flex when it was in the beta stages and uses it
daily on all of his projects. He also finds it exciting to build applications to create unique user interaction.
You can see more and contact him at .


29040ffirs.qxd:WroxPro

2/26/08

9:34 AM

Page x


29040ffirs.qxd:WroxPro

2/26/08

9:35 AM

Page xi

Credits
Executive Editor

Vice President and Executive Group Publisher

Chris Webb


Richard Swadley

Development Editor

Vice President and Executive Publisher

William Bridges

Joseph B. Wikert

Technical Editor

Project Coordinator, Cover

Joe Johnston

Lynsey Stanford

Production Editor

Compositor

Daniel Scribner

Craig Johnson, Happenstance Type-O-Rama

Copy Editor

Proofreader


Cate Caffrey

Sossity Smith

Editorial Manager

Indexer
Melanie Belkin

Mary Beth Wakefield

Production Manager
Tim Tate


29040ffirs.qxd:WroxPro

2/26/08

9:35 AM

Page xii


29040ffirs.qxd:WroxPro

2/26/08

9:35 AM


Page xiii

Acknowledgments
I thank the acquisitions editor, Chris Webb, and the development editor, William Bridges, for their work
helping to get the book completed through a constantly changing timeline.
I give special thanks to Danny Joscher, Frank Ferrara, Joey “the Schnoz” Fiorello, and “the Boss,”
Mr. Fiorello from J&R Discount on Long Island, NY. I spent a great deal of time with these people over
many years and learned a lot from each of them. Mr. Fiorello is actually responsible for getting me
started in programming by splitting the cost of my first laptop with me. One more thing that needs to be
said, especially to Frankie: Let’s Go, Islanders!!
Most importantly, I thank my wife and best friend, Kim, and my daughters, Skye and Coral. Anyone
who has been through the experience of writing a technical book knows how much time and effort are
involved and how important the support of the people closest to you is. I love you all!


29040ffirs.qxd:WroxPro

2/26/08

9:35 AM

Page xiv


29040ftoc.qxd:WroxPro

2/26/08

9:36 AM


Page xv

Contents
Acknowledgments
Foreword
Introduction

xiii
xxi
xxv

Part I: Getting Started

1

Chapter 1: Introducing AIR (Adobe Integrated Runtime)

3

Online versus Desktop Applications
The Runtime Environment
The AIR File

The Tools
The AIR SDK
Flex Builder 3
Dreamweaver CS3
Flash CS3


Installing the AIR Runtime
Set up the SDK on Mac
Set up the SDK on Windows
Create Your First AIR Application
Summary
Exercise

Chapter 2: Rich Internet/Desktop Applications
What Is RIA?
Adobe Flash
Adobe Flex
XUL
OpenLaszlo
HTML/AJAX
Windows Presentation Foundation/Silverlight
JavaFX

History of RIA
Benefits of RIA

4
4
4

4
5
5
6
6


6
6
8
10
14
14

15
15
15
16
16
16
17
17
17

17
18


29040ftoc.qxd:WroxPro

2/26/08

9:36 AM

Page xvi

Contents

Moving to the Desktop
Traditional Desktop Applications

18
18

C
C++
Java

19
19
19

Summary

19

Chapter 3: The Programming Languages

21

AIR Flex/Flash Applications

21

MXML
ActionScript 3
Using the Documentation


21
22
23

AIR HTML Applications
HTML (Hypertext Markup Language)
JavaScript
CSS (Cascading Style Sheets)
XHTML (Extensible Hypertext Markup Language)
DOM (Document Object Model)
AJAX (Asynchronous JavaScript and XML)

Summary

Chapter 4: The AIR Development Tools
Introduction to Flex Builder 3
Flex Builder Eclipse Stand-Alone and Plug-in
Flex Builder Perspectives

Introducing Flash CS3
The Stage

Introducing Dreamweaver CS3
Code/Design View
The AIR/Dreamweaver CS3 Extension

Summary

Chapter 5: Building an Application
Create Your First AIR Flex Application

Testing with Flex Builder
Debugging with Flex Builder
Testing with SDK
Debugging with the SDK

Create Your First AIR HTML Application
Testing with Dreamweaver CS3

xvi

26
26
27
28
30
31
34

34

35
35
36
36

45
45

49
49

51

51

53
53
53
55
55
56

56
57


29040ftoc.qxd:WroxPro

2/26/08

9:36 AM

Page xvii

Contents
Testing Your Applications with the SDK
Debugging with the SDK
Create Your First AIR Flash Application
Summary

Chapter 6: Packaging the AIR

The AIR Configuration File
<application/>
<id/>
<version/>
<name/>
<filename/>
<description/>
<copyright/>
<icon/>
<initialWindow/>
<visible/>
<installFolder/>
<customUpdateUI/>
<allowBrowserInvocation>

<fileTypes/>

Packaging to AIR
Packaging
Packaging
Packaging
Packaging

with
with
with
with

Flex Builder 3
Dreamweaver CS3

Flash CS3
the SDK

Summary

59
59
61
63

65
65
65
65
66
66
67
67
68
68
71
72
73
74
74
74
74

76
76

79
82
85

86

Part II: Adding Data

87

Chapter 7: Working with Remote Data

89

AMF/Remoting
ColdFusion

REST
JSON
XML

Web Services
SOAP
WSDL

89
89

96
96

98

99
100
100

xvii


29040ftoc.qxd:WroxPro

2/26/08

9:36 AM

Page xviii

Contents
MashUps
Import Web Service (WSDL)
Summary
Exercise

Part III: AIR APIs
Chapter 8: Accessing the File System
File System Security
Working with the File System
Directories
Files
Asynchronous versus Synchronous

Creating Temporary Files and Directories

Browse for Files
Summary
Exercise

Chapter 9: The Windowing API
Creating Windows
NativeWindow
NativeWindowInitOptions
Window (mx:Window)

Summary
Exercise

Chapter 10: Interacting with the O.S.
Dock Icons
Menu
Bounce
System Tray Icons
Menu

Context Menus
Summary
Exercise

xviii

104
107

110
110

111
113
113
113
113
120
127
130

132
134
134

135
135
135
138
143

152
152

153
153
155
156
159

161

163
167
167


29040ftoc.qxd:WroxPro

2/26/08

9:36 AM

Page xix

Contents
Chapter 11: The SQLite Database
About SQLite
Getting Started
Creating a SQLite Database
Creating a Table

169
169
169
170
172

Working with Data


175

Saving Data
Retrieving Data
Updating Data
Deleting Data

175
179
185
190

Summary
Exercise

Chapter 12: Communication between AIR and the Operating System
Drag-and-Drop Support
DragIn
DragOut

Copy and Paste
Paste
Copy

Native Menus
Basic NativeMenu
Native Menu with Submenus
Native Menu with Dividers
Menu Select Handler
Keyboard Activated Menu


Summary
Exercise

Chapter 13: Application Status

195
195

197
197
197
202

204
204
207

210
210
211
212
212
214

215
216

217


Checking for an Internet Connection
About the <Updater> Class

217
220

Methodology for Application Updates
Updating the Application
Putting It All Together

221
223
228

Summary
Exercises

231
231

xix


29040ftoc.qxd:WroxPro

2/26/08

9:36 AM

Page xx


Contents
Part IV: The AIR Components
Chapter 14: The File System Components
FileSystemDataGrid
FileSystemHistoryButton
FileSystemList
FileSystemTree
FileSystemComboBox
Summary
Exercise

Chapter 15: The HTML Component
About the Safari WebKit
Using the HTML Component
Basic HTML
Using the Location Property

Navigating History
historyBack()
historyForward()
historyGo()
HTMLHistoryItem

HTML Document Object Model
innerHTML
getElementsByTagName()
getElementById()

PDF Support

Summary
Exercise

233
235
235
237
238
240
242
243
243

245
245
245
245
246

247
249
249
249
249

251
252
253
255


255
257
257

Appendix A: Taking a Flex App to the Desktop

259

Appendix B: Solutions for Exercises

281

Index

xx

305


29040flast.qxd:WroxPro

2/26/08

9:40 AM

Page xxi

Foreword
By Edward Mansouri


The life cycle of Internet-based applications is taking an exciting turn toward the desktop.
The earliest Internet-based applications used on a large scale by the general public were the ASCII textbased e-mail and web clients of the 1980s, such as the University of Washington’s Pine and the University
of Minnesota’s Gopher.
With the introduction of the first graphical browser, NCSA Mosaic in September 1993, an era of browserbased, graphics-intensive Internet content and applications began and would last more than 13 years.
Over time, remarkable advancements have been made in the browser itself to serve increasingly rich
content including audio and video as well as the ability to provide Web developers with client-side runtimes with languages such as JavaScript and Microsoft’s Active X to further customize the way users
interact with Web content. In addition, the presentation capabilities made available to designers continue
to accelerate with languages such as Cascading Style Sheets (CSS).
Also, another important technology that has evolved as consistently and reliably as the browser itself is
the Shockwave Flash (SWF) file format from Macromedia (acquired by Adobe in 2005).
Flash first appeared on the scene in 1996, largely as a tool in the graphic designer’s repertoire for embedding animated and engaging content into a web site.
A few years later, Flash would begin its turn toward appealing not just to designers but also to developers
with the advent of ActionScript, a simple yet robust language for scripting the behavior of Flash content.
The ability to ingest data into a Flash “movie” and submit it to remote network locations quickly
emerged, as well as the ability to create rich user interfaces derived from remotely assembled data and
content.
The Rich Internet Application (RIA) era had arrived.
The promise of a browser-based application that offered some of the same user experiences of the familiar desktop-based application emerged, notably drag-and-drop functionality.
Also, no longer did a web site or web application need to rely on the conventional “stateless” model of
the Web in which each page of a web site needed to be unloaded and fully replaced by the next with
each click of a hyperlink.
The discovery by JavaScript programmers of the browser’s XMLHttpRequest object (and Microsoft’s
MSMXL object in Internet Explorer) was equally significant and introduced the world to yet another paradigm for Rich Internet Applications with AJAX (Asynchronous JavaScript and XML) — Google Maps,
perhaps, is the most famous.


29040flast.qxd:WroxPro

2/26/08


9:40 AM

Page xxii

Foreword
Despite these remarkable advancements to the landscape for Internet-based content and applications,
the fact remained that all functionality was limited to the restrictive sandbox environment of the web
browser.
While AJAX has offered many developers an elegant platform upon which to build Rich Internet
Applications, developing an AJAX-based application that will execute with equal reliability across all the
major web browsers is a monumental task that requires substantial extra knowledge and coding time.
In 2006, Adobe announced that it was working on a groundbreaking new platform called Apollo that
would open up incredible capabilities for the Rich Internet Application developer: a cross-operating system
runtime that would enable HTML/AJAX-centric developers as well as Flash/Flex-centric developers to
build applications that could bring together the best characteristics of a web application with the capabilities of a desktop application.
In March of 2007, Apollo launched as a public alpha and moved quickly to a public beta three months
later in June along with a rename of the product to AIR, the Adobe Integrated Runtime.
The ramifications of AIR were immediately recognized as developers blogged and boasted about taking
existing HTML/AJAX, Flash, or Flex-based applications and adding desktop components to them as
they repackaged them as AIR applications in a matter of hours.
During the remainder of 2007, several special interest communities emerged to evangelize AIR, including
Mr. Tretola’s own EverythingFlex.com site and my O2Apps.com site, both of which provide a comprehensive array of sample applications, code, and tutorials for AIR developers of all skill levels.
My programming background is in the development of E-Learning software, and my interest in AIR
came about from my long-standing goal to bring desktop functionality to the landscape for online students and teachers.
Dating back to 2000, I have attempted to do this with technologies such as WebDAV, Java, and C++. With
each attempt, I was unsatisfied with the lack of stability I was able to achieve as well as the inherent
complexities involved with the technologies, not to mention the need to tackle much more difficult programming languages.
AIR has provided a way for me to leverage the existing Web development skills I’ve been building since
1996 (namely, HTML, JavaScript, CSS, ActionScript, Flash, and Flex) to build useful desktop-based functionality that allows my users to do things such as dragging and dropping files from the desktop to an
Internet-based online classroom — something virtually impossible in a strictly browser-based application.

It became apparent that it was not only what you could do with AIR that appealed, but the ease with
which you could do it.
For the creative designer, you will love how AIR lets you mix HTML and Flash content together. Using
techniques such as the Flex Builder HTML Component, you can create custom-shaped HTML interfaces
that can offer your clients and users a great first impression and experience.
Furthermore, the reach you’ll attain with your AIR applications will likely be far beyond any other
alternative.

xxii


×