Flash Builder 4 and Flex 4 Essential Training

Flash Builder 4 and Flex 4 Essential Training

with David Gassner

 


Adobe's open source programming framework, Flex SDK, combined with Flash Builder (formerly named Flex Builder) simplifies and streamlines the process for building rich internet applications, or RIAs. Author David Gassner explores the newest release of this powerful toolset in Flash Builder 4 and Flex 4 Essential Training, explaining critical information for developers who are upgrading from Flex 3, but also for those who are new to RIA development. The course culminates in the development of a complete application, integrated with a PHP application server and a MySQL database. Exercise files accompany the course.
Topics include:
  • Understanding the history of Adobe Flex
  • Installing the Flash Builder components
  • Using Eclipse views and editors
  • Programming with MXML and integrating MXML and ActionScript
  • Debugging Flex applications
  • Adding visual controls to a Flex application interface
  • Displaying images and dynamic data
  • Formatting with CSS
  • Skinning Spark components with MXML graphics
  • Creating data entry form components
  • Integrating with Apache, PHP and MySQL

show more

author
David Gassner
subject
Developer, Web
software
Flash Builder 4, 4.5, Flex 4, 4.5
level
Beginner
duration
10h 28m
released
Mar 22, 2010

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:04Hi! My name is David Gassner.
00:07I'd like to welcome you to Flash Builder 4 and Flex 4 Essential Training.
00:11Adobe Systems has been working on this critical upgrade to the Flex development
00:15platform for about two years.
00:17There are plenty of new features in both the Software Developer's Kit and the
00:21Integrated Development Environment, Flash Builder 4.
00:24In this series, I'll show you how to build and deploy Rich Internet
00:27Applications, including how to use the MXML and ActionScript 3 programming
00:32languages, and how to use the Flex implementation of Cascading Style Sheets, and
00:38create an MXML-based component skins with the new Spark component architecture.
00:43I'll give you a tour of the newly renamed Flash Builder 4, formerly known as Flex Builder.
00:49You'll see examples of the product's new code generation and debugging features
00:53and how to integrate Flex applications with application servers, using Flash
00:57Builder's new data-centric development tools.
01:00This course is designed for anyone who wants to learn how to develop Rich
01:04Internet Applications for Flash Player 10, but it also contains great
01:08information for developers who have some experience with earlier versions of Flex.
01:12I hope you find that the Flex 4 SDK and Flash Builder 4 give you all the tools
01:17you need for building dynamic applications for the web and the desktop.
Collapse this transcript
Using the exercise files
00:00Just a quick word about the Exercise Files I'll be using.
00:04If you're a Premium Member of the lynda.com Online Training Library, or if you're
00:08watching this tutorial on a disc, you have access to the Exercise Files used
00:12throughout the title.
00:13In the Exercise Files folder, I've collected some assets for you to use
00:17during our tour of building Rich Internet Applications with Flash Builder 4
00:21and the Flex 4 SDK.
00:23They include starting and solution projects in applications, graphic files, and
00:28an SQL script to create a sample MySQL database, and other useful assets.
00:35Most of the projects are in the form of FXP files, a new zip-based format that's
00:41supported by Flash Builder 4 that can be imported into Flash Builder.
00:45I've copied the Exercise Files folder to my Desktop, but you can put it wherever you want.
00:50To get started with the video series, you'll need to follow the instructions in
00:53Chapter 1 where I describe how to install Flash Builder.
00:57You can choose to work through the entire video series from start to finish, or
01:01if you'd like to jump in a particular chapter, you can use the starter files for that chapter.
01:06For each chapter, make sure you follow the instructions for importing the
01:09starting project from the Exercise Files folder.
01:12The last chapter in this video series describes how to integrate Flex
01:15applications with PHP, one of the most popular available application servers.
01:20That chapter includes instructions for downloading and installing the required
01:23server software for Windows and Mac OS X.
01:27This is also where you'll find the SQL file that you can import into MySQL to
01:31create the sample database.
01:34In this chapter, you'll need to follow the steps sequentially.
01:37You'll learn how to install PHP, Apache, and MySQL, configure a Flex project for
01:42use with PHP and create a PHP data connection in Flash Builder.
01:47If you have a lynda.com subscription that doesn't give you access to the
01:50Exercise Files, you can follow along the instructions and demonstrations
01:54on screen and use any files you want as you create your own Rich Internet Applications.
Collapse this transcript
Understanding the prerequisites
00:00The Flex Software Developers Kit and Flash Builder 4 are designed for use by
00:04application developers, specifically, developers who work with object-oriented
00:09languages, building either small- scale or enterprise applications.
00:13You will be doing some programming in this course and some knowledge of the core
00:17programming language, ActionScript 3, is recommended.
00:20If you haven't worked with ActionScript 3 before, as long as you worked with
00:24some programming language that uses object-oriented programming principles,
00:28you'll feel pretty comfortable very quickly.
00:31Examples of languages that are very similar to ActionScript 3 include Java, C#,
00:35C++, and JavaScript.
00:39If you'd like to learn a little bit more about ActionScript 3, take a look at
00:42lynda.com video series ActionScript 3.0 in Flex Builder Essential Training.
00:48This course is a couple of years old, but the language hasn't changed very much since then.
00:53ActionScript 3 is still the same language that we used in Flex 2, Flex 3, and
00:57now in Flex 4, and is also used in Flash Professional.
01:01Once you've gone through the series, you might want to learn about how to build
01:04desktop applications using Flex and Flash Builder.
01:08If you'd like to learn these skills, take a look at the video series AIR
01:11Essential Training and AIR for Flex Developers, also available in the lynda.com
01:16Online Training Library.
Collapse this transcript
1. Getting Started
Introducing the history of Adobe Flex
00:00Before we get started with Flex 4, I would like to tell you a little bit about
00:03the product's history.
00:05When Flash MX was released in 2002, it was branded as the new way to build Rich
00:10Internet Applications, known by the acronym RIA.
00:14The term was invented at Macromedia to describe a new class of applications
00:18that would offer the benefits of being connected to the Internet, but would
00:21solve many of the nagging issues that had plagued browser-based applications
00:25since the mid 1990s.
00:27Using Flash Player to host graphically rich applications would address issues
00:31such as differences between web browsers and their implementation of Cascading
00:35Style Sheets and JavaScript, and because such applications would be able to
00:40leverage Flash Players strengths, including animation and delivery of audio and
00:45video, the applications could be both functional and visually compelling.
00:51The first portion to the new frontier of RIA is met with mixed success.
00:56Many applications built and delivered with Flash MX were very impressive,
01:00but developers were madly pulling their hair out.
01:03Flash application source files were binary, making it difficult to
01:07integrate with source control systems, and ActionScript 1 wasn't
01:11particularly object-oriented.
01:13There was no enforcement of code placement standards.
01:17ActionScript 1 and 2's loose data typing and lack of strong compile-time error
01:22checking led to phenomena such as silent failure, the moment when something
01:27that's supposed to happen and doesn't and no errors or other information are
01:31offered as to the reason.
01:32Developers who would work with Java, C- Sharp or other object-oriented languages
01:37were accustomed to using sophisticated code editors.
01:40Working in the Flash ActionScript Editor slowed their productivity.
01:44Flex 1 was released in 2004 as a server-based product.
01:49It introduced MXML and used ActionScript 2.
01:53Developers placed code on the server, and the application was compiled upon
01:57request from a browser.
01:59Flex Builder 1, built on top of the Dreamweaver code base, was a better code
02:03editor and was included for those organizations that purchased a server license.
02:08Flex 2 went even further in 2006 with the introduction of ActionScript 3, a true
02:14object-oriented language.
02:16The Flex 2 SDK was free and Flex builder 2 was the first version of the
02:21integrated development environment delivered as an eclipse plug-in.
02:26FlexBuilder was now licensed to per developer.
02:29Flex 3 came along in 2008 and offered developers the ability to deliver desktop
02:34applications based on Adobe Air, also known as the Adobe Integrated Runtime.
02:40Anything you could do in Flex 3 on the web, you could now do on the desktop.
02:45The Flex 3 SDK added new visual controls, such as the Advanced Data Grid, that
02:50were licensed as part of Flex builder 3 Professional, now known in Flash Builder
02:554 as the Premium edition.
02:57Flex Builder 3 added new debugging features, such as the Flex Profiler, that
03:02improved developer productivity.
03:04With Flex 4, Adobe introduces a new way of implementing Visual Designs.
03:09The new Spark Component Set includes a radically redesigned component skinning
03:13architecture that combines the ease of MXML-based programming with the power of
03:18Flash Player's visual rendering capabilities.
03:21The Flex 4 SDK is a transitional release.
03:24It establishes the way forward for the Flex Component Architecture, but still
03:29depends on some older components that were used in Flex 3 and haven't yet been
03:33replaced with Spark based versions.
03:36These are now known as the MX Components, and they remain vital to Flex
03:40Application Development.
03:43Flex 4 applications can take advantage of the new features of Flash Player 10,
03:48including the Flash Text Engine and Text Layout Framework, that enable complex
03:53Text Presentation and 3D animations.
03:57The software formerly known as Flex Builder, now named Flash Builder 4, is
04:02positioned as the ActionScript Editing product of choice for both Flex and Flash developers.
04:08Flash Builder 4 includes new code refactoring and formatting features and the
04:13Network Monitor debugging tool that traces network traffic when a Flex
04:17application communicates with an Application Server at run time.
04:22You can use Flash Builder to edit ActionScript code for projects built in
04:26Flash Professional CS5.
04:29Flex has changed a lot over the years, but it remains a powerful platform for
04:33creating and deploying cross-operating system applications, delivered through
04:38the web or to the desktop.
Collapse this transcript
Installing Flash Builder on Windows
00:00The process of installing Flash Builder on Windows is fairly simple.
00:04You can choose between two architectures, the standalone and the plug-in version.
00:09If you're already an Eclipse Developer and you have at least Eclipse 3.4, you
00:14can choose the plug-in version.
00:15Otherwise, you should use the standalone installer.
00:18I am going to show you to use the standalone installer here.
00:22I've already started up the installer application. The first screen is the Adobe
00:26Software License Agreement. Read through it, and then if you accept its terms,
00:31click to the Accept button.
00:32On this screen, you are asked for a serial number or to select a trial.
00:37If you don't have a serial number, choose Install this product as a trial.
00:42Then either way, with or without a serial number, you will see a dropdown list
00:46that allows to select a language that you want to install.
00:49I am going to choose English (North America) and click Next.
00:54This is the Install Options screen.
00:56The only available options are whether you want to install the Flash Player 10
01:01ActiveX control or Plug-in control.
01:04Notice that they are both about the same size, a little bit over 5 MB.
01:08I've recommend that you go ahead and install these, even if you already have
01:11Flash Player installed.
01:13That will ensure that you have the correct version of Flash Player, Flash Player
01:1710 Debug version, that you'll need for Flex Application Development.
01:22You can also modify the location of the installation, if you like.
01:26On Windows, the default installation is under C:\Program Files\Adobe, and then
01:32there will be a new folder under there named Adobe Flash Builder 4.
01:37Click the Install button, and your installation will start.
01:41Along the way, if you have any browser windows open, such as Internet Explorer,
01:46Firefox or Google Chrome, the Installer Application will ask you to close them
01:59before it continues with the process.
02:02This is important, because it allows the Installer to complete the installation
02:05of Adobe Flash Player without any file conflicts.
02:10Once the installation is complete, you'll have three options:
02:13You can click the Done button - and that will close the Installer
02:16Application completely,
02:18you can click on the View Video Tutorials link to take a look at some online
02:21documentation, or you can immediately start Adobe Flash Builder by clicking this button.
02:27I am not going to start it from here because I want to show you how to start
02:30Flash Builder from the Windows Start menu.
02:32I'll close the Installer.
02:36If you are working on Windows Vista or Windows 7, you can simply type in 'flash
02:41builder,' and the link to the application will appear.
02:45If you are working on Windows XP, go to the Start menu and go to All Programs
02:50and from there, locate Adobe Flash Builder 4 and open the application.
02:56The first time you open the application, if you didn't provide a serial number
03:00during the installation, you'll be prompted for the serial number again, or to
03:04accept using the application as a trial.
03:07You'll have 60 days to work with Flash Builder 4 from the time you first start it up.
03:13Just click the Start button, and then you'll go to this screen asking you for an Adobe ID.
03:19An Adobe ID is your e-mail address.
03:22If you don't have one yet, you can create one by clicking the Create An Adobe ID button.
03:28An Adobe ID is free.
03:30It just gives you a way for Adobe systems to keep in touch with you.
03:33If you don't want to enter your Adobe ID at this point or create one, you can
03:37click SKIP THIS STEP, and that will take you directly to Flash Builder 4.
03:44When Flash Builder opens, it shows the welcome screen in the central area, which
03:49includes links to various online documentation resources.
03:53If you've gotten this far, you've succeeded in installing Flash Builder 4
03:57on Windows.
Collapse this transcript
Installing Flash Builder on Mac OS X
00:00If you typically do your software development on Mac OS X, and if you don't
00:04already use Eclipse for java or other software development, you will want to
00:09install the standalone configuration of Flash Builder for Mac.
00:12This configuration includes the Flash Builder plug-in, a complete copy
00:16of Eclipse 3.5.1 and everything else you need to do basic Flex
00:21application development.
00:22When you download the installer from the Adobe website, it comes to you as a DMG File.
00:27When you double-click that file, it mounts as a virtual disk, and within that
00:32disk, you will find an install application.
00:35When you run the install application, it takes a few moments to extract itself
00:39and then opens up to this license agreement screen.
00:43Read through the license agreement, and then if you agree with its terms, click Accept.
00:47On this screen, you are asked for a serial number.
00:50If you haven't purchased Flash Builder yet, you can use the product in trial
00:53or evaluation mode.
00:56Click the radio button labeled Install this product as a trial.
00:59Then, whether you install a serial number or not, you will be asked for a language.
01:05You can install Flash Builder 4 using a number of different languages.
01:08I am going to choose English (North America) and then click Next. That's it!
01:15There are not many options available.
01:17When you install Flash builder 4 on Mac, it will install, by default, into the
01:22/Applications folder.
01:23Under that folder, you will find a folder called Adobe Flash Builder 4 and
01:27within that, the application.
01:29Click the Install button to complete the installation process.
01:33You might be prompted for your Administrative Password, because there are some
01:37changes that need to be made to your system to install Flash Builder 4.
01:42Enter your Password and then click OK or press Return, and the installation will continue.
01:47The amount of time it takes for your installation to complete depends on your
01:51computer's resources and speed.
01:53Once you have completed the application, you will be able to start up Flash
01:56Builder by navigating to the Mac hard disk, from there, to the Applications
02:01folder and from there, to the Adobe Flash Builder 4 folder, and then just
02:05double-click the application to start it up.
Collapse this transcript
Installing Flash Builder as an Eclipse plug-in
00:00If you already use Eclipse in your application development work, you can
00:04choose to install Flash Builder as a plug-in on top of an existing Eclipse installation.
00:10There are some restrictions you should know about.
00:12First, you must be using at least Eclipse 3.4, and if you are working on Mac
00:17OS X, you must use the older Carbon Architecture, rather than the newer Cocoa Architecture.
00:23Flash Builder 4 is not compatible with the newer version.
00:26If you want to download a recent copy of Eclipse,
00:29go to the website www.Eclipse.org/ dowloads, and on this screen, you will be able
00:35to choose from many available distributions of the Eclipse workbench.
00:40I have installed the Eclipse IDE for Java Developers on my system.
00:44I like this particular distribution because it's among the smallest of the
00:47distributions, but it includes everything I need for Flex development.
00:52If you are working with PHP, you might try this one, Eclipse for PHP Developers.
00:57And you can also go with the classic version, Eclipse Classic 3.5.1. Download the
01:04version of Eclipse that you want and install it.
01:06You typically install Eclipse just by extracting the contents of an archive file.
01:11I am working on Windows, and I have placed my copy of Eclipse in the Program
01:15Files/Eclipse folder.
01:17Here is my application, Eclipse.EXE.
01:20I am not going to start it up yet.
01:22I will wait until after I have installed Flash Builder 4.
01:27Once you know where your Eclipse installation is, start up the Flash
01:31Builder Plug-in installer.
01:33This is the version for Windows.
01:34I will click OK on the initial screen, and then I will see this text
01:40information telling you that I am about to install the primary Flash Builder 4 Plug-in files.
01:45I will click Next again.
01:47Then I read through the license agreement, and if I agree with its terms, I will
01:51click the appropriate radio button and click Next.
01:54On this screen, you are asked where you want to install the Flash Builder
01:574 executable files.
02:00This is not the location of your Eclipse installation.
02:03You will be asked for that later in the process.
02:06I recommend that you accept the default setting and click Next.
02:10Now, you are asked whether you want to include a bundled copy of Eclipse.
02:14If you already have Eclipse installed, select Plug into another copy of Eclipse,
02:19and then select your Eclipse folder.
02:21My copy is under C:\Program Files\eclipse.
02:27Click next, and then on the final screen, review the installation information
02:33and then click Install.
02:34It will take a few minutes to complete the installation.
02:39Along the way, if you have any web browser windows open, you might be
02:43prompted to close them to allow the installer to install Flash Player 10, the Debug version.
02:50Once the installation is complete, click done to close the installer and
02:55then start up Eclipse.
02:56I will go to my Program Files folder, to the Eclipse folder, and then I will
03:08double-click on eclipse.exe and start the application.
03:15If this is the first time you have started up this copy of Eclipse, you will be
03:19prompted for a workspace.
03:21I will describe workspaces in other videos of this series, but essentially, a
03:25workspace is a folder on disk that keeps track of your projects.
03:30Your projects can be in the workspace folder, but don't have to be.
03:34If you see the Workspace launcher dialog, just click OK and continue.
03:40The amount of time it takes to start up Eclipse after doing a Plug-in
03:43installation depends on your computer's resources and memory.
03:47Once Eclipse has started, go to the menu and select Window > Open Perspective
03:53and then select other.
03:56You should now see three perspectives that are associated with Flash Player.
04:00They are named Flash, Flash Debug and Flash Profile.
04:05Click on Flash and click OK, and that will switch you to the Flash Builder
04:09perspective in Eclipse.
04:12From this point forward, you should be able to work with the Flash Builder in
04:15exactly the same way as you do with the standalone version.
04:19You might find that there are some differences in Keyboard shortcuts or menu sequences,
04:24but for the most part, everything that you see in the standalone version is also
04:27available in the Plug-in version of the Flash Builder 4.
Collapse this transcript
Creating the Hello World application
00:00Whenever you learn a new programming language or development platform, it's
00:04traditional to create an Application that says 'Hello World'
00:08that outputs a simple string to the screen.
00:11I'll show you how to do this in the context of creating a brand new Flex Project
00:15and Flex Application, and along the way, I'll show you how Flash Builder 4 helps
00:19you get the job done.
00:21Open Flash Builder 4.
00:22You'll see the Start Page, shown in the central area.
00:26The Start Page includes links to various documentation resources, including
00:30videos and online documentation.
00:33Of special note is a link labeled Moving from Flex 3 to Flex 4.
00:37If you've been working with Flex 3, I encourage you to explore this area of the documentation.
00:42Every Flex Application is stored in a Flex Project in Flash Builder.
00:47So, to get started, create a brand new Flex Project. Go to the menu and select
00:52File > New > Flex Project.
00:55In the New Flex Project dialog box, type in the name of your Project.
01:00I'll name my Project HelloWorld.
01:02Your Project, by default, is placed in an area known as the workspace.
01:07A workspace is a folder on your hard disk which contains configuration
01:10information for your copy of Flash Builder.
01:13By default, a New Project is placed under the workspace folder, and you'll see
01:17that on Windows 7, the workspace folder, by default, is set to C:\Users, then
01:23the username, which on my computer is Administrator, and then Adobe Flash Builder 4
01:29You'll see a similar path on Mac OS X and a slightly different path if you're
01:33working on Windows XP. Accept the default location of the Project and go to
01:38the Application type.
01:40You set the Application type to indicate whether the Application will be
01:43deployed through the Web, running in Adobe Flash Player, or to the Desktop as a
01:48native application running on Adobe AIR.
01:50I'll accept the default setting of a Web Application.
01:54The next setting has been added in Flash Builder 4 to this dialog.
01:58It allows you to choose the particular version of the Flex Software Developers
02:02Kit that you want to use to build this Application.
02:05The default is the new Flex 4 SDK.
02:08If you like, you can still use Flash Builder 4 to build Flex 3 Applications.
02:13You would select Use a specific SDK and then choose the included Flex 3.5 SDK,
02:20which requires Flash Player 9 or higher.
02:22We're going to be building Flex 4 Applications in this video series, so I'll
02:27use the default SDK.
02:30The next option allows you to select an Application server.
02:33Flash Builder 4 has integration capabilities with four application servers:
02:38ASP.NET, ColdFusion, J2EE Applications, such as LiveCycle Data Services
02:43and BlazeDS, and PHP.
02:46In Flash Builder 4, you can add server configuration information to a project
02:51after the project has been created.
02:53That's a new feature of the product.
02:55But for this HelloWorld Application, we don't need any application server
02:59functionality, so I'll leave Application server type set to None or Other, and
03:05then I'll click Next.
03:06On this screen, you're asked for the name of the Output folder.
03:10The Output folder is where the Debug version of your application is built
03:14during the compilation process.
03:16The default is a folder named bin- debug, under the Project's root folder.
03:21You can change this if you like, but it's important to note that this folder
03:25will contain only the Debug version of your Application.
03:28A version for the Application SWF file is significantly larger than the version
03:32you'll deploy, because it contains debug symbols and lots of Meta information.
03:38I'll accept the default setting for the Output folder and click Next.
03:42On this screen, I'm asked what Component set I want to use.
03:46I can either choose the default, MX + Spark, which means that I'm using both the
03:51older Components from Flex 3, now known as the MX Components, and a set of
03:56Components that are new to Flex 4, known as the Spark Components.
04:00If you're building a Flex 3 Application, select MX only, and that will make your
04:05Application SWF file significantly smaller.
04:09The Framework linkage option allows you to define how your Application will be
04:13built, either as a single monolithic SWF file or as a smaller SWF file with
04:19linked SWF files known as runtime shared libraries.
04:23I'll talk more about that option in another video.
04:26The Build path library section asks you where existing code is that you want to
04:31incorporate into your Application.
04:34By default, there's a section called Flex 4.0, which refers to files in this SWC format.
04:41These are precompiled Component libraries, which collectively contain the entire Flex SDK.
04:47The Build path libraries also refer to a folder named libs.
04:51This is a folder where you can drop your own precompiled Component libraries, or
04:55SWC files, and any code within those libraries will automatically be available
05:00to your Flex Project.
05:01There are some other options below that configure the compilation process.
05:05The Main source folder is the name of the folder under the Project root, where
05:09you'll create your Main Application files.
05:12And the Main Application file, by default, is set to the same name as the
05:16Project itself, but with the .mxml extension.
05:20You could name this file anything you want, but there are certain conventions
05:23that you must follow and others that you should follow.
05:26The name of the Application will become the name of an ActionScript class in the
05:31background, and so you want to use class naming conventions.
05:34You can use letters, numbers and underscores, but you must start with
05:38an alphabetical letter and by convention, the initial character should be uppercase.
05:44The Output folder URL option indicates where the browser will navigate to, when
05:48you test the Application from Flash Builder.
05:51When you leave this value blank in an Application that does not use an
05:55Application server, it will result in loading the Application from the local
05:59hard disk at run time.
06:00That'll be fine for most testing purposes, but if you want to incorporate a
06:05simple Web server, you can place the Output folder underneath the Web server's
06:09document root and then put in an equivalent URL here.
06:14I'll click Finish to create the New Project and the New Application.
06:20Once the Project and Application have been built, the New Application opens in
06:24the Editor region in Flash Builder.
06:26On my system, the Application opened in Source mode.
06:30That means that I can see the actual source code of the Application.
06:34I'll click the Design button, and that will switch the Application over to a
06:38WYSIWYG, or what you see is what you get appearance, and this will allow me
06:43to do a little bit of visual design by dragging and dropping objects into the Application.
06:50Now, in the lower left corner, there's a view named Components.
06:54The name is hidden a little bit because of the size of my screen, but this view
06:58allows you to select and drag Components into your Application.
07:04I'll scroll down a bit and locate the Label Component, and I'll drag it into
07:08place in the Application.
07:10With the Component still selected, I'll go to the Properties view in
07:14the lower-right corner.
07:15I'll set the Text Property of the Label Component to Hello World.
07:20I'll then click into the font size control, which is under the Text section, and
07:25set it to a size of 18.
07:27And then I'll click this icon, which will cause that control to be bold.
07:32Now I'll go back to Source mode and show you the result, a Label control placed
07:37in a specific position on the screen, because of these two properties, x and y.
07:43The Text Property has been set to a value of Hello World, a style named font
07:48size has been set to a value of 18 - which means 18 pixels - and the fontWeight
07:54has been set to a value of bold.
07:55I'm going to change the values of the x and y Properties manually to 100 pixels
08:01each, and that means that the Label will be placed 100 pixels from the left and
08:07100 pixels from the top.
08:08I'll then save my file by pressing Ctrl+S on Windows, or Cmd+S on Mac.
08:15Then I'll run my first Application in the browser by going to the menu and
08:19choosing Run and then scrolling down, Hello World.
08:25After a few moments, the Application opens in the browser and shows the
08:29Hello World message.
08:30Now this may look like an HTML page - and in fact, if you look at the URL in the
08:35browser, you'll see that it's loaded in HTML file, but the HTML file is simply a
08:41wrapper for Flash Player, and it's Flash Player that's rendering this text.
08:46I can tell that by right-clicking anywhere on the surface of the Application,
08:51and I'll see the Flash Player 10 About screen.
08:55So, that's your Hello World Application.
08:58In another video, I'll describe the anatomy of this Application a little
09:01more carefully, describing not just the code in the Application, but also
09:06the structure and names of the files that are compiled and generated by the
09:10Flex SDK.
Collapse this transcript
Understanding the anatomy of web applications
00:00Flex Applications have a unique architecture.
00:03They incorporate two programming languages:
00:06MXML and ActionScript 3.0, and are compiled as a combination of files that work together.
00:12I'm going to describe the basic anatomy of a Flex Application, both the simplest
00:17aspect of its code and also its compilation architecture.
00:20I will start with the code.
00:22First, I am going to expand the font size in Flash Builder.
00:27You do this by going to the menu and choosing Window > Preferences, and then
00:32under the General category, open Appearance and choose Colors and Fonts.
00:38From there, go to the list of categories on the right and open the Basic category.
00:44Scroll down to the bottom of the list and choose Text Font.
00:49Click Edit and then select the font you want.
00:52I will choose 16 pixels and click OK and then click OK again, and now the font
00:59will be displayed in a nice, large size.
01:02Flex Applications are known in Flash Builder as MXML Applications.
01:06Every main application file is built as an MXML file.
01:10MXML is a pure XML-based language.
01:14The root element of every web-based Flex Application is named Application, and
01:20it has a namespace prefix of 'S,' standing for Spark.
01:24Within a new Flex Application, you'll also see namespace prefixes declared for
01:28FX, which is used for many compiler tags, and also tags that describe certain
01:33ActionScript classs, and one named MX, which is used to describe older
01:38components that are now known as the MX components.
01:42Wherever you see a tag with the 'S' prefix, that's an instance of a component
01:46that's a part of Flex 4.
01:47For example, this is the new Spark version of the Label component.
01:53Each tag describes an instance of an ActionScript class.
01:56So, this is an instance of the Label class, and it has certain styles and
02:01properties that are being set in the MXML Code.
02:04The X and Y properties determine the object's position on the screen.
02:09The text property describes the value that it displays and the fontSize and
02:14fontWeight settings describe its appearance.
02:16I will be describing a lot more about the code architecture of MXML files
02:22throughout this video series.
02:24Now, I am going to show you the anatomy of the compiled files.
02:28Take a look in the bin-debug, or output folder, in your project, in the
02:32Package Explorer view.
02:35When you open the bin-debug folder, you'll see that there's an application file
02:39named HelloWorld.swf.
02:42That's your compiled application, and using your Flex Project's default settings,
02:47you will see a whole bunch of other SWF files.
02:50These are known as Runtime Shared Libraries, or RSLs, and collectively, they
02:56include the entire Flex SDK in a compiled form.
03:01When you deploy your application, you need to deploy both your application's SWF
03:05file and all of these RSL files.
03:09There's also a file name HelloWorld.html.
03:12This is the HTML wrapper file that loads Flash Player, which in turn loads the
03:17SWF file, and then there's a folder named history, which contains three files:
03:22history.css, .js and historyFrame.html.
03:26These files are used to manage navigation at run time.
03:30You don't have to use them - and many applications don't, but if you want to
03:34use features known as Deep Linking or History Management, these files are also required.
03:41And then, finally, there is a file named swfobject.js.
03:45This is a JavaScript library that's included in Flash Builder 4, that's used by
03:49the HTML Wrapper file to deploy Flash Player.
03:52A SWF object is actually an open source free Library, and you can learn more
03:57about it at its home website,
04:00by going to Google and typing 'SWF Object' and then navigating to the Project
04:07Page. You'll find all sorts of documentation about the SWF Object
04:12JavaScript file here.
04:16Now some developers prefer to deploy their SWF files as one large monolithic file.
04:21That means that the SWF file includes both the specific application code and
04:25also the entire Flex SDK.
04:27I will go to HelloWorld.swf, and I will right-click on it and select Properties,
04:33and I'll show you its initial size is 71,000 bytes, approximately.
04:38So, that's the size of the file without the entire Flex SDK.
04:41I will cancel out of that, and then I will go to top of the Package Explorer
04:47to the Project Name.
04:49And I will right-click on the Project and choose Properties.
04:53Then I will go to the category labeled Flex Build Path.
04:59I am going to change the value of the Framework Linkage option.
05:04By default, the SDK files are compiled into runtime-shared libraries.
05:08I'm going to change that to Merged into code.
05:11I will click OK, and then I'm going to clean the project.
05:17When you clean the project, it clears out all of the files in the output folder
05:21and re-compiles the project from Scratch.
05:24I will select Project > Clean and then click OK.
05:29It takes just a few moments to rebuild this very simple project.
05:32Then I will go to HelloWorld.swf.
05:35I will right-click on it, once again, choose properties and show that the file
05:40is much larger, 780,000 bytes, approximately.
05:45So, this SWF file includes both the specific application code and also
05:49the entire Flex SDK.
05:51I will cancel that.
05:56Go back to the Project Properties.
05:58Once again, go to the Flex Build Path and change back to the SDK default of
06:03using runtime shared libraries and click OK again.
06:08That automatically rebuilds the project, and you'll see the RSLs, or Runtime
06:12Shared Libraries, reappear.
06:14So, that's a little bit about the basic anatomy of a Flex Application, both its
06:18basic code architecture and also how the files are created in the output folder.
06:24Now once again, these are not the files that you deploy to your Web Server.
06:28This is the Debug version of the application.
06:31I'll show you how to create a deployable version known as the Release Build in
06:35a separate video.
Collapse this transcript
Creating a release build
00:00The compiled version of a Flex Application that's created in a project's output
00:04folder is the Debug version, and it contains a lot of debug information that
00:09makes the application significantly larger than it needs to be.
00:12When you get ready to deploy your application, you should create what Flash
00:16Builder calls a Release Build.
00:18A Release Build folder will contain exactly the same set of files as the output
00:22folder, but the Application's SWF file will be significantly smaller.
00:27I will show you how to create a Release Build using the HelloWorld project that
00:31I created in another video.
00:32I will go to the menu and select File > Export > Release Build.
00:40You can also get to this feature by going to Project > Export Release Build.
00:45Either way, you will go to the Export Release Build dialog box.
00:50In this dialog box, you select a Project and an Application.
00:53You can only export one application at a time in a Release Build.
00:57I will talk about the View Source option a little bit later.
01:01You indicate the name of the folder in which you want to create the Release
01:04Build and click Finish. That's it.
01:08The Release Build is created in a folder underneath your Project Root.
01:12So, let's take a look at the relative size of the Application's SWF files.
01:17Here is the SWF file for the Debug version of the application.
01:20I will right-click on it and choose Properties and show you that it's
01:24about 71,000 bytes.
01:26Then I'll go to the Release Build version.
01:30I will locate the same file.
01:32I will right-click on it and choose Properties, and this version is a little
01:38more than half the size of the other one.
01:40Now this is a trivial application that just says HelloWorld, but you can imagine
01:45that as your application gets more complex, the relative size of the Release
01:49Build versus the Debug build will become more and more important.
01:54You will also see that the RSL files that are included with the Release Build
01:57are in a different format called SWZ.
02:00This is an archived, or compressed, version of the SWF format, which is
02:05significantly smaller than the debug version.
02:08Taken collectively, the application is going to be significantly smaller, and in
02:13a web environment that's absolutely critical, because it means that the
02:16application will download more quickly and will start up on the user's system
02:20for the first time much more quickly.
02:23These are the files that you deploy to your Web server.
02:26You copy all of the files into Bin Release folder up to your Web server in
02:30whatever folder you like and then to help your users start the application,
02:35provide them with a link to the HTML file.
02:38Don't send them directly to the SWF file.
02:40That doesn't give you control over the size of the application in the browser.
02:44Instead, you want to load this one.
02:46Now, if you want to test the release build in a browser, try this. Right-click
02:51on the HTML file, or Ctrl+Click on Mac, and then select Open With > Web browser,
02:58and that will open the application directly within Flash Builder, within a
03:03Nested Web browser.
03:06Now, I will show you one other aspect of exporting a Release Build - using the
03:10View Source feature.
03:11I will, once again, go to the menu, and I will select Project > Export Release Build.
03:17And I will change the name of the folder to bin-release-withcode, and I will
03:23select the option Enable view source.
03:26When you select this option, you can then select source files that you want the
03:30user to be able to see when they choose a View Source option.
03:34By default, all of the files within your project will be included.
03:38I'm just going to include the HelloWorld.mxml file in my Published Source field,
03:44and I will click OK.
03:46And then I will click Finish, and then I will scroll down to my new Release
03:53Version of the application.
03:55A new source view is included with the deployable application, which includes a
04:00whole set of files that I can look at. And within the source folder,
04:04I will find an HTML version of the XML file, which has color coding.
04:10Now watch what happens when I run this version of the application in a browser.
04:14I will go to the generated HTML Wrapper.
04:17I will right-click on it and select Open With > Web Browser, and then with the
04:24application open, I will right-click on it, and the Flash Player menu now shows
04:29an option labeled View Source.
04:30I will click that option, and I see a new Web Browser window, which shows the
04:36code in the application file.
04:39I can then go to the source folder, over here in this tree, and if there's more
04:44than one source code file available, I can select the one I want to look at.
04:48Finally, all the source code that I included with the Release Build will
04:52be available in this zip file, and a link to download the Flex SDK is also included.
04:58You can use this View Source feature to share code and demonstrations with
05:02other Flex developers.
05:04It's a great teaching tool and a great way to show new techniques that you've
05:08learned about developing applications in Flex 4.
Collapse this transcript
2. Using Flash Builder 4
Choosing a workspace
00:00Flash builder projects are stored in Workspaces.
00:04A Workspace is a folder on your hard disk that contains a table of contents
00:08referencing your projects and also a lot of configuration information.
00:12The concept of a Workspace is a part of Eclipse, the underlying development
00:17environment that Flash Builder is designed to work on.
00:20You can switch Workspaces easily by creating a new folder on your hard disk.
00:25I typically use Workspaces to manage groups of projects.
00:28For example, I have one Workspace to manage all of the projects for this video series.
00:32I'll show you how to switch Workspaces.
00:36From the menu, go to File > Switch Workspace.
00:40If you've previously used other Workspaces, you might see them listed on
00:44the menu that shows up.
00:46Otherwise, you can click Other, and then you can choose any existing folder on
00:51your hard disk, or you can create a new one.
00:53I'll choose the folder C:\flex4esst.
00:59Now this is a folder that already exists on my hard disk, but if it didn't
01:03exist, it would be created for me.
01:05You can also choose Options from this section, labeled Copy Settings.
01:10In this section, there are Options to copy the Workbench Layout and what are
01:14called Working Sets from the current Workspace to the new Workspace.
01:18I'll leave those on unselected and click OK.
01:22When you click OK, you should see that Flash Builder closes and then reopens,
01:28and when it reopens, it should show the default appearance, including the
01:32Flash Builder start page.
01:34Now you can reference a single project for more than one Workspace.
01:38I created my initial project named Hello World in a folder under my home
01:42directory called Adobe Flash Builder, but I can reference it in this Workspace as well.
01:46I'll go to the menu and choose File > Import Flex Project.
01:51Then I'll choose Project folder, I'll click Browse, I'll navigate to my
01:58Administrator folder and from there, to Adobe Flash Builder 4 - that's the
02:03folder in which I'd created my original project.
02:06Then I'll choose the project, Hello World, I'll click OK and click Finish.
02:12I can then open the project and open its application files.
02:16If I switch back to the original Workspace, the project will still be referenced
02:19there, but it's the same project.
02:22Any changes I make to the code or configuration of the project will look exactly the same.
02:27Finally, it's worth mentioning that a lot of the configurations in Eclipse, such
02:31as your Font display or the arrangement of the views, or windows on the screen,
02:36is a part of the Workspace.
02:38So when you switch from one Workspace to another, you'll lose those
02:41configurations and need to rebuild them.
02:44For the rest of this video series, I'll be building all the projects in the
02:47flex4esst folder, or Workspace, and so I'll leave this as my selected Workspace.
02:54But if at any time I need to choose a new Workspace, I'll go the menu and choose
02:57File > Switch Workspace again.
Collapse this transcript
Importing and exporting projects as FXP files
00:00Many of the exercise files in this video series are provided in the form of FXP files.
00:07An FXP file is a ZIP archive file that contains a project, its source code
00:13files, its configurations and other associated assets.
00:17The FXP format is new to Flash Builder 4, and it's also used by Flash Catalyst,
00:23Adobe's new application that enables graphic designers to easily prototype
00:28interactive Flex applications.
00:30I'm going show you how to export a Flex project as an FXP file in Flash
00:35Builder 4, then I'll show you the file's contents and finally, I'll show how to re-import it.
00:41I'll be working with a project named HelloWorld.
00:44The project has one application, Hello World.mxml, in its source folder, and
00:49it has a bin-release folder, because in this project, I've previously
00:53exported a release build.
00:55Now before you try to export an FXP file, the project must first be open in Flash Builder.
01:02Then from the menu, select File > Export Flex Project.
01:07In the Export Flex Project dialog box, make sure you've selected the project
01:12that want to export.
01:13I've selected the only project I have open, Hello World.
01:17And then you can click the browse button and select the location where you want to
01:20create the file, and you can set the file name.
01:24The default file name will start with the project name and end with .fxp.
01:29The option labeled validate project compilation, when checked, causes the
01:33project to be rebuilt before the FXP file is created.
01:38I'll click Finish to create the FXP file.
01:40It takes just a few moments to create the file on my desktop. Here is the file.
01:46It's named Hello World.fxp.
01:49Now I'm going to make a copy of this file and give the copy a new file extension of .zip.
01:56I'll copy and then paste it to the desktop, and then I'll rename it with
02:01an extension of .zip.
02:02If you see a confirmation message asking if you mean to change the file name
02:06extension, click Yes or Ok.
02:09Now the reason I created the ZIP file was so that I could look at its contents
02:14outside of Flash Builder.
02:15I'll double-click the Zip file. On Windows Vista, and on Windows-7, by default,
02:21this opens the Zip file as a folder.
02:23If you double-click the zip file on a Mac, by default, it creates a new folder,
02:28extracting the contents of the Zip file to that folder.
02:31And if you've installed other software that's associated with the Zip file
02:35extension, such as WinZip, you'll open the Zip file in that application.
02:41Either way, you'll be able to look at the contents of the Zip file.
02:44The Zip file is a compressed file that contains the application assets, such as
02:50the source folder with the application MXML files.
02:53If you have any other folders that aren't related to the application, such as
02:57this bin-release folder that I created, it will also be included in the FXP
03:01file, and you'll see a bunch of other files that start with a dot.
03:05These are configuration files. You can explore them in any text editor.
03:11So now, I have an FXP file, and I'll show you how to import it.
03:15I'll go back to Flash Builder, and I'm going to delete the current project.
03:19I'll right-click on it, or Ctrl+Click on Mac, and choose delete, and then I'll
03:25choose Also Delete Contents under the name of that folder.
03:29When you delete the contents of the project, the operation cannot be undone. I'll click Yes.
03:35That removes the project from the workspace and deletes the project and all of
03:39its files from the hard disk.
03:41Now I'll re-import the project from the FXP file.
03:45I'll choose File > Import Flex Project. I'll browse, I'll go to my desktop, and
03:53I'll choose the file Hello World.fxp.
03:57I'll select the file and then click Finish to import the project.
04:03I'll open the project, open the source folder, open the default package and open
04:08the application file, Hello World.mxml.
04:12And you'll see that all the code is in place, exactly as it was when I created it.
04:17So, the FXP file format is new to Flash Builder 4, is shared with Flash Catalyst
04:23and is a ZIP archive format that's designed to export and import Flex projects.
Collapse this transcript
Setting Flash Builder preferences
00:00Eclipse and Flash Builder are both designed to be highly configurable.
00:04When you first install them, they behave in a particular way, but there are many
00:08changes you make to make the application look and behave the way you want.
00:12I am going to show you how to change a couple of very common preferences.
00:16First of all, I am going to modify the font on my screen so that you will be
00:20able to see it more easily.
00:21And I will also show you how to control which browser is being used when you
00:25test your application.
00:26I will go to the menu and choose Window, and then down at the very bottom,
00:31choose Preferences.
00:33The Preferences dialog box includes all of the Eclipse preferences;
00:37that is everything having to do with the underlying Eclipse workbench and then
00:41also all of the preferences that are part of Flash Builder.
00:44I will start with the font that's used to display my code.
00:47I will go to the General category, open that up and from there, go to
00:52Appearance, and then I will choose Colors and Fonts.
00:56In this screen, I will choose Basic and then scroll down to the bottom of the
01:00list of all the different font settings.
01:02And I will choose Text Font.
01:04That's the font that is used by all the text editors in Eclipse and Flash Builder.
01:09Then I will click Edit, and notice that on Microsoft Windows the default font is Courier New.
01:15On Mac OS X, you'll see a different setting.
01:18I am just going to increase the font size from 10 to 14.
01:21I will click OK, and then click OK again, and now the font is much larger on the screen.
01:28You should be able to see this a lot more easily in the video series.
01:31For example, you can choose any font you have installed on your local hard disk
01:36and set it to any size and style you like.
01:38Here is another preference that developers frequently like to change, the browser.
01:43When I run my applications, right now, they're opened in Firefox.
01:48That's because Firefox is my default system browser.
01:51You can run the application, as I have previously shown, by choosing Run and
01:55then choosing Run and the name of the application from the menu.
01:59But you can also run the application by clicking the Run button on the Toolbar.
02:03I will click the button, and that opens up the application in Firefox.
02:08Now I'm going to change my browser preferences.
02:11From the menu, I will choose Window > Preferences and then, still in the General
02:16category that I was working with before, I will choose Web Browser.
02:21Notice the selection is set to Default system Web browser.
02:24I am going to change to Internet Explorer and click OK.
02:28I will run the application, and now it opens up in Internet Explorer.
02:32I will switch back to Firefox by selecting Window > Preferences, and this time,
02:41I will explicitly set it to Firefox instead of just assuming that
02:44that's my default system browser.
02:46I will click OK, and once again, run the application.
02:53So, that's, once again, a very common preference.
02:56The final preference I will show you is how to set your indentation.
02:59By default, Flash Builder creates Flex applications using tabs as the
03:04indentation characters, and it displays four space characters per tab.
03:09You can change these settings both for MXML and for ActionScript files. Here's how.
03:15I will go back to the Preferences dialog, and this time, I'll go to the
03:20Flash Builder settings.
03:22These settings have to do with Flash Builder's own components, not Eclipse's.
03:25I will go to the Indentation section.
03:29Notice here that it says that I'm indenting using Tabs and that the Tab size is 4 spaces.
03:35I will choose Spaces instead of Tabs and then set the Indent size and Tab size
03:40to 2 characters each.
03:42I will click OK, and notice that the display has changed so that it's
03:472 characters per tab.
03:49But if I place the cursor in some of the whitespace inside the code and
03:53press the left arrow and right arrow a few times, you will see that those
03:57are still tab characters.
03:59The file wasn't automatically rewritten for me just because I made
04:02that preference change.
04:04But here's a great new feature of Flash Builder that will allow you to reindent
04:08and reformat your code automatically.
04:10I will select all of the code in the file and then go to the menu and choose
04:14Source > Correct Indentation.
04:17And notice now, if I place the cursor in the whitespace and move the cursor
04:21around, that now it's all made up of spaces.
04:25Also, you'll see that all the attributes have been aligned with each other.
04:28This is sort of the default appearance of Flex applications in the
04:32Flex documentation.
04:34And if you want to replicate that, those are the settings that you would use.
04:38If you don't like the indentation of the attributes, that's yet another setting.
04:41I will go to the menu and choose Window > Preferences, and this time, I'll open
04:47up the Indentation category and look at MXML files.
04:52Here are some other preferences you can set.
04:54I will uncheck the option Align attributes, and now all the attributes will
04:59simply be two characters indented, regardless of whether there are any other
05:03attribute above them. I will click OK.
05:07This time, I will select everything with a keyboard shortcut.
05:09Ctrl+A to select all on Windows, or Command+A on Mac.
05:14Then I will use the keyboard shortcut for Indentation, Ctrl +I on Windows,
05:20or Command+I on Mac.
05:22So, this is how you can easily format your code and set the Preferences for how
05:26the formatting will look.
05:28This is the code appearance that I will be using throughout this video series.
05:31I will be intending with spaces rather than tabs and using two spaces before each line.
05:37This will allow me to get as much code on the screen as possible, but you can
05:41configure your own copy of Flash Builder to your own preferences:
05:45Tabs or Spaces, Firefox, Safari or Internet Explorer, whichever browser you want
05:51to use, whatever font you want to use.
05:54And I encourage you to explore the other available preferences so that you can
05:58modify Flash Builder to your liking.
Collapse this transcript
Using Eclipse views and editors
00:00The Eclipse interface is made up of views and editors.
00:04Both views and editors are rectangular regions that serve particular purposes.
00:10The area where you see your source code is called the editor region.
00:14Eclipse is installed with some basic editors, including a web browser and a text editor.
00:20Flash Builder 4 adds a few other editors.
00:23This is called the MXML editor, and it's used every time you open a file with
00:28a .mxml extension.
00:30The MXML editor can be used in two modes: Source mode and Design mode.
00:36The purpose of design mode is to give you a WYSIWYG, or what you see is what you
00:40get, look and what your application is going to look like.
00:44It isn't 100% effective;
00:46that is to say it isn't going to show the every application exactly as it will
00:49look when you run it in the browser.
00:52That's because the design mode can only evaluate code that's complied into the
00:57application, rather than run at run time.
01:01But it can give you a strong sense of what the application will look like.
01:04That's the MXML editor.
01:06You will see, when you start creating ActionScript classes, that there is also
01:10an ActionScript editor, which shares a lot of behaviors and capabilities with the
01:14MXML editor, but is actually completely separate component.
01:19All of the other rectangular areas that surround the editor area are called Views.
01:24When you first open Flash Builder, you have a Package Explorer view, an Outline
01:29view, Problems, Data/Services, Network Monitor and ASDoc view.
01:37There are many other views available.
01:39You can go to the menu and choose Window > Other Views, and you can see a
01:44listing of all the different views that are included in Flash Builder, but also
01:49all the views that are available in all of your other Eclipse plug-ins.
01:52For example, the Team category is where you would access views, say for
01:56subclips, a plug-in that manages subversion repositories.
02:01If you use CFEclipse, you will see the views for that product here as well and
02:05if you use ColdFusion Builder, Adobe's new plug-in for ColdFusion developers,
02:09you will see those views here as well.
02:12Views are designed to be configurable.
02:15You can resize them, dock them in other areas of your interface, or make them float.
02:19For example, let's say that I wanted to make my Problems view appear in a different area.
02:25I can click on the tab and then drag and dock it somewhere else on the screen.
02:31Similarly, if I don't want to use a particular view, for example, I am not going
02:34to use ASDoc, I can move the cursor over the tab and then click the X icon, and
02:40that view will go away.
02:42Whenever you need to bring a view back, just go to the menu and choose Window,
02:46and for example, I would like to bring my ASDoc view back, click the item and it will reappear.
02:53If you want to make a view float, that is not to be docked into the interface,
02:58go to the tab, right-click on it, or Ctrl+Click on Mac, and choose Detached.
03:04That view can now be dragged around the screen, or if you are using a
03:08spanned monitor environment with two monitors, you can drag it over to the second monitor.
03:13If you want to redock the view, right- click on its tab again and deselect Detached.
03:20It will drop into whichever area is closest.
03:23You can then click on the tab, drag it and replace it exactly where you want.
03:27I will click and drag it and place it back in its original position.
03:32You can also maximize any view or editor.
03:35There are few ways of doing this.
03:36For example, I would like to look at my code in fullscreen.
03:41So, I will go to the tab for the MXML editor I am using and double-click it.
03:46And that opens it up to fullscreen.
03:48Double-click again and it brings it back to its original size.
03:52You can also use keyboard shortcuts for this: Ctrl+M on Windows, or Command+M on Mac.
03:59It's a toggle keyboard shortcut, maximizing and restoring as needed.
04:05There's one other trick that's available.
04:07If a particular view or editor is maximized, you can go to the tools over on
04:11the left side, and the very first button at the top of that list will be labeled Restore.
04:16Click that button and it brings the viewer editor back to its original position.
04:22So that's a look at how views and editors work.
04:24There is lot more to know about them, but I will show you some tricks in other
04:29videos as we create our Flex applications.
Collapse this transcript
Using Flash Builder perspectives
00:00In Eclipse, a Perspective is a particular arrangement of views on the screen.
00:05When you first install Flash Builder, you're installing Eclipse 3.51, if you're
00:09using the standalone configuration.
00:12And regardless of whether you are using the standalone or the plug-in edition,
00:16Flash Builder is installed with three built-in Perspectives named Flash, Flash
00:21Debug, and Flash Profile.
00:24There are a few different ways of switching between these Perspectives.
00:28One way is to go through the menu.
00:29I'll choose Window > Perspective, and I'll see the three primary Perspectives of
00:35Flash Builder displayed here.
00:38I'll choose Flash Debug, and that will switch to a new arrangement of views on
00:42the screen, with the Debug view at the top-left, the Variables, Breakpoints, and
00:47Expressions views in the top-right, the Console, Data/Services and Network
00:52Monitor views at the bottom and the Editor in the vertical center.
00:57Then I'll go to another Perspective.
00:58I'll choose Window > Perspective > Flash Profile.
01:03This takes you to the Flash Profiler tool, a tool that's only available if you
01:08have a license for the Premium Edition of Flash Builder.
01:11Finally, I'll go back to the Flash Perspective, which is your primary
01:15editing Perspective.
01:17Here's another way of switching between Perspectives.
01:20In the top-right corner, you'll see a little icon that has a set of buttons, and
01:25if you click and drag the border to the left, you'll see buttons displayed for
01:28Perspectives that you've recently visited.
01:31Once again, here's Flash, Flash Debug, and Flash Profile.
01:37Now a Perspective, once again, is a particular arrangement of views and editors,
01:42and you can create your own custom Perspectives.
01:45For example, let's say that I wanted to take the Outline view and dock it
01:50over on the right side.
01:51So, I'll click and drag it until it docks, and then I'll drop into place.
01:56Then perhaps I never used the ASDoc view.
01:59So, I'll click that tab and then click its X icon, and it will disappear.
02:03So now let's say that this was my custom arrangement, how I typically like
02:08to use Flash Builder.
02:10I'll go to the menu and choose Window > Perspective > Save Perspective As, and
02:15I'll name this David Custom.
02:20Now David Custom appears in the Perspective selector in the top right, and if
02:24I go to the menu and choose Window > Perspective > Other, I'll see my
02:29Perspective listed there.
02:31So, now I can easily switch back and forth between the different Perspectives.
02:35There is Flash Debug, there is David Custom, and I'll go to the menu and
02:40choose Window > Perspective > Flash, and that'll take me back to the default Perspective.
02:47Finally, I'll show you how to reset a Perspective.
02:50Let's say, for example, that you had deleted various views,
02:53you had redocked others, and you had detached still others.
02:57And you had gotten the Perspective to a customized point where you knew you
03:01didn't want to save that.
03:03You can easily reset a Perspective to its current setting as stored on disk by
03:08going to the menu and choosing Window > Perspective > Reset Perspective.
03:14When prompted, click OK, and that will restore all the views and editors to
03:19their original positions.
03:21If you've customized your current Perspective, then when you close Flash Builder
03:25and reopen it, the Perspective will be restored to however you had it when you
03:30closed the application.
03:32Perspectives are how you customize the appearance of Flash Builder.
03:36You can create as many customized Perspectives as you want, and you can use
03:39the Perspectives that are included with Flash Builder 4 and switch easily
03:44between them.
Collapse this transcript
3. Using Flex Programming Languages
Understanding MXML
00:00You build Flex 4 applications with two programming languages:
00:03MXML and ActionScript.
00:06MXML is a pure XML-based language, also known as a Markup Language, which is
00:11actually a facade or a convenience language for ActionScript, which means that
00:16when you code in MXML, it gets compiled into ActionScript in the background.
00:20For the demonstrations in this chapter, I'll work on a new project.
00:24I have a number of starting files that are included with the Exercise Files.
00:28I'll first start by closing any existing projects.
00:32In the Package Explorer, if you have a project open, such as HelloWorld,
00:36right-click on it, or Ctrl+Click on Mac, and choose Close Project.
00:41Then follow these steps to import an existing project.
00:45Go to the menu and choose File > Import Flex Project.
00:51Navigate to the Exercise Files folder, which I've placed on my Desktop.
00:55Go to the 03_Languages to the begin folder and then select and import the
01:01file Languages.fxp.
01:04Open the project, open its source code root folder named src, open its default
01:09package, and then double-click and open the file HelloWorld.mxml.
01:15This is the simplest of Flex applications, displaying a simple string on the screen.
01:20I am going to use this application to describe the nature of MXML and
01:25its component parts.
01:27Every Flex main application file is built as an MXML file.
01:31That means two things.
01:32Number one, that it has a .mxml extension, as this one does, HelloWorld.mxml,
01:37but also that its root element uses a tag name, or an element name, that's a part
01:42of the MXML language.
01:44Every main application starts with a root element of Application.
01:48The Application element in MXML represents an ActionScript Class named
01:53spark.components.Application.
01:56You can find out what the equivalent ActionScript Class is for any MXML tag by
02:01just hovering the cursor over the tag name.
02:04You'll see a little bit of help pop-up.
02:05It shows you right away the name and package in which the class is stored.
02:09But then if you press the F2 key, and that's the same keyboard shortcut on
02:13either Mac or Windows, you'll get an expandable window, and you'll be able to
02:18open it and scroll it to see more documentation.
02:21Press Escape to make the help go away.
02:24In the root element of each MXML file, you'll find a number of XML
02:28namespaces declared.
02:29When you create a brand-new application, or component, in Flash Builder these
02:33three namespaces are included automatically.
02:36The first is associated with a namespace prefix of fx.
02:40This new namespace represents the MXML language itself, and it tells us that
02:44we're working with a version of MXML that was created in 2009.
02:48The second namespace is associated with the prefix 's,' and it stands for Spark.
02:53This namespace is associated with all components that are members of the new
02:57Spark framework that includes the application tag itself.
03:01And finally, the third namespace is associated with the namespace prefix mx.
03:06This namespace is associated with the older components, the components that were
03:10part of Flex 3 but which we still use in the Flex 4 SDK.
03:15When you declare an object using MXML in a Flex application, you start off with
03:19the XML prefix - in this case, the letter s which means the Spark framework -
03:24and then the name of the tag.
03:26This tag, in turn, is associated with what's called an XML Manifest.
03:31An XML Manifest, which is stored in the flex SDK folder, is a mapping of the
03:36MXML tags to their equivalent ActionScript Classes.
03:39Some classes are associated with the mx prefix, some with the s prefix and some with fx.
03:45Here is an example of a prefix that's associated with fx.
03:48I'll place the cursor inside the Declarations block.
03:51This is where you place non-visual elements, such as simple variable
03:55declarations, value objects, class instances and instances of what are known as
04:00RPC components, or Remote Procedure Call components.
04:04I'm going to declare a simple string.
04:06I'll type in a less than character to start the tag, and then type the word
04:10'string.' The list that pops up is called the Code Assist List, and it shows me
04:14a list of all known tags that I can place in this position.
04:18I can either press Enter or Return to select an item from the list, or I can use
04:22the mouse and double-click.
04:23Notice that the Code Assist tool automatically fills in both the tag name
04:27and its correct prefix.
04:29I'll assign the string an id, which is the equivalent of a variable name, and
04:33I'll name it myString.
04:35And then I'll type in the less than character and Flash Builder automatically
04:39adds the required end tag.
04:41And I'll type in a value of a simple string.
04:44So, that's one way of declaring a variable in Flex.
04:48When you declare a variable like this in the Declarations block, it makes it
04:51persistent for the lifetime of this application, or if you're working in a
04:55custom component, for the lifetime of the component.
04:58It also makes it public to the rest of the application, meaning that any
05:02other part of the application can access that variable, myString, using simple dot notation.
05:08So, now we have examples of the Application component, the string and the label.
05:12Now, let's take a look at an example of an mx component, a component from the
05:16Flex 3 framework that has not been replaced by a new version.
05:20I'll place the cursor after the label tag.
05:22I'll type in a less than character and start off with data.
05:26There are a number of components that start with the word 'data.' Notice
05:29that some are members of the Spark Component Group and others of the MX Component Group.
05:34I'll choose the DataGrid.
05:35A DataGrid is a component that displays data in rows and columns.
05:39I'll complete the tag by just typing in a slash and greater than character, and then I'll look at the
05:44application in Design mode by clicking the Design button, and I'll show you that
05:49the DataGrid tag results in creating this component, the DataGrid component.
05:54Now, I'm not going to fill it in with complex data right now.
05:57The purpose of this lesson is to understand the nature of MXML.
06:01When you use MXML, you are declaring instances of classes.
06:05There are some MXML tags that don't represent classes.
06:08For example, the fx declaration tag is used to wrap instances of non-visual
06:14classes, and there are a number of these types of tags as well.
06:17When you build a Flex application, the main application file, again, always is
06:22in an MXML file, and then the rest of the application will be made up of a
06:25combination of MXML and ActionScript files.
06:28And I'll describe the use of ActionScript Code in other videos.
Collapse this transcript
Setting properties and styles with MXML
00:00The MXML programming language is used typically to declare instances of visual
00:05objects that are presented on the screen and also of non-visual objects that you
00:10use at run time to assist in running the application.
00:14I'm going to create a brand-new application.
00:16Each Flex project in your workspace can contain one or more applications.
00:21Here is how you create a brand-new one.
00:23Go to the Project, right-click and choose New > MXML Application.
00:29Or you can go to the menu and choose File > New > MXML Application.
00:34Either way, you'll land in this dialog box, the New MXML Application dialog.
00:39Enter the name of the application without a .mxml extension.
00:43I'm going to create a brand-new application called HanselAndPetal.
00:47This is the name of our fictional company that sells plants, flowers and the like.
00:52And I'm going to be building a web- based Flex application for the company
00:55throughout this video series.
00:57Also on this screen, you can select something called a layout.
01:00There are four layouts available.
01:02The default is something called Basic Layout.
01:05This is a layout that will let you place objects on the screen anywhere you
01:09want, and they'll hold their position.
01:10If you have worked with Flex 3, it's just like Flex 3's Absolute Layout,
01:15Horizontal and VerticalLayout layout objects either from left to right, or
01:19from top to bottom.
01:21And the new TileLayout replaces the old Flex 3 tile container;
01:25that is to say it causes the application's objects to layout in rows and columns.
01:31I'm going to choose the VerticalLayout architecture to cause objects to stack
01:35from top to bottom and click Finish.
01:37When you first create an application, it has a root application tag, the
01:41three namespaces that I've described in other videos and a minWidth and
01:45minHeight attribute.
01:47This will restrict the user from reducing the application beyond a certain level.
01:51The layout is declared in Flex 4 as a child element of the Application Class.
01:56It's still a property.
01:58And if you already had an object that was an instance of a Layout Class, you
02:02could apply it using an attribute.
02:03But this is the most common syntax that you see.
02:06The purpose of this video is to show you how to set styles and properties using MXML.
02:11Styles and properties are set in MXML as XML attributes.
02:15And you really can't tell the difference by looking to see whether something is
02:19a style or property.
02:20For example, the minWidth and minHeight settings are properties.
02:24Now I am going to apply another setting to the application tag, which is a style
02:29that is which affect its visual appearance.
02:31I am going to set a value called backgroundColor, I'll type in the word back and
02:35then press Ctrl+Space to bring up a list of matching attributes, and then I'll
02:40choose backgroundColor.
02:41You set the background color style to a color value.
02:45And just like in HTML, when you are working in MXML, you use hexadecimal values,
02:50six characters preceded by a pound sign.
02:53I'll use this color #1E5C75, and then I'll look at the application in Design
03:00mode to see the effect of that style.
03:02And I'll see that the application has this deeper color as its background.
03:06Now, I'll go back to Source mode and place the cursor after the Declarations
03:10element and press Enter, or Return, to create a new line.
03:14As I've showed in other videos, you can create a label by using the tag s:Label
03:19and setting its text property to a value.
03:22I'm going to give this label a value of Hansel and Petal, and then I am going to
03:27apply a few other attributes, which, again, are styles.
03:30I'll set a fontSize style of 36.
03:34It's worth mentioning that in Flex all fonts are measured in pixels, never in
03:39points, ems, or any other unit of measurement.
03:42I'll also set the color to #FFFFFF.
03:46I finish the tag by adding in the slash and the greater than sign at the end,
03:50and then I will, once again, look at the application in Design mode and show you
03:54that a label object appears in the top left corner of the application.
03:57Using the Vertical Layout, if you'd like to add a little bit of padding around
04:01the objects within the application, you don't do it in the Application tag,
04:05like you did in Flex 3.
04:07Instead, you do it in the Layout tag.
04:08I am going to use a set of padding settings, which in Flex 3 were set as styles
04:14and in Flex 4 are set up as properties.
04:17I'll place to cursor after VerticalLayout and press the Spacebar and then type
04:22'p-a-d-d,' and that brings up a list of the four padding settings.
04:26I'll set paddingBottom, paddingTop, paddingRight and paddingLeft all to values of 10.
04:34I'll once again, look at the application in Design mode, and I'll see that I
04:43have a little bit of extra space now around the objects.
04:47Finally, I'll show you how to add other complex properties to an object.
04:51Specifically, I am going to add a Drop Shadow to a label.
04:54A Drop Shadow is one type of filter that you can apply to visual objects in Flex.
05:00Every visual control in the Spark components set has a property called filters.
05:05It's an array and can take more than one filter object.
05:08And so in order to declare this kind of complex object in MXML, you break up the
05:12top-level tag, in this case the label, into a Begin tag and an End tag.
05:17I did that just by typing in the greater than character and allowing Flash
05:21Builder to autocomplete the element.
05:23Now, I'll add the filters property.
05:26The filters property is declared as a child element of the parent element, in
05:30this case the label.
05:32And once again, it's declared as a Begin tag and an End tag.
05:35And then I'll declare a DropShadowFilter.
05:38Notice that there are two versions of the Drop Shadow filter class that are
05:42shown in the code assist list.
05:44The first one, which I currently have highlighted, has a prefix of mx and is
05:48designed for use on the mx components.
05:51I am using this Spark label component, so I'll use the Spark DropShadowFilter.
05:56I'll apply a color to the DropShadowFilter of #CCCCCC - that's a light gray, and
06:03then I'll close the tag with empty tag syntax.
06:06I'll, once again, look at the application in Design View, and I'll see a nice
06:10drop shadow is being displayed.
06:12Finally, I'll test the application in the browser and show that the appearance
06:15of the Drop Shadow looks the same as it did in Design mode.
06:18So this has been a look at how to set properties and styles using MXML.
06:23Once again, they look the same - they appear as XML attributes - but in the
06:27background, they are translated as ActionScript styles and properties.
Collapse this transcript
Translating MXML to ActionScript 3
00:00As I've mentioned in other videos, MXML is translated into ActionScript code
00:05during the compilation process.
00:07I'll show you an easy trick that allows you to see the actual ActionScript code
00:11before it's compiled into SWF bytecode.
00:14For this demonstration, I'm going to use the HanselAndPetal application that
00:18I've created previously, but you can do this with any Flex application.
00:22First, I'll go to the Project Properties.
00:24To get to the Project Properties, you can either right-click on the project
00:28name in the Package Explorer and choose Properties, or you can simply select
00:33the Project in the Package Explorer, and then go to the menu and choose Project > Properties.
00:39Either way, you start in this dialog, properties for the current project.
00:43The Resource category shows you the location of the project on your hard disk.
00:48There're many other categories that I'll be talking through throughout this video series.
00:52The one I'm concerned with here is called the Flex Compiler category.
00:56It shows you all of the different compiler options, including which SDK is being
01:01used to build the application, what is the minimum version of Flash Player
01:05that's required by this version of the Flex SDK, and many other options.
01:09Here's how you cause the compiler to keep the generated ActionScript code after
01:14it's been translated from your MXML.
01:16I'll place the cursor into the Additional compiler argument's text entry and
01:21then put in a space and then the following compiler argument,
01:25-keep-generated-actionscript=true. I'll click OK to save the property changes,
01:36and then I'll go to the bin-debug or the output folder for this project.
01:41I'll find a new folder called generated.
01:44I'll open up that folder, and I'm going to expand the size of the Package
01:48Explorer view so we can see all of these files more clearly.
01:51You'll see many files with a .as extension that have been generated.
01:55There are a bunch of files that start with an underscore character.
01:58These are considered private files.
02:01You can open them up and examine them if you want, but they're
02:03fairly boilerplate.
02:04But I find these files more interesting.
02:08Here's the file that's the ActionScript equivalent of your MXML application.
02:12If the name of your application is HanselAndPetal.mxml, then the name of the
02:17equivalent ActionScript file is HanselAndPetal-generated.as.
02:22I'll double-click to open that file and then double-click the tab on the new
02:26file to open it to fullscreen.
02:29I recommend that you spend some time examining the code that's generated in
02:32these ActionScript files.
02:34It's not only a great way of understanding how MXML is translated into
02:38ActionScript, but if you're new to ActionScript 3, it will help you learn
02:41the language itself.
02:42Here are some examples.
02:44I'm going to press Ctrl+F to bring up the Find/Replace dialog.
02:47If you're working on Mac, that's Command+F. And I'm going to type in the word
02:51'Label' and click Find.
02:54That takes me to a function that's returning an array, and within the array,
02:58there is a call to a function called HanselAndPetal Label1_c.
03:03Take a look at that function.
03:05Within that function, there is a variable named temp datatyped as a Label,
03:09constructed from the Label constructor method.
03:12There is a text property set to HanselAndPetal, a filters property, which is
03:17getting its filter from yet another internal function, a method called setStyle
03:21- that's setting the font size, and another that's setting the color, a couple
03:25of other lines of code that bind it all together, and then a return statement.
03:29The result of all this code is to construct the Label object that was declared
03:34here in my MXML code.
03:36If you explore this code, you'll find all of the different aspects of the
03:39application translated exactly as they're going to be executed when the
03:44application runs in Flash Player.
03:46Now generating this ActionScript code doesn't do anything to improve the
03:50performance or functionality of your application.
03:52It's just a learning tool, and in some cases, is great for debugging as well.
03:56But it's an incredibly useful trick and great for understanding how MXML and
04:02ActionScript relate to each other.
Collapse this transcript
Using ActionScript in MXML files
00:00Nearly all Flex applications are built with a combination of MXML and ActionScript code.
00:05I am going to show you how to incorporate ActionScript code into your MXML
00:09applications in a couple of different ways.
00:12I am going to start with an application that's a part of the Languages project
00:16that comes with the Exercise Files.
00:18If you don't have access to the Exercise Files, you can create this
00:21application on your own system.
00:23I will open the application code to fullscreen.
00:26I am going to start by showing you how to execute ActionScript code when the
00:30application first starts up.
00:32I will talk about events and the entire Flex event architecture in another
00:36chapter of this video series, but it's important to know from the beginning that
00:41there is a particular event that is dispatched by the application, when the
00:45application has finished loading into Flash Player and is ready for use.
00:50The name of this event is creationComplete, and you can react to the
00:55creationComplete event by executing some arbitrary ActionScript code that you write.
01:00Here's the easiest way to use the creationComplete event.
01:03Place the cursor at the end of the starting Application tag, after the
01:07backgroundColor setting, press the Spacebar and start typing in the name of the
01:13event, creationComplete.
01:15When you see the event name appear in the code assist list, press Enter or
01:19Return to select it, and you will see a prompt labeled Generate
01:23CreationComplete Handler.
01:24This is a new feature of Flash Builder 4.
01:26Flash Builder automatically generates a function for you that will be called
01:31when this event is dispatched.
01:32To select the option, either press Enter, or Return, or double-click it.
01:36I will press Enter on my computer, and that fills in the rest of the
01:40creationComplete attribute with the call to a new method called
01:44application1_ creationCompleteHandler, and the function
01:48
01:48application1_ creationCompleteHandler is created for me.
01:51Now, to execute code when the application starts up, just put the code
01:56inside that function.
01:57Here's something you might want to do in ActionScript instead of in the MXML.
02:01I have been told by the marketing department of Hansel and Petal that the name
02:06of the company is always spelled with an ampersand in the middle, instead of
02:09the word 'and.' And so I go to the Label control, and I type in an ampersand
02:14character, and I Save my changes, and I see a little Error icon appear over on the left.
02:20I move the cursor over the Error icon, and I see the message, The entity name
02:25must immediately follow the '&' in the entity reference. Wow!
02:29What the heck does that mean?
02:30Well, the ampersand character is one of five reserved characters in XML, not
02:35just in MXML, but in all XML-based languages.
02:39A reserved character is a character that you can't place literally within
02:43certain XML contexts, and an ampersand character can't be placed in an attribute
02:48value without it being turned into what's known as the XML entity.
02:52So one way around this would be to spell out the entity, and it would look like this &.
02:59I will save and run the application, and you will see that that entity, &,
03:06is translated into the literal semicolon when the application runs.
03:10But if you prefer, you can set this label's text property in ActionScript code instead.
03:15So, I am going to strip out the text property from the Label tag, and then I am
03:20going to go to the function that was generated, and I will Delete the comment
03:24that was added to the function automatically.
03:27Notice that the Label control has an ID of headline.
03:30That's going to allow me to address it in my ActionScript code.
03:34I will type in 'headline.,' and then in the code assist list, I will choose the
03:39text property from the list of all of the Label classes, properties, and
03:43methods, and I will set its value to Hansel & Petal.
03:47This time, because I am working in ActionScript code, I can use the reserved
03:52characters that weren't allowed in the MXML.
03:54I will Save and run the application again.
03:57And once again, you see the correct display in the application, with the literal
04:01ampersand, but I haven't had to play XML formatting tricks to make it happen.
04:07You use ActionScript code to handle the logic of your Flex application, to react
04:12to events that are dispatched by the application or its components, or any time
04:16you want to have a little bit more flexibility in how you write your code.
04:20The MXML code gets compiled into the application and is typically used to
04:24declare objects that are always there.
04:27But ActionScript code is more flexible.
04:29You can use conditional clauses, looping constructs, and everything else that
04:33the language offers.
04:34And since we already know that MXML is really ActionScript in the background, it
04:39just tells you that you can choose between the two languages in many cases.
04:43The only exception to this is that the main application file itself is always an MXML file.
04:49Other than that, a lot of the work that you do in Flex applications can be
04:53done in either language.
Collapse this transcript
Declaring and using variables
00:00You can declare variables using either MXML or ActionScript code.
00:05For this demonstration, I will use the application file UsingVariables.mxml.
00:10I will open it up to fullscreen so we can see as much code as possible.
00:14If you want to declare a variable using MXML code, and the variable is going to
00:18have a non-visual data type -
00:20that is something that's not a label, a button, or something else the user can see -
00:24you place that inside the <fx:Declarations> element.
00:27I am going to Select and Delete the comment inside the Declarations element, and
00:32then, on a new line of code, I will create a String variable.
00:35The String, Number, Date, Boolean, and other data types that are built into
00:41Flash Player are represented in the Flex SDK with the fx namespace prefix.
00:47I will start by typing in a less than character and then 'str.' And then I will
00:53select the <fx:String> tag.
00:55When you declare a variable using MXML, if you want to address it in your
01:00ActionScript code, assign it an ID, just like you do with visual objects.
01:05I am going to assign an ID of helloString, and then I will type in the greater
01:10than character to close the tag.
01:11And Flash Builder automatically adds the matching end tag, and I will type in a
01:15String of 'Welcome to our store.'
01:18Next, I will add another Label.
01:20I will do this part in Design mode so I can place the Label exactly on the
01:24screen where I want it.
01:26I restore the Editor to regular size, and then click the Design button.
01:30And then I will go down to the Component's view and drag in a new Label into the application.
01:35I will place it on the screen, somewhere where it looks good, and then I
01:39will set some Styles.
01:40I will set this to 24 pixels and Italics.
01:45Then I will go back to Source mode, and I will look at my new Label that's been
01:49added to the application.
01:52In order to be able to address the Label, I will add an ID attribute of welcomeLabel.
02:01So, I have a string called helloString and a label called welcomeLabel.
02:05To put them together at run time, I will click into the creationCompleteHandler
02:09function and apply the variable value to the object property, with this code,
02:15welcomeLabel.text = helloString.
02:20I will Save and run the application, and there's my welcome message displayed on the screen.
02:30You can also declare variables using ActionScript code.
02:33If you declare a variable inside a function, the variable is local to the function.
02:38And if you declare the variable outside the function, it represents a variable
02:42that persists for the lifetime of the application, and it's available to any
02:46code within the application MXML file.
02:49To declare a variable inside a function, you start with the keyword 'var' and
02:54then the name of the variable that you want to define.
02:56I will declare a variable called myLocalVar.
03:00For any variable, you always add a colon at the end and then a data type.
03:05This is a syntax known as post-colon data typing, and it's a critical part of
03:10the ActionScript 3 syntax.
03:11I am going to set this variable to a data type of String, and then I will set
03:18its value using an equals operator and a literal String.
03:22This variable will only persist for the time that it takes to execute this function.
03:26When the function is done executing, it will go out of memory.
03:30If you want to declare a variable that persists, declare it before any
03:34functions, but after import statements.
03:36I will place the cursor after the import statement and declare another variable.
03:40I will call this one myPublicVar, and once again, data type it as a String, and
03:51then assign a literal String.
03:53This variable will be available to all the code throughout the MXML document.
03:58Now, when I Save the changes, I will see a little warning icon appear next to
04:02that line of code, and I see the message, var 'myPublicVar' will be scoped to
04:07the default namespace: UsingVariables:
04:10internal.
04:11It will not be visible outside of this package.
04:13Here's what that message is talking about.
04:16Whenever you declare a variable that's outside of the function, it's known as an
04:20instance variable, that is a variable that is a member of the current instance
04:25of this application, or when you get into writing custom components, the current
04:29instance of the component.
04:30These types of variables should have access modifiers applied.
04:34There are four access modifiers in ActionScript 3:
04:37Public, Protected, Private, and Internal.
04:41If you don't declare one, the variable is automatically Internal, meaning that
04:45it can be addressed by the code in this component and also any other components
04:50in the same package or folder.
04:52More commonly, when you declare a variable in an application outside of
04:56functions, you use the keyword or access modifier Protected.
05:00I will type in the word 'protected' and, once again, Save my changes, and you
05:04will see the warning go away.
05:06Protected means that the variable is available to this application and also to
05:11any components that extend or inherit capabilities from this application.
05:16So that's Protected and Internal.
05:18The other two access modifiers are Public, which means that a variable is
05:21available to the entire application and to the outside world, and Private,
05:26which means that the variable is only addressable by code in this application or component.
05:31So those are the three ways that we declare variables in Flex applications,
05:35using MXML code, and it's worth mentioning that when you declare variables in
05:39this way, that they are always Public to the entire application, as variables
05:43that are local to a function and as variables that are declared outside of
05:47functions and are therefore controlled by their access modifiers.
Collapse this transcript
Understanding ActionScript data types
00:00Most variables that you declare in flex applications have associated data types.
00:05In this demonstration, I am going to show you how to use various data types, how
00:10to do calculations with some of the most commonly used data types, and then how
00:14to convert those data types into Strings for display on the screen.
00:17I will be working in the application file, UsingDatatypes.mxml.
00:22I will start with the String data type.
00:25As I have shown in other videos, when you declare a variable in ActionScript
00:293, you start with the variable name, and then after a colon, you declare the
00:33type of the variable.
00:35The variable name follows the naming conventions of all identifiers in ActionScript 3.
00:41It consists of letters, numbers, and underscores, and starts with either an
00:45underscore or an alphabetical letter.
00:48By very strong convention, variables that are Protected, Public, or Internal
00:52start with a lowercase initial character.
00:54Variables that are Private are frequently named with an underscore as
00:58their first character.
00:59I am going to declare variables in each of the functions that are already a
01:03part of this application, one for each of four commonly used data types in ActionScript 3:
01:09Strings, Numbers, Booleans, and Dates, and I will start in the function for the String class.
01:15I am going to use a Label that's been placed at the bottom of this application -
01:21that has an ID of out - to display results for each of the functions.
01:26Going back to the functions, stringBtn_ clickHandler, I will add the following code:
01:31var strTemp:
01:34String equals, and then I am going to show you how to take two Strings and push
01:39them together in an operation known as concatenation.
01:43The plus character is used as the concatenation operator in ActionScript.
01:47So, I will start off with a simple string of Hello, and then I will add a plus
01:52operator, and then I will wrap up the expression with another literal string of World.
01:59And then I will display that value using the following code, out.text = strTemp.
02:07I will Save and run the application, and then click the Show String button.
02:14And there's the result, the String Hello World is displayed in the out Label.
02:20One other thing about Strings that's worth mentioning, in ActionScript 3 you can
02:23use either double quotes or single quotes.
02:26If you prefer, you could format your code like this.
02:29I will replace the double quotes for both literal strings with single quotes,
02:35and the code will work exactly the same as it did before.
02:39You can interchange double quotes and single quotes in ActionScript 3.
02:42As long as you match them up correctly, everything should work fine.
02:46Now, I am going to show you how to use numbers.
02:49There are actually three data types for numbers in ActionScript 3:
02:53the Number class and the primitive types int for Integer and uint for Unsigned Integer.
02:59I am going to focus here on the Number class.
03:01I will declare a variable called numTemp inside the numberBtn_clickHandler
03:07function, and data type it as a Number.
03:12Now, for this expression, I am going to take two numbers and add them together,
03:17typing in the values 100 + 200.
03:21Notice that the plus operator is the same character as was used in
03:24string concatenation.
03:26We say that the plus character is an overloaded operator, meaning that it has
03:31more than one purpose.
03:32In any expression that includes at least one literal String, the plus operator
03:37is used for concatenation, but in an expression that only has literal numbers
03:42or variables that are data types as Numbers, the plus operator does
03:45mathematical addition.
03:47Now, I am going to display the result in my out label, like this.
03:51I will type out.text = numTemp;.
03:56Now, watch what happens when I Save the changes to my code.
04:00I see an error in the code.
04:02It's represented both here on the line of code and also down here in the Problems view.
04:07I will open up the Errors list and see the message Implicit coercion of a value
04:13of type Number to an unrelated type String.
04:16Well, that tells me that the Number class is not automatically convertible to a
04:20String in ActionScript 3.
04:23There are a couple of ways of handling this, but the simplest way to turn
04:26this strongly typed value, a Number, into a String, is to use the Number
04:31class' toString method.
04:33The toString method is implemented on every ActionScript class.
04:37It doesn't always give you a useful output, but it does, in the case of
04:41these basic data types.
04:43So I will assign the value of numTemp to the text property of the Label using
04:48the expression, numTemp.toString.
04:53Notice that there is an optional argument in the toString method.
04:56That's shown in the Bubble help that's shown on the screen right now.
04:59I am not going to use that optional argument.
05:01I will just put in the closing parenthesis.
05:03I will Save my change and the error goes away.
05:06I will run the application.
05:09I will click the Show Number button, and I see the result of the calculation.
05:15Now I will add two more variables, one for a Boolean value that can have a value
05:20of true or false, and one for a Date.
05:23For the Boolean demonstration, I will declare a variable called boolTemp, and I
05:28will data type it as Boolean.
05:31This time, I won't assign an initial value.
05:34The reason is because I want to show you that every variable has a default value.
05:39In the case of the Boolean data type, the default value is false.
05:44And then I will use the syntax to output the value, out.text
05:48= boolTemp.toString.
05:52Now, for the Date class, I am going to create a Date typed variable using a
05:56different kind of syntax.
05:59I will say var dateTemp:Date = new Date ();
06:07Of all of the simple data types in ActionScript 3, the Date class is the most complex.
06:13You don't simply assign a value.
06:15Instead, you create an instance of the Date class, calling what's known as
06:19its constructor method.
06:21To call a class' constructor method, you start with the keyword 'new,' and then
06:25you call a method that has the same name as the class itself, and you use opening
06:30and closing parentheses at the end.
06:32Many constructor methods allow you to pass in initialization values.
06:36For instance, the Date class constructor method allows you to pass in Year,
06:42Month, Date, and so on, but if you don't pass those values in, as I am not doing
06:46here, the new instance of the Date class will represent the current date and
06:51time on the user's client computer.
06:54Now, I will output the value of dateTemp as a String using the
06:58expression dateTemp.toString.
07:01You see the pattern here.
07:03For any non-string variable, if you need to cast or convert it to a String, you
07:08use the toString method.
07:09I will, once again, run the application in the browser, and now I will be able
07:14to test all four data types.
07:17Here is the result of the String, Hello World, the Number, a calculated value,
07:23the Boolean, a default value, and the Date, the current date and time.
07:29So that's a look at how to use various data types in ActionScript 3.
07:32You will see plenty of other examples throughout the rest of this video
07:36series, but having an understanding of what the data types are and how to set
07:41variables as particular data types is incredibly valuable as you build your
07:45Flex applications.
Collapse this transcript
Getting language and API help
00:00Flash Builder 4 is delivered with a complete online help system called The Adobe
00:05Community Help Application.
00:07This is a new application that's based on Adobe AIR, that's included not just
00:11with Flash Builder 4, but is also bundled with all of the new Creative Suite
00:155, or CS5, products.
00:17The purpose of the Community Help Application is to give you access to all of
00:21the online help provided by Adobe, but also to help and other supporting
00:26documentation that's been created by the very wide community of Flex and other
00:30application developers.
00:32To open up the Community Help Application, go to the Flash Builder menu and
00:36choose Help > Flash Builder Help.
00:38The first time you open this application, you'll probably see some dialog boxes
00:43pop up that prompt you to download offline help.
00:46If you click the appropriate buttons on those online help dialog boxes, it will
00:50result in downloading the current version of the help for Flash Builder 4.
00:54And then later on, if you are working in Flash Builder, and you are not
00:58connected to the Internet, you'll still have access to the API help documents.
01:02I am going to give you a little tour of this new Adobe Community Help Application.
01:07First of all, there are a number of Preferences that you can set.
01:10To set the Preferences, go to the menu and choose Edit > Preferences.
01:14In the General Settings, you can indicate whether you want use the Dark or the Light theme.
01:18I'll choose the Dark theme and click Done to show you the difference, and you'll
01:22see a different look and feel for the application.
01:26I'll go back to the Preferences and switch back to the Light theme.
01:30I'll go back to the Preferences dialog again and show you that if you prefer,
01:34you can open the help system in a Web Browser.
01:37Once you select this option, you will no longer be able to open up the Community
01:41Help Application directly from within Flash Builder.
01:44As indicated in this note, you can actually reopen the Community Help
01:48Application again from your hard disk.
01:50If you are working on Mac, you'll find it under the Applications folder, and if
01:55you are working on Windows, you'll find it under the Program Files folder.
01:59Next are the Download Preferences.
02:01This is where you can indicate what content you want to download and save
02:04locally on your system.
02:06Whenever the content changes online, you'll be prompted, again, to re-download
02:10and keep your local content up-to-date.
02:13There are also categories for Updater Settings and for Local Content.
02:17When you are inside the Community Help Application, you can open up the Action
02:21Script 3.0 Reference.
02:23And from there, you can click on any of the links in the top-right corner.
02:27Notice this message.
02:29This is the ActionScript 3 Reference for the Flash Platform.
02:32You can also get links for various specific implementations of Flash or other
02:37tools from the links on the screen.
02:39You can get to the top-level Table of Contents for the entire ActionScript 3
02:43Reference by clicking on this link.
02:46Up at the top of the screen, you'll find a link that takes you to the equivalent
02:50page on the Adobe website.
02:52And if you click that link, it will open the same page in a Web Browser.
02:56So, if you prefer working in a Web Browser interface, that's an easy way to get
03:00there without disabling the Help Application itself.
03:03Now I'm going to keep this application open and just switch back to Flash Builder.
03:08The reason I keep the application open in the background is because I frequently
03:12look information up in the API Documentation as I am coding.
03:16There are so many classes and so many styles, properties, events and so on, that
03:21nobody can keep it all in their head.
03:22Knowing how to find information quickly in the API Documentation is critical to
03:27being productive in Flex.
03:30Let's say, for example, that I want to look up the Documentation for the
03:33Application Component.
03:34I'll place the cursor inside that tag and then press the keyboard shortcut Shift+F2.
03:40This is the same keyboard shortcut for both Windows and for Mac.
03:45And the result will be that I come back to the Community Help Application, and
03:49the Documentation for that component is displayed automatically.
03:53I'll switch back to Flash Builder again, and this time, I'm going to click into
03:58a data type that's declared in ActionScript code.
04:00I'll look up the Documentation for the String class.
04:03Once again, I click into the name of the class and press Shift+F2.
04:08The Help Application comes to the foreground, and I am taken to
04:11that Documentation.
04:13So, this is how you can quickly find information for any ActionScript class or
04:18for any MXML tag that represents an ActionScript class.
04:21You can also use the Search capabilities of the Community Help Application.
04:25Now this takes a little bit of fine-tuning.
04:27Let's say, for example, that you wanted to find information for the
04:30Application Component.
04:32You might type in the word 'Application' and then press Enter or Return, and
04:36you'd find that you don't get to the API Documentation.
04:39Instead, you're taken to a whole series of Documentation Sections and Articles
04:45having to do with Building Flex Applications in general.
04:48If you want to get to the API Help for a particular class, you have to type in
04:52the entire package and class name.
04:55For example, I'll type in spark.components.Application and then, once again,
04:59I'll press Enter or Return,
05:02and now I'm taken to the API Documentation for that Component.
05:06This is a little bit clunky, because the reason I look at the API Documentation
05:10sometimes is to find out what the package is.
05:12And so this is why the context-sensitive Help within Flash Builder is so useful.
05:17If you know how to get to the context- sensitive Help, by simply placing the
05:21cursor and then pressing the keyboard shortcut, Shift+F2, you'll be able to
05:25get all the API Documentation you need, and it will be right there online on
05:30your client's system.
Collapse this transcript
4. Using Flash Builder Debugging Tools
Using the debug version of Flash Player
00:00Flash Builder 4 includes many valuable tools for debugging and fixing your
00:05applications during the development process.
00:08In order to use these tools, you must have the Debug version of Flash Player.
00:12This version is installed automatically when you install Flash Builder 4, but
00:16there are many circumstances where you might find that it's overwritten by other
00:20installations, or you need to update your copy of Flash Player.
00:23Here are some of the ways you can get the Debug version of Flash Player.
00:28First of all, when you install Flash Builder 4, the installation folder includes
00:32a subfolder called player.
00:35This will be the case both on Windows and on Mac.
00:38On Windows, the default location is under C:Program Files/Adobe/Adobe Flash
00:44Builder 4, while on Mac OS X, it's under its Applications/Adobe Flash Builder 4.
00:51From there, you can go down to the player folder, and within that folder, you'll
00:55find a folder for your Operating System.
00:58I'm working on Windows 7, so I have a folder called win.
01:02Under that folder, I find three executable installers, one for the ActiveX
01:07version of Flash Player - that's the one that works under Internet Explorer, and
01:11one called Plugin - that's the version that works under Netscape, Firefox and
01:16other plug-in compatible browsers.
01:19There is also an application called FlashPlayer.exe.
01:23This is called the standalone version of Flash Player, and you can
01:26double-click it to open it.
01:28And from there, you can select File > Open and open any SWF file to play it.
01:34So, that's how you can reinstall Flash Player on your system if you need to.
01:39It's also very important that before you use any of these installers that you
01:43uninstall the existing versions of Flash Player.
01:46On Windows, you can do that from the Control panel.
01:49On both Windows 7 and Windows Vista, just go to Uninstall a program, and on
01:55Windows XP, choose Add or Remove Programs, and you'll find the various versions
02:00of Flash Player there.
02:02If you want to uninstall Flash Player on Mac, you can download the appropriate
02:06application from the Adobe website.
02:08Go to www.adobe.com/go/14157.
02:17That will take you to a knowledge-base article that includes links that you can
02:21use to download uninstallers for Mac and other operating systems.
02:26Now if you need a more recent version of Flash Player, you can get it from the Adobe website.
02:31The most common way to download Flash Player from Adobe is to go to this URL,
02:36get.adobe.com/flashplayer, but this is not the version that you need.
02:43This version is the Non-Debug, or the standard version of Flash Player.
02:48In order to get the Debug version of Flash Player, go to the Adobe website and
02:52click into the Search dialog and type in 'debug flash player.' The first item
02:59that shows up in my Search is labeled Adobe Flash Player - Downloads.
03:04I'll click that link, and I am taken to this page
03:07www.adobe.com/support/ flashplayer/downloads.html.
03:16On this page, there are many hyperlinks to download various versions of the
03:20Flash Player, not just the most recent version.
03:23As of the date of this recording, you'll find installers going back to Flash Player 8.
03:28And whenever Flash Player 10.1, the newest version of Flash Player, is released,
03:33which will be after Flash Builder itself is released, a link for that version
03:37of Flash Player will appear here as well.
03:39At the top of the page, you'll find links to the most recent version of
03:42Flash Player, the Non-Debug version, but if you scroll further down you'll
03:46find the Debugger version.
03:48Now again, if you want to ensure 100% compatibility with your applications, just
03:53install the Debugger version of Flash Player that's included with your Flash
03:57Builder installation.
03:58Use these links if you need to go back to previous versions or you need to
04:02update to a newer version.
04:04Finally, I'll show you how to find out whether you have the Debug version of
04:08Flash Player installed.
04:10The easiest way is to go to any Flash-enabled website.
04:14I'll go to the Adobe website's homepage, which always has a bit of Flash content.
04:20To find out whether I have the Debug version of Flash Player, I'll move the
04:24cursor over any area of the Flash content, and then I'll right-click, or
04:29Ctrl+Click on Mac.
04:31If you're running the Debug version of Flash Player, you'll see a menu
04:35choice labeled Debugger.
04:37It doesn't matter whether it's enabled or not.
04:39If the menu choice appears, then you are running the Debug version of Flash Player.
04:44If you want to find out exactly which version of Flash Player you have
04:47installed, choose the About menu choice.
04:51That will take you to a webpage on the Adobe website, and it will show you which
04:56version of Flash Player you have installed.
04:58I have version 10.0.42.34.
05:03The first number is called the Major version, the second, the Minor version, the
05:07third is the Update, and the last is the Maintenance Number.
05:12So that's how you can find out which version of Flash Player you have installed,
05:16and also whether it's the Debug version.
05:18Again, you must have the Debugger version of the player installed in order to
05:22use the Debug tools that are included with Flash Builder 4.
Collapse this transcript
Starting and stopping debugging sessions
00:00Once you've confirmed that you have the debug version of Flash Player installed,
00:04you can then use the debugging tools that are a part of Flash Builder 4.
00:08For the demonstrations in this chapter, I'll use the set of Flex applications
00:12that are included with the Exercise Files.
00:15If you have access to the Exercise Files, you can import them now.
00:19Go to the menu and choose File > Import Flex Project.
00:23In the Import dialog box, Browse and navigate to the Exercise Files folder.
00:29I've placed it on my Desktop.
00:31From there, go to 04_Debugging, and from there, to the begin folder.
00:36Locate, in the Import, a file called Debugging.fxp.
00:40I'll choose the project archive and then click Finish to complete the Import.
00:46I'll open up the package in the Package Explorer view.
00:50Open up the source folder, the default package, and I'll start with the
00:55file debugging.mxml.
00:59I'm going to start by showing you the Flash Debug Perspective.
01:03This is the definition of a Perspective, a particular arrangement of views and
01:07editors that's included with Flash Builder 4.
01:10To use the Flash Debug Perspective, go to the menu and choose Window >
01:15Perspective > Flash Debug.
01:18Or if you've already been using Perspectives, you might find a button for Flash
01:22Debug at the top-right corner of your interface labeled Flash Debug.
01:27When you use the Flash Debug Perspective, you'll see a number of views that you
01:30haven't used before.
01:32In the top-left corner is the Debug view.
01:35The Debug view includes tools on the toolbar that will let you
01:38Terminate debugging sessions,
01:40Resume existing debugging sessions, and Step Into code.
01:45I'll show you those in other videos.
01:47In the top right corner, you'll find the Variables view, the Breakpoints view
01:51and the Expressions view.
01:53At the bottom, you'll find the Console view, the Data/Services view and the
01:58Network Monitor view.
02:00Of these, the only one that's not really used for debugging is the Data Services view.
02:05I'll be talking about this view at the end of this video course, when we talk
02:08about working with application servers.
02:11For now, I'm going to remove it from this Perspective, by just clicking the X icon on the tab.
02:17The Console view is used to output debugging messages.
02:21When you run an application in Debug mode, you'll see messages appear in the bottom.
02:25My Console is displaying debugging messages already, because I've been running
02:29debugging sessions in the past.
02:31To start a debugging session, open an application and then go to the menu
02:35and choose Run and then Debug, and the name of the application that you
02:40currently have open.
02:41You'll also see that there is a keyboard shortcut available.
02:44It's F11 on Windows and Command+F11 on Mac.
02:49You can also start a debugging session from the toolbar by clicking on the
02:52button with a picture of a bug on it, or if you have multiple applications in
02:57your project, you can pull down the menu and choose an application that you want to debug.
03:01I'll run an application in Debug mode by clicking the button on the toolbar.
03:08Now once you're in the browser, you really can't tell that you're in a debugging session.
03:12The URL, for example, looks exactly the same, and the application looks the same.
03:17But now, I'm going to switch back to Flash Builder by pressing Alt+Tab on
03:21Windows, or Command+Tab on Mac.
03:24If I now look at the Debug view, I'll see that most of the buttons on the
03:28toolbar have come to life:
03:30the Suspend button, which temporarily causes the application to suspend its
03:34operation, the Resume button, which causes the application to resume, Stepping
03:40buttons, which are disabled, because I don't have any code to Step Into yet, and
03:44other buttons that let you navigate through the application.
03:47Here is a very important button that I want to point out right away,
03:50the Terminate button.
03:51When you run an application in Debug mode, it's called running a debugging session.
03:56The Terminate button allows you to stop the debugging session and go back to
04:00more of a normal Flash Builder editing mode.
04:02The Terminate button is displayed in two places in your user interface, both in
04:07the Debug view where my cursor is pointing now and also in the Consol view, where
04:12it shows up also enabled.
04:14You can click either of these buttons to terminate the debugging session.
04:18Now when I click the button, watch what happens to my browser.
04:22I'm going to arrange my Windows so you can see both on the screen at the same time.
04:27I'll use one of my favorite new Windows 7 techniques.
04:30I'll click and drag the browser all the way over to the right corner and then
04:34drop it, and it will snap into place, taking up one half of the interface.
04:38Then I'll go to Flash Builder interface and do the same thing, dragging it to the left.
04:45Now they are each taking up half of the screen.
04:47That's a Windows 7 trick
04:48you won't have it available on Vista or Windows XP, or on Mac for that matter,
04:52but either way, arrange your windows so you can see both applications.
04:57Now I'm going to terminate the debugging session by clicking the button.
05:01If you're using Windows and Firefox as I am, when you click the Terminate
05:05button, you'll see that the browser closes.
05:08That doesn't happen on all operating systems in all browsers.
05:12But even if it doesn't close the browser, it does terminate the connection
05:16between Flash Builder and the browser.
05:18Now I'm going to run the application in Debug mode again.
05:21I'll choose Run > Debug, and that opens the browser again.
05:27I'm going to make sure that I only have this one tab open.
05:32I'll click and once again, drag over, and snap into place.
05:37Now this time, I'm going to close the browser, and watch what happens to all the
05:41buttons on the Debug view.
05:43When I close the browser, that, once again, terminates the connection between
05:47the browser and Flash Builder, and again, terminates the debugging session.
05:51So, when I say terminate the debugging session, you can either go to Flash
05:56Builder and Terminate, or you can simply close the browser.
06:00If you're working on an Adobe AIR desktop application, debugging works
06:04exactly the same way.
06:06If you terminate from Flash Builder, you'll end up closing the application.
06:10If you close application, you'll terminate the debugging session in Flash Builder.
06:15So those are the basics about navigating the Debug Perspective and starting and
06:19stopping a debug session.
Collapse this transcript
Tracing application execution
00:00One of the simplest and most valuable tools you have available in Flash Builder
00:04and Flash Player is the trace function.
00:07The trace function allows you to send a debugging message to Flash Builder's
00:11Console view at run time.
00:13You can use the trace function to output values of variables and to tell you
00:17when particular bits of code execute, so you can trace the flow of execution in a
00:22Flex application at run time.
00:24For this demonstration, I'll use the application traceDemo.mxml, which is
00:29included in the debugging project of the Exercise Files.
00:33If you have access to the Exercise Files, open the application now.
00:38In this application, I'm listening for an event called creationComplete.
00:43I've described this event previously.
00:45It's dispatched by the application to tell you when the application has opened
00:50up in Flash Player and is completely available for use.
00:53Inside the method, I'm setting the text property of a label control to the value
00:58of a variable strHeadline.
01:01Now I'm going to show you how to add a trace command to simply tell you when
01:05the event is occurring.
01:08I'll place the cursor after the bit of code inside the function, and I'll call trace.
01:14The trace function is called a top -level function in Flash Player.
01:18That means that it's built into Flash Player, and you don't need to reference an
01:22object with dot notation.
01:24For example, I don't have to say Player.trace or anything like that.
01:29I simply call the trace function.
01:31When you call the trace function, you can then pass in a simple string or a
01:35concatenation, or combination of values that result in string.
01:39I'm going to pass in a literal string of creation complete event.
01:46At the end of the call to the trace function, I'll put in the semicolon to
01:49terminate the statement.
01:51Then I'll save and run the application, being sure to run it in Debug mode.
01:57Before I run the application, though, I'll make sure that I can see all of my
02:00views surrounding the Editor region.
02:04I'll click the Debug button on the toolbar, and if prompted to save.
02:08I'll click OK, and then the application runs in the browser.
02:14In the background, because of the way I have my windows arranged, I can see
02:18the Console view appears in Flash Builder and my trace message appears,
02:22creation complete event.
02:24So, the trace method results in sending this message over to the Console view so
02:29I can find out what happened.
02:31Now I'm going to terminate the debugging session and show you another use of
02:34the trace function.
02:35I'll go to the button with the label of Trace Me.
02:39I'll place the cursor at the end of all of the attributes but before the
02:42closing slash character.
02:44I'll press Spacebar, and then from the list of available attributes, I'll type
02:49in 'cl,' and I'll choose the click event, moving the cursor to it and then
02:54pressing Enter, or Return.
02:57Then I'll press Enter, or Return, one more time to generate the function that will
03:01be called when the click event happens,
03:03that is when the user clicks the button.
03:05Here is the generated function.
03:08It's named automatically button1_clickHandler.
03:13Now I'll select and delete the automatically-generated comment code and replace
03:18it with another call to the trace function that looks like this.
03:21Trace and then I'll type in a literal string of The value of strHeadline is.
03:29I'll make sure I put an extra space at the end and then close the string
03:33with a double quote.
03:35Then I'll put in the Plus operator, which will be in this context, the
03:38concatenation operator.
03:41Then I'll output the value of the variable strlHeadline.
03:46Notice that the entire expression, that is the literal string and a variable
03:51combined with the Plus operator,
03:53are wrapped inside parentheses.
03:54I'll once again, save my file.
03:57This time, I'll use the keyboard shortcut, Ctrl+S on Windows, or Command+S on Mac.
04:02I'll once again, Debug the application.
04:05Now watch what happens in the Console view, in the background, whenever I click
04:09the button labeled Trace Me.
04:12Every time I click, the value that I pass into that trace function is output
04:16in the Console view.
04:17The trace function gives you a way of finding out not only when certain code is
04:21being executed, but also easily outputting the values of various variables.
04:26The one restriction about it is that any value that you pass into the trace function
04:30must be serializable, or convertible to a string.
04:33I'll show you one more example.
04:35I'll do this work, once again, in the to clickHandler function of the button.
04:40I'll place the cursor after the first trace function, and then I'm going to
04:44declare a variable data typed as an array.
04:46This is a more complex type of variable than we've seen before.
04:50An array is an ordered collection of values.
04:54I'll create a variable with the keyword var, and I'll name the
04:57variable (stooges:Array).
05:04Here is a bit of shorthand that lets you create an array very simply and easily.
05:08I'll start with an opening square bracket, and then I'll type in a series of
05:12values separated with commas:
05:14"Moe", "Larry", and "Curley".
05:22Then I'll output the value of that array by using the trace function.
05:26I'll type in trace(stooges).
05:30Now an array is a complex object.
05:32It's not a string, but if the values in the array are simple values, the entire
05:37array can be serialized, or converted to a string automatically by the
05:41ActionScript Virtual Machine.
05:44I'll save my changes and run the application in Debug mode.
05:50I'll check my Console to make sure that my initial trace message is being
05:54displayed, and then I'll click Trace Me, and here is the result.
05:58In the Console, I get The value of strHeadline is Hansel & Petal.
06:04I get this serialized version of the array, Moe, Larry and Curley separated with commas.
06:09So when you use the trace function, here is the rule.
06:12You can either pass in a literal string, a variable that can be turned into a
06:15string, or any other complex object that can be turned into a string.
06:20Either way, it will be automatically shown in the Console view.
06:24There are ways of targeting the output of the trace function to other debug or
06:28what are called logging targets.
06:30This is an advanced technique, but, for example, in an Adobe AIR application
06:35where you have access to the local hard disk, it's possible to redirect the
06:39output of the trace function to a text file on the disk.
06:43It's even possible with a couple of little configuration changes to make the
06:47output of trace appear in the text file that's created automatically by Flash Player.
06:51Those are advanced techniques, though.
06:53If you're just getting started with Flex, this is the easiest way to use the
06:57trace function, outputting strings to the Console view.
Collapse this transcript
Using breakpoints
00:00Flash Builder 4 and the debug version of Flash Player have the ability to use Breakpoints.
00:06Breakpoints give you a way of suspending the application's execution
00:09temporarily, so that you can examine its internal state, including looking at
00:14values of variables, view states, and so on.
00:18For this demonstration, I'll use the application Breakpoints.mxml that's
00:22included with the Debugging project of the Exercise Files.
00:26I'll open the application and expand it to fullscreen by double-clicking its tab.
00:31In this application, I have a button with a label of Debug, and when the user
00:36clicks the button, it executes this function, which sets a simple variable data
00:41typed as an 'int,' or integer.
00:46Now there are a few different ways of setting a debug Breakpoint in Flash Builder.
00:51One way is to go to the line where you want to freeze or suspend the application.
00:55And then double-click to the left of the line number - that shows a little round
01:00icon - and that's the Breakpoint.
01:02When you double-click again, it takes it away.
01:05You can also right-click on the same location and choose Toggle Breakpoint.
01:10Right-click again, Toggle it again, and that takes it away.
01:13There is also a keyboard shortcut, Ctrl+Shift+B on Windows, or Command+Shift+B
01:20on Mac, and that does, yet again, the same thing, turning the Breakpoint on and off.
01:25Now, you can't set a Breakpoint just anywhere.
01:28The rule is fairly simple though.
01:30You can set a Breakpoint on any line that includes ActionScript code.
01:34For example, this line includes a declaration of a variable, so that would be okay.
01:39You can also set Breakpoints on the last line of a function.
01:43That's the line that includes the Closing Brace character.
01:47But you can't set Breakpoints on lines that only have an MXML code but no ActionScript.
01:52So for example, if I try to set a Breakpoint here on line 19, where I only have
01:57the beginning tag and a text property, while Flash Builder shows the Breakpoint
02:01icon, it won't succeed in freezing the application at Debug time.
02:07Now to show you how to use your first Breakpoint, double-click on the line that
02:12includes the variable declaration, and you should see the little bubble that
02:15shows the Breakpoint up here.
02:17Then before you run the application, make sure that your editor is restored, so
02:22you can see all the other views around it.
02:24That'll help you get into the debugging perspective most easily.
02:28Also, to reproduce what I'm about to show you, make sure you're in the Flash
02:32perspective, not the Flash Debug perspective.
02:35Then Run the application in Debug mode.
02:39The application opens up in the browser and shows the Label and the Debug button.
02:44Now, when I click the Debug button, that will cause the ActionScript Virtual
02:48Machine to hit the line of code where I've placed the Breakpoint.
02:51I'll click the button and over in Flash Builder, I'll see a dialog box appear.
02:57If it doesn't actually take focus, just switch over to Flash Builder.
03:02I've just clicked on the dialog box in the background to bring the focus forward.
03:06Here is the message you're getting.
03:08Flash Builder is telling you that the kind of launch you're doing, specifically
03:12a Breakpoint execution, should open the Flash Debug perspective, and it asks you
03:17if you want to do that right away.
03:19If you always want to go to the Debug perspective when you hit a Breakpoint,
03:23click on this option, Remember my decision.
03:26I'm going to leave the option unchecked so I can show you, in future videos, how I
03:30get back to the debugging perspective during a Breakpoint, but I'll click Yes
03:34for now, and that will take me to the Debug perspective.
03:38So, in the Editor view, which is now in the vertical middle of the interface, I
03:43now see a highlight bar, showing me which line of code is about to be executed.
03:48To step through the code, go to the Debug view, and you can now use one of
03:53these three buttons:
03:55Step Into, Step Over, and Step Return.
04:00If I click the Step Over button, that will execute that line of code and go to the next line.
04:05In a more extensive function, if I've already done all the inspection work that
04:09I want to, and I want to continue running the application, all I need to do is
04:14click the Resume button, or press the associated keyboard shortcut.
04:18When you click Resume, it won't automatically take you back to the browser.
04:22You have to do that part yourself.
04:24I'll press Alt+Tab on Windows, or Command+Tab on Mac.
04:28Go back to the browser, and I'll see that the browser is live.
04:31Now, I'll click Debug again to execute the Breakpoint again, because I want to
04:35show you what happens to the browser while you have the application suspended.
04:39Notice that in the browser, if I try to click the button, that brings focus to
04:44Flash Player and to the browser, but it can cause a lot of problems, such as
04:48freezing Flash Player and disabling the browser.
04:51In this case, when I double-clicked, that caused Flash Player some confusion.
04:56I've got my browser in a bad state at this point.
04:59So, to terminate the debugging session, I'll come back to Flash Builder and
05:03click the Terminate button.
05:05That closes the browser, and I can start over again.
05:09Here is another little trick you can use, once you have gotten into breakpoints.
05:12If you want to start up an application again, you can either go through the same
05:16steps as before - going to the Editor view for the application and running the
05:21Debug session from the menu or from the toolbar, or you can go to the Debug view
05:27and locate a terminated debugging session.
05:30Then right-click on the debugging session, or Control+Click on Mac, and choose
05:35Relaunch, and that will cause the application to run again from the beginning.
05:41So that's a look at how to use basic Breakpoints.
05:44Here is one other trick that you can use.
05:47The Breakpoints view, which is shown in the tabbed interface in the upper
05:51right-hand corner of the Flash Debug perspective, has a listing of all of the
05:55Breakpoints they you have set.
05:57You can right-click on any Breakpoint, or Ctrl+Click on Mac, and choose a
06:02Breakpoint Properties.
06:04In Flash Builder 4, there are some new features having to do with Breakpoints.
06:08One of the most valuable is the ability to set conditions for a Breakpoint.
06:12For example, if I only want to respect a Breakpoint, that is suspend the
06:17application when a particular condition is true, I can do it like this.
06:22I can type in an ActionScript expression, i==1, and as a result, that Breakpoint
06:28will only be respected when those two values match. I'll click OK.
06:35Notice in the Breakpoints view, that the Breakpoint is now shown as being been conditional.
06:40I'll Run the application and click the button.
06:47You'll see that the Breakpoint is not respected.
06:49The reason is because the Breakpoint has been placed at the beginning of the
06:53statement, rather than after it.
06:56The variable value does not yet have a value of 1.
06:59So, if I want the Breakpoint to be respected, I would change the condition from
07:03i=1 to i=0, because that is the default value of an integer.
07:10So, conditional Breakpoints are a new feature of Flash Builder 4, and
07:13Breakpoints in general are incredibly valuable for freezing the application at
07:17run time and seeing what's going on inside the application, including checking
07:22variable values and checking view states and anything else having to do with the
07:27application's internal state.
Collapse this transcript
Using the Variables view
00:00When you use debugging sessions and breakpoints, it gives you an opportunity to
00:04look at the state of internal values, or variables, inside a Flex application.
00:09For this demonstration, I'll use the application Variables.mxml, which is
00:14included in the debugging project in the exercise files.
00:18I have the file open on the screen already.
00:21Notice, in this version of the application, that there is a function that gets
00:25executed when the user clicks a button.
00:28Within the function, it declares a variable called myVar as an integer, and sets
00:33its initial value to 1.
00:35I'll set a breakpoint on the line that declares and sets the variable's value.
00:40Then I'll return the Editor to its original size and run the application in Debug mode.
00:48I'll click the Debug button, and that causes me to come back to Flash Builder,
00:53and I'll click Yes to go into the Flash Debug perspective.
00:58In the top-right corner of Flash Builder, in the Tabs section, you'll find a
01:02view called Variables.
01:04This view has a tree organization that lets you examine the values of variables
01:09and of properties of all the objects in your Flex application at the moment
01:14where the application is suspended at the breakpoint.
01:17Now, I'm going to expand the size of the Variables view to fullscreen.
01:21I'll double-click on its tab, or you can click once on the tab, and then press
01:26Ctrl+M on Windows, or Command+M on Mac.
01:30Notice that the variable value is initially set as undefined.
01:35If I look at the code that's being executed and the position of the breakpoint,
01:39I'll find something called the Current Instruction Pointer.
01:42This tells you where the debug breakpoint is currently suspended.
01:46Because I've suspended the application before that line of code is executed, the
01:51value of the variable int is undefined, rather than a value of 1.
01:55Now I'll rearrange everything so I can see both the Variables view and the code.
02:01Then I'll go to the Debug view, and I'll click the Step Over button.
02:05When I click that button, the line of code is executed, and the value of the
02:09variable is changed to 1.
02:11Now, I'll add a little bit of code, so we can see the use of a breakpoint and
02:15the Variables view in a little more useful way.
02:19I'll terminate the debugging session.
02:22Then I'll double-click the Editor tab to expand it to fullscreen, and I'll add
02:27some new code into the function.
02:29After the initial setting of the variable, I'll add a few lines of code that
02:33each increment the value of the variable by 1.
02:35I'll use this shorthand expression, myVar ++.
02:41Now, I'll clone that line of code, like this.
02:44On Windows, I'll hold down Ctrl+Alt, on Mac Command+Option, and then press
02:51the down arrow key.
02:53Each time you do that, you'll be cloning the current line of code.
02:57You can also clone upwards by holding down the same keys, Ctrl+Alt on Windows or
03:03Command+Option on Mac, and pressing the Up arrow.
03:06Once again, I've set my initial breakpoint right before the first line of code is executed.
03:12I'll save and once again, run the application in Debug mode.
03:22I'll click the Debug button, and that takes me, once again, into the breakpoint.
03:27I'll make sure that I can see all of my views.
03:30I'll click into the Editor view and scroll down a bit, so I can see where the
03:34Current Instruction Pointer is.
03:37Then, once again, I'll click the Step Over button a few times.
03:40Watch what happens in the Variables view when the myVar variable value changes.
03:46Each time I click, the value changes from undefined to 1, to 2, to 3, to 4.
03:55Then when I'm done executing the application, I can click the Resume button.
03:59Now, I'll show you one other very important thing to know about the nature
04:02of the Variables view.
04:04I'll terminate the debugging session and return to the Flash perspective.
04:10I'm going to declare an additional variable, this time outside of the
04:13clickHandler function, and it's going to be a complex variable.
04:16I'll type in the word 'Protected' as the access modifier, then var, then, once
04:22again, I'll create my stooges array, which I used in a previous exercise.
04:30Now, once again, I'm going to run the application in Debug mode and then click
04:35the Applications button to trigger the breakpoint.
04:40I'll go to the Flash Debug perspective and show you that the variable stooges,
04:44which I declared outside of the functions, is not displayed in the default
04:48presentation of the Variables view.
04:51To see it, I'm going to expand the Variables view to fullscreen by
04:54double-clicking its tab, and then I'll go to the item labeled "this".
04:59In the context of a Flex main application file, the expression "this" refers to
05:04the application itself.
05:06When you declare a variable outside of any functions, that variable is a
05:10property of the application object.
05:14I'll expand this and show you that the variable stooges appears right here.
05:19If I expand that, it will show me the actual values, or items, in the array, 0, 1, and 2.
05:26There are couple of things to learn from this demonstration.
05:29One is that variables that you declare outside of functions are properties of
05:33the application, or if you're working within an MXML component, as I'll show
05:37you in another video, that they are properties of the instance of the current component.
05:42These variables don't show up at the top-level of the Variables view.
05:46Instead, they are shown under this, which refers to the application object itself.
05:52You can also find any other properties of the application.
05:55For example, this application has an object, which is a Label Control.
06:01It has an ID of headline, and it also shows up under "this."
06:06You can open up, or drill down, into the object to see all of its properties,
06:11styles, and other settings.
06:14So for example, I'll scroll down through all these different items until I find
06:19the text property of the Label, and it shows me its value is "Hansel and Petal".
06:25So, the Variables view gives you access not just to top-level variables that
06:30you might declare within a function, but also to all of the properties of your
06:33application and also to all of the visual objects that are child DisplayObjects
06:38to the application.
06:40You can navigate through the Variables view as much as you want.
06:44In fact, you can either recursively move around.
06:47So, for example, if I go to the parent property of the Label Control, that takes
06:52me to a Group component that's wrapped around the Label.
06:55If I go from there into the inherited section, and once again, scroll all the
07:01way down to the parent, and then all the way down again, if I keep going far
07:06enough, I'll end up back at the application.
07:09The Variables view gives you a recursive, tree-oriented display that lets you
07:14find the value of any property, any style, any object.
Collapse this transcript
Watching ActionScript expressions
00:00A Watch Expression is an ActionsScript Expression that you can evaluate at run
00:04time in the Expressions view.
00:06The Expressions view appears, by default, in the Flash Debug perspective.
00:10I'll switch to that perspective by clicking on the Flash Debug button that
00:14currently appears in my perspective selector area.
00:18The Expressions view appears in the top -right corner of the interface, in the
00:22same tabbed area as Variables and Breakpoints.
00:25I'll switch back to the Flash perspective and open an application named
00:29WatchExpressions.mxml from the Debugging project.
00:33In order to evaluate a Watch Expression, you place the Expression in
00:37the Expressions view.
00:38There are a couple of ways of doing that.
00:41Let's start with this variable, stooges.
00:43It's declared outside of any properties, and so therefore is a persistent
00:47property of the application.
00:49As I've shown in other videos, I can evaluate this variable in the Variables
00:53view, but I have to drill down a couple of layers to find it.
00:57To make it easier to work with, I'll add it to the Expressions view.
01:01I'll double-click to select the variable name, and then I'll right-click on
01:04the variable name, or Ctrl+Click on Mac, and I'll choose Create Watch
01:09Expression from the menu.
01:11I'm currently in the Flash perspective, and the Expressions view is
01:14automatically added to the interface in the lower area.
01:18If I switch to the Flash Debug perspective, I'll see that it appears in
01:22the top-right area.
01:24You can also add Expressions to the Expressions view by right-clicking on the
01:28view and choosing Add Watch Expression.
01:32Here, you can type in any complex Expression.
01:34For example, if I type in this.width, that means I want to see the current width
01:40of the application when it's suspended.
01:43Notice I'm not pressing Enter or Return at the end of the string.
01:46That's very important.
01:48I'll click OK, and now I have two Expressions to evaluate.
01:53Finally, I'll scroll-down in the code to the function that will be called when I
01:57click the button in the application, and I'll add a breakpoint.
02:01This will allow me to easily suspend the application, and examine my Expressions.
02:06Now, I'll run the application in Debug mode.
02:10When the application appears in the browser, I'll click the Debug button, and
02:14that will bring me back to Flash Builder, and I'll be in the Debug perspective.
02:18Here is the Expressions view.
02:21The stooges variable appears because I right- clicked on it, and chose Add Watch Expression.
02:28You'll see that you can actually examine very complex objects, including arrays,
02:32objects, data collections and so on.
02:36Here's my this.width Expression.
02:39It shows the width of the application in pixels.
02:42So that's a simple look at how to work with Expressions.
02:45You can add as many Expressions into the Expressions view as you want.
02:49Expressions are managed on a per project basis.
02:52If you're working with multiple applications, that means that when you switch to
02:56another application in the same project, the Expressions that you've placed in
03:00this view won't automatically go away.
03:02To remove them, just select one at a time, and click Remove Selected
03:06Expressions, or you can click the Remove All Expressions button, and that will
03:11remove them all from the Expressions view.
03:14Once again, Expressions can only be used when the application is suspended
03:18during the breakpoint, but you can use as many as you want, and you can use very
03:23complex Expressions, much more complex than you could in Flex Builder 3.
Collapse this transcript
Using watchpoints to detect state changes
00:00A WatchPoint is a new feature of Flash Builder 4.
00:04WatchPoints allow you to watch particular expressions in your Flex
00:07application and suspend the application when the value of that expression
00:11changes at run time.
00:13To demonstrate WatchPoints, I'll use the application file WatchPoints.mxml,
00:18which is in the debugging project of the Exercise Files.
00:21In this application, a variable called strHeadline is declared outside of any
00:26functions and is therefore a persistent property of the application.
00:31This variable has an initial value of Hansel and Petal with the word 'and' in the middle.
00:35As the application starts up, a function called resetHeadline is called and
00:40inside this function, the value of that variable is applied to the label.
00:46As a result, when you run the application, initially, the label shows that value,
00:51Hansel and Petal with the word 'and.'
00:54Now returning to the code, when the user clicks the button labeled Change
00:58Headline, we call this function, which changes the value of strHeadline, recalls
01:04resetHeadline and once again, changes the label.
01:08So, demonstrating the application again, initially, it shows Hansel and Petal
01:13with the word 'and,' and when I click the button, changes it to the ampersand.
01:19Now here is the demonstration of a WatchPoint.
01:21You can only set a WatchPoint when the application is suspended at a breakpoint.
01:25So, I am going to set a breakpoint right here, at the line that declares
01:30the variable myVar.
01:32That will allow me to suspend the application upon a button click.
01:36Then I'll restore the editor to regular size and run the application in Debug mode.
01:43When the application shows up, I'll click the Debug button and then return to
01:47Flash Builder and switch to the Flash Debug perspective.
01:51In the Flash debug perspective, I'll double-click the Variables view, so I can
01:56see all of my variables.
01:59Then if it isn't already expanded, open up the tree branch labeled "this" and
02:04locate the strHeadline variable, right-click on the variable and choose Toggle Watchpoint.
02:11Now, whenever the value of that variable changes, the application will be
02:15suspended, and I'll return to Flash Builder.
02:17I'll restore the size of the Variables view and then click the Resume button in the Debug view.
02:27Then pressing Alt+Tab on Windows or Command+ Tab on Mac, I'll switch back to the application.
02:33Next, I'll click the button Change Headline.
02:36This results in changing the value of the variable, and I immediately come back
02:41to Flash Builder, and I am shown that the current instruction pointer is placed
02:45on the line of code right after that variable was changed.
02:49So that's how you use WatchPoints.
02:51Once again, you can only define a WatchPoint when the application is already
02:56suspended at a breakpoint.
02:58You go to the Variables view and you locate the expression that you want to watch.
03:02You right-click, choose Toggle Watchpoint and then resume running the application.
03:08Whenever that variable's value changes, the application will be suspended, and
03:13you will be able to inspect everything about the application's internal state.
Collapse this transcript
5. Using Visual Controls
Understanding MX and Spark components
00:00One of the most important new features in the Flex 4 SDK is a new set of
00:05components known as the Spark components.
00:08We still use many older components from Flex 3.
00:11These are now known as the mx components, and they still use the mx namespace prefix.
00:17For example, we still use the DataGrid component.
00:20There isn't a Spark version of that in Flex 4, and it's still declared within
00:24the mx prefix before the name of the control.
00:28The Spark components are new to Flex 4, and they use the namespace prefix 's,' as in Spark.
00:35In this example, I'm declaring an instance of the RichEditableText control
00:40using the Spark prefix.
00:42This is a list of some the most commonly used new components.
00:46You'll notice many of the component names match older components that we used in Flex 3.
00:51For example, Application and WindowedApplication are new versions of the primary
00:56Application containers.
00:59There're also new components that have names different than Flex 3.
01:02For example, Group, VGroup and HGroup are non-skinned containers;
01:08that is their designed to layout their contents but not have any visual
01:12appearance of their own.
01:14And there's a new container called DataGroup that's used to accomplish a lot of
01:18the same functionality as the old Repeater component.
01:21The BorderContainer is new to Flex 4.
01:24It's designed to render a rectangular area that contains other content and whose
01:28visual appearance can be defined using simple style declarations.
01:32There's a new version of the panel container and a new container called
01:36NavigatorContent that's specifically designed for use with the ViewStack,
01:40TabNavigator and AccordionContainers.
01:44And there's a new version of the Window Container, designed for creating native
01:48Windows in Adobe AIR desktop applications.
01:51The Spark components include many new controls.
01:54They are new versions of the Button and ButtonBar, a completely new control -
01:58called ToggleButton, and new versions of CheckBox and RadioButton.
02:02There are text display controls, a new version of the Label, and a new control
02:07called RichText, and new versions of text entry controls, including
02:11RichEditableText, TextInput and TextArea.
02:15There are new versions of the List and ComboBox controls and a completely new
02:19control called the DropDownList.
02:21And there's a new control called the Spinner.
02:25As I've mentioned, many of the older MX-based components are still in use.
02:29In Flex 4, they have not been rewritten in the new Spark Component architecture.
02:34These include the NavigatorContainers, ViewStack, TabNavigator and Accordion,
02:39the DividedBox containers - including VDividedBox and HDividedBox - and the
02:45FormContainers, Form and FormItem, among commonly used controls that we
02:49still use in Flex 4.
02:51The MX controls include DataGrid, AdvancedDataGrid and OLAPDataGrid.
02:56We still use the Alert component and Spacers, HRules and VRules.
03:01When you move to the new Spark Components, you'll need to learn something
03:04about how to use them.
03:06One of the most important features of the Spark Component architecture is the
03:10ability to skin certain components.
03:13There's a primary class called SkinnableComponent and two derived classes named
03:17SkinnableContainer and SkinnableDataContainer.
03:20Any class that's derived from these ActionScript classes supports the new custom
03:25skinning architecture.
03:27There's also some new support for describing vector graphics in MXML - using
03:32primitive classes named Rect, Ellipse and Path - and new Rich text rendering
03:37capabilities in Flash Player 10 such as the ability to display Bidirectional
03:42text and display text in wrapped columns.
03:45All of these new controls and capabilities together make up the most important
03:50new features of the Flex 4 SDK.
03:53The Flex 4 SDK has been described as a transitional release, going from the
03:58older component architecture, represented as the mx components to the new
04:03component architecture, represented as the Spark Components.
04:07In Flex 4 Applications, you can pick and choose from the two component sets
04:12using the ones that make sense for your application.
04:14Wherever there are two versions of the components, such as Label, Application or
04:20panel, Adobe strongly recommends that you use the new version of the component
04:24in a Flex 4 application.
04:26But wherever they haven't had time to create new versions, such as with the
04:30DataGgrid, we still use those older components.
04:33And Adobe has done a lot of work to make sure that they fit in well in the new
04:37Flex 4 architecture.
Collapse this transcript
Declaring visual controls with MXML
00:00Most of the time, you declare controls using the MXML programming language.
00:05MXML, as I've described in previous videos, is a markup language that allows you
00:10to define the look and appearance of your application.
00:13For the demonstrations in this chapter, I'll use a project that's included with
00:17the exercise files, named Controls.
00:20If you have access to the Exercise Files, you can import it now into Flash Builder.
00:25Select File > Import Flex Project.
00:28In the Import Flex Project dialog, browse and navigate to the 05_Controls folder
00:34under your Exercise Files, and from there, to the begin folder, and locate and
00:39import the file Controls.fxp.
00:43This project has a number of applications that I will be using for all the
00:46different demonstrations.
00:48It also contains a number of assets in a subfolder called assets, including some
00:53JPG graphic files, a SWF file, and a couple of PNG files.
00:59I'll start with the Application file, ControlsWithMXML.
01:04As you can see on the screen, this application starts with next to nothing.
01:08It has a backgroundColor setting that sets it to our standard background color
01:12for our sample application, Hansel and Petal.
01:16When I run this version of the application in the browser, it shows just the background.
01:20I'll close the browser and return to Flash Builder.
01:24When you declare a control using MXML code in an application file, the control
01:29is actually placed inside something called a Group.
01:32The Group has an automatically created ID called Content Group.
01:37Now you won't see this Content Group in your code - it happens completely in the
01:41background - but all you need to do is declare the control using MXML code.
01:47So, for example, I'm going to create a simple Label.
01:49I'll place the cursor after the declarations element, and I'll make a blank line of code.
01:55Then I'll type in a less than character and start typing the name of the element
01:58or control I want to place, 'la.'
02:01That takes me to the Label Component.
02:04Notice that the Label Component, as shown in the content assist list, is a
02:08member of the package called spark.components.
02:11That tells you that it's the Spark version of the Label Control, not the old MX version.
02:16I'll press Enter or Return to select the tag, and then after I press the
02:21Spacebar, I'll be shown all of the attributes of this tag.
02:25Attributes can be properties, styles, or event listeners.
02:30I'm going to type in 'te.' That will get me to the text property, which is the
02:35most commonly used property of the Label Control.
02:38I'll select it, and I'll type in a value of Enter Text and a colon.
02:44Then I'll move the cursor to the end of the line, and type in a slash and a greater than.
02:50This /> character sequence means that this is an empty tag;
02:55that is to say there is no matching end tag.
02:58Now, if I look at the application in Design view, which I'll do by clicking
03:02the Design button at the top of the editor, I'll see my Label up here in the top-left corner.
03:08When you declare a control using MXML, by default, it's placed in the top-left
03:14corner of its container.
03:15Literally, its X and Y properties are set to default values of 0.
03:21Now, don't worry about the placement of the Label quite yet.
03:24Go back to Source view by clicking the Source button.
03:27You can also use the keyboard shortcut for switching back and forth
03:30between Source and Design.
03:32Hold down the Ctrl key, and press the tilde key, which is right next to the number 1.
03:37That's the same keyboard shortcut on both Mac and Windows.
03:41That will toggle you back and forth between Source and Design mode.
03:46Now, I'll add another MXML component.
03:49I'll place the cursor after the Label tag and make a new line, and now I'm going
03:54to add a TextInput control.
03:56I'll type in a less than character and type 'te,' and then I'll scroll down to
04:01TextInput and press Enter or Return to select it.
04:05The TextInput control is an example of a data entry control.
04:09It's interactive and allows the user to type in a single line of text.
04:12When you use a TextInput control, you nearly always assign an id attribute.
04:18The id is a unique identifier, and it will allow you to address the control in
04:23your ActionScript code.
04:25I'm going to give it an id of myInput.
04:28And then, once again, I'll move the cursor to the end of the line, and put in
04:30the empty tag sequence.
04:33Finally, I'll add a button.
04:35I'll type a less than character and start typing the name of the control.
04:38I'll select s:Button, and I'll assign the label property of the button.
04:45The label is a string that appears on the face of the button, and I'll type in
04:49a string of Click me, and once again, add a slash and a greater than character at the end.
04:56Now, if I switch over to Design view, you'll see that these controls are stacked
05:00on top of each other and that the control I declare last overlaps the others.
05:05So, the button is in the foreground, the TextInput control is right behind it,
05:09and the Label is completely invisible.
05:11Don't move them around with the mouse.
05:13Instead, we're going to wrap those controls inside a container.
05:18And this is the first use of a container, other than an application, that you all have seen.
05:23I'm going to use a container called the BorderContainer.
05:26The BorderContainer allows you to add a backgroundColor and border to a simple
05:31container to wrap other controls.
05:34I'll place the cursor above the label tag and make an extra line, then type in
05:38a less than character and type 'bor.' That takes me to the BorderContainer component.
05:44I'll press Enter, and this time, instead of putting in the slash, I'm going to
05:49put in just the greater than character.
05:52Flash Builder then autocompletes the BorderContainer tag;
05:56that is, it creates the end tag for me.
05:58And then I'm going to move the BorderContainer end tag down a few lines.
06:02I'll place the cursor on the line that has the end tag, and then I'll hold down
06:07the Alt key on Windows, or the Option key on Mac, and press the down arrow key.
06:13And I'll do that three times to move the code down that far.
06:16Now, I'm going to look at the file in Design view again, and I'll see that
06:22everything is inside the BorderContainer, which is represented by this white
06:25rectangle, but the objects are still stacked one on top of another.
06:30So, I still have a little bit more work to do to lay out the controls.
06:33I'll go back to Source view, and now I'm going to add a layout object.
06:41The Spark container components use layout objects rather than a simple layout
06:46string, as was the case in Flex 3.
06:49Here is how you declare it.
06:50I'll place the cursor on the empty line after the BorderContainer start tag.
06:55I'll type in a less than character to begin a new tag, and I'll type in 'la.'
07:00That takes me to all of the components that start with the characters la, and
07:05I'll choose the s:layout attribute.
07:07I'll press Enter, or Return, and then put in the greater than character, and
07:13that will result in autocompleting the layout tag set.
07:17And then within the layout tag set, I'm going to add something called a
07:21HorizontalLayout object.
07:23I'll type in a less than character and then hori.
07:27That will take me to the HorizontalLayout object.
07:30I'll press Enter, and I'm going to add some other attributes, including a
07:35verticalAlign attribute, set to a value of middle, and then paddingRight, and
07:41paddingLeft settings of 10 pixels each.
07:44For each, I'll type in 'padd,' I'll press Ctrl+Spacebar, and I'll choose
07:50paddingLeft first, type in a value of 10.
07:53Move the cursor past the Attribute setting, and add paddingRight, also set to a value of 10.
08:01So, now I'm declaring an object using MXML code, and setting its attributes.
08:07I'll add in the slash and greater than and then, once again, I'll look at the
08:10file in Design view.
08:12And I'll see that the objects are now nicely aligned, the Label, the TextInput
08:17control, and the Button.
08:18I'll go back to Source view, and add a few more properties.
08:23I'm going to assign the BorderContainer an explicit height of 50 pixels.
08:28I'm going to assign the BorderContainer an explicit height of 50 pixels, and
08:33then I'm going to add some styles to set its appearance.
08:37I'll add a borderStyle of inset.
08:42I'll add a borderColor of a light gray, using the hexadecimal value #CCCCCC.
08:50And I'll also add a borderWeight of 4.
08:55The borderWeight, it's the width of the border, in pixels.
08:59I'll look at the file in Design view, and now my BorderContainer wraps the
09:05controls and has a nice little border around it.
09:09In Design view, I'll click on the outer area of the BorderContainer, making sure
09:14I don't click on the controls within it, and then I can place it anywhere on the
09:18screen using the Design view placement capabilities.
09:22I'm going to snap it into place at the top-left corners
09:26and then look at the file in Source view again, and I'll see that X and Y
09:30properties have been added.
09:33Finally, I'm going to quickly format my code.
09:37I'll press Ctrl+A to select all of the code, or Command+A on Mac, and then go to
09:43the menu and choose Source > Correct Indentation, and that nicely indents all of
09:48the code and shows how each object is contained within each other object.
09:52I'll save my changes by pressing Ctrl+S, or Command+S on Mac.
09:58And then I'll run the application in the browser, and here is the result.
10:03My Label, my TextInput control, and my Button are nicely aligned within the
10:08BorderContainer, and the BorderContainer is placed exactly where I want it on the screen.
10:13So, this is a look at how to declare various types of controls and
10:17containers using MXML code.
10:20The MXML code is translated in the background into ActionScript code, which
10:24would be much more complex than this.
10:26You use XML Attributes to set properties and styles, and you also use XML
10:32Attributes in each MXML declaration to set properties, styles, and listen
10:39for events.
Collapse this transcript
Declaring visual controls with ActionScript
00:00As I've described in other videos, MXML is a facade, or a convenience
00:05language for ActionScript.
00:07When you write MXML code, you are really writing ActionScript, and the
00:11ActionScript code is generated for you during the compilation process.
00:15If your application calls for it and if it's your preference, you can
00:19instantiate and add controls to your user interface using pure ActionScript code.
00:24And I'll show you how to do that in this demonstration.
00:27I'll use the application ControlsWithActionScript.mxml, that's a part of the
00:31controls project in the Exercise Files.
00:35In this application, I've created a BorderContainer.
00:38The BorderContainer has an id of container.
00:41When you add an id attribute to a container, it allows you to address the
00:45container in ActionScript code.
00:48It has a number of properties and styles that control its appearance, and a
00:52horizontal layout object that will cause its nested child objects to lay out
00:57from left to right.
00:59The application also has a creationComplete event listener.
01:03When the application starts up, it dispatches the creationComplete event to tell
01:08you that it's ready for use.
01:10And you can add code into the function that's called upon the event.
01:14I'm going to add some code into that function so that I'm placing controls
01:19within that container, using pure ActionScript.
01:23Now at its current state, when I run the application, it looks like this.
01:28It shows the BorderContainer object in the top-left corner, but it has no
01:32contents, currently.
01:33I'll close the browser and return to Flash Builder.
01:38I'm going to add three controls to the container:
01:41a label, a text input control and a button.
01:44I'll add them one at a time, in exactly that order, so that they layout from left to right.
01:50I'll start with the label.
01:52In order to declare a control using pure ActionScript, start with the var keyword.
01:58This is going to be a reference variable that refers to a complex object in memory.
02:03Assign a variable name.
02:05You can name the variable anything you want, but you should follow the standard
02:09convention that variable names start with a lowercase character.
02:13You also can't include any spaces or special characters in variable names.
02:18I'm going to name my variable myLabel.
02:21Then I'm going to type in a colon character, and I'll set the datatype of the
02:26variable as the Label class. I'll type in 'l-a.'
02:31Now, notice, in the list of available classes, that the mx version of the
02:36label appears at the top.
02:38I want to make sure that I'm using the Spark version of the Label class.
02:42So, I choose Label-spark.components, and then I press enter.
02:47Notice when I select Label from the list of available classes that Flash Builder
02:52automatically adds an import statement for that class.
02:56With all visual components, when you want to refer to them using pure
02:59ActionScript code, you must import the class before you refer to it in the rest of your code.
03:05Now, I'm going to place the cursor after the declaration of the variable, and
03:10I'll put in an equals assignment operator.
03:13I'll type in the keyword 'new,' and then I'll call the constructor method for
03:17the label component.
03:20I'll type in 'L-a-b' again.
03:21I'll, once again, make sure that I'm choosing the Label class in the
03:25spark.components package.
03:28I'll type in an open paren, and I'll see a little pop-up help that shows me the
03:32syntax of the Label class's constructor method.
03:36Notice that the constructor method takes no arguments.
03:39I'll close the call and finish the statement with the semicolon.
03:44Now I'm going to set the Label control's text property.
03:48In another video where I declared the same control using MXML code, I set the
03:53property using an XML attribute.
03:55This time, because I'm working in ActionScript, I'll set it using dot notation
04:00like this, myLable.text = Click me.
04:07This is a literal string, so it's wrapped in quotes, and the statement ends with a semicolon.
04:12Now, I'm going to add the control to the container.
04:16Each Spark-based container has a nested group that has an id of content group.
04:22In order to add an object to a container, you don't add it directly to the container.
04:27Instead, you add it to its content group, like this.
04:29I'll start with the id of my borderContainer object, container.
04:34Then I'll put in a dot, and then I'll refer to its content group object.
04:39Then I'll put in another dot, and I'll call the method addElement.
04:45Notice that there's both an addChild and an addElement.
04:48When you're working with Spark components, you should always use the addElement method.
04:54I'll select that method, and then I'll pass in as an argument the variable
04:58that's referring to my label object in memory.
05:01Now, I'm ready to test the application.
05:04I'll save and run it.
05:06And as the application starts up, that method is called, and the result is that
05:11the label appears inside the container group.
05:15Now, I'm going to add two more controls:
05:18a text input and a button.
05:21I'll follow exactly the same set of steps for each of the other controls.
05:25I'll create a variable referring to the object I want to use.
05:28I'll set any required properties, and I'll add it to the container's content group.
05:34I'll create a variable called myInput, data typed as TextInput.
05:39Once again, make sure that you're using the Spark version of the class.
05:43Then I'll instantiate it by calling its constructor method.
05:49For the Input class, I'm going to set its id.
05:53The id is the same property that you set in the MXML, but it's not automatically
05:57set for you when you instantiate an object using ActionScript code.
06:04By setting the id property, it ensures that you can address the control in
06:08exactly the same way as an object that was instantiated with MXML.
06:12And then I'm going to copy and paste the line of code that added the label.
06:16I'll select that line of code and press Ctrl+C, or Command+C on Mac.
06:20Then I'll move down to the blank line and paste, pressing Ctrl+V, or
06:25Command+V on the Mac.
06:27Then I'll change the argument from myLabel to myInput.
06:30I'll follow the same set of steps for the button.
06:36I'll declare the variable.
06:39I'll set the variable name as myButton.
06:41I'll make sure I data type it as the Spark version of the button control.
06:48I'll instantiate it using the Button class's constructor method.
06:53For this control, I'll set the Label property.
06:59And finally, I'll once again, paste in the code that adds an element to
07:02the content group of the container, and I'll change the argument to refer to myButton.
07:10So there's all the code, adding three controls to an existing container, using
07:15pure ActionScript code.
07:16I'll test the application in the browser, and there is the result.
07:21The application has the same appearance as if I had declared all these controls
07:25using MXML code, but it's now done completely with ActionScript.
07:29When you learn how to use ActionScript code to instantiate and add controls to a
07:35user interface, it gives you the ability to build very flexible Flex
07:39applications, but it also teaches you a lot about how MXML is translated into
07:44ActionScript in the background.
Collapse this transcript
Using text display controls
00:00The Flex 4 SDK includes five new text controls that are members of the
00:05Spark component set.
00:07These controls are named Label, RichText, RichEditableText, TextInput and TextArea.
00:14For this demonstration, I'll use the application TextControls.mxml, which if you
00:20have access to the Exercise Files, you'll find in the controls project.
00:24In this application, I've created a BorderContainer.
00:27It has a border, and it uses a vertical layout object, which will cause its
00:32nested objects to stack from top to bottom.
00:35I'll start by creating a Label.
00:38The Label control is the simplest of the five text controls.
00:42It can display wrapped text, which makes it different from the Label control
00:45that was used in Flex 3.
00:47But it doesn't do background colors, borders, scrollbars or anything else
00:52that's at all fancy.
00:53I'll type in the Label tag, making sure that I use the Spark version of the
00:58component, and I'll set the text property to My Label.
01:02I'll close the tag, and then I'll look at the application in Design View and
01:09show you that the Label displays as a very simple presentation.
01:13Again, the Label control is very limited, but it's the most lightweight of
01:17the five text controls.
01:19You can add fontFamily settings.
01:21For example, I'll set my fontFamily to Georgia, fontSize, which I'll set to 24
01:27pixels, and other font display styles.
01:32There is the control again, this time displayed with the new font settings.
01:36The next control, in increasing order of complexity and capability, is RichText.
01:43The RichText control, once again, has a text property, which I'll set to My Rich
01:47Text, and also supports all of the font settings styles fontFamily, fontSize,
01:52fontWeight and so on.
01:54But unlike the Label control, it supports the backgroundColor setting and other
01:59rich presentation capabilities.
02:01I'll set the background color to #EEEEEE, which is a light gray.
02:07And for this control, I'll set it to an explicit width of 200.
02:10I'll look at the application in Design View and show you that this control
02:17displays with the background color.
02:20The next control is called RichEditableText, and it's exactly what it says.
02:25It's actually an input control, but without any of the chrome, that is
02:29borders and scrollbars that are typically associated with the TextInput and TextArea controls.
02:35You use it like this.
02:37I'll declare and instance of it, RichEditableText, and as with other input
02:42controls, you typically assign an ID so that you can refer to its text property.
02:49I'm going to, once again, set a backgroundColor.
02:51I'll use a named color, this time, of aqua and an explicit width of 200.
02:59And I'll set the initial text property to Enter text here.
03:06This time, I'll save and run the application in the browser.
03:09The RichEditableText control is interactive, so you'll only see its full
03:14capabilities by looking at it in a running application.
03:18I'll click into the control, select the text and Backspace, or Delete, and then
03:23type in My own custom text.
03:26As with TextInput and TextArea controls, you can refer to the value that the
03:31user types in with the text property of the object.
03:36Finally, two more controls:
03:39the TextInput and the TextArea.
03:43I'll create a new TextInput control within id of myInput and just use it in its simplest form.
03:50And I'll create a TextArea control with an id of myArea.
03:56And then I'll show you the application in Design view.
03:59The difference between TextInput and TextArea is that the TextInput control is
04:03used to receive a single line of text, whereas the TextArea control supports
04:08linefeeds, lots of special characters and a much richer presentation.
04:13With both controls, you can use an attribute called displayAsPassword.
04:19If you set that attribute to true and then run the application, as the user
04:23types a value in, they'll only see asterisks.
04:27This mimics the behavior of the password control in HTML.
04:32You can also set the same property on the TextArea control, but it doesn't make
04:36much sense to do that.
04:38It's also worth noting, with the TextArea control, that when you type in a lot of
04:42code - which I'll do just by typing a whole bunch in - you'll see that the
04:46TextArea control automatically presents a scrollbar.
04:50That doesn't happen on the TextInput control.
04:53So, if you want to receive a large amount of text, use the TextArea control, and
04:57if you want to accept a single line of text, use TextInput.
05:01So those of the five new text controls in Flex 4:
05:04the Label, RichText, RichEditableText, TextInput and TextArea.
Collapse this transcript
Displaying rich text
00:00Most of the Text controls that are included in the Flex 4 SDK support a special
00:05property called content, which allows you to present mixed text, using a
00:10particular style of markup.
00:12All of the controls, except for the Label Control - that is the RichText, Rich
00:16editable Text, Text Input, and TextArea Controls - support this property.
00:22I'll demonstrate how to use the property, with the file RichTextDisplay.mxml,
00:27which I'm showing on the screen.
00:29It starts off as a pretty much empty file with just a background color.
00:34I'll first create a RichText control, and I'll leave out any x or y properties so
00:39that it appears in the top- left corner of the application.
00:42Now, when you declare the content property using MXML markup, you typically do
00:47it as a child element.
00:49So, I'm going to type in a greater than character, and that will cause Flash
00:53Builder to autocomplete the RichText element, and then I'll add the content
00:59property as another tag set, typing in the less than character and then 'con'
01:04and then content, and I'll allow Flash Builder to autocomplete that tag set.
01:10The content property represents an instance of a class called TextFlow.
01:15The TextFlow class is a tree of objects, and you can add a number of different
01:20kinds of objects to the tree, including paragraphs, divs and spans.
01:25For example, to add a paragraph to the content, you use the tag s:p, which
01:32represents an instance of a class called ParagraphElement.
01:36This is called a TextFlow element and each of the TextFlow elements supports
01:41all of the standard font styles, such as font family, font weight, font size and so on.
01:47In this example, I'm going to set the font family for the paragraph to verdana,
01:52a sans-serifed, or non-serifed, font.
01:55Now I'll autocomplete that tag set, and I'll be able to add other tags inside of it.
02:01I'm going to create three sets of spans, like this.
02:04I'll first type in the beginning tag and type in the closing greater than
02:08character so that Flash Builder will auto complete the tag.
02:11And then I'll eliminate the extra white space that Flash Builder adds in between the tags.
02:17Now, I want to present Hansel and Petal as three different spans, allowing me to
02:22apply different font settings.
02:25I'm going to clone this line of code twice.
02:27I'll hold down the Ctrl and Alt key on Windows, or if you're working on Mac, hold
02:32down Command and Option and then press the down arrow twice.
02:37And now I have three span elements inside the paragraph.
02:41I'll type in the strings that I want to present.
02:44The first span will be the word Hansel, the second string will be ampersand and
02:51because I'm working in the context of MXML, I'm going to use the entity for the
02:56ampersand that looks like this, &amp;.
03:02And then finally, I'll move to the third span and type in the string Petal.
03:07Now, I'll look at the application in Design view to see how I'm doing.
03:11Here's my simple RichText object.
03:13Right now, it's being displayed in the default font size.
03:18I'll go back to Source view and make some changes to these spans.
03:21In the first one, I'll add a fontSize of 36 and a color of #CCCCCC.
03:33I'll go to the second span and add a fontSize of 54 pixels and a color of
03:39#EEEEEE, and I'll go to the third span and add a fontSize, once again, setting
03:46it to 36, and this time, setting the color as black or #000000.
03:54Now for simplicity, I'm using grayscale colors, but if you're a graphic designer
03:59and have a particular appearance that you want to create, you can use whatever
04:02hexadecimal color values you want.
04:04Here is the result.
04:07When I go back to the Design mode,
04:09I see the resulting RichText object contains all three strings in separate
04:14spans, but it's all a single object.
04:17So, I can now click and drag the object and place it anywhere on the screen I want.
04:22So the RichText Control is one of the Controls that supports the content property.
04:27Once again, the content property is an instance of a TextFlow object and the p
04:33span and other tags that you use to add to the content represent particular
04:38classes in the Flex 4 SDK.
04:41Using RichText, Rich editable Text, TextInput and TextArea, you can take
04:47advantage of the enormous capabilities of Flash Player 10's Text Layout
04:51Framework, using the content property and the TextFlow class.
Collapse this transcript
Using text input controls
00:00As I've described in other videos, there were three TextInput controls in the Flex 4 SDK:
00:07the RichEditableText control, which is the lightest weight of the controls, the
00:11TextInput control, which can accept a single line of text and the TextArea
00:16control, which can accept a larger amount of text.
00:20For this demonstration, I'll use the application TextInputControls.mxml, which
00:25is a part of the controls project in the Exercise files.
00:29This application declares three controls, one of each type.
00:34Each has an ID of myRich Editable text, myInput and myArea.
00:38I am going to show you how to capture the value that the user types into a control.
00:44I've created a Button with a label of Show values and three Label controls with
00:49IDs of richTextLabel, inputLabel and areaLabel.
00:54When the user clicks the button, I'll use a little bit of ActionScript code to
00:58capture the value that the user has typed into each of the controls and display
01:02those values in the three labels.
01:05First, I'll add an eventListener for the button control.
01:08I'll place the cursor after the label attribute and before the closing slash
01:14and press the Spacebar.
01:15This brings up a list of the available attributes of the Button tag.
01:18I'm going to use the click attribute, I'll type 'cli' to get to that part of the
01:24list and then press Enter to select that attribute.
01:28Then I'll press Enter, or Return, again to generate a function that can be
01:32called when the user clicks the button.
01:35Now, that function will have been created above the Visual controls in a
01:39new fx:Script section. Here it is.
01:43It's called button1_ClickHandler.
01:46Now to capture the values, I'll get rid of the comment that was
01:49generated automatically.
01:52Each of the three text entry control supports the text property.
01:56It will return the value that these user has typed in.
01:59I'll take that value and pass it to the text property of the matching label, like this.
02:06I'll start off with richTextLabel. text = myRichEditableText.text.
02:14Now, if you are wondering how I typed that in so quickly, for each control, I am
02:18just typing the first few characters of the ID and then pressing CTRL+SPACE.
02:23If the control has an ID that matches the string that I've typed in and there
02:28aren't any other classes or objects that match that string, Flash Builder
02:32autocompletes the ID for me.
02:34Next, I'll grab and display the value that the user types into the TextInput control.
02:40I'll type in inputLabel.text = myInput.text and finally, I'll handle
02:47the TextArea control.
02:49Once again, I have a matching label, this time called areaLabel.
02:53I'll use the code areaLabel.text = myArea.text.
02:59So now I am grabbing the values from the three controls and displaying those
03:04values in the three labels.
03:06I'll run the application in the browser, and then I'll click into each of the
03:12three data entry controls, and I'll type in some simple text.
03:17This is the rich editable text control.
03:25This is the text input control and notice that the text input control is only
03:29displaying asterisks because I have it's displays password property set to True.
03:36Then I'll type a value into the TextArea control, This is the text area control.
03:40I'll click the Show values button and show that I have captured the values and
03:47displayed them in the labels.
03:48When you work with data entry controls in Flex, you don't have to grab them in a
03:53form or any other kind of container in order for the data to be usable.
03:57You grab the values using pure ActionScript code, or as I'll show you in another
04:02video, with binding expressions, and then you take those values and if you like,
04:06display them on the screen as I've done here, or using RPC components, such as
04:12HTTP service, web service, or Remote Object, send those values to a server.
Collapse this transcript
Using button controls
00:00The Flex 4 SDK includes a number button controls, controls that the user
00:05interacts with to take some action.
00:07I'm going to show you each of these controls in turn and talk about when you use
00:11them, and how you use them.
00:12For this demonstration, I'll work with this application, ButtonControls.mxml.
00:17It's a starter application that just has a BorderContainer with a vertical layout.
00:22As I add ButtonControls inside the BorderContainer, they'll stack from top to bottom.
00:26I'll double-click the tab at the top of the Editor to look at the code in fullscreen.
00:30First, I'll show you the Spark version of the button control.
00:35You start with an s:
00:36Button tag, and then you apply a Label.
00:40The Label attribute is a string of any length.
00:43I'll type in a string of Click me.
00:45When the user clicks the button, it dispatches a click event, and you can react
00:50to that click event by executing some code.
00:52I'll press the Spacebar and then choose the click event from the list and then
00:57press Enter or Return to generate a clickHandler.
01:01Before I fill in the generated function's code, I'll complete the Button
01:04tag, moving the cursor to the end of the line and typing in a slash and a
01:09greater than character.
01:10Now I'll go to the clickHandler method.
01:13I'll select and delete the comment, and then I'm going to use a very popular
01:17class in the Flex SDK called the Alert class.
01:21The Alert class presents a message as a pop-up window to the user.
01:26Here's how you use it.
01:28I'll type in the word 'Alert,' and then I'm going to cause Flash Builder to
01:32automatically add an import statement for the Alert class.
01:35I'll press Ctrl+Space, and then Enter, or Return, and choose the Alert class.
01:41Flash Builder adds an import statement above the function, and I can now call
01:45the Alert class's methods as much as I want.
01:48I'll type in a dot, and I'll call a method called show, and I'll pass a simple
01:53string in of You clicked the button.
01:56Now, I'll talk more about the nature of these events and how you handle them in
02:00another video, but for now, all you need to know is that when the user clicks
02:04the button, a pop-up window will appear with the message that I typed in.
02:08I'll save the change and run the application.
02:11And when it appears in the browser, I'll click the button labeled Click me, and
02:15I'll see a pop-up window with the message You clicked the button.
02:19So, that's the simplest sort of button that's supported in the Flex SDK.
02:23Now, I'll show you another control that has a slightly different behavior.
02:27The Flex 4 SDK includes the ToggleButton.
02:30This is a new component that didn't exist in Flex 3.
02:33In Flex 3, you could do ToggleButtons, but they were handled in a different way.
02:37In Flex 4, it's a completely new class.
02:40I'll declare ToggleButton, and I'll give it an ID of myToggle.
02:44I'll give it a label of Toggle Me, and just as with the standard button control,
02:52it supports the click event.
02:54I'll create a click attribute and then generate the clickHandler, and just as
02:59before with the Button control, I'll complete the tag by moving the cursor to
03:03the end of the line and typing in a slash and a greater than character.
03:06The ToggleButton control supports a property called selected, which returns
03:11either true or false.
03:12By default, its value is false, but each time the user clicks the button, the
03:16value of the selected property toggles between true and false.
03:20I'm going to use that Alert class to display the current state of that property.
03:24I'll delete the generated comment and type in the beginning code, Alert.show,
03:30and then I'll type in the following literal string, The value of
03:33myToggle.selected is.
03:36And then I'll close the string with the double quote, put in a + operator as
03:39a concatenation operator, and then I'll output the value of that expression, myToggle.selected.
03:46Now I'll demonstrate the use of the property, running the application.
03:50Each time I click that button, it's going to change the value of the selected property.
03:55The first time I click it, it changes to true, the second time, it's false.
04:00Also, notice a difference in the appearance of the button.
04:02By default, a Toggle button looks just like a standard button, but when you
04:06change its value to true, it shows a dark shaded area instead of the
04:10original light-colored area.
04:11That's the visual indicator to the user that the state of the button has changed.
04:15And when you click it again, it returns to its original appearance.
04:19You can modify that appearance by skinning the component, creating a custom skin.
04:24That's a skill that I'll show you in another chapter of the video series.
04:28Finally, I'm going to show you one other button that's a part of the Flex 4 SDK.
04:32This is a control that was part of Flex 3 and has not been replaced in Flex 4.
04:37So, if you want to use it, you can still use the original version.
04:40It's called a LinkButton.
04:41I'll type in a less than character and then 'l-i-n-k,' and I'll choose the
04:46LinkButton control.
04:47Notice that the LinkButton control is a part of a package called mx.controls,
04:52and it uses the MX prefix.
04:54So, this tells you that this control is a member of the MX component group.
04:58As with the standard button control, you apply a label.
05:01I'll use a label of Click me, just like before, and also just like the button,
05:07it dispatches a click event.
05:10I'll generate a clickHandler for the Link button.
05:12I'll make sure I finish the tag with the slash and a greater than character at the end.
05:17And then, once again, I'll use the Alert class to show the user what they clicked.
05:22I'll type in Alert.show and then display a literal string of You clicked the link button.
05:31I'll save and run the application.
05:34And when it appears, I'll show the appearance of the link button.
05:37It appears initially as a simple label, but when the cursor moves over it,
05:41it changes color, and then when you release the mouse button, it dispatches
05:45the click event, and you can react to that event by executing some ActionScript code.
05:50So these are three buttons that you can use in your application:
05:53the standard button, the toggle button and the link button.
Collapse this transcript
Loading images at runtime
00:00The Flex 4 SDK includes two controls that present images on the screen, the MX
00:06image control and the Spark bitmap image control.
00:09Of these, however, only one of them is capable of loading images at run time,
00:14either from the web or in a native desktop application from the local hard disk.
00:18That's the MX image control.
00:20Both controls can embed images;
00:23that is compile the images into the application.
00:25But if you want to load images dynamically, you must use the MX image control.
00:30Here's how you use it in its simplest form.
00:32I'm going to use the application RuntimeImages.mxml.
00:36That's a part of the Controls project.
00:38I've also included some image files to play with that are stored under the
00:42assets subfolder, under the src folder.
00:45I'll be working with these two images in this exercise: aloe_vera.jpg and bonsai.jpg.
00:51The image control is capable of loading four types of images at run time:
00:56JPG files, GIF, PNG and SWF, or SWF files that have been generated out of
01:03Illustrator or Flash.
01:05Typically, in Flex applications, you choose between the JPG and PNG files.
01:10JPG files frequently come from digital cameras and represent photographs,
01:15whereas PNG files can either be photographic in quality, or in some cases are
01:20created from computer rendered graphics.
01:22Here is how you declare an image control using MXML.
01:25I'll place the cursor after the layout section and make a little bit of extra
01:29space, and then I'll create an MX image tag.
01:34The image tag has a source property, which you set to a value that indicates the
01:38location and name of the graphic relative to the location of the application.
01:42I'm going to start by displaying the file aloe_vera.jpg. It's in the assets
01:47folder, so I'll type in the name of the folder and then a forward slash and then
01:52the name of the file. That's it.
01:56Now as the application loads into the browser, it will automatically load the
02:00graphic from the hard disk, or if the application is downloading from the web,
02:05it will download it from the website.
02:07I'll save and run the application, and there is the result.
02:11The graphic has displayed on the screen.
02:13Now if you want to change the graphic at run time, there are two ways to do it.
02:17One way is to change the source property of the image control, and the other's
02:22to call a method called load.
02:23I'll show you both.
02:25First, in order to be able to address the image object in ActionScript code, I'm
02:30going to assign it a unique ID of myImage. Then I'm going to create a couple of
02:35buttons in the application.
02:36I'll look at the application in Design view, and then I'll drag in two buttons,
02:44and I'll place them above the image.
02:47Then I'm going to change the labels on the buttons to indicate their actions.
02:51In the first one, I'm going to Change the source property, so that's the label I'll put in.
02:56Notice that I simply double-clicked on the Button in order to change its label.
02:59For the second Button, once again, I'll double-click it, and this time, I'll
03:03call the load method.
03:05Now I'll go back to the Change source button, and this is a little trick that
03:09you can use in Design view to add some ActionScript code that will be executed
03:13when the user clicks the button.
03:15With the button selected, I'll go to the Properties view, which is in the
03:18lower-right corner.
03:19I'll go to the On click property, and I'll click the little lightning bolt
03:23button and choose Generate Event Handler.
03:26That takes me to Source mode.
03:28If I look at the Button control, I'll see that it's been generated down here
03:32below the BorderContainer, although it's presented above it on the screen.
03:37And the click event attribute has been added and the method or function that's
03:41being called has been added as well.
03:44Now I'm going to change the graphic at run time by changing the value of the
03:48object's source property.
03:49I'll use this code myImage. source = assets/bonsai.jpg.
03:58I'll save and run the application, and when I click the Button, it results
04:03in changing the image.
04:05Notice that the BorderContainer in which the image is wrapped is resizing itself
04:09automatically to accommodate the new image dimensions.
04:13Now I'll add some code for the second button.
04:16I'll go back to Design mode.
04:18I'll click the button labeled Call load.
04:20I'll go to the Properties view and once again, Generate an Event Handler.
04:25And this time, instead of setting the source property, I'll call the load
04:28method, and I'll switch back to the original image, aloe_vera.jpg.
04:37I'll run the application, I'll click the first button to change to the bonsai
04:42plant, and then I'll call the load method and show that I'm switching back.
04:48So both approaches, changing the source property or calling the load method,
04:52result in re-downloading an image from the web or in Adobe AIR application from
04:57the local hard disk and allowing the image control to display it.
Collapse this transcript
Embedding images
00:00The Flex 4 SDK includes a number of ways to embed images in an application.
00:05When you embed an image, it becomes a part of the application's SWF file.
00:09There are pros and cons to this approach.
00:11On the positive side, an embedded image is displayed instantly when you want it to show up.
00:17You don't have to download it from the website or from the local disk, so
00:20there is no latency, or delay, between the time you request the image and the
00:25time it's displayed.
00:26On the downside, when you embed an image, it expands the size of your SWF
00:30file, and especially when you're working with an application that's downloaded
00:33from the Web at run time, it takes longer for the application to download
00:37before the user can see it.
00:39I'm going to show you how to embed an image using the two image controls:
00:44the MX image control and the Spark bitmap image control.
00:48I'll start with the Spark bitmap image.
00:50The Spark bitmap image control is one of a set of controls known as primitives.
00:56This means that they aren't complete components and technically what this means
01:00is that if you try to add a bitmap image control, let's say to an older MX
01:05container like VBox or HBox, you won't be able to.
01:09It doesn't extend a very special class called UIComponent, but you can place
01:13a bitmap image control within any Spark-based container, such as the
01:17application component.
01:19For this demonstration, I'm using EmbedImages.mxml, from the Controls project.
01:24I'll create a bitmap image control and just like the image control, it has
01:29a Source attribute.
01:30But with a bitmap image, you can't use downloaded or run time images.
01:35You can only use embedded images.
01:36I'm going to use this graphic, flowers.png.
01:40I'll start with something called the @Embed compiler directive.
01:44A compiler directive is a special bit of syntax, in Flex, that tells the compiler
01:50what to do, rather than how to call something at run time.
01:53When you call the @Embed compiler directive in the context of an MXML tag, it
01:58gets an @ character as a prefix, and it looks like a function, but it has an
02:03upper case E at the beginning.
02:05You type in the beginning of the directive, and then after the opening
02:08parenthesis, you put in a single quote and then the relative location of the
02:12file that you want to embed.
02:13I'll type in assets/flowers.png, and then I'll move the cursor to the end of the
02:20line and complete the tag.
02:22Now I'm going to look at the application in Design view.
02:26There is the graphic that I'm embedding in the application.
02:28Now I'm going to right-click on the PNG file in the Package Explorer and go to
02:33the Properties and show you that this is a fairly sizable PNG file.
02:38The result of embedding this PNG file in the application will be that the
02:42application's SWF file grows by this amount, and it's quite significant.
02:47So, it's something you have to watch out for when you build your Flex applications.
02:50If the graphic is too large, you might think about taking it into Fireworks or
02:55some other graphics application and optimizing it, just like you would for a
02:59website, reducing its resolution in its native size to get it down to the
03:04smallest possible size.
03:05Once I've created the bitmap image, I can then resize it all I want.
03:10One way of resizing it is to look at the application in Design mode.
03:14And then use your mouse, click the corner, hold down the Shift key to make sure
03:19that you're maintaining the aspect ratio - that is the ratio between the height
03:24and the width - and then just drag and resize.
03:27If you look at Source mode after that operation, you'll see that that results in
03:31sending an explicit width and height in pixels.
03:35Here's another way of doing the same thing.
03:37You can scale an image by setting properties called scaleX and scaleY.
03:42So, for example, if I wanted this image to be three quarters of its original
03:46size, I would set the properties scaleX and scaleY to values of .75.
03:53With the scale properties, the value 1 means full-size, a fractional value means
03:58smaller and a value larger than one means larger.
04:01Now I'll look at the application in Design mode, and there's the result, and
04:05I'll drag it down toward the lower right-hand corner of the current
04:08application dimensions.
04:10That'll be good for now.
04:12Now I'm going to embed another image.
04:14This time, I'm going to embed a SWF file.
04:16This SWF file was created in Adobe Flash.
04:19There are a couple of ways of working with SWF files in Flex.
04:23If you're working with a static SWF file, such as this one - that is a SWF file
04:27that doesn't have an active timeline and doesn't have any embedded ActionScript
04:31code - you can simply treat it as an image.
04:34I'm going to embed this SWF file in the application, but it's important to note
04:38that the bitmap image control cannot deal with SWF files.
04:41That's because SWF files are vector graphics, and the bitmap image control is
04:46designed to work with bitmap images.
04:48So this time, I'm going to use the MX image control.
04:53Just like with the bitmap image control, you can use the embed compiler
04:57directive and compile the SWF file into your Flex application.
05:01I'll type in relative filename assets/ FlashLogo.swf and just like with the
05:09bitmap image, I'll use the scaleX and scaleY to set the image's dimensions as
05:14they're displayed to three quarters of the image's original size.
05:17I'll look at the application in Design mode and show you that the graphic that
05:22was generated in Flash is being displayed correctly.
05:25I can now click and drag and place it anywhere on the screen.
05:28When I do that, that adds X and Y properties, placing the object in an absolute
05:34position relative to the top- left borders of the application.
05:39Now I'll save and run the application in the browser and show you that the
05:43images are a part of the application and that they're loaded instantly up on
05:47application startup.
05:48So that's a look at how to embed images with the image tag in the bitmap image.
05:53They both result in compiling the image into the application, making the
05:56application SWF file larger, but allowing you to present the images
06:00instantly upon request.
Collapse this transcript
Presenting Flash content
00:00The Flex 4 SDK and Flash Player both deal with SWF files - that is Flash content
00:07with the file extension .swf - in the same ways they do other image files.
00:12You can either load SWF files at run time, or you can embed them in your application.
00:17As I've shown in other videos, you can't use the Spark BitmapImage control to
00:22display Flash content.
00:23Instead, if you're working with Static Flash content, you use the Image tag,
00:27just like you do with JPEG, GIF or PNG files.
00:31But if you're working with a Dynamic Flash presentation, either one built in
00:35Flash Professional or in Flex itself, there's another control that's designed to
00:40load those kinds of files, called the SWFLoader class.
00:44I'll demonstrate how to use this with an application called FlashContent.mxml.
00:48I'll place the cursor inside the BorderContainer object that's already in that
00:53application, and I'll declare a new object named SWFLoader.
00:58The SWFLoader class is the super class, or the parent class, of the Image class,
01:04and so a lot of the features of the Image class, such as the use of the Source
01:08property and the Load method, are also implemented in SWFLoader.
01:12I'll give this SWFLoader object an id of loader and close the tag.
01:18Now the SWFLoader object is a Visual Container, and it's going to load a SWF
01:22file - that is a precompiled Flash application - into the position where you place the class.
01:28I'm going to size the SWFLoader object explicitly.
01:31I'll click on the Design button to look at the application in Design mode.
01:35The SWFLoader object shows up in the top-left corner of the BorderContainer.
01:40I'm going to click in the lower right corner, and then drag out the size of the container.
01:45And I'm going to scroll down a bit so I can see the bottom of the container, and
01:49then I can fit the SWFLoader to just about the right dimensions.
01:53So, I'm leaving just a little bit of border around the container.
01:57Then I'll scroll back up to the top of the Application, and I'm going to drag in
02:01a Button Control from the Components view in the lower-left corner.
02:06When the user clicks the button, I'm going to react by loading a precompiled SWF
02:10file into the application.
02:11I'll double-click on the button to provide a Label, and I'll make the
02:16Label, Load Flash Content.
02:20And then, in order to execute a little bit of ActionScript, with the button
02:23still selected, I'll go to the Properties view, and I'll click the button with
02:27the lightning bolt next to On click and choose Generate Event Handler.
02:33This takes me to a new generated function that will execute when the user clicks the button.
02:38Now, I gave the SWFLoader object an id of loader, so when the user clicks the
02:43button, I'll use this code to load an existing SWF file.
02:47I'm going to use another of the Flex applications that I've built in this
02:50Project, named RuntimeImages.mxml.
02:54I'll call loader.load, and then I'll pass in the relative URL of the SWF file
02:59that I want to load.
03:00The name will be RuntimeImages.swf.
03:05Now that SWF file does not yet exist in my Source directory, but because I
03:10created the application RuntimeImages in a previous video, the SFW file for that
03:15application has been created in the Output folder.
03:18Both compiled applications will be in the same directory, and so the first, Flash
03:23content, will be able to load the second, RuntimeImages.
03:26I'm going to scroll back up and choose the FlashContent.mxml file again, and
03:32then I'll run it by clicking the Run button in the toolbar.
03:35Now when the application first loads, the BorderContainer is empty, but when
03:39I click Load Flash Content, it results in loading the other application as a
03:44subapplication, or a child application of the first, and it's completely interactive.
03:50Any functionality that I built into the child application, that is, the
03:53application that's nested in the larger one, will be completely active.
03:57So, I can click the button to change the Source, to call the Load method and do
04:02anything else that that child -nested application can do.
04:05So the general rule is, if you're loading a static SWF file, one that's designed
04:10just to present some vector graphics, use the image component.
04:14If the Flash content that you're loading is interactive, dynamic or has
04:18animations, use SWFLoader instead.
Collapse this transcript
Passing data with binding expressions
00:00In Flex applications, there are a number of ways to pass data, or information,
00:04around from one object to another, but one of the key techniques that
00:08developers use, and one of the things that makes Flex somewhat unique, is the
00:12use of Data Bindings.
00:14A Data Binding is a relationship between two objects, or more accurately,
00:18between two properties of two objects, that allows one object to broadcast
00:23changes to its properties and another object to listen for those changes and react to them.
00:28There are a couple of ways of declaring bindings.
00:31There's a shorthand and a longhand version.
00:33I'm going to show you the shorthand version here and then show you the longhand
00:37version in another video.
00:38I'll be working in an application called BindingExpressions.mxml, which is part
00:43of the Controls project in the Exercise Files.
00:47I'll place the cursor inside the BorderContainer - that's in the application
00:51already - and I'm going to create two controls:
00:54a TextInput and a Label.
00:57I'll start with the TextInput control, and I'll give it an id of myInput, and
01:04then I'll create a Label control, and I'll assign it an id of myLabel.
01:11Now let's say, for example, that the user types a value into the TextInput
01:15Control, and you want to snag that value, which will be represented in the
01:18control's text property, and pass it into the text property of the Label.
01:24You could do this by listening for an event and executing some ActionScript, but
01:27you can do it much more easily with a lot less code using a binding.
01:32Here's what the binding looks like.
01:34We describe the two objects in the binding relationship as the source and the destination.
01:39The TextInput control's Text property is the source, and the Label control's
01:43Text property is the destination.
01:46To use a shorthand binding, you start off in the destination object.
01:50You set the property of the destination object, and you refer to the id of the
01:55object you want to get the information from and its property, wrapped in a pair of braces.
02:00I'll type in the beginning brace and then type in 'm-y' and then press Ctrl+Space
02:06and select myInput from the list of available Controls.
02:10The reason Ctrl+Space worked there to bring up the content assist tool is
02:15because I had put in the brace character first.
02:18Then I'll put in a dot and select the property I want to get the value from,
02:22text, and I'll close the binding Expression with the closing brace.
02:27So that's the basic syntax of a binding.
02:29You wrap the expression inside a pair of braces, and you refer to the property
02:33of the object that you want to grab.
02:36By placing the binding expression in the destination object's property
02:39declaration, it will grab the value and place it in the Label.
02:44Now I'll test the application in the browser.
02:46I'll click into the TextInput control and start typing.
02:51So, as I type characters into the TextInput control, each time I add a new
02:56character, it triggers an event, which, in turn, triggers the binding.
03:00We call that executing the binding.
03:02The Label control is listening for the value and whenever it changes, it
03:06grabs it and presents it.
03:09In Flex 4, you can actually do something called two-way Bindings.
03:13To demonstrate this, I'll create another TextInput control, and I'll assign this
03:18one an id of input2.
03:22Now the first TextInput control is my Source object.
03:26You can actually use a single Source object with multiple targets and to do
03:31that, I'm going to select, copy and paste the existing Text property with the
03:36binding expression from the Label.
03:38I'll select it, I'll copy it to clipboard, I'll click into the second TextInput
03:45control, and I'll paste it into place.
03:50Now in its current state, as I type a value into the first TextInput control,
03:55the value will be presented in both the Label and the second Input control.
03:59But a two-way binding means that you can type into either of the Input controls,
04:04and the new value will be reflected in the other.
04:06Here is the shorthand for creating a two-way binding in Flex 4.
04:11Place the cursor before the existing binding expression, and then just add a
04:15single At character.
04:17That means it's a two-way binding, passing data back and forth between the two controls.
04:22I'll save and run the application, and I'll click into the first Input control
04:27and type input 1, and you'll see the value represented in both the Label and the second Input.
04:33Now I'll click into the second Input and change the value to input 2, and
04:39you'll see that value reflected both in the first Input and in the Text control.
04:44In a sense, we're now daisy chaining bindings.
04:46The first Input is connected to the second Input through the two-way binding,
04:51and the Label is still connected to the first Input.
04:54So, I make a change in the second, and that change ripples back through
04:57the chain of bindings.
04:59Finally, I'll show you one other use of binding expressions that will be useful.
05:03Up at the top of the application, I've created a control called a DropDownList.
05:07This is a DropDownList that present two strings, the names of the two graphic
05:12files I've used elsewhere in this chapter, aloe_vera.jpg and bonsai.jpg.
05:17I'm going to click into the commented section and then go to the menu and choose
05:22Source > Toggle Block Comment.
05:26When the user clicks on this control, it presents a list of the strings that are
05:30a part of its data provider, aloe_vera.jpg and bonsai.jpg.
05:36Now here's how I'm going to use those values.
05:38I'll place the cursor inside the BorderContainer, after any of its other nested objects.
05:45Then I'll create an Image tag.
05:47Notice that I'm using the MX Image tag and not the Spark BitmapImage.
05:51Only the MX Image tag will be able to do this trick because I'm going to be
05:55loading images at run time, based on changes in a binding expression.
05:59I'll set the Source property, starting with a string representing the location
06:06of my Image files assets/, and then I'll append the binding expression.
06:12The ID of my DropDownList is imageList, shown on line 9, and so I'm going to
06:18use this expression to get the string represented by the row that the user has
06:23selected, imageList.selectedItem.
06:28The selectedItem property returns the currently selected value from the list.
06:33I've wrapped that entire expression inside braces to make it a
06:36binding expression.
06:38So now, the Source property is a combination of a literal string,
06:42representing the location of the image and the binding, representing the name of the image file.
06:46I'll save and run the application and at the beginning, you'll notice that the
06:51image shows up as a broken image icon, because I'm not successfully loading an
06:56actual graphic file.
06:57But then I'll choose one of the items from the dropdown list, and you'll
07:01see that the graphic is successfully loaded and now I can switch back and
07:05forth between the two graphics by simply switching the item I've selected from the list.
07:10And the binding expression in the MX Image tag does all of the other work.
07:15Binding expressions are a critical aspect of Flex application development.
07:18They create relationships between visual and non-visual objects and make it very
07:23easy to pass data around the application without having to write a lot of
07:27explicit ActionScript code.
Collapse this transcript
6. Using the Flex Event Architecture
Handling dispatched events
00:00The Flex 4 development environment is event-oriented;
00:04that is, you typically execute ActionScript code in reaction to events that are
00:08dispatched by the application platform and your specific application code.
00:13For all the demonstrations in this chapter where I will talk about the
00:16event architecture in Flex, I'll use a project that's included with the Exercise Files.
00:21If you have access to the Exercise Files, import the project now.
00:25Choose File > Import Flex Project from the menu.
00:28Click the Browse button next to File and navigate to the Exercise Files
00:32folder 06_Events, begin.
00:35From there, select and import the file Events.fxp.
00:39Then open the Project, open its src folder, open the (default package) and open
00:45the application file EventLogger.mxml.
00:49This is a completed application that presents a Text control and a couple of buttons.
00:55The first button is going to be used to demonstrate different kinds of events
00:58that are dispatched by Flex controls.
01:01As each event is dispatched, the application handles the event and logs it by
01:06registering it in this TextArea control.
01:08When the user clicks the second button, labeled Clear Event Log, the TextArea
01:13control is cleared, so you can wipe out your event log and start over.
01:16Now, I am not going to talk too much right now about how this application is built.
01:20I'll show you how to build the sort of application in other videos of the chapter.
01:25I'll just go ahead and run the application.
01:27As the application starts up, the Button control dispatches three events.
01:32Every Visual control in the Flex SDK dispatches these events during
01:36its construction cycle.
01:38These are sometimes called life cycle events, and they happen with every
01:42control in the SDK.
01:44As a Visual Control is constructed, upon application startup or when it's first
01:48used in an application, it dispatches these three events:
01:52preinitialize, initialize and CreationComplete.
01:55They tell you, the developer, the stage of construction that that control is at.
02:00The preinitialized event tells you that the control has been read and
02:04constructed in memory, but is not at a state where you could present it on the screen.
02:08The initialize event tells you that the control not only has been read into
02:12memory, but also all of its properties and styles have been read into place.
02:17Finally, the CreationComplete event tells you that the control is completely
02:21ready for use in the application.
02:23These events always occur in the same order, and again, they're called life cycle events.
02:28Every component in your application dispatches these events, including
02:32the application itself.
02:33One useful thing to know about the application's events is that events always
02:37happen after the nested components events.
02:40So, for example, if you have a button inside a container, which is inside an
02:44application, The button's CreationComplete event would happen first, then the
02:48containers, and finally, the application's.
02:50So, when you get an application CreationComplete event, that means that the
02:55entire application is ready for use.
02:58Now here is some other events that I am listening for and handling on this
03:02Event Dispatcher button.
03:03I am going to move the cursor over the button, and you'll see in the EventLogger
03:07that two events occur called rollOver and mouseOver.
03:10The mouseOver event is called a raw mouse event.
03:13It tells you the physical aspect of what happened, that the user move the
03:17cursor over an object.
03:18The rollOver event is more of a logical event.
03:22It doesn't happen on all of the Visual controls in the Flex library, although
03:25it happens on most.
03:27Typically, if you want to react to the mouse moving over an object, you use
03:31the rollOver event.
03:32When I move the cursor off of that button, I get mouseOut and a rollOut event.
03:37Now I'm going to clear the event log and start over.
03:40I'll move the cursor over the Event Dispatcher button again.
03:44I'll click down with the mouse button, and you'll see two events occur,
03:47mouseDown and buttonDown.
03:49Now, keeping the mouse button pressed, I'll move the cursor off the button, and
03:53you'll see events named mouseOut and rollOut.
03:56Now I'll clear the event log again and show you another sequence.
03:59I'll move the cursor over the button, click down.
04:03Once again, I get mouseDown and a buttonDown.
04:06And then I will release the mouse button without moving the cursor off the
04:09object, and I get a mouseUp and a click event.
04:12The click event is one of the most commonly used events for Button controls.
04:17It tells you that the user clicked down on the button and released the button
04:21while keeping the cursor over the Button object.
04:24There are many other events that happened during the sequence, but you don't
04:27have to react to all of them.
04:29So, how do you find out which event a particular object dispatches?
04:34As with all such code, you start with the documentation.
04:37I'll look at the application in Source mode and show you how the events are being handled.
04:42Each of the events that I am handling is represented by an attribute in the
04:45Button tag, so there's preinitialize, initialize, CreationComplete and so on.
04:51In Flex, there is isn't a single universal event handler.
04:54You have to listen for each event you're interested in individually.
04:58But for each of those events, I am calling a single global function,
05:01called eventHandler.
05:03This function is defined up here, at lines 8 to 11.
05:07It has a single line of code, which is writing information about the event to
05:11the TextArea, which has an ID of Event Log.
05:14I am capturing the name of the event, which is expressed with this syntax
05:18event.type and then appending a line feed to the end of the line with the
05:23syntax \n. I'll talk about each element of this syntax in other videos, but for
05:29now, I just want to show you how to find out which events a particular
05:33component can dispatch.
05:35I'll go to the Button tag.
05:37This is the button that was dispatching all of those events that I was
05:40handling and logging.
05:41I'll press Shift+F2 to go to the documentation.
05:44When the Adobe Help application opens and shows me the API Documentation for the
05:48button class, I first maximize it so I can see it in fullscreen mode.
05:53Then in the list of the links, I'll click on Events.
05:57In the Events section, you'll see a link labeled Show Inherited Events.
06:01The Spark Button component, which is the component I'm looking at here, doesn't
06:05have any of its own events.
06:07Instead, it inherits them from all its superclasses.
06:11Here, for example, is the click event, which is defined in an object
06:14called InteractiveObject.
06:16To find out more about the click event, click into the link, and you'll see all
06:20sorts of information about the nature of that event. To go back to the original
06:24documentation for the Button, click the back button.
06:27So, these are all the events that the button control is capable of
06:30dispatching. Wherever you see an event name that is shown with the Adobe AIR
06:35Icon, such as contextmenu,
06:37that means that this is an event that can only occur in a native desktop
06:41application that's deployed with Adobe AIR.
06:43But otherwise, all of these events can be dispatched by the Button control, and
06:48you can listen for them in a variety of ways.
06:52So in other videos in this chapter, I'll show you a variety of approaches for
06:56listening to and handling these events using both MXML and ActionScript code.
Collapse this transcript
Using inline event handlers in MXML
00:00When an event occurs, you can react to it either with code that you put right
00:04into an MXML tag, or you can react by calling a custom function.
00:09If all you need to do when an event occurs is to execute a single statement of
00:13ActionScript code, it's frequently the easiest thing and the shortest amount of
00:17code to write the ActionScript code right there in the MXML tag.
00:21For this demonstration, I'll use the application file, InLineHandlers.mxml.
00:27I'll open and maximize the application file.
00:30This is a new version of my EventLogger application, which only has the Visual controls:
00:36the Label at the top, the TextArea control that will be used to log the events -
00:40that I'll use to listen for and react to events - and another Button that
00:44already has code to clear the Event Log.
00:47I'll start in the Button with an ID of eventListnerButton and a label of Event Dispatcher.
00:52I'll place the cursor after the Label attribute and press the Spacebar.
00:56Then type the first few characters of the click event.
01:00I see the click event displayed in the code assist list, and I press Enter, or
01:04Return, to select it.
01:05Now, if all I need to do is execute a single line of ActionScript code,
01:09I don't need a complete ClickHandler.
01:11Instead, I'll press Escape to clear the code assist list and then type in the
01:16ActionScript code I want to use, right here.
01:18I'll type in eventLog.text = and then in a pair of single quotes 'You Clicked.'
01:26So, my ActionScript code is right here inline in the MXML tag.
01:31Again, if that's all the code I need, there is no reason to write more.
01:35I'll save and run the application and show you the result.
01:38When I click the button, I see the message You Clicked.
01:41Now, if I click it multiple times, you'll see that it doesn't change.
01:45That's because in my code, I'm just setting the value of the text property, but
01:50I'm not appending to the existing text property.
01:53So now, to allow myself to log multiple events, I'm going to go back to the
01:57click Event Handler in the code.
01:59I am going to add plus before the equals.
02:02The plus equals operator, in ActionScript 3, appends text to an existing text value,
02:08rather than replacing the old value.
02:10Then I'll add a line feed right after You Clicked.
02:14I'll run the application.
02:16Then each time I click the button, I'm getting another item in the log.
02:20When I clear the Event Log, I am starting over, and then I can continue
02:24logging the clicks.
02:26So, that's how you create ActionScript code as an InLineEventHandler.
02:30Once again, if you only need to execute a single ActionScript statement, this is
02:35the easiest way to do it.
Collapse this transcript
Using custom event handler functions
00:00When you need to execute multiple lines of ActionScript code in reaction to an
00:05event, you're best off creating a custom function and calling that function when
00:10the event is dispatched.
00:12I'll demonstrate this in the Application file, HandlerFunctions.mxml.
00:18In the beginning state of this application, I'm listening for the click event on
00:21a button, and I'm logging the event by incrementally adding text to the text
00:27property of a TextArea you control with an ID of eventLog.
00:31In previous videos of this series, I've already described how to use Flash
00:35Builder 4's new feature that generates event handler functions from scratch, but
00:40in this example, I'm going to code it manually, so you can see how to write the
00:44tightest, most concise code, if all you need to do is call a single function.
00:49I'm going to start above the BorderContainer and create a little bit of extra
00:53space between the Application tag and the BorderContainer tag.
00:57Then I'll create a script section.
01:00I'll type in a less than character and type 'scr' and press Enter to select the
01:06fx:Script tag and then type the greater than character to autocomplete the tag.
01:12You can call any function you want in reaction to a click event.
01:16Typically, these functions have a name consisting of the name of the event
01:20you're listening for, plus the word Handler at the end and optionally, the id of
01:25the object that's dispatched in the event.
01:29I'm going to create a protected function.
01:31That means it's a function that's available to any code within this application
01:35and any ActionScript classes that extend the application.
01:40Now I will name the function clickHandler.
01:43For this function, I don't need to receive any arguments, or parameters,
01:48so I'll just put in an opening and closing parenthesis.
01:51And then at the end of the function, :void.
01:54This is a return data type declaration, and the word 'void' means that this is a
01:59function that won't return any values.
02:02When you write an event handler function for Flex, typically, you don't return a value from it.
02:08Now, right after the function declaration, I'll put in a pair of braces, and
02:13then within the braces, I can write as much ActionScript code as I want.
02:17I'll go down to the click event of the Button control that I'm listening to,
02:22and I'll select all of the ActionScript code between the double quotes for the Click attribute.
02:27Then I'll release the mouse button, and then click and drag and move that code
02:32up to the clickHandler method.
02:33So, now that code is being called when the method is called.
02:38I'll add a semicolon to the end of the line.
02:40It's not technically required, but it's a good style to use.
02:44And then in order to call the function, I'll go back to the Button controls
02:47Click attribute, and now instead of executing the code directly, I'll call the function.
02:53I'll type in the word 'click,' press Ctrl+Space and Flash Builder automatically
03:00fills in the method call.
03:01I'll save my changes, and I'll run the application and then click the
03:08button multiple times.
03:09And you'll see that the code is still working exactly the same way as before,
03:13but now, I have a function in which I can add as much code as I need to.
03:18So, that's how you create simple custom event handler functions.
03:22When you use Flash Builder's handler generation tool, you'll see quite a bit
03:26more code than this, but in many cases, it's not really necessary.
03:31If all you need to do is react to a particular event of a particular object,
03:35then the eventHandler function probably doesn't need any more than this:
03:39a simple function, returning void, and executing whatever ActionScript
03:44code you need to.
Collapse this transcript
Understanding event objects
00:00Each event in the Flash Platform, including the Flex framework, dispatches
00:05something called an Event object.
00:07An Event object is an instance of an ActionScript class, which contains
00:11properties that tell you all sorts of useful information about the nature of the event:
00:16what object dispatched the event, the name of the event
00:20and all sorts of other contextual information that can be incredibly valuable.
00:25To demonstrate this, I'll use the application file EventObjects.mxml.
00:30That's part of the project files.
00:32In the current state of the application, the button listens for its click event
00:37and reacts by calling a function called clickHandler.
00:41Right now, I'm not handling the Event object and so I have no way of getting
00:45this valuable information.
00:48Here is how you would redesign a function so that you're listening for and
00:51handling the Event object.
00:54First, I'll go to the place where I am calling the eventHandler function.
00:58That's the click attribute of the Button tag.
01:01When the event occurs, the name of the Event object variable is always the same.
01:06It's always named 'event' with all lowercase characters.
01:11So, to capture that Event object, you pass the Event object to the
01:15function, like this.
01:16You just type in the word event.
01:18It doesn't matter whether you're working with a visual control and
01:21non-visual control.
01:23It doesn't matter what the name of the event is.
01:25At the moment of handling it, the name of the object is always the same, event.
01:30Now, I'm going to refactor, or redesign, the function to receive that Event object.
01:35Within the parentheses, in the method signature, I'll type the word 'event' again.
01:41This will be the name of the parameter that I'm receiving.
01:44Now, you don't need to name it event here.
01:46You'll see some developers name it e, or ev, or evObj or EventObj.
01:53The name of the parameter doesn't matter.
01:55It can be anything you like, but the data type of this parameter is critical.
02:00You can always data type the Event object as a class named event.
02:06The Event class is the base class, or the super class, for a whole set of other
02:12event style classes that are a part of the Flex SDK.
02:16Each event that occurs dispatches its own type of event, but all of these event
02:21classes are subclasses, or are extended from the event class.
02:26So, even if you know you're getting some other event, such as a MouseEvent
02:30class - that's the class that's actually dispatched by a button's click event -
02:35you can always put in event as the data type, because it's the super class.
02:39And in the world of object-oriented programming, the polymorphic nature of this
02:44architecture means that you can refer to the object either as its native type or
02:50as its superclass type.
02:52Now, I'm going to do a little bit of debugging to examine the structure of the
02:56Event object when it's captured.
02:59I'm going to place a breakpoint on the line that includes the code,
03:02eventlog.text et cetera.
03:04Then I'll save, and I'll run the application in Debug mode by clicking the Debug
03:09button on the toolbar.
03:11When the application opens up in the browser, click the Event Dispatcher button.
03:16Then switch back to Flash Builder, if it doesn't take focus.
03:19And if you're prompted to open the Flash Debug perspective, click Yes.
03:24Now, take a look at the Variables view.
03:26Here is the event parameter that's being passed into the clickHandler function.
03:31I'm going to double-click the Variables tab to make it display fullscreen, and
03:35then I'll open up the tree structure that shows me what's in the Event object.
03:40Notice, first of all, that the name of the variable is event.
03:44That's because that's what I named it in my method signature.
03:47And also notice that the data type is flash.events.MouseEvent.
03:52This is the native data type of the Event object for this particular object and
03:57this particular event.
03:59Also, notice all of the different properties that are members of this Event object.
04:04A MouseEvent object, for example, has properties called ctrlKey and shiftKey,
04:10and altKey, which tell you whether those keys were being held down at the
04:13moment the event occurred.
04:16But under this inherited section, you'll see all of the properties that are
04:20members of this superclass, that is the event class or its superclasses.
04:26I'll open up the inherited section and show you two critical properties that are
04:30members of every Event object.
04:33The type property is the name of the event that occurred, and its value, in
04:38this case, is click.
04:40The target is a reference variable, which refers to the visual or non-visual
04:44object that dispatched the event, which, in this case, is a button.
04:50I'm going to drill down a little further here, going down to the target
04:53property, and from there, into its inherited section.
04:57And I'll scroll down to the label and show you that, in fact, this is the button
05:02with a label of Event Dispatcher that generated the event.
05:07So let's go back to the code.
05:09I'm going to terminate the debugging session by clicking the Terminate button on
05:13the Debug View, and then I'll go back to the Flash perspective.
05:19When you listen for this event, once again, you can either data type it as the
05:23superclass, event or as the subclass, MouseEvent.
05:28I'm going to use the MouseEvent class, because that's going to give me the
05:31ability to address all those other detail properties, such as, ctrlKey,
05:35shiftKey, and altKey.
05:38Then I'll add some code to the clickHandler method.
05:42I'll start by appending text to the eventLog.
05:46First, I'll output the name of the event.
05:53The name of the event is in the type property.
05:55So, I'll output, The event name is, and then I'll append to that event.type.
06:06Next, I'll log the id of the object that dispatched the event.
06:10I'll say The id of the target object is, and then I'll refer to event.target,
06:18that refers to the button control, and from there, to id, which is the id of the
06:23Button control as declared in my MXML code.
06:27And finally, I'll use one of the special properties of the MouseEvent class,
06:31like this, eventLog.text +=, then the literal string, Shift key pressed, and
06:39then I'll append to that event.shiftKey.
06:43And that will tell me whether the Shift key was being pressed when the
06:46click event occurred.
06:47I'll run the application and click the button, and I'll see all of this
06:51useful information.
06:53Now notice it's all being kind of run together.
06:55So, I'm going to make one more change here, and I'm going to add a line feed to
07:00each of these lines.
07:02I'll type in plus and then the '\n,' I'll select that and copy it to the
07:10clipboard by pressing Ctrl+C, or Command +C on Mac, and then I'll paste it into
07:14each of the other lines of code by pressing Ctrl+V, or Command+V on Mac.
07:20Then I'll, once again, run the application, and now I should see a nice, clean
07:26logging of information.
07:29The event name is click.
07:31The ID of the target object is eventListenerButton and Shift key pressed is false.
07:38This time, I hold down the Shift key and click the button again, and I'll see
07:42that Shift key pressed returns true.
07:45So this is how you can use Event objects to get detailed information about the
07:49events that have occurred.
07:52If you want to find out which event class is being used for a particular event,
07:57use the documentation.
07:58I'll go to the Button control and press Shift+F2 to go to the API
08:02documentation for that class.
08:05From there, I'll click the Events link, I'll click the click link, and that will
08:11take me to the description of that event.
08:14Notice here that the documentation tells me exactly which event class will be
08:18used, flash.events.MouseEvent.
08:21And I can click into that link and get all the detailed information about
08:25that class as well.
08:27The API documentation is critical when you start working with the event
08:30architecture of Flex.
08:32It will tell you which event class is being used for which object and which
08:36event, and what detailed information is available in that event class.
Collapse this transcript
Using event listener functions
00:00As I've shown in other videos, you can listen for and react to events using
00:05MXML-based inline event listener code, but another, and somewhat more flexible
00:10approach, is to create your event listeners using ActionScript.
00:14To demonstrate this technique, I'll use the application EventListeners.mxml.
00:19In this version of the application, I once again have a function called
00:23clickHandler, which is accepting an event object, datatyped as MouseEvent.
00:28This is the event class that's used whenever you deal with a mouse event, such
00:32as click, mouseover, mouseup, mousedown and so on.
00:37Right now, in the application, I have a Button with an ID of
00:41eventListenerButton, which has a click event Handler declared using MXML.
00:46When I run the application and click the button, I get output that tells me all
00:50about the event that occurred.
00:52Now, I'm going to show you how to register that Event Listener using
00:56ActionScript code instead.
00:57I'll close the browser and return to Flash Builder.
01:01I'm first going to remove the click event from the MXML tag.
01:07Next, I'll go to the application tag and add a creationComplete event handler,
01:12using Flash Builder's automatic handler generation feature.
01:17I'll press the Spacebar, after the backgroundColor attribute, and choose the
01:21creationComplete event from the list.
01:24Then when the Generate CreationComplete Handler prompt appears, I'll press
01:28Enter, or Return, to automatically generate an event handler function and the matching call.
01:35Notice that this syntax follows the same rules as the clickHandler function that
01:39I created manually in another video.
01:42In order to be used in an event handler architecture, event handler functions
01:46should receive a single argument, the event object, datatyped as the appropriate
01:52class for that object and event, and should always return void.
01:56Here is how I'm going to add the event listener using ActionScript code.
02:01I'll go to the application1_ creationCompleteHandler function, remove the
02:05generated comment, and then I'll start with the id of the object to whose
02:11event I want to listen.
02:13The id is eventListenerButton.
02:16Then I'll put in a dot, and I'll call the function addEventListener.
02:21When you call addEventListener, you pass in two arguments minimally.
02:25The first argument is a string, which is the name of the event you're listening to.
02:30Now, I could pass in the simple string "click," wrapped in quotes, like this.
02:36But the problem with that approach is that it's very prone to error.
02:39If I misspell the event name, for example, if I leave out the c, then I'll be
02:44listening for an event that will never happen.
02:47And it won't be an error that can be caught by the syntax checker at compilation
02:51time, or by the runtime engine.
02:53It will just be a bug in my program that's very hard to track down.
02:58So instead, when you type in the addEventListener function and the opening
03:01parenthesis, you'll see a list appear of classes and their contained constants.
03:07These constants represent the names of events that this control is capable of
03:12dispatching, in this case, a Button control.
03:16I'm going to be listening for this event, MouseEvent.CLICK.
03:21And notice, if I move the cursor over the word CLICK, I see a description of the
03:25constant, that it's a string and that it's a member of the MouseEvent class.
03:30Its value is the word click as a string all lowercase.
03:35Now the second argument that you must pass in is the function that you want to
03:40call when the event occurs.
03:41You only pass in the name of the function.
03:44You're not calling it at this moment.
03:46And technically speaking, you're passing in a reference to the function as an object.
03:51I'm going to type in the name of the function, clickHandler.
03:55And that now means that when the click event occurs, the clickHandler
03:59function will be called.
04:01The syntax of the clickHandler function is already known to the object that
04:05will call it, because it follows the signature, or model, for every eventHandler function.
04:11It receives the event object, and it returns void.
04:16I'll run the application again and test it, clicking the Event Dispatcher button.
04:20And I see that even without the MXML- based listener, my click event is still
04:24being heard and responded to.
04:27Now the Flex 4 SDK allows you to add and remove event listeners
04:31using ActionScript code.
04:34First, to demonstrate this, I'm going to rename this function,
04:37application1_creationCompleteHandler, and I'm going to use Flash Builder 4's
04:42refactoring capability to rename the function and update all of its references.
04:47I'll right-click on the function name and choose Refactor > Rename.
04:54And I'm going to change the name of the function simply to initEvents.
05:00You can name the function anything you want,
05:02but all I'm doing with it is initializing event listeners.
05:06I'll click OK, and see that the name of the function has been changed.
05:11And the call to the function has been changed as well.
05:14Now, I'm going to scroll down to the bottom of the code, and I'll find a button
05:19there labeled Add Listener.
05:21And I'll add an event handler for this button.
05:25And in the clickHandler button, I'm going to copy and paste the code from the
05:29initEvents method, creating a copy of it in this button's clickHandler.
05:34Then I'll go to this button, labeled Remove Listener.
05:37I'll add a click event.
05:39Once again, I'll generate an automatic Handler function for it.
05:43And when the user clicks this button, I'm going to remove the event
05:46listener, EventListenerButton. removeEventListener, and I'm going to pass in
05:52exactly the same two arguments:
05:54MouseEvent.CLICK, and the name of the function, clickHandler.
05:59So now, when the user clicks this button, it will remove the event listener from
06:04the object, and it will no longer respond to the event.
06:07I'll run the application and test it.
06:10As the application starts up, it's adding the event listener automatically.
06:14So, if I click the button, I'll get a response.
06:18I'll clear the event log, and then click once on Remove Listener.
06:22I don't see any visual response because I didn't do anything to make it respond
06:26in the code, but now when I click the Event Dispatcher button, nothing happens.
06:30I'll click Add Listener again, and now I, once again, am responding to the event,
06:36executing my ActionScript code.
06:39So to add an event listener, you use the addEventListener method, and to remove
06:44it, you call removeEventListener.
06:46In both cases, you pass in the name of the event you're listening for and a
06:50reference to the function you want to call, and the function must follow the
06:54standard event handler syntax, accepting a single argument datatyped as the
06:59appropriate event class and returning void.
Collapse this transcript
7. Laying Out an Application
Setting the application's layout property
00:00In Flex 4 applications, you lay out the application's appearance using a variety
00:05of containers that are known as Layout Containers.
00:08The application itself is a layout container, as is the panel, the group, and
00:13a number of others.
00:15For the demonstrations in this chapter, I'll use a Project that's part of
00:18the Exercise Files.
00:20If you have access to the Exercise Files, go to the menu and choose
00:23File > Import Flex Project.
00:27Browse and select the file Layout.fxp, which you'll find in 07_Layout/begin
00:33folder, under Exercise Files.
00:36Select and import the file, and that will create a new project called Layout.
00:43Open the new project and in the default package, open the application
00:48file AppLayout.mxml.
00:51I'm going to take a look at this application in Design mode.
00:54You'll see that it presents a variety of visual objects, including an Image
00:58control, a Button, a CheckBox, a Date Chooser, a PopUpButton and a Data Grid.
01:06These objects are scattered around the screen, and they are currently placed in
01:10their particular positions based on their X and Y properties.
01:14For example, the Image has an x property of 31 and a y of 40, meaning that its
01:19place 31 pixels from the left and 40 pixels from the top.
01:24The Application container, and all other spark containers that use the new layout
01:29architecture, by default, use architecture called Basic Layout.
01:33If you've worked with Flex 3, this will be similar to Flex 3's Absolute Layout,
01:38meaning that objects are placed in specific pixel positions on the screen, or
01:42relative to other borders if you know how to use constraint-based properties.
01:46If you prefer, you can set up your application to stack items from top to
01:51bottom, using a special class called VerticalLayout, or from left to right
01:56using HorizontalLayout.
01:58You can set these properties in code or you can do it in Design mode, and the
02:02code will be generated for you.
02:04I'll start in Design mode.
02:07I'll click on the background of the application, and then over in the Properties
02:11view in the lower right-hand corner, I'll scroll down to the bottom, and I'll
02:15open up a dropdown list that shows the four available Layout classes.
02:19Now in Flex 3, the Layout property was set to a simple string:
02:23a Vertical, Horizontal or Absolute.
02:25In Flex 4, you set the Layout property to an instance of a complex class.
02:31These are the four Layout classes that are provided for you.
02:33I'm going to start with a VerticalLayout.
02:37Notice in Design mode that as soon as I choose that item that objects are now
02:41stacked from top to bottom.
02:44Also, once you've chosen that layout architecture, you can add properties,
02:48such as the Gap between objects, the Alignment, the Row Height and Count and
02:55 Padding settings.
02:56So for example, if I want to provide a 30 pixel Padding all around the application,
03:01I would add a value of 30 to each of the Padding settings:
03:07Left, Right, Top and Bottom.
03:09Now, when I run the application, the result will be a nice 20 pixel padding
03:13between the border of the application and the first visual object, and that's on
03:18all sides, because I said all four Padding properties.
03:23Now, before I choose another Layout architecture, I'll show you the resulting code.
03:27I'll click the Source button, and show you that the Layout property has been
03:33populated with an instance of the VerticalLayout class.
03:36Each of the settings that I set through Design mode has been translated into an MXML attribute.
03:43If you don't want to use VerticalLayout, perhaps you want to use
03:45HorizontalLayout. It works exactly the same, but instead of Vertical, you type
03:51in 'Horizontal' or choose that setting from Design mode.
03:56Once again, look at the application in Design mode and now I'll see the objects
04:00laid out from left to right.
04:02Notice that these objects have their vertical tops aligned with each other.
04:07If you don't like that, you can change it using a style called verticalAlign.
04:10I'll change it to middle, and then I'll look at the application in Design mode
04:16again and now the objects are aligned in the middle of the screen.
04:22So, that's how you set the Layout of an application in Flex 4.
04:26You can choose from any of the Layout classes, and the ones provided with the
04:30Flex 4 SDK include HorizontalLayout, VerticalLayout, TileLayout and BasicLayout,
04:37which is the default, and allows you to place objects in positions relative to
04:41the top-left corners of the application.
Collapse this transcript
Using Spark Group containers
00:00Flex 4 provides a number of Layout containers that allow you to group and
00:04lay out content together.
00:06The most low level of these containers, and the lightest weight, is called
00:10the Group container.
00:11There are three variations on the Group:
00:14Group itself, and then two subclasses of the Group container, named VGroup, for
00:19vertical layout, and HGroup, for horizontal layout.
00:22For this demonstration, I'll use the application GroupDemo.mxml.
00:28This application has three buttons laid out in specific positions, based on
00:33their X and Y positions.
00:35I'm going to group them together and initially, I'm going to use a class called VGroup.
00:40A VGroup is a Group Container that already has vertical layout built into it.
00:46I'll go to the code and place the cursor before the three button tags.
00:51Then I'll create a paired s:Group tag set.
00:55I'll type in the less than character then the word Group, and I'll choose
01:00s:Group from the list of available classes and then autocomplete the MXML element.
01:07Now I'll take the three buttons, and I'll drag and drop them and move
01:12them inside the Group.
01:14I'll select all of that code and then choose Source > Correct Indentation to
01:19properly show the nesting of the objects within the container.
01:23If you use a simple Group Container, it's going to implement basic layout by
01:27default, just like the application.
01:29For example, here I have a group containing the three buttons and notice that
01:34the buttons are retaining their positions relative to the top-left corner of the
01:38Group, rather than the application.
01:41If I were to reposition the Group by setting its X and Y properties, the entire
01:45Group would move around.
01:47If on the other hand, I want to Group these three buttons together and make them
01:51stack on top of each other,
01:53I can either add a Layout property, set to an instance of the VerticalLayout
01:57class, or more simply, I can simply change the Group to a VGroup.
02:02The VGroup stacks objects from top to bottom, because it automatically has a
02:08layout property of VerticalLayout.
02:10I'll show the same application in Design mode now, and there is the result.
02:15Now the object's layout is determined by the Gap, Alignment and other
02:20properties of the VGroup.
02:23Now, before I copy and paste this code to show you other groups, I want to show
02:27you that the X and Y properties of the three buttons are still in the code, but
02:32they aren't doing anything anymore.
02:34Because I've placed the buttons inside a VGroup, the Layout is now automatic.
02:38So, I'm going to remove the X and Y properties from all three buttons.
02:43Then I'm going to copy and paste the Vgroup.
02:45I'm going to set the second VGroup's X property to a value of 250, meaning that
02:54it will be placed 250 pixels from the left side of the application.
02:59For the second Group, I'll change the VGroup to an HGroup.
03:04Then I'll look at the application in Design mode, and there is my second Group.
03:10Because I used an HGroup, the objects are laid out from left to right.
03:15Now one of the limitations of both the VGroup and of the HGroup and the
03:18Group itself is that you can't provide skinning, or visual presentation, to the container.
03:24The Group object is the lightest weight of all the containers.
03:28It doesn't support background colors.
03:30It doesn't support borders, or any other visual presentation.
03:34Essentially, a Group is always invisible and only presents its nested content.
03:39If you want to group content together and add some visual presentation to the
03:43container itself, there are two main options.
03:48One is to use the BorderContainer.
03:50The BorderContainer component is one I've shown in other videos.
03:54It provides some of the most commonly used styles from the world of Flex 3, such
03:58as Background Color, Simple Borders and so on.
04:02The other one you can use is called SkinnableContainer.
04:06A SkinnableContainer implements the Layout property just like BorderContainer
04:11and the group components, but it supports the Spark Skinning Architecture, which
04:16allows you to create a separate MXML skinning component and bind that skin to
04:21the container either during compilation or at run time.
04:25I'm going to copy and paste the three buttons into the BorderContainer and into
04:30the SkinnableContainer.
04:33Then I'm going to cause them to stack on top of each other by adding a Layout
04:37property set to VerticalLayout inside the application.
04:46I'll take that Layout property.
04:49I'll Copy it to the Clipboard, and I'll Paste it into both the BorderContainer
04:55and into the SkinnableContainer.
04:57The point I want to make here is that the Layout property is the same for all of
05:02these Spark-based containers.
05:04The big difference between them is that Groups are invisible, don't have any
05:09visual presentation, whereas BorderContainer and SkinnableContainer do have
05:13visual presentation.
05:15The BorderContainer is typically used when you want to set simple styles and the
05:19SkinnableContainer, when you want to do something more complex.
05:24I'll save and run the application.
05:27The first three buttons are in the VGroup.
05:29The second, three laid out horizontally, are in the HGroup.
05:34The third are in the BorderContainer, and the final three buttons are in the
05:38SkinnableContainer, which currently, does not have a skin applied.
05:42So, those are the primary ways that we lay out objects on the screen in Flex
05:464 Applications.
Collapse this transcript
Using the Spark Panel container
00:00The Flex SDK implements a special container called the panel, which has the
00:05appearance of a dialog box but is really a two-dimensional container, just like
00:09other containers I've shown in other videos.
00:12For this demonstration, I'll use the application panelDemo.mxml.
00:17This application presents a number of labels.
00:21There are three labels that have titles or category names, and three other
00:26labels that show larger amounts of text.
00:30Now I'm going to show you how to take the text that's presented, using the Label
00:34objects in this application, and wrap the text into panels, so that they have
00:39more of a dynamic appearance.
00:42I'll start above the first Label, after the fx:Declarations element.
00:46I'll create my first panel like this.
00:48I'll type in a less than character and then 'p-a.' That takes me to the panel component.
00:55Notice that there are two versions of panel, the older MX version and the new Spark version.
01:01In a Flex 4 Application, you typically want to use the new version.
01:04I'll select s:panel, and then type in the greater than character to
01:09autocomplete the panel.
01:10Now, there are a couple of places where you can display content in a panel.
01:14The first is in the title.
01:16The title is a simple string that gets presented in the top-left corner of the
01:20header of the panel.
01:22I'll go to the Label that displays the text Shrubs.
01:26I'm going to cut that text from the Label and move it into the title
01:30attribute of the panel.
01:33I'll set title="Shrubs".
01:38Then because I no longer need it, I'll delete the Label.
01:42Next, I'm going to take the Label that has the longer text
01:47and I'm going to cut and paste it and place it inside the panel.
01:51So, now the panel contains a Label with a large amount of text.
01:56The Label is word wrapping the text automatically.
01:59Now, let's take a look at the application in Design view and see what happened.
02:05The panel Container can be moved anywhere on the screen right now, because the
02:09application is using the default basic layout.
02:12The title appears in the top-left corner in the header region, and any content
02:17that you place within the panel is placed within its content group, which, by
02:22default, has a white Background.
02:24Now, you'll notice that the Layout is a little bit interesting, because I'm
02:28getting some extra Padding at the top and on the side.
02:31If you want to control the layout of a panel, you do the same thing as with the
02:36Application, Group and other containers.
02:39You set a Layout property.
02:41Just like those containers, the Layout property supports the default
02:45BasicLayout, Horizontal, Vertical and TileLayout.
02:48I'll add a Layout object, set to VerticalLayout.
02:58Then I'll set the four Padding settings to 10 pixels each.
03:13Here is the result.
03:14Now, the panel presents the text with 10 pixels all around.
03:18The width of the panel, right now, is being determined by its nested Label control.
03:23The Label has a width of 200 pixels.
03:25You add the extra padding all around, and you end up with the panel width.
03:30Now, I'll repeat the exercise for the other labels.
03:36In order to make this go a little bit more quickly, I'm going to Copy and Paste
03:40the panel two times.
03:45Then I'll go and get all the text and move it into the right place.
03:48I'll go to the Label with Container Plants, go to the second panel and Paste
03:55that into the title.
03:58Then I'll go and get the text below Container Plants, select all of that
04:05text, copy it to the clipboard, and then come back to the Label that's nested within the panel,
04:13I'll select and delete all of the text that's already there, and then paste the new text in.
04:20Then I'll do the whole operation again, this time, for the last Label and panel.
04:29I'll go to the third panel and paste in the text, Herbaceous Perennials.
04:39Then I'll go grab the text from the final Label and Paste it into the Label
04:43inside the third panel.
04:46Finally, I'll delete all of the remaining labels at the bottom of the application.
04:51If you're following along and want to check your work, take a look at the
04:54Outline view in the lower- left corner of Flash Builder.
04:58You should see three panels, with three different titles of Shrubs, Container
05:03Plants and Herbaceous Perennials.
05:06Within each panel, there should be a Layout object and the Label.
05:10And because I copied and pasted the VerticalLayout classes, I'm using the same
05:14padding for all three panels.
05:16Now to lay out the panels, I'll add a Layout object to the application itself.
05:22I'll add a layout tag set as a property, and then, I'll use the
05:27HorizontalLayout object.
05:29Because I'm using HorizontalLayout for the panels, I no longer need the X and Y settings,
05:34so I'll go through and remove those from all three panels.
05:40If you have trouble finding any of the code at any point, go to the Outline view
05:45and use the outline to get to the object you want to modify.
05:48Here is the panel for Herbaceous Perennials.
05:54I'll Run the application, and there is the result, three panels with three
06:00labels and each with an appropriate title.
06:03So the panel container can be used to present any sort of content.
06:07In these panels, I'm only presenting one label per panel, but you can add as
06:12many visual controls and objects to your panels as you like.
06:15And as I'll show you in a separate video, you can also add a control bar at the
06:19bottom of the panel to display buttons and other interactive controls.
Collapse this transcript
Using the Application and Panel control bars
00:00The Flex 3 SDK included a container called the Control Bar.
00:04The Control Bar was a container designed to be placed at the bottom of a panel.
00:09When you placed the control bar at the bottom of the panel, it created a footer
00:12in the panel, and you could then add other objects, horizontally laid out, such
00:17as buttons and other interactive objects, creating a wizard style interface.
00:23Flex 4 implements the same sort of functionality but as a property of the panel
00:27called controlBarContent.
00:29I will show you how to use this in an application called ControlBarDemo.mxml.
00:36This application contains a panel.
00:38The panel has a title and a rich text object with some mixed content.
00:43Now, I am going to add a series of buttons in the panel, like this.
00:46I will go to the code, and I will place the cursor at the bottom of the panel,
00:51just before the closing tag, and I will make a little bit of space.
00:55In the Spark version of the panel container, controlBarContent is a property of
01:00the panel, rather than its own separate object.
01:02So, you declare it like this, controlBarContent as a paired tag set.
01:09Now, any objects that you place within this property will be laid out
01:13horizontally from left to right, in a bordered area.
01:16So, I will create a few buttons.
01:18I will give the first one a label of Click me, and then I will create a second
01:25button, and I will give that a label of No, click me.
01:30Now, I will look at the application in Design view and show you that the buttons
01:34are laid out side-by-side.
01:37In some applications, you might want to create some space between the buttons,
01:41in fact, having each hung on the right or left side of the container.
01:46You can do that in the controlBarContent by adding a Spacer Control.
01:50The Spacer Control is a member of the older mx component set, and it has not
01:54been replaced for Spark, so we still use it.
01:57It looks like this.
01:59It has an mx prefix, and you set its width or height to the number of pixels, or
02:05percentage of available space that you want it to take up.
02:09So, for example, I will set this spacer's width to 100%,
02:14meaning take up as much space as you possibly can.
02:17The result will be to push the two buttons outward,
02:20so that they will be flushed with their left and right sides of the panel.
02:23I will save and run the application this time, and you will see the two buttons
02:28lined up in the panel at the bottom in the footer area.
02:32The spacer is invisible and takes up the space where I have my cursor right now.
02:36The two buttons are pushed to the side, with padding around the buttons, with
02:40the padding size being determined by the skin that's applied to the panel.
02:45Now, if you want to change the appearance of a Spark panel, you do it through skinning.
02:50I will be showing you how to define a skin for a Spark-based component in
02:54another chapter of the video series,
02:56but it's worth knowing that if you are a Flex 3 developer that many of the
03:00properties and styles that we use to modify the appearance of these components
03:05no longer apply to Spark components.
03:07Instead, you create custom skins and so for example, if you want curved corners,
03:13you would do it through the skin.
03:15If you want to change the color of the shadings in the header and footer, you do
03:19it through the skin.
03:21So, this is a look at how to create controlBarContent in a panel.
03:25Once again, it's a property of the panel component, in Flex 4, and when you add
03:29visual objects, they are laid out horizontally from left to right.
Collapse this transcript
Using the BorderContainer
00:00Spark-based containers, such as the panel, the skinned container, and the
00:05application, require custom skins if you want to make drastic alterations
00:10in their appearance.
00:11But there are lots of times in a Flex 4 application where you simply want a
00:15rectangular area with a different background color, background image, border
00:20or rounded corners.
00:21The BorderContainer component is designed to provide this capability with just
00:25simple Style settings.
00:27I have already used the BorderContainer in other videos in this series, but I
00:31am going to demonstrate it thoroughly here, so you can see its various capabilities.
00:36For this demonstration, I will start with the file BorderContainerDemo.mxml.
00:41This application has a Group container wrapped around an image.
00:45The image is displaying an embedded SWF file.
00:47I'll run the application in the browser and show you that the group container
00:52that's wrapped around the SWF file doesn't have any visual appearance of its
00:56own. All you see is the graphic.
00:58I would like to wrap this graphic inside a container that has a particular
01:03background color and a particular border.
01:05So, here are the steps.
01:08First of all, I am going to replace the group with a BorderContainer component.
01:13I will make the change to both the starting and the ending tags for this element.
01:23Then I am going to add a VerticalLayout object as the layout property.
01:28The reason I am adding the VerticalLayout object, even though this container
01:32only has one nested object, the Image, is that the VerticalLayout, and the
01:36HorizontalLayout components allow the use of the padding styles.
01:40This will allow me to add a little bit of space around the image without having
01:44to do too much additional work.
01:46I will set the four padding styles, paddingBottom, paddingLeft, paddingRight and
01:57paddingTop, all to 20 pixels each.
02:01Then I will look at the application in the Design mode and show you that the
02:05BorderContainer creates a white background by default.
02:09Now so far, this isn't that much more interesting than what I already had.
02:13To get a really striking appearance, I am going to need to add some additional styles.
02:17So, I will go back to Source mode.
02:21Here are some of the styles that are supported in the BorderContainer.
02:25First, I will add a backgroundColor set to a value of #6DB6C6.
02:34Now, after adding each style, I will show you what the application looks like in Design mode.
02:39And you will see that I now have a nice compatible color, displayed by
02:43the BorderContainer.
02:45Now, I will add a border.
02:46The border will be of a particular color with a particular weight and
02:51a particular style.
02:52I will start with the borderStyle setting.
02:56The borderStyle can be set to one of two values: inset or solid.
03:00I will use an inset border.
03:03Next, I will set a borderColor.
03:06The borderColor can be set to any hexadecimal color value.
03:09I will use this one, #84CBDA, and then I will set the borderWeight to a value of 10.
03:19That means that the border will be 10 pixels all around.
03:22Let's take a look at Design view and see how we are doing.
03:25So, now I have some nice background colors and a good border around the graphic.
03:30I would also like to have this container have rounded corners.
03:34Again, if you are working with one of the Spark components that uses custom
03:37skins, this requires a bit of work.
03:40But if you are using the BorderContainer it's a simple style
03:43called cornerRadius.
03:44I will go back to Source mode, and I will set the cornerRadius to a value of 15.
03:51Once again, I will look at the application in Design view and show you that the
03:55cornerRadius style results in creating a rounded corner on both the inside and
04:01the outside of the border.
04:03Finally, I am going to add one more style.
04:06If you want a drop shadow on a visual object in Flex, typically you have to
04:10add a Filters property and then one or more Filter objects, including the
04:15DropShadow Filter object.
04:17But again, the BorderContainer component is designed to give you shortcuts for
04:22some of the most commonly used visual effects.
04:26And so, I will use this setting, dropShadowVisible=true.
04:30I will look at the application in Design view again, and there is my
04:34DropShadow, down at the bottom.
04:36Now, the BorderContainer automatically will size itself to accommodate
04:40its nested objects.
04:42And right now, its size is determined by its nested graphic.
04:45I am going to resize the graphic by first clicking on it in the middle of the object,
04:51and then I will move the cursor down to the visible handle in the lower-right corner.
04:55I will hold down the Shift Key to make sure that as I resize it, I maintain the
05:01graphic's aspect ratio.
05:03And then I'll reduce the size down to 250x154 pixels.
05:08And you will see that when the graphic resizes, so does the
05:12surrounding BorderContainer.
05:14Then finally, I will click outside the graphic, and that selects the
05:18BorderContainer, and then I will click and drag down and to the right,
05:21until the BorderContainer snaps into place.
05:24I will go back and look at the source code now and show you that that results
05:29in placing the object 14 pixels from the left and 13 pixels from the top border
05:34of the application.
05:35I will save and run the application in the browser, and here is the result.
05:42My logo is now nested within a rectangular area, with a nice background color,
05:48border and rounded corners, all created with very simple styles.
05:53You could create this same appearance, using a complex custom skin.
05:58But if all you need to do is affect those settings, the background color, the
06:02rounder corners and the border, then the BorderContainer component is the
06:06easiest way to get the job done.
Collapse this transcript
Using constraint-based layout properties
00:00Flex 4 containers can place objects in a variety of ways: using the x and y
00:05properties in basic layout, meaning objects are placed relative to the top left
00:10borders of a container, using vertical layout, horizontal layout or tile layout.
00:16Another very common approach to placing objects on the screen is to use
00:19Constraint properties.
00:21Constraint properties only work in containers that use basic layout, that is
00:26that support the x and y properties. But instead of limiting yourself to placing
00:31objects relative to the top left border, you can also anchor objects to the
00:36bottom, right, horizontal center and vertical center.
00:41For this demonstration, I'll use an application named ConstraintDemo.mxml.
00:46When I first open the application in Design mode, you'll see that it has
00:50three main components:
00:52a BorderContainer containing a logo, a panel containing some text and an Image control.
01:00The application looks good when I present it in this particular size, but when I
01:05run the application in the browser, you'll see that not everything is good.
01:10The application looks great when it first starts up, but when the user resizes
01:15the browser everything stays in the same place.
01:17The purpose of using Constraint properties is to position objects dynamically,
01:22based on the size of the container and the properties that you apply.
01:26You can set Constraint properties either in code or in Design mode.
01:30I'll start in Design mode and then show you the generated code.
01:34I'm going to start with the flowers picture.
01:37Notice in Design mode that I've placed it in the lower right-hand corner of the
01:41screen, but you saw when I ran the application in the browser that the image
01:45stayed in the same place.
01:47I'd like it to always be in that lower right-hand corner.
01:50I select the Visual control in the Design area and then go to the Properties
01:55view and scroll down to the bottom, the Size and Position section.
02:00This is called the Constraints interface.
02:02It allows you to select any of the six constraints, which are, respectively:
02:08left, right, top, bottom, horizontal center, and vertical center.
02:14Each of these checkboxes represents one of those constraints, and when you select
02:18one of the checkboxes, you're actually adding that Constraint to the object.
02:23So if I click on this checkbox, that adds a right Constraint and below the
02:29Constraints interface, a text field pops up.
02:32Notice that the control is set automatically at zero pixels from the right
02:36edge of the application.
02:38Now the reason that happened was because that's where the object was positioned
02:42in my current Design dimensions.
02:44I'll deselect the Constraint and move the object a little bit and then choose
02:49the Constraint again, and this time, you see a different value.
02:53So don't take the initial value that is proposed by Flash Builder too seriously.
02:58It only shows you the position of the object in the current dimensions of the Design area.
03:03I'll select the object, check the Constraint, and then click into the text
03:08field and set its value to zero, and that glues or docks the object to the right
03:14side of the screen.
03:15Now, I'll also set a bottom Constraint for this object.
03:18I'll click the option, then go to the matching text field and once again, enter
03:24a value of zero and when I press Enter, that saves the change, and the object
03:29will now be in the lower-right corner.
03:32I'll save and run the application and when the browser opens, you'll see the
03:38flowers are glued to the bottom-right corner of the application and if the user
03:43resizes the browser, the object moves accordingly.
03:47Now let's take a look at the code that was generated.
03:49I'll close the browser and go back to Flash Builder and here's the BitmapImage
03:54control that's presenting the flowers.png file.
03:57I'll refactor the code a bit, adding a line feed so that I can see all of the
04:02attributes of the tag onscreen.
04:04Here are the two Constraint settings that I've applied:
04:07the right and bottom attributes.
04:09When you set these values to zero, that means that you're making the object
04:13flush to the border.
04:15Now let's take a look at a couple of other Constraints.
04:18I'll click on the panel, and then, once again, go over to the Properties view,
04:22and scroll down to the Size and Position section.
04:25This time, I'm going to use the horizontal center Constraint.
04:29I'll click that option and set to a value of zero.
04:36I'll save and run the application, and you'll see that the dialog box appears in
04:41the direct horizontal center of the application, and as I resize the
04:46application, it snaps into place, staying centered.
04:50You can also use Constraints to resize objects, by adding two Constraints in
04:55the same dimension.
04:56So for example, for this panel, if I want to center it but also stretch and
05:01contract it as the browser is resized, I can get rid of the horizontal center
05:06and instead set both left and right Constraints, and I'll set those values
05:11to 150 pixels each.
05:14I'll run the application in the browser, and now watch what happens as the user
05:19resizes the browser.
05:21The panel changes its size automatically, and the Label control, which is nested
05:26within the panel, has its width set to 100%, so it stretches and contracts at
05:31the same time as the panel itself.
05:34Once again, let's take a look at the code.
05:36I'll select the panel in Design view and then go to Source mode and show you
05:41that the left and right attributes have been set.
05:46So, once again, the names of these attributes are left, right, top, bottom,
05:52horizontal center, and vertical center.
05:55With the centering properties, you can use either positive or negative values.
06:00For horizontal center, a positive value means that the object is offset to the
06:04right and a negative value offset to the left, and for the vertical center
06:08property, a positive value offsets it downward and a negative value upward.
06:14Constraint properties, again, are only usable in containers that use basic
06:19layout, which is the default layout for all Spark components that support
06:23the layout property.
06:25If you use horizontal or vertical layout in an Application or other container,
06:29then objects are layed out automatically, and these Constraint properties
06:33don't do anything for you.
06:34But for basic layout containers, they give you a way of placing objects
06:38dynamically and repositioning them automatically, without having to do a lot of
06:42complex calculations.
Collapse this transcript
Declaring view states
00:00View States are a critical part of the Flex architecture that allow you to
00:04modify the appearance of components and applications at run time, based on simple states.
00:10Each application, and each custom component that you build, automatically has
00:15a default state, and when you set properties and styles they're members of
00:19that default state.
00:20But the Flex framework allows you to define multiple states and call them by
00:24name, so that when you change from one state to another you can add and remove
00:29objects, change the values of properties and styles and change the ActionScript
00:34code that gets executed when certain events are dispatched.
00:37For this demonstration, I'll use an application called ViewStateDemo.
00:41This application has a company logo, another graphic and then a panel in the
00:47center, which contains a set of data entry controls.
00:51Looking at the application in Design view, you'll see that the controls
00:55collect a User name and a Password and that there are two buttons with labels
01:00of Log In and New User.
01:02The intent of this sort of dialog box is to allow an existing user to type in
01:06their User Name and Password and Log In, or for New Users to be able to click
01:11the button and see additional information that they can enter to register for the first time.
01:16In many application development environments, you would need two separate
01:19components, one login form and one register form.
01:23The View State architecture allows you to create a single form and then change
01:28its behavior and appearance depending on what State you're in.
01:31When you create View State, you do it at the MXML document level;
01:35that is you declare the States in the MXML document, in this case, the
01:40application, and when you change from one state to another, the rules associated
01:44with that state are executed.
01:47Now to demonstrate this, I'm first going to define a new State.
01:51The easiest way to do that is in Design mode.
01:54I have the application open in Design mode already.
01:58I'll go up to the top-right corner of the Flash Builder interface, to the States view.
02:03Notice, in the States view, that the component, or application, already has
02:08a State named State1.
02:10It's not explicit though.
02:12The carets around the State name mean it's the default State.
02:15Now I'm going to add a new explicit State.
02:18I'll click on this New State button and in the dialog that pops up, I'll name my
02:24New State register, and I'll say that this will be a duplicate of the original
02:29default State and click OK.
02:32In the States view, I now have two explicit States:
02:35State1 and register.
02:39I'll go back to State1 and look at the application in Source mode and show you
02:43that this code has been added to the application.
02:46The State element contains one or more State object declarations, and each has a name attribute.
02:53Notice that with the State class you assign a name and not an id, and that's
02:58because there's an advantage in being able to address a State as a string,
03:02rather than as a reference object.
03:05If you have experience working in Flex 3, you might remember that States were
03:09defined there too, and you added a bunch of code at the State level, right
03:14here in the State tags.
03:16In Flex 4, the State syntax has changed dramatically.
03:20Instead of declaring what happens in all the States right here in the States
03:24section, you declare it in the tags themselves that are modified when you change
03:29from one state to another.
03:30To demonstrate this, I'll go back to Design view, and I'll go to the Log In button.
03:36Notice in the States view that I currently have State1 selected.
03:40I'll switch to the register State, and then I'll change the label of the Log In
03:44button from Log In to Register.
03:48Because I selected the State named register before I made that change, the
03:53change only applies to that State.
03:55Now I'll take a look at the code for that button.
03:59I'll double-click the tab to expand to fullscreen, and you'll see that there
04:03are now two attributes in the Button tag, label, which contains the default
04:09setting of the Label attribute and label.register, which is the value of the
04:14Label when we're in the register State.
04:18If you prefer, you can apply the new value for the property in the new State in pure code.
04:24I'll go to the second button that has a label of New User, and I'll type in
04:28label equals, and then in double quotes, I'll set the new value of Existing
04:33User, then I'll go back to label and type a dot character, and I'll see a list
04:39appear that shows me the available States for the current MXML document
04:44I'll choose the register State and now I have exactly the same syntax.
04:49I'm going to add a little bit of code to the Spacer to cause these buttons to be
04:53pushed apart. I'll add width equals 100%, then I'll restore the editor to normal
05:01size and look at the application in Design mode again and watch what happens as
05:06I switch from one State to the other.
05:08I'll go back to the original State.
05:10The buttons have labels of Log In and New User and the new State, Register.
05:16Here's another thing that you can do with States that goes beyond just changing values.
05:21When a user is registering for the first time, they have to provide both their
05:25user name and their first and last name.
05:29I'll select and copy the HGroup that contains the Label and TextInput
05:33control for the user name.
05:35I'll copy that code to the clipboard and then paste it in twice.
05:44For the first version I copied in, I'll change the Label to First name and
05:49the TextInput id to firstNameInput, and then I'll go to the second HGroup
05:55that I pasted in, and change its Label to Last name and its TextInput id to lastNameInput.
06:04Now I only want these two HGroups to appear when I'm in the Register State.
06:09So, I'll go to the HGroup, and I'll use a new attribute that's a part of the
06:12Flex 4 SDK, called includeIn.
06:16There's also an excludeFrom attribute.
06:19For any visual component, you can use either the includeIn attribute or the
06:24excludeFrom, but not both at the same time.
06:27For the HGroup, I'll say that I want to include the HGroup in the Register State,
06:32and now it won't appear in the initial default State.
06:37And I'll do the same thing for the HGroup containing the lastNameInput and Label.
06:44Now once again, I'll go back to Design mode, and this is what the form looks
06:49like when I'm in the Register State, and now I'll go back to the default State,
06:53and you'll see that the form resizes and so does its containing panel.
06:58So now I've defined States.
07:00You can continue on with this model, making whatever changes you need to,
07:05to styles, properties and event handlers, and you can add as many States to
07:10any MXML document as you need to, to get the variations you need in your
07:15visual presentations.
Collapse this transcript
Changing view states at runtime
00:00Once you've defined multiple View States, you can change States at run time by
00:04changing the value of the currentState property.
00:08The currentState property is implemented in all visual controls, in both the MX
00:13and the Spark component sets.
00:15To demonstrate this, I'll use the application file ViewStateRuntime.mxml.
00:21This application picks up where the ViewStateDemo Application left off.
00:25In this version of the application, I've already defined some View States.
00:29The names of the States are State1, the default State, and Register.
00:33I have a couple of objects that are being added into the presentation when I
00:38change the Register State - that's these two HGroups and their nested controls
00:44- and I'm also changing the properties of two Buttons in the Register State,
00:48the first one changes from Log In to Register and the second one from New User to Existing User.
00:54Now I'm going to show you how to move from one State to another.
00:58I'm going to go to the second Button that starts with a Label of New User and
01:02changes to Existing User when it's in the Register State.
01:05The design of the application is that when the user clicks this button, they
01:09should see the expanded version of the dialog box, so they can type in all of
01:13the information that's needed to register as a new user.
01:18I'll add a click event handler for the Button.
01:22I'm not going to add a full event handler function because I only need to
01:25execute a single statement of ActionScript code.
01:28It'll look like this, "currentState='register'".
01:33Now notice that I applied the State as a string, rather than as a variable name.
01:39I've wrapped quotes around that value, and I use single quotes around the string
01:44because I want to properly nest the string inside the double quotes that are
01:48wrapped around the entire ActionScript statement.
01:51I'm also going to add another click handler, this time for the Register event.
01:55I'll add another click attribute, and this time, I'll set the value as
02:01currentState=' '
02:05Notice that I'm not referencing State1, although I could.
02:09CurrentState equals quote quote means go to the default State, which would be whatever
02:14State is declared first in the State's list.
02:17Then to distinguish this event handler from the first one, I'll go back to the
02:22word click, type in a dot and choose the Register State.
02:28So having two different event handlers is just like having two different values
02:33for a style or a Property.
02:35The first one without a dot and without a State name is what happens in the
02:39default State, and the second one that ends with dot register is what happens
02:44in the Register State.
02:46Now I'll run and test the application.
02:50When the application first opens in the browser, it's in the default State,
02:53also known as State1.
02:57When I click the New User button, I go to the Register State, and when I click
03:02the button again, I go back to the default State.
03:04I'll make one other change in the dialog box.
03:08Notice that the title of the panel isn't changing.
03:11I left this for the very end of the exercise because I wanted to show you how
03:16easy it is to make changes to State management even after you've defined the
03:20initial State names, and you've added the functionality that will change States at run time.
03:26I'll go back to Design mode in Flash Builder. In the States view, I'll choose the
03:31register State, and then I'm just going to double-click on the panel header, and
03:36I get a little text input that allows me to modify the title of the panel.
03:40And I'll change it to Please Register.
03:42Then I'll go back and look at the code that was modified and show you that
03:47Flash Builder reacted to that gesture by adding title.register="Please
03:53Register" to the tag.
03:55The original default title is still there.
03:57I'll test the application again, and now my View State management should be complete.
04:05When the Application first opens, I see the User name and Password data entry.
04:10When I click on New User, I see a Register button and a title of Please Register
04:16and the new visual controls, but when I click on Existing User, I'm taken back
04:21to the original presentation.
04:23View States are a very easy way to make incremental changes to
04:26visual presentations.
04:28They're not necessarily the best way to make changes to all aspects of your Flex applications.
04:34You can also use navigator containers, which I described in a separate chapter
04:37of the video series, but the new View State syntax, that's a part of the Flex 4
04:42SDK, is a lot easier to use and manage than the old Flex 3 version.
04:48And it's just as powerful, allowing you to design and maintain your
04:52application very easily.
Collapse this transcript
8. Using Custom Components
Defining MXML components
00:00When you build a large-scale Flex application, you want to break it down into
00:04small pieces known as components.
00:07While it's possible to create complete applications in a single MXML file,
00:11they quickly become difficult to maintain.
00:13These are sometimes called monolithic applications, that mix up the application file,
00:18all of the individual visual components, and any ActionScript logic.
00:22In this chapter of the video series, I'm going to describe how to create MXML
00:26components, reusable bits of code written in MXML as MXML files that can then be
00:32instantiated or used in your applications.
00:36For all of the demonstrations in this chapter, I'll use a project that's
00:39included with the Exercise Files.
00:41Go to the menu and choose File > Import Flex Project, Browse, then select and
00:47import a file called Components.fxp.
00:50Open the new project's source folder and it's default package and then open the
00:55application file HanselAndPetal.mxml.
00:59In this application, there is a BorderContainer, which contains an image tag
01:03which in turn embeds a SWF file.
01:05I am going to run the application and show that it displays that graphic and
01:09another one in the lower right-hand corner.
01:12This application has a nice beginning visual appearance, but already we can see
01:16there's quite a bit of MXML code at the top level of the application.
01:20You might want to take that code and convert it into a reusable component that
01:24can then be used in other parts of the application as necessary.
01:27So I am going to start by creating a component that encapsulates the
01:31presentation of the logo.
01:33When you create your own custom components, it's strongly recommended that you
01:36place them in sub-folders or packages within the application, rather than in the
01:40source code root folder.
01:42This really improves your ability to maintain the application over the long-term.
01:46I've already created a folder called components and I am going to place my
01:50new component there.
01:51I'll right-click on the components folder or Ctrl+click on Mac and choose New > MXML Component.
01:58In this dialog, I'm asked for the component name.
02:01A component name is the same thing as a class name.
02:04You have to follow ActionScript naming conventions.
02:07The name can include letters, numbers and the underscore characters.
02:11But in a component definition, it should always start with an initial uppercase
02:15alphabetical character.
02:17I'm going to name my first component SimpleComp.
02:20Then I'm going to indicate which existing component I want to base it on.
02:24This relationship will be that of a superclass and a subclass or of inheritance.
02:29When you create your own custom component, you can extend any visual
02:32component in the Flex SDK.
02:35Now, my goal is to replace all of this longer code with a single component call.
02:40So I'm going to base my first component on the BorderContainer.
02:44I'll click the Browse button and in the dialog that pops up, I'll type the
02:48beginning of the name of the component that I want to extend, BorderContainer,
02:52and then I'll select it.
02:53The Based on value now indicates the fully qualified name of the component,
02:58spark.components.BorderContainer.
03:01The BorderContainer component supports absolute widths and heights, but if you
03:06want it to just resize itself automatically based on its nested contents,
03:10delete the width and height from this dialog and then click Finish.
03:14This is what a basic component looks like.
03:17It's just like an MXML application but the root element, instead of being
03:21Application, is the component that this component extends or subclasses.
03:26Now, to replicate the functionality at the top level of the application, I'll go
03:30back to the application.
03:32I'll start by selecting and copying all of the nested content within
03:36the BorderContainer.
03:37I'll take the layout setting and the image and I'll copy it to the clipboard.
03:42Then I'll go back to the component, I'll make a little bit of space between the
03:45tags and I'll paste the copied code into place.
03:49There are other important settings in the original code that I'm going to need.
03:54I'll go back to HanselAndPetal.mxml and I'll select all of the styles that are
03:58creating the BorderContainer's appearance.
04:01Notice that I'm not taking the x and y properties, which determine the position
04:05of the BorderContainer.
04:06Those will be set from the application level eventually.
04:09I'll copy those settings to the clipboard and return to my component.
04:14Then I'll paste those settings in the place and now my component is complete.
04:19I can look at it in Design view, completely isolated from the rest of the
04:22application, and see that it successfully presents the BorderContainer and its nested image.
04:28I'll return to HanselAndPetal.mxml.
04:31So that's how you create a very basic MXML component.
04:35Once you have defined an MXML component, you can use it in any part of the application.
04:40You can instantiate it.
04:41That is, create an instance of the component using MXML or ActionScript code.
04:47I'll show you how to do those steps in separate videos.
Collapse this transcript
Instantiating components in MXML
00:00Once you have defined a custom component, you can instantiate it and use it in
00:04your application in a variety of ways.
00:06In this demonstration, I'll show you how to instantiate and place a visual
00:10component on the screen using MXML code.
00:13I'll use this application. CompWithMXML, which has a background color setting and
00:18a bitmap image that displays flowers in the lower-right corner.
00:22I'll also use this completed version of a component called LogoComp.mxml.
00:30This component displays a graphic wrapped in a BorderContainer and, in fact,
00:34the component's superclass is BorderContainer.
00:37There are a number of styles that determine its presentation, but its size
00:41is based on the nested content, that is, the image control with the embedded SWF file.
00:46I'll go back to CompWithMXML. mxml, the main application file.
00:51In order to instantiate in MXML component using MXML code, you first add a
00:56custom namespace that declares the location of the component.
01:01This namespace follows the same basic syntax as the namespaces that are built
01:05into the application already.
01:06You start with xmlns then colon and then an arbitrary prefix.
01:12The prefix is then mapped to the folder in which the component is located.
01:16I am going to first show you how to create the namespace prefix manually and
01:21then I'll show you a shortcut that Flash Builder can provide.
01:24I'll create a blank line underneath the existing namespace declarations
01:28then I'll type in xmlns:
01:30and then the name of a prefix.
01:34Now, again the prefix itself is arbitrary. It can be any string at all.
01:37I'll put in an equals operator and a beginning quote and then the name of the
01:42sub-folder in which the component is stored.
01:45This part has to be very, very specific.
01:47It must be the name of the folder and it is case sensitive.
01:52The name of my sub-folder is components. You type in the name of the folder then
01:56a dot and then an asterisk.
01:58The asterisk is a wildcard that means that the comps prefix is mapped to all
02:02components within that folder.
02:04I'll complete the namespace declaration and now I can instantiate as many copies
02:09of the component as I want.
02:11If there is more than one component in that folder, I can use as many of those as I want.
02:16Now, I'll move the cursor down to the empty space below the Application start tag.
02:20I'll type in a less than character character and then my new prefix, comps.
02:25Notice that there are bunch of components already in that folder.
02:29If you've worked through the video prior to this one, you would have created a
02:32component called SimpleComp and that's shown here.
02:36Here is the component I'm using for this demonstration, LogoComp.
02:39I'll select it from the list and then I'll complete the tag with the empty tag
02:44syntax, slash and greater than.
02:46Now, in the original application, the BorderContainer had x and y properties.
02:51I didn't copy the x and y properties into the custom component because the
02:55placement of the component should be up to the application.
02:59So I'll go back to the application and I'll add those properties here.
03:02I'll set both X and Y to values of 10 each.
03:08I'll save and run the application and there is the result.
03:10The custom component is displayed exactly as it was before.
03:14But now at the top level of the application, the code is much cleaner.
03:18Here are a couple of cool tips about working in Flash Builder with custom components.
03:23If you want to jump to the code for the component, move the cursor off the
03:27component declaration.
03:28Then hold down the Ctrl key on Windows or the Command key on Mac, and move the
03:34cursor over the component declaration until you see it underlined.
03:37Then click and you will jump tot the component definition.
03:41If the file wasn't already open, it would have been opened for me.
03:45Here is another trick that you can use.
03:47You can double-click on the component and once again it will take you to the
03:51code that defines that component.
03:53So that's one way of declaring MXML components in MXML code.
03:58Now, I'm going to strip out all that code and start over again because I want to
04:01show you a shortcut that Flash Builder provides.
04:04I'm going to start off with the application in its simplest form, without the
04:08custom namespace and without the component instantiation.
04:12I'll type in a less than character and then instead of typing the namespace prefix,
04:17I'll type the name of the component, logo.
04:20Notice now that Flash Builder designates the namespace prefix as the same as the
04:26folder name, components.
04:28When I press Enter to select the component, Flash Builder auto-completes the
04:32tag, but it also adds the required namespace prefix.
04:36This time, setting the prefix to the same name as the folder.
04:39Once again I'll add the x and y properties, setting the position of the object
04:44at 10 pixels from the left and 10 pixels from the top.
04:48I'll run the application and there is my custom component displayed in the
04:52top-left corner of the application with the requested positioning respected.
Collapse this transcript
Instantiating components in ActionScript
00:00Just as with the visual components that are included with the Flex SDK, you can
00:05treat your own MXML components as ActionScript classes, instantiating them and
00:10adding them to the display list of the application using ActionScript code.
00:14For this demonstration I'll use the application file CompWithAS.mxml and I will
00:20use the completed MXML component LogoComp.mxml but I will treat it as an
00:25ActionScript class this time.
00:27Going back to the application, I'll run the application in the browser and
00:31show you that it currently displays the flowers graphic down in the lower right-hand corner.
00:36Now as the application starts up, I am going to create an instance of that
00:40component and place it on the screen in a specific position using
00:44ActionScript instead of MXML.
00:47Going back to the application code, I will first add an event listener for the
00:51creationComplete event in the Application tag.
00:55I place the cursor after the background color setting and press the Spacebar and
01:00I will choose the creationComplete attribute.
01:02When I press Enter to select the attribute, I am prompted to generate a
01:06creationCompleteHandler and I press Enter or Return to generate both the
01:10handler and the call.
01:12Now I place the cursor inside the handler function and I will delete
01:16the generated comment.
01:17The first step in working with an MXML component as an ActionScript class is
01:22to create an instance of it.
01:23I will create a variable called logo and I will set its datatype as LogoComp in
01:30the components package.
01:31When I select that class from the list of available classes, notice that Flash
01:36Builder automatically adds an import statement, shown here on line 7.
01:40That import statement is required if you are going to work with MXML components
01:44as ActionScript classes.
01:47So that's the declaration of the variable.
01:48Now I will instantiate it by calling the constructor method.
01:52I put in the new keyword and then call the constructor method, which is the same
01:57as the component name.
01:59So now I have an instance of the component in memory.
02:02Next I will add x and y properties to determine its position within its container.
02:07I will set logo.x to 10 and logo.y to 10.
02:13Finally I will add the component to the display list of the applications
02:17content group like this, this. contentGroup.addElement, and then I will pass in
02:24the logo object.
02:27Remember that in Flex 4, you always add visual elements to the content group of
02:32the container and not to the container itself.
02:34So there is the completed code.
02:37The flowers image is still being placed on the screen with the BitmapImage tag,
02:41but the logo is being placed dynamically using ActionScript code that is
02:45executed as the application starts up.
02:47I'll run the application and show that the logo is successfully being placed 10
02:52pixels from the top and 10 pixels from the left.
02:55So it's up to the developer whether you want to use MXML or ActionScript code.
03:00For a simple operation like this, typically you will find MXML takes a lot less
03:04code than ActionScript, but ActionScript gives you a lot more flexibility
03:08because you can use conditional logic, loops, and any other sort of code you
03:13need to accomplish what your application needs.
Collapse this transcript
Declaring and using custom properties
00:00An MXML component is really an ActionScript class under the surface and just as
00:05with all classes, it supports custom properties, methods, event, and other
00:10members that go along with API objects.
00:13In this demonstration, I will show you how to define custom properties in an
00:17MXML component and then pass values into these properties from an application.
00:22I will use the application UseCompWithProps.mxml.
00:26This version of my sample application has three components right now.
00:30In the top left corner is a logo, the lower right corner shows an image, and in
00:35the center there's an instance of the custom component.
00:38The custom component has three visual elements, a label, a rich text object, and another image.
00:45Let's take a look at the code that makes up the component.
00:48From the application I'll go to the tag, which is the instantiation of the
00:52component, and I'll hold down the Ctrl key on Windows or Command on Mac and move
00:56the cursor over the component instance.
00:59When I see the underlying, I click and that takes me to the implementation
01:03of the component, a file called CompWithProps. mxml, which is located in the components folder.
01:10This custom component is derived from the BorderContainer component.
01:13It has a number of styles that determine its appearance.
01:16It has a vertical layout with padding of 20 pixels all around and then the three
01:21visual objects I mentioned, the Label, the RichText and the Image.
01:25I'm going to declare three public properties that the application can use to
01:30pass in values into this component, so that instead of displaying literal values
01:35such as Bonsai $45.99 or the bonsai. jpg file, I will be able to reuse this
01:41component and pass the values in that I want to display.
01:44You can declare custom properties using either MXML or ActionScript code.
01:48But most developers prefer to use ActionScript because it gives you
01:51more possibilities.
01:53I'm going to place the cursor after the layout declaration and press Enter or
01:57Return to create a new line and then I'll add a script section.
02:00I will type in a less than character and SCR and then press Ctrl+Space
02:05and choose fx Script.
02:08Then I'll type in the greater than character and Flash Builder auto
02:11completes the script section.
02:12The syntax for declaring a public property looks just like declaring a variable.
02:16If you want to be able to set and get the value from the application, you must
02:21declare the variable as public.
02:23So I'll start off with word public and then the keyword var and then I'll set
02:28the name of the first variable as productName.
02:31I'll set its value as a String and I'll also set it with an initial value of Bonsai.
02:37So this is my first public property. It has a name, a data type, and an initial value.
02:43You don't have to set the initial value but by doing so it will make it easier
02:47to test the component when you first drop it into an application.
02:50I will declare two more variables.
02:53The next one will be named price.
02:55It will also be set as a string for the moment because I want to maintain any
02:59formatting that's passed in from the application.
03:02And it will set its initial value as 45.99.
03:06And then I'll set the last variable and name it image and once again set it
03:11as a string and this variable's initial value will be bonsai.jpg.
03:16So those are my three public properties.
03:19Now, I will show you in a little while how to use those properties within the
03:23component using binding expressions.
03:25But for the moment, I am just going to save the file by pressing Ctrl+S or
03:29Command+S and return to the application.
03:32At the application level, I can now inspect the properties that I have
03:36declared in the component.
03:37I'll place the cursor at the end of the attributes for the CompWithProps
03:41instance and I'll press Enter to move down to a new line and then Ctrl+Space to
03:47bring up the Code Assist list.
03:49I will type in the beginning of the first property that I declared, productName,
03:53and show you that Flash Builder already knows about the property.
03:57I'll select that property and I'm going to set it to a new name of Aloe Vera.
04:02Now, I'll move the cursor passed the attribute and press the Spacebar.
04:05That will bring up code list again.
04:07I'll select the price property and set its value to 24.99 and finally I'll
04:12set the image and the name of the image file that I want to display will be aloe_vera.jpg.
04:18So one of the things that Flash Builder brings to the table is the ability to
04:22introspect components as you make changes to them and to show you the
04:25available custom properties and methods.
04:28I will save my changes to the application and return to the component.
04:32Now in order to display these values, which are now dynamic at runtime, I'm
04:36going to replace the literal strings in the components with binding expressions.
04:41I will select the word Bonsai and delete it and replace it with a
04:45binding expressions starting with the brace character and then the
04:48variable name, productName.
04:50I will do the same thing for the price.
04:52I will delete 45.99, notice I am leaving the $ sign in place, and I'll replace it
04:58with a binding expression for price.
05:00So notice that you can use binding expressions in the context of XML elements,
05:04just like you can in attributes. And then finally I'll replace the value bonsai.jpg
05:09with a binding expression for the variable image.
05:13Notice that I'm leaving the location of the image files intact.
05:17Assets/ is a literal string because that's always going to be the same for every
05:22product I want to display.
05:24What's going to change is the file name.
05:27Now when I save these changes, you'll see three warnings appear in the trough
05:32next to the line numbers, way over on the left.
05:35This is a very common warning that you'll see when you use binding expressions.
05:39"Data binding will not be able to detect assignments to" and then the
05:42ActionScript expression that I am including in the binding.
05:45The issue here is that I've declared these variables without making them
05:50bindable, and in ActionScript 3, if the property isn't marked as bindable, then
05:55it's not going to broadcast at runtime when it's value changes.
05:58In an MXML component, you have to declare each individual property as bindable.
06:03There is no way for example to the group a bunch of properties together and make
06:07them all bindable with one instruction.
06:10So for each variable, I'll make an extra line above the variable declaration
06:15and added a metadata tag called Bindable.
06:17I will copy that line of code to the clipboard. I will select the entire line
06:21and then press the keyboard shortcut for copying, Ctrl+C on Window, Command+C
06:26on Mac and then I will paste that line of code above each of the other variable declarations.
06:30I will press Ctrl+S or Command+S to save and you'll see that the warnings go away.
06:36So this now a complete custom component with public properties marked as
06:41bindable, so that visual objects within the component can get and display the
06:46values dynamically at runtime.
06:48I go back to the application and run it and show that the custom component
06:52instance now displays the values that are passed in from the MXML declaration.
06:58So this has been a look at how to declare custom properties in a custom MXML component.
07:03They are declared just like variables, but if you want to set and get them from
07:06the application you mark them as public.
07:08If you want to axis those values within the component using binding
07:12expressions, you mark each property as bindable and then you pass the values in
07:17from the instance of the component, in this example from the application,
07:21making sure to pass values in that match the declared data types of the
07:25properties in the component.
Collapse this transcript
Declaring and using custom methods
00:00As I've described in other videos in the series, a custom MXML component is
00:05really an ActionScript class beneath the surface and it supports the
00:09implementation of custom properties and functions.
00:12I'm going to show you how to declare a public function in an MXML component and
00:16then how to call that function in order to pass values into the component.
00:21For this demonstration I'll use the application file, UseCompWithFunction.mxml.
00:26It calls a version of a custom component called CompWithFunctions.mxml.
00:32This component has three public properties named ProductName, Price and Image.
00:37These properties are marked as bindable and are used in binding expressions in
00:41the visual controls nested within the component.
00:43When you declare a function within a custom component, it's then called a method.
00:48Using object-oriented programming vocabulary, a method and a function are pretty
00:52much the same thing.
00:53But a method specifically is a function that belongs to a class definition.
00:58Here is the syntax for declaring a method within a component.
01:01I'm going to place the cursor after the three property declarations and
01:06I'll create a couple of empty lines.
01:08Now, here is the syntax for declaring a public method or function.
01:12I will start off with the word public and then the keyword function.
01:15Then I will set the name of the function as set product.
01:20Now if you're familiar with the concept of implicit setter and getter functions,
01:24I'll tell you right now that that's not what this is.
01:27This is an explicit function that I'm designing to be called from the
01:31application as a function.
01:33Implicit setters and getters are supported by ActionScript 3 but they're not the
01:38focus of this demonstration.
01:40When you declare function in ActionScript, you should always declare its return data type.
01:45You do this after the parenthesis, starting with colon and then a data type that
01:50you might want to return.
01:51In this example, the function will be called without returning any value.
01:56So I put in the keyword void at the end of the function.
02:00Then I'll add the braces and this is where I'll add the body of the function.
02:04Now the when the application calls the function, it's going to pass in three
02:08new values which will be then applied to the three variables, productName, Price and Image.
02:13So I am going to place the cursor inside the parenthesis and declare three
02:18arguments that can be passed in.
02:19I will use the same names as the variable names, productName, data typed as a
02:25string, price, data typed as a string and image, data typed as a string.
02:30Now to take those values and pass them to the public properties, here is
02:34the pattern I'll use.
02:35Notice that the property name declared as public and the argument have the same name.
02:41So I need a way of distinguishing between the two.
02:44The property is a member of the current instance of the component, which
02:48you refer to as this.
02:51So here is the syntax to pass the argument value to the public property value.
02:56This.productName = productName.
03:00When you reference the word productName without a prefix, it defaults to the
03:04argument. That takes precedence over the property.
03:07But when used to prefix this, you're explicitly talking about the property
03:12that's a member of the component.
03:14I'll repeat that syntax for the other two properties. this .price=price and
03:21this .image = image.
03:24Now I'll expand the editor to full screen to look at all the code.
03:27So I have three public properties each with default values, but I also have a
03:32public function or method that can be called from the application to reset those values.
03:36I will save my changes and return to the application.
03:40Notice that this version of the application has a button with a label of Show Bonsai.
03:46When the user clicks this button, I'm going to call the public method that I
03:49just declared and pass in the new values for the bonsai product and it will
03:53replace the values for Aloe Vera that are currently displayed.
03:57I'll place the cursor at the end of the attributes for the button control,
04:01I'll press the Spacebar and choose the click attribute and then I'll press Enter or
04:06Return to generate a clickHandler function.
04:09In order to call the methods of this component, I need to assign it an ID.
04:14I will go back to the component and I'll add an ID attribute and set it to productDetail.
04:19So now I can address this object and its member properties and functions.
04:24I'll go to the generated handler method and delete the auto-generated comment.
04:29I'll start with productDetail and then put in a dot, and then I'll use
04:34the setProduct method.
04:35Notice that Flash Builder shows me that names and data types of the arguments I need to pass in.
04:40So I will pass in Bonsai and then finally the image name bonsai.jpg.
04:48So when I click the button, I will be passing these values in and the
04:52implementation of the method within the component will take the values and save
04:56them to the public properties, the bindings in the visual controls will grab
05:00those values, and the screen will automatically update.
05:03I will save and test the application.
05:06When it first opens, it shows the details for Aloe Vera.
05:09When I click the Show Bonsai button, the custom component updates instantly
05:14and notice that it reformats and resizes itself to match the new dimensions of the image.
05:19So that's a look at how to build a custom function in an MXML component and how
05:24to call that function from the application.
Collapse this transcript
Declaring and setting component states
00:00The view states architecture gives you a way of modifying a component
00:04presentation at runtime.
00:06Just as with applications each MXML component has a state's property and you can
00:11define as many different states as you want to and then change from one state to
00:15another at runtime in a reaction to particular events.
00:19For this demonstration I'll use an application called UseCompWithStates.mxml.
00:24This application has two instances of a component called CompWithStates.
00:28It passes different values into the two instances and places them side-by-side
00:34using an HGroup container.
00:35When I run the application it shows the two components laid out side-by-side
00:40from left to right and each shows a product name, a price, instructions for
00:45care, and a product image.
00:48Now I'm going to redesign the MXML component so that in its initial state it
00:52doesn't show all this detail.
00:54It just shows the product name and the image.
00:56But when the user moves the cursor over the component, it expands to this full-size.
01:01I'll close the browser and return to Flash Builder then I'll hold down the
01:06Ctrl key or Command on Mac and click on one of the component instances to go to the code.
01:12The first step is to add state declarations.
01:15You can do this in either Design or in Source mode.
01:18I prefer to do it in Source mode so I can see exactly what the code looks like.
01:22I'll place the cursor after the layout setting and add a states property.
01:28Within the states property I'll add an initial state with the name of the State1.
01:33You don't have to use the word State1 for your default state.
01:36It can really be anything you want.
01:37But this is the term that would be used if Flash Builder generated the code for me
01:41and so for consistency I'm using it.
01:44Then I'll add a second state and this is the state that I'm really going to use
01:48my code and I'm going to give it a meaningful name of detail.
01:53So all of the settings that I've added to the BorderContainer that's containing
01:57the components and to all of the other visual controls, those settings will
02:00apply in the default state, State1.
02:03Now I'm going to define what happens when I change to the detail state.
02:07First of all, when I'm in the initial state, I'm going to stop the controls that
02:11are displaying the price and instructions from appearing at all.
02:15I'm going to do that by adding the includeIn attribute and setting it to a value of detail.
02:21So if I included in the detail state, then it's not included in the default state.
02:25I'll do the same thing for the label that's displaying the instructions.
02:30I'll set includeIn once again to detail.
02:35Now I'm going to resize the image.
02:37When these product components appear at first, I want the image in this
02:41component to be of a different size when it's displayed in the default state as
02:45opposed to the detail state.
02:47So here are the changes I'll make.
02:49I'm going to reset the values of the scaleX and scaleY properties to .3 each
02:55so that instead of being 60% of the original size of the image, they're now
03:0030% of the original size.
03:02Then I'll declare two new versions of scaleX and scaleY, this time associating
03:07the new values with the detail state.
03:09The code will look like this.
03:11scaleX.detail=".6" and scaleY.detail=".6".
03:18So now the default display is 30% of the native size of the image and the detail
03:23shows 60% of the original size or twice the default presentation.
03:28I'll save those changes and return to the application and run it.
03:32When the application displays now, it shows the initial presentation, that is,
03:37the images are half the size that they will be the detail display and I'm not seeing
03:42the price or the instructions.
03:43So now I'll go back to the component code and add a little bit of event
03:48handling to change from one state to another when the cursor moves over the
03:53component instance.
03:55I'll use the two events, rollOver and rollOut.
03:58When the cursor moves over the object I'll get a rollOver event and I'll change
04:02it to the detail state and when the cursor moves off of the component instance,
04:06I'll get a rollout even and I'll react by switching back to the original state.
04:10I'll place the cursor at the end of the BorderContainer start tag after all of
04:14the attributes and press Enter to make a new line.
04:18I'll use the rollOver event first and I'll react with a single statement.
04:23"currentState='detail'"
04:26In this context currentState is a property of the current instance of the component.
04:30I could add a prefix of this, as in this.currentState=detail,and it would mean
04:35the same thing, but it's not necessary. The this prefix is implied.
04:41Then I'll add a rollOut event and I'll reset the currentState to either a blank
04:46string or the explicit state of State1.
04:50I'll save my changes and go back to the application and run it again and once
04:54again when the application first appears it displays the default presentation
04:59with the title and the small image.
05:01But when I move the cursor over the object it expands, when I move it off,
05:06it restores to its original size and I can switch back and forth between the
05:10components showing them each in their default and detail states.
05:15The great thing about using view states in custom components is that you
05:18can expand, reduce them, change properties, change styles and even change the
05:23way they react to different events using the new view state architecture
05:28that's a part of Flex 4.
Collapse this transcript
Dispatching events from custom components
00:00Custom MXML components can be both interactive and they can encapsulate data and
00:05functionality in such a way that you have to find a way to share data from the
00:09MXML component with the rest of the application.
00:12It is possible with MXML components that you instantiate in MXML to reach
00:17into the component to get data out of them, but that violates the rules of encapsulation.
00:22MXML components are mostly reusable and most maintainable when they're black boxes
00:27and only share data with the rest of the application when they determine
00:30it's the right time.
00:32In order to define this encapsulation and black box behavior, Adobe recommends
00:37that you share data and information with the rest of the application from an
00:41MXML component by dispatching events.
00:44In this demonstration I'll use an application called UseCompWithEvents.mxml.
00:50This application has two instances of a custom component called CompWithEvents.
00:56This version of the custom component includes a button labeled Add to Cart and
01:01when the user clicks that button, I want to instruct the application to add an
01:05instance of this product to a shopping cart, let's say.
01:09So here are the steps you need to follow.
01:11First, you need to understand how to add a custom event to the API of a custom component.
01:17An event is a member of the component or its underlying class, just like a
01:22property or a method.
01:23But unlike properties and methods you don't declare events initially
01:26in ActionScript code.
01:28You declare it in a metadata tag called event.
01:32In this custom component I'm going to place the cursor after the states
01:36declaration and above the Script section and I'll create a pair of
01:40fx:Metadata tags.
01:43Within the metadata tags you add one event metadata tag for each event that this
01:48component might dispatch.
01:50I'm going to declare an event and I'm going to assign it a name of addtocart and
01:56this will tell the rest of the application that this is a custom event that this
02:00component might dispatch.
02:02You can optionally also indicate the data type of the event object that will
02:07be dispatched. Just like the events that are dispatched from the Flex SDK's own components,
02:12whenever you dispatch your own event, you'll be dispatching an instance of an event class.
02:17You do this with the type attribute and you set it to the fully qualified name
02:23and package of the event class that you're going to dispatch.
02:27I'll dispatch an instance of the super class of event classes,
02:30flash.events.Event.
02:34So now I've added the event addtocart to the API of the component.
02:39I'll save those changes to the component and go back to the application code,
02:43because at this point I want to show you that Flash Builder knows about the
02:47event that's a part of that component.
02:49I'll place the cursor after the first line of the first CompWithEvents instance
02:54and I'll type in the first few characters of the event name, add, and press Spacebar
03:00and show that the event name appears in the list of available attributes of this component.
03:05Now I'm not quite ready to do something with that yet, so I'm going to press
03:08Ctrl+Z or Command+Z on the Mac to undo those changes.
03:14I'll return to the component definition.
03:17Now in order to dispatch an event you follow these steps.
03:21First you create an instance of the event class that you said you're going to use
03:26as the type for the event object and you'll pass in the name of the event
03:30into the constructor method of that event class.
03:33That will be known as the event object's type and then you'll call a method
03:37called dispatch event and pass the event object in.
03:40The result of this is that the component dispatches or throws the event upward
03:45through the containership so that the application which instantiated the
03:49component will receive the event and be able to react to it.
03:53I'll go to the button that has a label of Add to Cart.
03:56I'll add a click event handler in MXML and I'll allow Flash Builder to construct
04:03an event handler function.
04:05So when the user clicks the button, all the code in that function will be called.
04:09I'll delete the automatically generated comment and the first step will be to
04:14create an instance of the event class.
04:18Within the function I'll use this code, var eventObj, data typed as the Event
04:24class, equals new Event and now I'm going to pass in the name of event that I've
04:31defined in the metadata tag, addtocart.
04:35The next step is to dispatch the event.
04:38I'll do that by calling the dispatchEvent method and passing in the eventObj.
04:44When that function is called, the event object will be dispatched up to containership.
04:50I'll save those changes.
04:52Now I'll go back to the application and I'll be ready to add event handlers to
04:56the two instances of my component.
04:59I'll start with the first one that has a product name of bonsai and I'll add an
05:04addtocart attribute.
05:06Now once again this appears in the list because of the metadata in the component
05:10and I'll generate a handler for that event.
05:14The name of my new event handler function is compwithevents1_addtocartHandler.
05:23Within the event handler function I'm going to refer to the target of the event object.
05:27This will refer to the instance of the component that generated or dispatched
05:32the event and I'm going to display the name of the product that the user
05:35selected using this code.
05:37I'll use the Alert class to display a pop-up dialog.
05:41The Alert class displays a pop-up window that can display two different strings,
05:46the body and a title.
05:48I'll type in the word Alert and then press Ctrl+Space and select the class from
05:53the list of available classes.
05:56This adds a required import statement.
05:59Then I'll use the show method of the Alert class and I'll pass in the following
06:03string, event.target.productName.
06:09event.target refers to the instance of the component that dispatched the
06:13event and productName is its product name property, which was set in the MXML declaration.
06:20I'll append to that a literal string, added to cart.
06:25Then I'll add another string after a comma.
06:29Product added to cart.
06:33I'll run the application and when the application first appears, it shows the
06:38default appearance of these components.
06:40I'll move the cursor over Bonsai and click Add to Cart and I get the message
06:45Bonsai added to cart.
06:48I'll click OK and clear the dialog.
06:51Now I'm going to add the event handler to the second instance of the component.
06:56I'll go to the first instance and copy the event handler code to the clipboard,
07:02then I'll go to the second instance and paste the event handler in.
07:06I'm calling the same function for each instance of the component.
07:11I'll save and run the application and show you now that I'm able to call the
07:15function and add each of the products to the shopping cart.
07:19Now this is obviously not a complete implementation.
07:23I would need to build shopping cart functionality internally, storing information
07:27that the user has selected, and I would also need more tightly control moving
07:31back and forth between the default and the detail states of the MXML component.
07:36But you can see that the MXML component is successfully sharing information with
07:41its containing application.
07:44To review the steps for each event that you want to dispatch from the component,
07:48you add an Event metadata tag.
07:51You set the name and optionally the type.
07:53If you don't set the type, it defaults to flash.events.Event.
07:58Then when you want to dispatch information, you construct the event object as an
08:03instance of the Event class, you pass in the name of the event to the
08:06constructor method, and you call the dispatchEvent method and pass in the event
08:10object and that shares the data, dispatching or throwing the event upward from
08:15the containership of the Flex application.
Collapse this transcript
Creating custom event classes
00:00When an MXML component dispatches an event, it frequently shares custom data
00:05with the rest of the application.
00:07I've shown you already how to dispatch a basic event, one that tells the rest of
00:11the application that something happened.
00:13But if you use the standard event class, there's no place in that class to
00:18share complex data.
00:20If you want to share a custom data you have to create your own custom event class.
00:24The rules for custom event classes are fairly straightforward.
00:27First of all they must extend or be subclasses of Event.
00:31Then any data that you want to share with the rest of the application is
00:35declared as public properties of the Event class.
00:38For this demonstration I'll create a brand-new event class.
00:42When you create custom classes in a Flex application, you should place them in
00:46folders or packages underneath the source code root folder.
00:50In my project I've created a folder called events and that's where I'll place
00:54my custom event class.
00:56I'll right-click on the events folder and choose New > ActionScript Class and
01:03that opens the New ActionScript Class dialog box.
01:06First check that the package is correct, that you're placing the class where you
01:09want it to go, and then assign your new class a name.
01:13Custom event classes frequently follow a convention where the first part of the
01:17name is the type of data you're sharing and the last part is the word event.
01:22So for an event class that's going to share data about a product, you might name
01:26the event class ProductEvent.
01:29Your naming convention can differ, but this is a common convention that a lot of developers use.
01:34Next, you must extend the Event class.
01:38You place this information in the Superclass text box and you can either type in
01:42the fully qualified Name and Package of the class or you can browse for it.
01:47I'll click the Browse button.
01:49I'll type in the word Event and choose the Event class in the flash.events
01:54package and click OK.
01:57You can leave everything else at its default settings including the option for
02:01generating a constructor from the superclass and then click Finish.
02:06So this is the syntax of a basic event class.
02:10Notice that by default the constructor method for my new class has three arguments.
02:15One required argument named type.
02:18This will be the name of the event that I'm dispatching.
02:21One named bubbles, which if set to true will cause the event to bubble up the
02:26containership and be dispatched multiple times.
02:30And one called cancelable, which if set to true means that the event can be
02:34canceled and any actions of the event can be prevented from happening.
02:38If you're simply sharing data with an event, you don't necessarily need
02:42these last two arguments.
02:44So I'm going to delete the last two arguments from the constructor method and
02:48also from the call to super.
02:51I'm going to leave the type argument in super, because that's how the name
02:55of the event or the event type gets shared with the superclass and that code is required.
03:01Now the next step in declaring the custom event class is to clear public
03:06properties for each element of data that you want to share with the rest of the application.
03:11I'm going to share two bits of information about the current product.
03:15The product name and the price.
03:16I'll place the cursor above the function declaration for the constructor method
03:22and I'll declare public var productName:String and public var price:String.
03:32So now my custom event class is capable of storing two bits of information,
03:37the product name and price.
03:39There's one more bit of code that you'll commonly see in a custom event class.
03:43It's called the clone method.
03:46The clone method can be called at any time to create a copy or a clone of the
03:51current event object.
03:52The syntax for the clone method looks like this.
03:55It overrides an existing function called clone that's a member of the Event class.
04:00So you typically start off with override public function.
04:05Because you include the override keyword, you now see a list of all of the
04:09functions that are members of the Event class, the superclass.
04:13I'll choose the clone method and the return data type for this method gets
04:18filled in automatically.
04:20It's Event, the superclass.
04:24Within the function I'm going to create a new instance of the current class.
04:29I'll create a variable called eventObj, data typed as ProductEvent, the current class,
04:35and I'll construct it by calling the ProductEvent constructor method and
04:41pass in the current event name or type.
04:45So now I have a copy of the ProductEvent class, which knows what the
04:48current event name is.
04:50I'm also going to set the productName and the price for the new copy like this.
04:55eventObj.productName = this. productName and eventObj.price = this.price.
05:05If the custom event class has more public properties than that, you copy
05:09them each one at a time.
05:11Finally, I'll return the new instance of the class. return eventObj.
05:18Now this clone method is optional in most cases.
05:21The only time it's absolutely required is if in an event handler you handle an
05:26event and you capture this object and then you re-dispatch the event by calling
05:31the dispatch event method again and passing in the same event object.
05:35In that case this clone method will be called automatically by the Flex framework.
05:40If you never are going to re-dispatch this event object, then the clone method
05:44probably isn't required.
05:45But a lot of developers expect to see it there, so if you want to create
05:49well-formed custom event classes, go ahead and create the clone method and
05:54everybody will be able to use it in all circumstances.
05:58So this is a complete custom event class now.
06:01It extends the Event class.
06:03It declares public properties for each of the elements of data it wants to share
06:07with the rest of the application, in the constructor method it calls the
06:10superclass's constructor method and passes in the type or the event name, and
06:15it implements the optional clone method so that if the event object has to be
06:19re-dispatched, it will work correctly.
06:21Now I'll show you how to use this event class, constructing an instance of the
06:25event class, populating it with data, and dispatching it in a separate video.
Collapse this transcript
Dispatching and handling custom events
00:00Once you have defined a custom event class that has custom properties, you can
00:04use that class to share data with the rest of the application.
00:08For this example, I will use the application file UseCompWithCustomEvents.
00:12A component called CompWithCustomEvents.mxml, which currently dispatches a
00:17custom event called addtocart, data typed as flash.events.Event.
00:22And a custom event class called ProductEvent2, which has two public properties
00:27named productName and price.
00:29I will start the changes to the application at the component,
00:32CompWithCustomEvents.mxml.
00:35Currently this mxml component dispatches an event named addtocart.
00:40The data type of the event object is flash.events.Event.
00:44I am going to replace that event declaration with my custom class ProductEvent2.
00:48I will delete the name of the event class and I will replace it with the fully
00:54qualified package and name of my custom class, events.ProductEvent2.
01:01Note that in this context Flash Builder is not able to help you auto complete
01:06the package and class name.
01:08You just have to know the class name and location and type it in correctly.
01:13Now, I will go to the function that's being called when the user clicks
01:16the addtocart button.
01:18Currently, it's declaring an eventObj data typed as Event and constructing it
01:23from that class's constructor method, passing in the event name.
01:26I am going to replace the data type of the EventObj, removing the Event class and
01:33replacing it with ProductEvent2.
01:34I will type in the beginning of the class name and then press Ctrl+Space.
01:39I will see a list of all classes that start with that string.
01:45And I will choose my event class, ProductEvent2.
01:46Now when I do that Flash Builder automatically adds an import statement for that class,
01:53up at the top of the code.
01:55I will go back down to the code, and I will also replace the constructor method
02:01name, taking out Event and replacing it with ProductEvent2.
02:07So now, I am constructing an instance of my custom class instead of the standard
02:12Event class and I can add data to it using simple dot notation.
02:16I will place cursor at the end of the line and press Enter to create a new
02:20line and I will pass in the two properties that I want to share with the rest
02:24of the application.
02:25eventObj.productName = this. productName and eventObj.price = this.price.
02:40So, here are all the steps that I followed in this code.
02:43I have instantiated my ProductEvent2 class and I have passed in the event name.
02:49I have populated the event object with data and I am dispatching the event to
02:54share the data with the rest of the application.
02:56My changes to the custom component are done.
02:59Now, I will go to the application.
03:02In the application I already have an event handler for the addtocart event.
03:06This event is being handled on two instances of my custom component.
03:11The one is for the bonsai plant and the one for aloe vera.
03:14Right now, I am getting the productName to display in the alert pop-up by
03:19referencing it from the event target.
03:21That is, the instance of the custom component.
03:24But now that data is being passed to me much more directly in the event object.
03:28So, I am going to redesign the event handler to address the event object directly.
03:33First, I will start in the method signature.
03:35I will select the event argument's data type, the Event class, and delete it and
03:41replace it with ProductEvent2.
03:44As before, I will get Flash Builder to add an import statement for me.
03:48I will type in the beginning of the ProductEvent2 class name and press
03:52Ctrl+Space and choose the class and Flash Builder adds the import statement at
03:57the top of the code.
03:59Then I will be able to address both of the properties of the event object without
04:04having to go through the target, which is the visual component that
04:07dispatched the event.
04:08I will delete .target from the expression.
04:13And now I am referring to the property of the event object rather than the property
04:18of the visual component.
04:19I will also change the second string that I am passing into the show method
04:24of the Alert class.
04:25Instead of displaying just a literal string in the title of the pop-up window,
04:30I'll show the user the price of the product that they added to the cart.
04:33Like this. I will type in a literal string of Price and then I will add another
04:40expression after a plus operator of event.price.
04:45And notice that because I data typed the event property as ProductEvent2
04:50Flash Builder knows that price and productName are properties of the event object.
04:53I will save my changes and run the application in the browser.
04:57I will move the cursor over the first item.
05:01I will click the Add to Cart button and I get both, the name of the product and
05:07the price displayed in title of the pop-up window.
05:09I will go to the second and click Add to Cart and once again, I get the details
05:15by handling the event object and displaying its properties.
05:19So, that is how you use a custom event class.
05:22Once you have created the custom event class to encapsulate data properties,
05:27you instantiate the custom event class, you populate it with data, you pass in its
05:31event name when you instantiate it and then you dispatch it.
05:35And in the application you listen for the event object, you put in the specific
05:39event class type that you know you are going to be receiving and in your event
05:43handler function you extract the data and do whatever you need to with it.
05:47Displaying it to the user, passing it to an application server or saving it
05:51locally, perhaps in an Adobe AIR application.
Collapse this transcript
9. Managing Application Navigation
Using the ViewStack container
00:00In Flex applications, you implement navigation by stacking views or
00:06rectangular components on top of each other so that the user can only see one view at a time.
00:13You then reveal or expose one view at a time to create a navigational pattern
00:17so that the user is going from screen to screen.
00:21Navigation is created in Flex using the Navigator containers.
00:25A particular style of containers that contains nested containers and only
00:30exposes one layer at a time.
00:32The parent container of these is called the ViewStack, and there are two related
00:36containers called the TabNavigator and the Accordion.
00:39For all of the demonstrations in this chapter, I will use a project that's
00:42included with the Exercise Files.
00:45If you have access to the Exercise Files, you can import the project now.
00:50Go to the menu and choose File > Import Flex Project, browse and navigate to the
00:57Exercise Files folder and from there to 09_Navigation. From there go to the
01:03begin folder, and select and import the file Navigation.fxp.
01:10Then open the Navigation project that's created.
01:13Open the src folder and the default package, and open an application file
01:18called ViewStackDemo.mxml.
01:22In this application, there are three instances of custom components named
01:27Home, Products and Login.
01:31They are stacked within a VGroup or a Vertical Group so that they are stacked
01:35one on top of each other going from top to bottom.
01:38The three components in turn display this welcome message, these two product
01:44elements, and a login form down at the bottom.
01:48Each of these three elements is designed to be a distinct view and the user
01:52should only see one of them at a time.
01:53So I am going to show how to wrap these components inside a ViewStack so that
01:59they are layered and one of them is visible at any given moment.
02:02I will close the application and return to Flash Builder.
02:06I will place my cursor above the VGroup and start by creating a ViewStack.
02:14In Flex 4, the ViewStack is still an MX component, not a Spark component.
02:20So you declare it with the MX prefix.
02:23When you declare the ViewStack you typically assign it an ID so that you can
02:27address the ViewStack in you ActionScript code and binding expressions.
02:31I am going to give my ViewStack an id of views and then I will put in the
02:36greater than character to close the tag and Flash Builder will auto-complete the element.
02:41The ViewStack is designed to wrap other containers but because it's an MX
02:46component and not a Spark component, it can only directly nest MX components or
02:51containers or if you want to nest Spark components, you have to wrap them in a
02:57special component called NavigatorContent.
03:00Each of the components that you see here, the Home, Product and Login
03:03components, are extended from MX components such as BorderContainer.
03:08And so, they must be wrapped in the NavigatorContent component in order to be
03:13compatible with the ViewStack.
03:16So, within the ViewStack, I am going to create three NavigatorContent elements.
03:20I will give the first one an ID of home and I will also assign a label of Home.
03:27I will show you how to use the label later on.
03:30I will declare each of the NavigatorContent elements as a pair of tags.
03:36And then I will go get the matching component, such as the Home component, and
03:40I will select the code and then drag it into place.
03:43So now, the NavigatorContent wraps around the Home component.
03:48Now I'll repeat that process for the Products and for the Login.
03:51I will create a NavigatorContent, assign it an id and a label,
04:02and then I'll go get the instance of the Products component and drag it into place.
04:07And finally, I will do the same thing for the Login component.
04:10I will create a NavigatorContent, assign an id of login, and a label of Log In,
04:19and then I will select and drag in the Login component instance.
04:25I no longer need the Vgroup.
04:27So I will select and delete it and now I have created the beginning of a
04:31navigational architecture for my application.
04:34I will save and run the application and you will see that the ViewStack appears
04:39in the top-left corner of the application by default.
04:43And it only shows the welcome message and not all the other components. They are hidden.
04:49Now, I am going to position the ViewStack on the screen where I want it.
04:52I will go to the MX ViewStack component and add a horizontalCenter property of
04:58zero, meaning that it will center on the screen and I will add a top property of 172.
05:03I will save and run the application again and now my ViewStack appears in the
05:10direct center of the screen.
05:12So far, I have created the ViewStack and wrapped it around my custom components
05:17but I have not done anything to provide navigational functionality to the user.
05:22That is, they do not have any way to move from one element of the ViewStack to another.
05:27You can provide navigation using ActionScript code or a particular dynamic
05:32component called the ButtonBar.
05:34I will show you how to use those techniques in other videos.
Collapse this transcript
Managing navigation with ActionScript
00:00The ViewStack container is a navigation container.
00:04It provides navigational functionality.
00:06But it doesn't have any visual interface of its own.
00:10In order to allow your user to move from one screen to another, you need to
00:13provide user interface controls.
00:16Buttons, link buttons, tabs or something like that lets the user move from one
00:21screen to another by clicking on items.
00:24You can provide this functionality in a couple of different ways.
00:27Either through manual navigation using ActionScript code or through a special
00:32component called the ButtonBar that binds to the ViewStack and creates the
00:36navigational functionality more automatically.
00:40In this demonstration, I will show you how to write a little bit of ActionScript
00:43code that reacts to some buttons and moves the user from one screen to another.
00:48I will be working with the application file ASNavigation.mxml.
00:54In this application, I have a ViewStack with three NavigatorContent containers.
00:59The NavigatorContent containers contain instances of the Home, Products,
01:04and Login components.
01:06If I run the application in its current state, I see that the ViewStack only
01:11exposes its first view or first layer.
01:14So I am going to create three buttons in the application that when clicked
01:18take the user to the particular layers that they want.
01:21I will return to the code.
01:24First, I'll create the buttons.
01:27I could place the buttons anywhere on the screen I'd like.
01:30But I'll take an opportunity here to show you a particular feature of the
01:34Application component in Flex 4.
01:36The Application component implements a property called controlBarContent.
01:41When you add visual objects to the controlBarContent array, it places them in a
01:46horizontal group across the top of the screen.
01:49And then if the rest of the application has to scroll up and down,
01:53that component stays docked to the top.
01:55I will add the controlBarContent right after the Declarations element,
02:00and before the logo.
02:04I'll create a pair of tags called s: controlBarContent and then I'll create three
02:09buttons, and I'll assign the first one an explicit label of Home.
02:15Now, I'm going to clone that line of code by holding down the Ctrl and Alt keys
02:19on Windows or the Command and Alt keys on Mac, and pressing the Down arrow.
02:25There is one more button and two more buttons.
02:29Then I'll release all of the keys, and I'll change the label for the second
02:33button to Products and for the third button to Login.
02:38Here's how the application looks now with the controlBarContent and the three buttons.
02:45I have buttons labeled Home, Products, and Login.
02:48And again, they are contained within his control bar that's docked to the top of the screen.
02:53Now, in order to move from one screen to another in a ViewStack, you can use
02:57one of two properties.
02:59The first property is called selectedIndex.
03:02If you want to navigate using the selectedIndex property, you have to know the
03:06index position of a component within the ViewStack.
03:10The first component, that is the NavigatorContent that wraps the Home component,
03:15would have an index of zero.
03:17The second would have an index of one, and so on.
03:20That can be useful in some applications, but a lot of times it doesn't make any sense.
03:25For example, in this application, I have three components called Home,
03:29Products, and Login.
03:30I don't care about the order in which they are declared and later on that
03:35order might change.
03:36So it's going to be more maintainable to address and expose these layers by
03:41their unique identifiers.
03:43To do that, make sure that you've assigned an id to each of the components
03:48that you want to expose.
03:50Then in order to navigate from screen to screen when the user clicks the
03:54buttons, use this code.
03:56I'll add a Click event and I'm not going to generate a full clickHandler
04:01function, because I only need to execute a single line of code which will look
04:05like this, views.selectedChild = home.
04:10The home reference refers to the home id of the NavigatorContent in the first position.
04:17Now, I'm going to select and copy that code that sets the click event and I'll
04:23paste it into the other two buttons, and I'll change the references for the
04:30selectedChild property to Products and to Login.
04:35So this is now programmatic navigation.
04:37I am listening for events that are dispatched by visual controls, which
04:42I have placed in the controlBarContent area, but could be placed anywhere on the screen.
04:47And when the user clicks on those buttons, I'm calling a little bit of
04:50ActionScript code to move from screen to screen.
04:53I will run the application and here's the result.
04:58I can now go from the Home screen to the Product screen to the Login screen.
05:04All of the navigation is being controlled by my explicit ActionScript code.
05:09Now, there are more automated ways of creating this form of navigation.
05:13Now, I'll show you one that uses a component called the ButtonBar in another video.
05:17But it's worth knowing that if you need to use explicit navigation, you can
05:21control it completely with your own ActionScript code.
Collapse this transcript
Using the ButtonBar component
00:00The Flex 4 ButtonBar component is designed to provide a navigational interface
00:05when you use a ViewStack.
00:07When you use the ButtonBar, it gets its data from the ViewStack through a
00:10property called dataProvider.
00:13You bind the two controls together using the dataProvider property of the ButtonBar.
00:18And then the ButtonBar will automatically create one button for each nested
00:22layer within the ViewStack.
00:24And it will display on its face a label which is derived from the Label property
00:29of the nested container.
00:31For this demonstration, I will use an application called ButtonBarDemo.mxml.
00:36This version of the application already has a ViewStack with three layers.
00:41Three NavigatorContent containers, each containing one component, for the Home
00:46screen, the Product screen, and the Login screen.
00:50Now, I'm going to add the ButtonBar component toward the top of the screen.
00:53I will place the cursor above the Logo component and then create the ButtonBar.
00:59And I will set its dataProvider using a binding expression, binding to the id of the ViewStack.
01:08Now, notice that each of the NavigatorContent components has a label attribute
01:13with the value set to strings that are viewable by the user:
01:17Home, Product and Login.
01:18When I run the application, these labels will be displayed on the faces of each
01:23of the three generated buttons.
01:26I'm going to add constraint properties to the ButtonBar to place it on the screen.
01:31I'll add a top setting of 10 pixels and a horizontalCenter of 0.
01:35So it will be centered on the screen and placed 10 pixels from the top.
01:39I will run the application and show you that the ButtonBar is generated
01:45just where I wanted it.
01:46And notice that the three buttons are created automatically one for each
01:50layer of the ViewStack.
01:52And also that the labels are displayed correctly, just as declared in the nested containers.
01:56I will click the Products button and that takes me to that layer and the Login button.
02:02And then finally go back to Home.
02:05Here's another thing to think about with ViewStacks.
02:08Notice as I cycle through the ViewStack that the positioning of the
02:12objects doesn't change.
02:14That's because even though the ViewStack is placed on the screen
02:17centered initially.
02:19When I navigate to other layers of ViewStack, it's not resizing automatically.
02:23That's a default behavior that can save on resources.
02:26But in a small-scale application like this, it's nice to have the ViewStack
02:30automatically conform to its nested contents.
02:34You can make that happen with a very simple property called resizeToCcontent.
02:38I will go back to the code
02:41to the mx: ViewStack tag and I will add a resizeToContent property set to a value of true.
02:47I will run the application again.
02:51And once again, I'll navigate through the different layers of the ViewStack.
02:55And this time you will see that the ViewStack positioning is changing
02:57automatically so that it continues to be positioned in the center of the screen.
03:03And I will mention one more thing about the ButtonBar while we are at it.
03:06Notice that the ButtonBar component generates toggle buttons, not simple command buttons.
03:12For developers who might have worked with Flex 3, the ButtonBar is designed to
03:16replace all of the navigator toolbars that were included with Flex 3:
03:20the ButtonBar, the ToggleButtonBar, the TabBar and the LinkBar.
03:25The reason you don't need those other components anymore is because the
03:29ButtonBar is a skinnable component.
03:31That is to say you can create your own custom skin for the ButtonBar component
03:37and give it any appearance you like.
03:39So the ButtonBar component is designed to bind directly to the ViewStack and
03:44produce all of the user interface controls you need to provide easy automatic
03:48navigation in your Flex application.
Collapse this transcript
Using the TabNavigator and Accordion containers
00:00The Flex 4 SDK includes two other useful navigation containers called the TabNavigator
00:06and the Accordion.
00:08Just as with the ViewStack, these are older MX components that have not been
00:12updated for Spark and Flex 4, but are still very usable.
00:16I'll demonstrate these in the applications TabNavDemo and AccordionDemo.mxml.
00:23I'll start in TabNavDemo.
00:25In the current version of the application, I'm displaying three containers in a
00:29ViewStack so that only one of the containers is visible at a time.
00:34In this version of the application, I don't have any user interface controls
00:38that the user can click to move from one screen to another.
00:40So I'm going to replace the ViewStack with the TabNavigator.
00:45I'll go first to the begin tag.
00:47I'll delete the term ViewStack and I'll replace it with TabNavigator.
00:52Then I'll go down to the end tag and replace that as well.
01:00I'll save and run the application and show you that the initial presentation
01:04doesn't look so great.
01:06Notice that the TabNavigator has an automatic background color that doesn't look
01:10too sharp in this application and also when you move from one screen to another--
01:14Right now I don't have the resized content property set to true, so the
01:18TabNavigator itself is retaining its original size creating a big rectangular
01:24spot in the background that I don't like.
01:26So here's how I'm going to solve the appearance of the TabNavigator in this application.
01:30I'm going to reset a couple of styles.
01:34First of all, I'm going to copy the backgroundColor setting from the Application
01:38to the TabNavigator so that it's sharing the same background color.
01:42I'm also going to get rid of the automatic border that appears in a TabNavigator
01:47to make sure it's not displaying an extra rectangle in the background.
01:51I'll set the borderStyle to none.
01:53I'll save and run the application again.
01:59This time you'll see that tabs appear that look pretty much the same as the
02:02toggle buttons produced by the ButtonBar.
02:05As I click around, I'm navigating from screen to screen.
02:09So the TabNavigator provides pretty much the same functionality as a ViewStack
02:14and a Spark ButtonBar.
02:16The benefit of the Spark ButtonBar is that it's skinnable.
02:19The benefit of the TabNavigator is that it's all a single container.
02:24Now let's take a look at the Accordion component.
02:26I'll close this application and go to AccordionDemo.mxml.
02:32This time, I'm starting with the TabNavigator and I'm going to make some changes.
02:36I'll change the TabNavigator to Accordion both in the begin tag and the end tag.
02:44I'm going to reposition the object a little bit higher on the screen.
02:47Instead of placing it 172 pixels from the top, I'll place it just 70 pixels from the top.
02:54Then I'm going to give it an explicit height of 500 pixels and an explicit
02:59width of 700 pixels.
03:01I'll run the application and let's see what the Accordion looks like.
03:07The Accordion is bleeding off the screen, because I made it a little higher than
03:11my browser will allow.
03:13So I'll so go back to the application and change its height from 500 to 400 and run it again.
03:21Now I'm able to see all of the various controls of the Accordion.
03:25The Accordion provides navigation through headers.
03:28Each header has a label that's derived from the nested components it's going to display.
03:33When you click on the link, you're navigating from one layer to another.
03:37Now just as with the TabNavigator container, you can control the appearance of
03:42the Accordion by setting its backgroundColor and other styles.
03:46Take a look at the Accordion container documentation for more ideas about how to use it.
03:50But again remember that these are older MX components.
03:54They are compatible with the new Spark architecture if you wrap your Spark
03:59components in the NavigatorContent container first.
04:02For the moment, the TabNavigator and the Accordion have not been rewritten into
04:07the new component architecture, but they work fine in Flex 4 applications.
Collapse this transcript
10. Using Cascading Style Sheets (CSS)
Understanding inline CSS declarations
00:00The visual appearance of a Flex application is determined by a combination of
00:04vector graphics, bitmap graphics, and cascading style sheets or CSS.
00:10The Flex 4 SDK implements the cascading style sheet standard.
00:14Its implementation is neither a superset nor a subset of the CSS
00:18implementation in a web browser.
00:20It has certain things in common and certain things that are different.
00:24For the demonstrations in this chapter, I'll import a project from the Exercise Files.
00:30If you have access to the Exercise Files, you can import it yourself.
00:34You'll find it in a folder called 10_CSS > begin and the name of the
00:38importable project is CSS.fxp.
00:44After importing the file, open the source folder, open the default package and
00:50open the file InlineCSS.mxml.
00:56Cascading style sheets are implemented internally different than actual properties.
01:00For example, the Label control has a property called text,
01:05but a number of styles such as fontFamily, fontWeight, Color, and so on that
01:10determines the appearance of the text.
01:13One of the main differences between a style and a property is in how you can set it at runtime.
01:18For example, properties can be set using dot syntax whereas styles have to be
01:23set using an ActionScript method called setStyle.
01:27But when you declare a style inline in an MXML tag, it looks just like a property.
01:33For example, if I wanted to set the size of a Label control, I'd use a
01:38style called fontSize.
01:41The fontSize style must be set to a numeric value, which is the number of pixels.
01:47As I've mentioned in other videos, font sizes in Flex are only measured in terms
01:52of pixels and never in terms of ems or any other unit of measurement.
01:57Here is another style that's available on all controls that display text.
02:01fontStyle takes a string.
02:04One of these two values, italic or normal.
02:07I'll set this first Label to a fontStyle of italic, and then run the application
02:12to show you the result.
02:16Here is the label that has a fontSize of 24 pixels and a fontStyle of italic.
02:23Now to find out what styles are available on any particular control, look at
02:27the API documentation.
02:29I'll place the cursor inside the Label tag and then press Shift+F2. That will
02:35take me to the Help screen for this control.
02:38Remember that that's the same keyboard shortcut on both Mac and Windows.
02:42Now if for any reason that context- sensitive help doesn't work, you can click
02:47into the text box and type in the fully qualified name of the class you're interested in.
02:52In the API documentation screen for this component, click the Styles link.
02:57Within the Styles section, you can either look at all styles that are applied
03:01for this component, including the styles that are natively set up on it super
03:05classes or you can hide the inherited styles and only look at the styles that
03:10are declared natively in this class.
03:12I'm going to look at all styles.
03:15I'll scroll down to the section that's start with the word font and show you
03:19that there is a style called fontFamily, another one called fontLookup,
03:22fontSize, fontStyle, fontWeight and so on.
03:27As I mentioned, certain styles accept strings but require particular values.
03:32The fontStyle for example requires values of either NORMAL or ITALIC or you can
03:37always put in value of INHERIT.
03:40These enumerated values are frequently represented as constants in
03:44particular classes.
03:45For example, the possible values for the fontStyle are represented in a
03:50class called FontPosture.
03:53You can use these classes in MXML declarations in inline styles if you prefer.
03:59I'll switch back to Flash Builder, and I'm going to remove the word italic.
04:03Then put in a binding expression.
04:06I'll start with a brace and type in the class name FontPosture, then a dot and
04:12then the constant name ITALIC.
04:14I'll close the expression and press Ctrl+S or Command+S on Mac to save the file,
04:21and you'll see an error occur.
04:23The message I'm interested in is "Access of undefined property FontPosture."
04:28Now the reason that's happening is because I'm referring to a class that has not been imported.
04:33Here is a very simple way to import the class without having to know ahead of
04:36time what its package is.
04:39I'll place the cursor after the class name and press Control+Space.
04:44That's again the same keyboard shortcut on both Mac and Windows.
04:48It appears that nothing happened, but if I scroll up to the top of the
04:52application, I'll find a Flash Builder has added a new Script section and the
04:57required import statement for the FontPosture class.
05:00Now I'll scroll back to the code and once again save the file and now the
05:05error goes away, and the constant wrapped inside of binding expression works correctly.
05:10I'll run the application to show you that the constant in fact does affect the
05:13appearance of the Label.
05:16Some developers prefer to use this style of coding instead of simple strings,
05:20because we you use a constant, for example by adding an s where it doesn't belong.
05:26When you save and try to rebuild the application, you'll get a compiler error
05:31such as this one, "Access of possibly undefined property ITALICs."
05:36So constants are available for styles that take strings in many cases, not all.
05:41If you're not sure, look at the documentation.
05:44So inline style declarations are nothing more than MXML attributes that name the
05:50style and pass in a value.
05:52Different styles take different data types.
05:55The documentation will tell you what data type is appropriate for which style
05:59and sometimes you can use constants to pass in the value that you want.
Collapse this transcript
Using embedded style sheets
00:00You can declare styles in three major ways, as inline styles, which I have shown
00:05in another video, as an embedded style sheet, or in an external style sheet.
00:10An embedded style sheet is placed within an MXML document, a main application file,
00:16or a custom MXML file.
00:19For this demonstration, I'll use the application EmbeddedCSS.mxml.
00:25When you declare a style sheet within an MXML file it's called an embedded style sheet
00:29and it's placed within a pair of tags labeled fx:Style.
00:34I'll place the cursor after the Script section in this application.
00:37I'll type in a less than character and then the beginning of the tag, sty.
00:43That takes me to the fx:Style tag name.
00:45I will select that tag from the list and type in the greater than character and
00:50Flash Builder auto completes the style element.
00:53Notice that Flash Builder adds a couple of namespace declarations.
00:58These operate exactly the same way as the namespace declarations in an MXML tag.
01:02For example, this is the Spark namespace declaration in the Application tag.
01:07The string library://ns.adobe.com/flex/ spark is associated with the prefix s.
01:19In a style element you are doing exactly the same thing but the syntax is different.
01:24You start with a string @namespace.
01:27Then after a space, you declare the prefix.
01:30Then within a pair of quotes the namespace string and you finish the
01:34declaration with a semicolon.
01:36This syntax for declaring namespaces in cascading style sheets is determined by
01:41the W3C's recommendation for namespaces in CSS.
01:45Adobe adopted this syntax for Flex 4 because it makes it possible to
01:50distinguish between style declarations for Spark components or MX components
01:55that share the same name.
01:58In order to declare a style for a particular component, you start with
02:02the namespace prefix.
02:04So I will type in the letter s for Spark and then I will type in the pipe character.
02:10You will find this character on your keyboard on the same key as the Backslash.
02:14Hold down the Shift key, press that key, and you will get this
02:17vertical character.
02:18Flash Builder shows me a listing of all of the known components that are members
02:22of the Spark namespace.
02:24For this demonstration, I'll choose the Label component.
02:28This is called a CSS selector.
02:31It's a way of determining how set of styles will be applied.
02:35In this case, I'm declaring a set of styles or a style rule, which will be
02:40applied to all instances of the Label class.
02:43Now after a space, I will type in a beginning brace character and press Enter
02:47and Flash Builder adds a closing brace and an empty line, and I can now
02:52declare one or more styles within the braces that will be applied to all
02:57elements that match the selector.
02:58I will type in the word font because there are many styles I could apply to a
03:02Label that start with that string and then I press Ctrl+Space.
03:06When you declare styles within an embedded style sheet, you can use either
03:11hyphenated style names as shown in the drop -down list or you can use CamelCase names.
03:17CamelCase is the style that you use in an MXML tag.
03:20For example, in the Application tag, BackgroundColor is using CamelCase.
03:26Once again I place the cursor after the word font and press Ctrl+Space, and
03:31I will choose the font-size style.
03:33I type in a colon and then a value of 20 followed by a semicolon.
03:39So this means that all label controls should be 20 pixels in size.
03:44I will also set a font-family of Verdana.
03:51Once again the syntax will be the property name, a colon, the property
03:55value, and a semicolon.
03:58Now I'll save and run the application and show you that the result is that all
04:03labels are 20 pixels and all labels are Verdana and the exception to this would
04:09be any Label control that already has an inline CSS declaration.
04:13For example, in this application, the first label has a font size of 24 set
04:19as in inline style.
04:21Anything that you set as in the inline style takes precedence over an
04:25embedded style sheet.
04:27However, that first Label control does not have a font-family setting so it uses
04:32the font-family style from the Label selector.
04:35So that's one very simple use of an embedded style sheet.
04:38Here is another way to think about using an embedded style sheet.
04:41This time I'm going to take the BorderContainer styles that are
04:44declared multiple times.
04:47This BorderContainer component, for example, has backgroundColor, borderStyle,
04:52borderColor, borderWeight and so on, and I have another BorderContainer right
04:56below it that is using exactly the same styles.
04:59So I am going to place the cursor after the Label selector and I will type in
05:03Border and press Ctrl+Enter.
05:06This is another approach to filling in selector names in Flash Builder.
05:10Instead of starting with the prefix, I start with the component name.
05:13I press Ctrl+Space to bring up the Code Assist list and choose the component I
05:18want to use and Flash Builder fills in the namespace prefix for me.
05:21I put in my braces and then I'm going to copy and paste all of these settings,
05:29moving them from the MXML tag to the styles tag.
05:34For each of these settings, I am going to remove the equals and quote before
05:37the value and replace it with colon and also remove the closing quote and
05:42replace it with a semicolon.
05:44I will do that for the backgroundColor, the borderStyle, the borderColor,
05:50the borderWeight, the cornerRadius and the dropShadowVisible styles.
06:20I will then go to each of the BorderContainers and remove those styles so that
06:24I only having to clear the styles once.
06:26I am now saying that all BorderContainers have these default styles.
06:32Notice I am leaving the x and y properties in the first BorderContainer and
06:36horizontalCenter, verticalCenter and width in the second.
06:39That's because those are properties and not styles and properties can't be
06:44declared inside an embedded style sheet.
06:46I'll run the application and show you that the BorderContainers look exactly the
06:51same as they did before with the borders, the backgroundColors, cornerRadius,
06:56dropShadows and so on but now those visual presentation settings are being
07:00determined within the embedded style sheet.
07:04Embedded style sheets have the advantage of grouping the code that's determining
07:08the visual appearance of the application with the application code itself.
07:13Many developers, however, prefer to place their CSS code in external CSS files.
07:18I will show you how to do that though in a separate video.
Collapse this transcript
Using type, style name, and id selectors
00:00When you use embedded or external style sheets to determine the appearance of a
00:04Flex application, you create style rules in selectors.
00:09The Flex 4 SDK supports a number of different selector types.
00:13I'll show you how to use them in this demonstration using the
00:16application, CSSSelectors.mxml.
00:21In the beginning state of the application, I have three buttons wrapped inside a BorderContainer.
00:26Within the BorderContainer, there is a panel and the panel contains a couple of
00:30Labels, a TextInput control, and a TextArea control.
00:35I'll take a look at the application in Source View and I'm going to start by
00:39creating an embedded style sheet inside the application, after the Application tag
00:44and before any of the visual components.
00:46I'll make a blank line and type in a less than character, then sty, and I'll
00:53press Enter or Return to select the fx:Style tag and type the greater than
00:57character to auto-complete the tag and add the name spaces for the Spark and MX component sets.
01:03The first selector I'll show you is one of the most commonly used.
01:06It's called the type selector.
01:08It's similar to a tag selector in HTML.
01:12You set the name of the selector matching the name of the component or class
01:16that you want to display the styles.
01:19So for example, if I want all Spark buttons in the entire application to show a
01:24particular font size, I would create a button selector like this.
01:28I'll type in the word Button, and then press Ctrl+Space.
01:33Notice that there are two button controls, the MX button and the Spark button.
01:37I'll choose the Spark button, type in a Spacebar, an opening brace and press Enter.
01:43And now, any styles that I declare between the braces will be applied to all
01:48instances of the button component throughout the application.
01:51You can declare type selectors only in the application itself, not in any custom components.
01:57I'll add the font-size style of 18 pixels and a color of red or I could use the
02:06hexadecimal value FF0000.
02:11I'll save and look at the application in Design mode and you'll see that all
02:15button controls are now displaying a red font in 18 pixels and I'll go back to the code.
02:22Now, if I have another kind of control to which I want to apply a certain set of styles,
02:26I can declare the styles again in a separate selector.
02:30I'll add a comment after the s|Button selector, and I'll add a selector for the
02:35Spark TextInput control.
02:37So now, all controls in the comma delimited list will receive the styles
02:41that I've declared.
02:43This is still called a type selector, but it's a list of type selectors now,
02:48instead of just a single selector.
02:50There is my three buttons and there is my TextInput control.
02:56You can also apply type selectors, taking into account the object-oriented
03:00inheritance of components.
03:02For example, the TextInput control and the TextArea control are both sub-classed
03:08from a special component called SkinnableTextBase.
03:12If you apply a style to the superclass, all of the subclasses receive that style as well.
03:18I'll start off by typing the word Skinn with two Ns and then I'll press
03:23Ctrl+Space and select the s|SkinnableTextBase class.
03:27Now this is not a class you would typically instantiate directly in a Flex application.
03:35But I'm going to apply a style called content-background-color and I'll set it
03:40to a named value of aqua.
03:41Then I'll look at the application in Design view and you'll see that both the
03:45TextInput and the TextArea controls have received this style, again because
03:50they are both subclasses of the SkinnableTextBase component.
03:55So that's a look at how to use type selectors.
03:59Now let's talk about how to use style name selectors.
04:02A style name in Flex is similar to a CSS class in HTML. That is, it's an
04:09arbitrary class name to which you explicitly add controls using MXML markup or ActionScript code.
04:16I'm going to declare a style name like this.
04:19I'll put in a dot and then apply the style name.
04:24The name of the style will be blueText.
04:27And I'll add a style to its rule, where the name of the style is color, and the
04:31value is #0000FF for blue.
04:36Now, on its own the style name doesn't do anything.
04:39If I look at the application in Design View, you'll see that none of the
04:42controls have changed.
04:43But I'll go back to the code and I'll go to the second button, and I'll add a
04:48style name property set to a value of blueText.
04:53Now notice that you used the dot when you defined the style name in the
04:57embedded style sheet.
04:59But when you refer to the style name in your MXML or ActionScript code, you just
05:03pass in the name itself without the dot prefix.
05:07Now, I'll look at the application in Design Mode again, and show you that the
05:11second button is using blue text.
05:15So that's how you use style names.
05:17Again, you define them in your style sheet and then you apply them using the
05:21style name property in any component that you want to use those styles.
05:26Finally, I'll show you one more CSS selector that's new to Flex 4.
05:30It's called the id selector.
05:32The id selector works exactly like it does in HTML.
05:36Let's take this Button component that has an id of button3.
05:41I can add styles directly in the MXML component or I can add a style selector
05:46in the style sheet.
05:47An id selector starts with a pound and finishes with the id of the control to
05:55which you want to apply the style.
05:57I'll add a pair of braces and I'll add the font-style style and set it to a value of italic.
06:06And I'll also add a text-decoration style, and set that to a value of underline.
06:11Then I'll look at the application in Design Mode again and show you that
06:16the one control that has the id that matches that selector has italics and underlining.
06:23So again, id selectors are new to the Flex 4 SDK.
06:27You couldn't use them in Flex 3.
06:29Both style name selectors and id selectors can be defined in either custom
06:34components or at the application level.
06:37In this example, I've defined my embedded style sheet in the application main file.
06:42And so the styles will apply not just to the application file, but also to any
06:47nested custom components.
06:49As I've mentioned, your type selectors must be defined at this level.
06:53But style name and id selectors can be defined in the custom
06:56components themselves.
Collapse this transcript
Using descendant and global selectors
00:00The Flex 4 SDK supports a couple of selectors that you can use to affect
00:04controls throughout an application or in very specific situations.
00:10In this demonstration, I'll show you the global selector and a new feature of
00:14the Flex 4 SDK called descendant selectors.
00:18The global selector is applied only at the application level that is you can
00:22declare it only in an embedded style sheet within the application or an external
00:26style sheet that's linked into the application main file.
00:30You can't use the global selector say in a style sheet that's linked into
00:34a custom component.
00:36When you declare the global selector, its properties are applied to all visual
00:40components throughout the application.
00:43Common uses of the global selector include setting a default font family,
00:46font size, and color.
00:49I'll demonstrate the global selector in the application file,
00:52AdvancedSelectors.mxml.
00:55I'll place the cursor above the existing style declarations in the embedded
00:58style sheet, and type in the name of the selector, global.
01:02Notice that it's in all lowercase.
01:04This is a requirement.
01:06Within the global selector, I'll set the font-family style and I'll set it to a
01:11value of _typewriter.
01:15_typewriter is a system font name.
01:18It doesn't refer to a specific font face such as Courier or Courier New, but
01:23instead to all fonts of that style, fixed-pitch typewriter style fonts.
01:28Similar system names include sans-serif and serif.
01:33So this means that all visual controls that render text such as labels and rich
01:37text controls throughout the application will use whatever is the system
01:42typewriter font on the users' computer.
01:46I'll save and run the application, and here's the result.
01:50Every visual control which previously might have been displaying Arial or
01:53Geneva, or whatever the standard font was, is now displaying a Courier style font.
01:59So that's the global selector.
02:01Going to the other extreme of applying styles only in very specific situations,
02:06the Flex 4 SDK now implements what are known as descendant selectors.
02:11A descendant selector, sometimes also called a contextual selector, is a
02:15selector that defines styles for an object only when it's placed within a container.
02:20For example, let's say that I wanted to set styles for all labels throughout the application.
02:27I'll put in a style selector for s| Label and then within the braces, I'll set a
02:32style called text-decoration and I'll set it to a value of underline.
02:37This means that all label controls are now underlined.
02:40I'll run the application and show you that the label controls within the panel,
02:46the label within the header of the panel, and even the label controls that are
02:50in the buttons are underlined.
02:52This is actually something very important to know about the nature of the
02:56Flex visual controls.
02:58A label control is used in a lot of different components.
03:02So, if you apply a style to the Label selector, you're affecting a lot of
03:06controls throughout the application.
03:09So, let's say instead that what I have really want to do is apply underlining
03:14only to labels that are contained directly within panels.
03:17Here is how I do it.
03:18I'll place the cursor before the Label selector, and I'll add s|Panel and
03:26I'll separate the two selectors with the space.
03:29This is now a descendant selector.
03:32It means that these styles are applied to labels only when they are direct
03:35child objects of a panel.
03:38I'll save and run the application again, and here's the result.
03:42Now, the Button control labels don't have the underlining, but the label
03:47controls within the panel do, both within the content area of the panel
03:51and within the header.
03:53In order to be more specific, you can apply a combination of styles.
03:57So for example, let's go down to the labels within the panel.
04:02Here are my two labels inside the panel.
04:05I'm going to give them a style name of content.
04:11Now that style name doesn't actually exist yet.
04:13That is, it hasn't been declared in the CSS style sheet.
04:17But now, I'll go back up to my style selector for labels within panels and
04:23after the Label selector, I'll add .content.
04:27So this now means that only labels that are within panels that are members of
04:32the style name content will receive underlining.
04:36I'll run the application and here's the result.
04:41Now the labels that are contained within the content area of the panel have
04:44underlining, but the label that's in the header doesn't.
04:47You might notice that there is an underline on the third button and if you
04:52look at the code, you'll see the reason for that is that I already have an id
04:56selector that's applying that underlining in that style sheet.
05:01So, understanding how to use cascading style sheets in a Flex application is a
05:05combination of understanding the rules of the different selectors, that the
05:10global and type selectors can only be applied in the application itself, while
05:14style name and id selectors can be applied in custom components as well and also
05:20understanding the precedence of the different selectors.
05:23The global selector has the lowest precedence.
05:26Style names have the next level of precedence.
05:29Type selectors and id selectors round out the rules.
05:33Cascading style sheet selectors and styles are then used to determine the
05:39overall appearance of your Flex application.
Collapse this transcript
Using external style sheets
00:00The styles that you declare in an embedded style sheet can also be declared in
00:04an external style sheet.
00:06External style sheets are simply text files with the file extension of .CSS.
00:11They must be placed in your source code root folder or in a package within the
00:15source code root and they are compiled into the application rather than being
00:19loaded at runtime by default.
00:22There are a few ways of creating an external style sheet, but I'd like to show
00:25you a particular approach that involves the use of Design Mode.
00:29I'll start with the application ExternalCSS.mxml.
00:34When I run the application, you'll see that it has a few buttons.
00:38The first button has some styles that are declared inline.
00:42So these styles are applied only to this particular button.
00:45There is a particular font- face and a particular font-size.
00:49The other buttons have default styles.
00:53Now I'll take a look at the code for the button controls, and show you that the
00:57first button has font-family and font -size styles, again declared inline.
01:03Here's how you can move inline styles to an external style sheet and along the way,
01:08create a brand-new external style sheet.
01:12I'll choose the button that has the inline styles in Design Mode and then look
01:16at the Properties view in the lower-right corner.
01:20I'll click the button Convert to CSS.
01:23This is called the New Style Rule dialog.
01:26It will move the styles that are declared inline in the selected control to an
01:30external style sheet.
01:32The first item to select is the Define style in list.
01:36I don't have any external style sheets already created, so the list is empty.
01:40So I'll click the button labeled New.
01:43Now again, you can create your external style sheets in a package or in
01:47the source code root.
01:48I'm going to leave the Package setting blank, choose a file name of styles.
01:54Don't add the .CSS extension here.
01:57If you do, Flash Builder will add it twice.
02:00That is to say, you might get a file called styles.CSS.CSS.
02:05Just put in the beginning of the filename you want to create. Then click Finish.
02:10There is the new file, src/styles.css, placed in the source code root folder of the project.
02:18Next, I determine how the style selector for the selected button will be created.
02:22The default is to apply the styles to a specific component, more specifically
02:27all Button controls.
02:29You can also add the styles to the global selector, to all components with a
02:34particular style name, or to a specific component with the style name.
02:38I'm going to use the default setting that is I'm going to apply the inline
02:42styles to all instances of the Spark button control and I'll click OK.
02:48And here is the new style sheet that results.
02:50It includes the Spark and MX name space declarations and it has a new
02:55selector that implements the styles that previously were declared inline in
03:00the button control.
03:01Now, I'll go back to the file, ExternalCSS.mxml.
03:05That's the MXML application I started with, and you'll see that all three of the
03:09buttons now have those styles.
03:11I'll go to the Source Mode, and show you the reason.
03:15Up toward the top of the file, a new style tag has been added to the application.
03:21It's an empty tag, that is it only has a begin tag, and not an end tag, and
03:25it has a source attribute that references the new external style sheet.
03:29You can take all of the other styles that you've already declared in an embedded
03:33style sheet and manually move them into the external style sheet.
03:37So I'm going to go up to my embedded style sheet and select just the styles.
03:42I'm not going to take the name space declarations or the wrapping text.
03:47And I'll cut those to the clipboard.
03:50I'll go to the styles.CSS file and I'll paste them in.
03:57Notice that when you paste in the new code that Flash Builder automatically
04:01fixes the indentation, so everything matches up really nicely.
04:05Now, I'll save my changes to styles. CSS and go back to the application.
04:11And I'm going to delete the fx:Style tag set that formally included the embedded
04:16styles, leaving just the fx: Style tag with the source attribute.
04:21I'll save and run the application and you'll see that all the styles that were
04:26previously controlled by the embedded style sheet are now being implemented by
04:30the external style sheet.
04:32When you use an external style sheet using the fx:Style tag, the styles are
04:37compiled into the application.
04:39They aren't loaded at runtime, like a browser might with an external style sheet.
04:43Instead they are part of the application and you can overwrite the styles in a
04:47few different ways at runtime by executing ActionScript commands that either
04:52modify explicit styles or style names or by loading styles from the disk or
04:57server that have been precompiled into SWF files.
05:01I'll show you how to use those techniques in other videos.
Collapse this transcript
Setting and getting styles with ActionScript
00:00The Flex SDK gives you a couple of ways of changing styles at runtime,
00:05including the setStyle method and a set of functions that are members of a
00:09class called the styleManager.
00:11I'll show you how to use both techniques in this demonstration.
00:14I'll start with the setStyle method.
00:17Every visual control supports the setStyle method.
00:21You don't set styles using simple dot notation.
00:23For example let's take this label with an id of myLabel.
00:28If I wanted to change its typeface at run time to say a Courier or I wanted to
00:33change its font size,
00:35I would not use the code myLabel.fontsize or myLabel.fontfamily.
00:41If you try to do that, the compiler will throw an error because you can't use
00:45dot notation on styles.
00:47Instead you call the setStyle method.
00:50The first requirement is that the control you want to modify at runtime has an ID.
00:54In this case, I'll use the label with an ID of myLabel.
00:58Then you call the setStyle method and you pass in minimally two arguments.
01:03The first argument is the name of the style as a string and the second is the
01:08new value, you want to assign to the style.
01:11I'll demonstrate this by adding a click event to this first button control.
01:16I'll click into the button tag and press the Spacebar and add a click attribute
01:21and allow Flash Builder to generate a click handler function.
01:26Then I'll scroll up to the top and see the new script section that Flash Builder has added.
01:32I'll delete the comment that was added in the function and replace it with this code,
01:36myLabel.setstyle.
01:40Then I'll pass in the name of the property and I'll use the style name fontSize
01:46and a new value, which I'll assign to a value of 24.
01:50Notice in the signature of the setStyle method that the second argument labeled
01:54newValue has a data type of an asterisk.
01:58That means that you can pass in any data type into var argument that you want.
02:03It's designed that way because different styles require different data types.
02:07Numbers, unsigned integers, strings and so on.
02:13I'll complete the statement with a closing parenthesis and semicolon and save
02:17and run the application.
02:22I'll click the button labeled Set Control Style and there is the result,
02:27the label size changes instantly.
02:29Now, you should be cautious using the setStyle method.
02:32It's fine using it on an individual control such as I've done here.
02:36But if you call the setStyle at the application level or on a container that has
02:40a very deeply nested containership hierarchy you can see sluggish performance.
02:46The reason is because the framework has to dig through the entire containership
02:50and figure out each individual object to which the new style should be applied.
02:54So if you need to set Styles at runtime on a single object, it should work fine.
02:59If you need to set them on a lot of objects at the same time, you might examine
03:04another approach, and here's one of those.
03:07The application component in Flex 4 has a property called styleManager, which is
03:12an instance of a class called styleManager.
03:14The styleManager class has a number of useful functions including one that can
03:19modify a style name at runtime.
03:22I'll demonstrate this using the second label in my panel.
03:27It has an id of redLabel.
03:29But to demonstrate the use of the styleManager I'll add a style name
03:33attribute of redFont.
03:36Now, this style name hasn't been declared anywhere in my application yet.
03:41If you go to the embedded style sheet, you'll see that there is no .redFont selector.
03:47Now, I'll show you how to create CSS style declaration object and apply styles to it
03:54and then apply that object to the style name at run time.
03:58I'll go to the applications second button and add a click event and generate a clickHandler.
04:06I'll go to the generated clickHandler method and remove the comment and I'll
04:10start by declaring a variable.
04:13I'll name the variable css and set its data type as CSSStyleDeclaration.
04:22Then I'll instantiate that object using the new keyword and the constructor
04:27method for CSSStyleDeclaration.
04:30This constructor method can take a couple of different arguments but they are
04:33all optional, so I'll call the constructor method with no arguments.
04:38Now, I'm going to use the same setStyle method that I used before.
04:42But I'll apply it to this abstract declaration.
04:45That is it hasn't been applied to any particular object or style name yet.
04:50I'll use css.setstyle, I'll pass in a string of "color" that's the name of the style,
04:57and a value of 0XFF0000 and that's the hexadecimal value for red.
05:05So now, I have an instance of the CSSStyleDeclaration class that represents my color.
05:10Now, I'm going to use this styleManager class, like this.
05:15The application has a property called styleManager. I'll start off by typing the
05:21word style and pressing Ctrl+Space and I'll choose the styleManager object.
05:27When you refer to styleManager object like this, it's shorthand.
05:31The long hand would be this.styleManager and every application has only one
05:36instance of the styleManager class, a singleton.
05:40Now, I'll type in a dot and I'll call the setStyleDerlaration method.
05:46Here, you can pass in any selector as a string.
05:50I'm going to use the style name selector .redFont.
05:54Now, remember that style name did not exist before. I am creating it on the fly now.
06:00Then I'll pass in my CSSStyleDeclaration. The name of it is simply css.
06:05I already created it.
06:07Then I'll pass in a required attribute, which is a Boolean value, which asks
06:12whether I want to do an immediate update on the application.
06:16I'll pass in a value of true and when the user clicks the button they should see
06:20an immediate change to the appearance of the application.
06:24I'll save and run the application.
06:27I'll click the Set Style Name button and you'll see the color of the font changes.
06:32Now, to make this a little bit more dramatic I'll go back to my code and add a new
06:37style to the CSS object.
06:39I'll place the cursor after this call to setStyle method and I'll add another
06:45call, css.setstyle, and I'll pass in fontSize and a value of 24.
06:53So, now my CSSStyleDeclaration object encapsulates two different styles and
06:59they will both be applied to the redFont style name when I click the button.
07:05I'll run the application to test it, click the button and there's a much more
07:10dramatic demonstration.
07:11I'll click the original button to show that I can still update a single object.
07:16You can see that both of these approaches for modifying the appearance of an
07:20application at runtime work great.
07:22So if you need to modify the style of a single object at runtime, just call the
07:28object setStyle method.
07:29If you need to modify more than one style or applies styles to a whole set of controls,
07:34use the CSSStyleDeclaration object to encapsulate the styles and the
07:40styleManager object's setStyleDeclaration method to apply the style object.
Collapse this transcript
Loading styles at runtime
00:00Neither Flash Player nor the Flex SDK have the ability to parse and load raw CSS
00:06style declarations at runtime.
00:08That's something that browsers know how to do with CSS.
00:11That is in a web browser you can create multiple cascading style sheet external
00:15files and then switch among them at runtime.
00:18But because Flash Player and the Flex SDK don't have that ability,
00:22you need another approach.
00:24So the Flex SDK includes the ability, first of all, to precompile CSS files as SWF files.
00:31Then the Style Manager class has a method called loadStyleDeclarations that can
00:36load a precompiled SWF file that contains CSS declarations and applied those CSS
00:41styles to the entire application, instantly at runtime.
00:45For this demonstration, I'll use an application named RuntimeCSS.mxml.
00:50The application has an embedded style sheet that applies styles to the
00:54BorderContainer, Buttons, TextInput controls and include a style name and an
00:59ID Selector.
01:01I'll run the application and show the result, which is that the Button, Label,
01:07Text Input controls and so on all have different styles applied.
01:12Now, I'm going to create a new external style sheet and I'll move the embedded
01:16styles into the external file.
01:19I'll go to the project source folder, right-click and choose New > CSS File and
01:27I'll name the file runtimestyles.
01:30Notice that I am not adding the .css file extension.
01:33In some cases, if you add the .css extension here, you'll get a file called
01:38runtimestyles.css.css.
01:41So I just put in the beginning of the file name and click Finish and here is
01:45the resulting file.
01:47Its name is runtimestyles.css and it already has the spark and mx
01:52namespace prefixes.
01:55Now, I'll go back to my application and I'll select all of my styles.
02:00I won't take the namespace declarations because my external file already has them.
02:04I'll click at the top of the style declarations, scroll down, hold down the Shift key
02:10and click at the bottom, then right-click and choose Cut.
02:16I'll go to the runtimestyles.css file and I'll Paste those styles into place and
02:22then I'll save the file.
02:23I return to RuntimeCSS.mxml and I'll select and delete the entire
02:31fx:style element.
02:32I don't need it anymore.
02:35Now, I'll save and run the application and show you what it looks like without
02:39all of those style declarations.
02:42Without the style declarations, the BorderContainer is just a simple white rectangle.
02:46All of the visual controls, such as the Button, the Label, the Text Input
02:50controls and so on all displayed the default font and it has a very simple plain appearance.
02:57I'll go back to Flash Builder and now I am going to compile that external style
03:01sheet as a SWF file.
03:04Flash Builder makes this incredibly easy.
03:06I'll go to the Package Explorer view, locate my new external style sheet,
03:11right-click on it and choose Compile CSS to SWF.
03:16Now that option on the menu is a toggle.
03:18That is to say if you right-click on the CSS file again you'll see that there's
03:22a check mark next to that option and that means that if you make any changes to
03:26the CSS file in the future, it will automatically be recompiled.
03:31Now, you won't see the SWF file appear in the source code folder.
03:35Instead, if you go and look at your output folder named bin-debug, you'll see
03:42that file runtimestyles. swf has been created here.
03:46I have a couple of other files that are left over from previous exercises.
03:51But I'll go to the Project menu and choose Clean and click OK and that will
03:56reconstruct the entire project as deployable files.
04:01I'll show you that what I end up with is just the SWF file itself.
04:05When you create a compiled SWF file, it ends up in the output folder and the
04:09original.css file is no longer copied to the output folder by default.
04:15Now, I'm going to add code to the application that loads that SWF file at runtime.
04:21I'll go to the Button with a label of Load Styles and I'll add a click event handler.
04:26I'll choose the click attribute and then have Flash Builder generate a
04:30clickHandler function.
04:34I'll go up to the generated code and remove the generated comment and I'll start
04:39with the styleManager object.
04:42The styleManager object is a property of the application, which is a singleton
04:47and implements an interface called IstyleManager2.
04:50I have used this object in other videos to demonstrate how to update styles at
04:54runtime using pure ActionScript code.
04:58From the styleManager object, I'll type in a dot and call the method
05:01loadStyleDeclarations.
05:04You notice that there are two; one with just loadStyleDeclarations and the other
05:08called loadStyleDeclarations2. Use the first one.
05:12Now, I'll pass in the URL that references the file I want to load.
05:17This is a string, so I'll wrap it in quotes and I'll pass in the name of the file,
05:22"runtimestyle.swf".
05:26The next value is a Boolean argument, which determines whether the new styles
05:30will be updated immediately.
05:33It defaults to true, but I'll go ahead and pass in a value of true anyway.
05:36The rest of the arguments have to do with security, whether the SWF file that
05:41you are loading is to be considered trusted content, the application domain and
05:45the security domain.
05:46For more information about those security settings, see the Flex documentation.
05:51I'll complete my statement by typing in a closing parenthesis and a semicolon.
05:56So now when the user clicks the button, the application will load the styles
06:00from the server and immediately update its appearance.
06:05I'll run the application and once again when it first loads it's showing the
06:08very simple presentation.
06:10When I click the button, the applications appearance updates instantly.
06:15Any styles that you declare in a runtime loaded style sheet will be applied to
06:19the entire application.
06:21Not just the components that are declared at the top-level main application file,
06:25but also all custom components and modules.
06:29So that's a look at, how to use at runtime loadable styles.
06:33Once again they must be precompiled into SWF format and use the styleManager
06:38object's loadStyleDeclarations method to load the SWF file from the server and
06:43update the application's appearance instantly.
Collapse this transcript
11. Skinning Spark Components with MXML and FXG
Drawing vector graphics with MXML
00:00The Flex 4 SDK includes a number of new ActionScript classes, which are used to
00:06render vector graphics on the screen and are at the core of the new Spark
00:10skinning architecture.
00:12In this chapter, I'm going to describe how to use these vector graphic
00:16classes, declaring instances of vector graphics with MXML code, and then how
00:21to incorporate those vector graphics into custom skins that are applied to Spark components.
00:28All of the exercises in this chapter will be done in a project that's included
00:32with the Exercise Files.
00:34If you have access to the Exercise Files, you can import this project now.
00:38Select File > Import Flex Project, then click Browse and navigate to the folder
00:4511_Skinning/begin and select the file Skinning.fxp, and import it.
00:52That will create a new project in your workspace named Skinning.
00:55Open the Project, open its source folder and its default package, and then open
01:00an application named MXMLGraphics.mxml.
01:05I'll expand the MXML editor to full screen.
01:08This is a very simple application that starts with a layout property set to
01:12VerticalLayout so that objects that I place in the application will stack
01:16from top to bottom.
01:18I'm going to show you how to declare instances of vector graphic objects using
01:23these new primitive classes that are a part of the Flex 4 SDK.
01:27There are three primary graphic classes that you can use, named Rect to create
01:32rectangles or squares, Ellipse to create ellipses or circles, and Path to create
01:38graphics that have arbitrary shapes.
01:41All three of these classes render graphics as a vector graphics.
01:45That makes them very scalable. In contrast to bitmap graphics where you show one bit at a time,
01:52vector graphics are calculated mathematically.
01:55So if you grow them or shrink them, they still retain smooth curves and other
02:00aspects of their presentation.
02:02These vector graphics are members of a package named spark.primitives.
02:06They are called primitives because that contrasts them with components.
02:11Full components such as the Group, Panel, Button and other interactive controls
02:16in the Flex SDK, all are implemented as subclasses of the UIComponent class.
02:22Those full components can be added to either Spark containers such as the
02:27application or to the older MX containers.
02:31In contrast, the primitive graphics can only be added directly to Spark containers.
02:37I'll show you how to use two of the primitive graphics here, the Rect and
02:40the Ellipse classes.
02:41I'll place the cursor after the layout property in the application, and I'll
02:47start by creating a rectangle, creating an instance of the Rect class.
02:53Each of the primitive graphics supports a width and height property, which you
02:56can set to explicit sizing with a numeric value, which is translated as the
03:00number of pixels, or with percentage values.
03:05For the Rectangle, I'll use explicit sizes with the width of 300 pixels and a height of 200.
03:13Each of the graphic classes supports a property named Fill and another one named Stroke.
03:18You use the Fill property to set the inner presentation of the graphic and
03:22the Stroke to set its border.
03:25When you declare these classes in MXML, you can set the Fill property using a child element.
03:31I'll create an s:fill child element.
03:34Then within the fill element, you can add instances of one of these four classes.
03:40BitmapFill, which is used to render a bitmap graphic in the center of a vector
03:44graphic, LinearGradient and RadialGradient, to create gradient presentations
03:49and SolidColor.
03:53For this first very simple graphic, I'll use the SolidColor class.
03:58I'll set its color property to #FF0000.
04:03If you like, you can also set the alpha property.
04:07The alpha property renders transparency and its set to a value from 0 to 1.
04:120 being fully transparent, and 1 being fully opaque.
04:16I'll give this rectangle an alpha property of .8 or 80% opaque and I'll complete the tag.
04:25Now I'll run the application in the browser and show you the results so far.
04:29The rectangle is rendered as a solid red color.
04:33It's a little lighter than a full red because of the transparency and the white background.
04:38So that's about as simple a graphic as you can render.
04:41Now I'm going to add a couple of other properties to it.
04:44I'm also going to add the Stroke property.
04:47The Stroke property renders a border around the edges of the rectangle.
04:51You set the Stroke property to one of these classes: LinearGradientStroke,
04:55RadialGradientStroke, or SolidColorStroke.
04:59I'll use SolidColorStroke.
05:00I'll set the color attribute to #000000.
05:07I'll set the weight property to 5.
05:09That means that the stroke will have a width of 5 pixels.
05:12I'll then save and run the application again.
05:19Now the rectangle has a 5-pixel wide black stroke all the way around.
05:24There are many other properties that you can add to the stroke object.
05:27You can set its color, shape, and other attributes.
05:30Take a look at the documentation for the Stroke classes for more information.
05:35Finally, I'm going to add radius settings to the rectangle.
05:40The Rect class supports properties named radiusX and radiusY.
05:44I'll set these to values of 10 and that will result in the graphic being
05:49presented with rounded corners.
05:53Once again, I'll run the application and here is my graphic with a border
05:57and rounded corners.
06:00Now I'll show you how to use the Ellipse class.
06:03You can use the Ellipse class to create a circle or an ellipse.
06:07If you set the width and height to the same values then it's a circle.
06:10Otherwise it's an ellipse.
06:12With the Ellipse class, I'll show you how to use percentage sizing.
06:16I'll set the width to a property of 75%, and that means that the graphic will
06:21shrink and grow to fill 75% of its container, the application.
06:27I'll give it an explicit height of 200 pixels.
06:31For the Ellipse, I'll set the Fill property to a gradient object.
06:35I'll create the Fill property as a child element of Ellipse and then declare an
06:40instance of the RadialGradient class.
06:43The RadialGradient class has a property named entries.
06:47The entries property is set to an array of GradientEntry objects.
06:51I'll declare the entries property as a child element of RadialGradient.
06:57Then I'll create three instances of the GradientEntry class.
07:01The GradientEntry class has a color property.
07:04I'll set the first one to a color of red using #FF0000.
07:12Then I'll clone that line of code, holding down Ctrl+Alt on Windows or
07:17Command+Option on Mac, and pressing the Down Arrow twice.
07:22Then I'll change the colors for the second and third GradientEntry objects.
07:27The second one will be green with a color of #00FF00.
07:33The last one will be blue with a color of #0000FF.
07:40I'll run the application and here is the visual result.
07:44The ellipse expands to fill 75% of the container, the application.
07:49If I resize the browser, you'll see the object shrink and grow to
07:53accommodate the change.
07:55Now the application has a min width setting.
07:58So if I shrink the width of the browser to less than the minimum width of the
08:02application, the graphics stops growing.
08:06So this is a look at how you can use a vector graphics in a Flex application,
08:10declaring them with MXML code.
08:12Once again, these are simply ActionScript classes that are parts of the Flex 4 SDK.
08:18In addition to the Rect class, which you'll use for rectangles and squares, and
08:22the Ellipse class, which you used for ellipses and circles, you can also use the
08:27Path class that renders arbitrary shapes.
08:31You'll be able to use these vector graphics in custom skins that you build for
08:35your Spark-based components.
08:37I'll show you how to incorporate such graphics in custom skins in other
08:41videos in this chapter.
Collapse this transcript
Exporting FXG files from Illustrator and Fireworks
00:00Adobe's Creative Suite 4 products, including Illustrator, Fireworks and Photoshop,
00:06have the ability to export vector graphics in a format called FXG.
00:11FXG stands for Flash XML Graphics, and it's an XML-based markup language.
00:17In Flex 4, you can use these FXG files as graphics in your applications.
00:22I am going to show you how to export these files from both Illustrator and
00:26Fireworks, which both have the ability to work with pure vector graphic formats.
00:32I'll start with Illustrator CS4.
00:34If you don't have access to Illustrator or Fireworks, you can just watch these
00:38demonstrations, and then I'll provide a finished version of an exported FXG file
00:43that you can use in the other tutorials of this chapter.
00:48In Illustrator, I'll open a vector graphics file.
00:50I'll select File > Open, I'll navigate to my Exercise Files folder, from there
00:58to the 11_Skinning folder, to the begin folder.
01:01And I'll select and open the file hp_logo.ai.
01:07This is a vector graphic design for use as a logo for the fictional
01:10company Hansel & Petal.
01:12Before you export an FXG graphic from Illustrator, you should resize
01:17the graphic's artboard.
01:19The artboard is like a canvas in Fireworks.
01:22It's the rectangular region that contains the graphic.
01:26If you export this graphic without resizing the artboard, the resulting FXG
01:30graphic will define a lot of whitespace around the actual vector graphics.
01:35I'll go to the toolbar and click the Artboard tool, and then I'll manually
01:40resize the artboard by clicking and dragging its handles.
01:44And I'll just eyeball it, resizing it so that I make sure I include all of
01:49the vector graphics.
01:51I'll resize from the top left and from the top right and then finally I'll go
01:57down to the bottom and resize vertically.
02:00Don't worry about being too exact here.
02:02However, it comes out will be fine and will be usable in your Flex application.
02:07Now, I'll export to the FXG format.
02:11I'll go to the Illustrator menu and select File > Save As.
02:14I'll set the File Type as FXG.
02:20Notice that Illustrator automatically gives the file an extension of .fxg.
02:25I'll add the letters AI to the end of the file name and I am also an upper case
02:30of least the first character of the file name here.
02:33That's because Flex is going to treat this as an ActionScript class and
02:37I'd like to follow ActionScript class naming conventions.
02:42Now, I'm going to save the file to my Desktop.
02:46I'll select my Desktop and then click Save.
02:51Illustrator presents a number of options in this dialog box.
02:54The Creative Suite 4 products export FXG 1.0.
02:59The Flex 4 SDK can also use FXG 2.0, which will be exported by the
03:04Creative Suite 5 products.
03:07I'll accept all of the default options and click OK, and that saves the file to the Desktop.
03:13Then I'll close the file in Illustrator selecting File > Close.
03:18The resulting file has been saved to my Desktop as HP_logo_ai.fxg.
03:22I'll come back to this file in a moment, but first I'll show you how to do
03:27exactly the same operation in Fireworks.
03:31I'll go to Fireworks and I'll open the file.
03:34I'll choose the File > Open.
03:36I'll once again navigate to the 11_ Skinning/begin folder and I'll select and HERE
03:42open the file hp_logo.AI.
03:46When you open an Illustrator file in Fireworks CS4, you are presented
03:49with these options.
03:51I'll click OK to accept all of the default options and that opens the file.
03:57Just as in Illustrator, the vector graphic has a large canvas.
04:02A canvas in Fireworks is similar to an artboard in Illustrator.
04:06And as in Illustrator, I'm going to resize the canvas to match the dimensions
04:11of the actual graphic.
04:13I'll go to the menu and choose Modify > Canvas > Fit Canvas, and that sizes the
04:20canvas to exactly match the dimensions of the graphic.
04:23Now here is how you export the fxg file from Fireworks.
04:27I'll go the menu and choose Commands > Export to fxg.
04:31I'll once again export to the desktop, and I'll click Select Desktop and
04:38then set the file name.
04:40Once again, I'll be using file extension of .fxg, and I'll use a name of
04:46HP_logo_fw.fxg, fw is for Fireworks, and I'll click OK.
04:56So, now I have two fxg files on my desktop, the original one from Adobe
05:01Illustrator and the new one from Fireworks.
05:05Notice that there is also a folder named HP_logo_fw.assets.
05:10This assets folder is designed to contain bitmap graphics that the vector
05:14graphic is depended on.
05:16In some cases you will see bitmap graphics created in that folder that implement
05:20various visual effects.
05:22In fireworks, even if no bitmap graphics are created, the assets folder is
05:26created anyway, but you will find that it's empty.
05:30Now, the next step is to copy the fxg file into your assets folder.
05:34I am going to click on the Illustrator version and on the Fireworks version
05:39holding down the Ctrl key on Windows or Command on Mac as I click and then I'll
05:43copy these to the clipboard.
05:46Then I'll go over to Flash Builder and I'll place these files into my assets
05:51folder, pasting the file into place.
05:55Then I'll open each of the two files.
05:58Here's the Illustrator version and here's the Fireworks version.
06:01I'll scroll down to the bottom of the Illustrator version and show you that it's
06:05about 260 lines long.
06:08When Illustrator exports an fxg file, it includes a lot of XML markup that's
06:13exclusively for the use of Illustrator itself.
06:16It's placed in an element named Private.
06:19You'll see that the tags within the Private section have a name space prefix of
06:22AI for Adobe Illustrator.
06:25That means that these tags are used by Illustrator and won't be used by Flex 4.
06:31The Fireworks version of the file much smaller.
06:34I'll go to that file and scroll down to the bottom and show you that it's only 184 lines.
06:40That's because Fireworks does not add private metadata to the file.
06:45All other things being equal, the file size of an fxg file will be significantly
06:50smaller than the Illustrator version.
06:52Now, in other tutorials of this chapter, I am going to show you ways
06:56of incorporating this fxg file as a primitive vector graphic in your Flex application.
07:02If you don't have access to the graphic applications, Illustrator and
07:06Fireworks, that's okay, because I've included a finished version of this file
07:11named H&P_logo.fxg.
07:14This version of the file was exported from Fireworks, so it represents the
07:18smallest possible file.
07:20Once you've learned how to export a vector graphic from one of the Creative
07:24Suite products, whether the format is FXG 10.0 exported from Creative Suite 4
07:30products, or FXG 2.0 exported from Creative Suite 5, you'll be able to use these
07:36fxg graphics in your Flex applications, treating them as bitmap graphics, as
07:41vector graphics, or as primitive ActionScript vector graphic classes, and I'll
07:47show you how to do that in other videos.
Collapse this transcript
Using FXG files in Flex applications
00:00Once you've exported vector graphics in the FXG format from Illustrator,
00:05Fireworks or Photoshop, you can incorporate them into your Flex applications as
00:10ActionScript classes.
00:12I'll show you how to do this in the application named AppWithLogo.MXML.
00:17In the application's beginning state, it uses a BitmapImage control to embed a
00:22PNG file named logo.png.
00:25I'll run the application in the browser and show you its current
00:29visual presentation.
00:32The PNG file is a bitmap graphic and it's been scaled, using the height and
00:37width properties of the BitmapGraphic class.
00:40Notice that the graphic pixelates showing stairstep artifacts.
00:45This is something that will happen whenever you scale a Bitmap Graphic such as a
00:49PNG, JPEG or GIF file.
00:53The advantage of using FXG graphics is that number one, they're very lightweight.
00:58They don't take up as much space on disk or in memory, and number two,
01:03they are highly scalable.
01:04If you shrink or grow them, they'll scale elegantly because vector graphics'
01:09sizing is calculated mathematically rather than as a series of bits.
01:15So I'll return to the code, and I am going to remove the BitmapImage control.
01:22I'll select it and just comment it out selecting, Source > Toggle Block Comment,
01:28then I'll place the cursor after that commented code.
01:31You can declare an instance of an FXG graphic using either MXML or ActionScript,
01:37but it's typically done within MXML and I'll show you that approach.
01:41The name of the file I'll be working with is HandPLogo.fxg.
01:46Just as within MXML or ActionScript file, I'm going to treat that as a class name,
01:51ignoring the .FXG extension and addressing the class just by the
01:55beginning of the file name.
01:57I'll type in a less than character and then the beginning of the file name, h-a-n-d.
02:04The file is displayed in the Code Assist list as an ActionScript class.
02:09I'll select it and Flash Builder auto completes the tag name.
02:14At the same time, it adds the required namespace prefix in the beginning tag
02:19of the application.
02:20The namespace prefix pattern matches exactly the same approach he used with MXML
02:25and ActionScript files.
02:27Flash Builder generates the custom XML namespace to match the folder or package name.
02:34Now I'll assign the Logo object, an explicit width and height, I'll set its
02:39width to 180 pixels and its height to 111.
02:44You can also look at the graphic in Design Mode and grab the handle and size it manually.
02:51Now I'll look at the application in Design Mode and you'll see that the scaled
02:54graphic is displayed very cleanly, nested within the border container.
03:01I'll save and run the application, and now the FXG file is incorporated into
03:06the Flex application.
03:08With both MXML graphics and FXG files, you can declare these in a Library at the
03:15top of an MXML document, and then refer to the graphic as a reusable class.
03:20So this is the simplest approach to using FXG files in your Flex applications.
03:25Treat them as ActionScript classes. Take a look at the documentation for more
03:30information about incorporating FXG graphics, including how to instantiate and
03:35add them to Flex 4 Containers, using pure ActionScript code.
Collapse this transcript
Creating a custom application skin
00:00The Flex 4 SDKs new Spark component architecture supports the creation and
00:06binding of custom skins to both Spark containers and controls.
00:11You define these skins using an MXML code as MXML components.
00:16The SkinClasses are sub-classed from a class called Skin, and you create them as
00:21their own separate MXML files and then bind them to component instances using
00:26the SkinClass style.
00:27I'll show you how to create a new custom skin for a Flex application using this
00:32application, AppWithCustomSkin.mxml.
00:35In the applications beginning state, it uses a backgroundColor attribute and
00:39then a combination of a BorderContainer, FXG file and a BitmapImage to create
00:44the appearance of the application.
00:46Here is the application in its beginning state.
00:50Now I'm going to replace all of this visual presentation with the custom skin
00:54and the only piece that I'm going to reuse is the FXP graphic. Everything else
00:58will be in the skin itself.
01:00I'll go back to the application and look at it in Design Mode.
01:04The easiest way to get started creating a custom skin is to create a copy of the
01:09default skin for the current component.
01:12You do this from Design Mode.
01:14Go to Design Mode and right-click anywhere on the surface of the application,
01:18then choose Create Skin.
01:21In the dialog that appears, first indicate where you want to create your new
01:25custom skin component.
01:27I typically created in a package named Skins, then assign your new component a name.
01:33I'd name it CustomAppSkin.
01:36This dialog box also asks you what the HostComponent is.
01:39That is, which component will use the new custom skin and it will default to the
01:43component that you right clicked on in Design Mode and it also asks you which
01:48existing skin you want to copy, if any. It defaults to the default skin for the
01:53component you right clicked on.
01:55In this case spark.skins.
01:57spark.ApplicationSkin.
02:00The final option allows you to remove ActionScript styling code.
02:03This is ActionScript code that's automatically added to some skins that allows
02:08you to pass certain styles in from the HostComponent and those styles are then
02:12used in the skin component.
02:14I'll keep the styling code in initially, so I can show you what it looks like,
02:18and I'll click Finish to create my new custom skin.
02:21The custom skin initially opens in Design Mode. I'll click Source Mode and
02:26review some of the code.
02:28The skin begins with an XML declaration at the top, and then the root element is
02:33declared as an instance of the SkinClass.
02:36After the Skin start tag, there is a metadata tag named HostComponent.
02:40The HostComponent metadata tag references the complete qualified name and
02:45location of the component that will use this skin.
02:48You declared the HostComponent attribute, so you can refer to the instance of
02:52the HostComponent using a variable named HostComponent with a lowercase h.
02:57The next section is a Script tag. This is the styling code that was referred
03:01to in the dialog box.
03:02The code for the application component skin allows you to pass in the background
03:07color style from the HostComponent. That style is then applied to a Rect class
03:12with an ID of bgRectFill.
03:14That vector graphic is declared down here.
03:17It defaults to a color of white but if you pass the background style in from
03:21the HostComponent using MXML code, that value is overwritten with this
03:26ActionScript code here.
03:28Now if you want to keep things simple in a custom skin, you can just delete the styling code.
03:33It will mean that you can no longer pass in the style from the HostComponent and
03:37then whatever style you set in the skin will always be the right color.
03:41So I'll select that Script section and delete it and then I'll go down to the
03:46backgroundRect setting and I'll set its color to the same color that I was
03:49passing in from the application, #1E5C75.
03:54I'll then go back to the application that's using the custom skin and look at
03:58it in Source Mode and I'll remove the backgroundColor from the application.
04:03Notice that the skinClass style has been set to the fully qualified name and
04:07location of the custom skin.
04:09I'll now save all my changes and run the application in the browser and
04:13you'll see that the background color is still there, this time implemented by the custom skin.
04:19Now I'll make some more changes.
04:21I'm going to eliminate the presentation of all these graphics in the application
04:24itself and I am simply going to put in a label with a text property of My Store.
04:30I'll set the fontSize to 18 pixels the fontWeight to bold and the fontStyle
04:37to italic.
04:41Now by default this label control will be placed in the top-left corner of the application.
04:47When I look at the application in Design Mode now here's the label.
04:51I'm going to use the custom skin both to add graphical elements to the
04:55presentation but also to add code that places that label in a particular
05:00location on the screen.
05:02I'll go back to Source Mode and then back to the skin itself.
05:05Now I'm going to add some vector graphics to the skin.
05:09I'd like to create a nested vector graphic that has BevelFiltered edges and
05:13rounded corners and is used as the nested area in which any content is
05:17presented in the application.
05:19After the rectangle that implements the background color, the next group expands
05:24to take up the entire space of the application because of its constraint
05:28properties of left, right, top and bottom being set to zero, and it has a
05:32VerticalLayout object as its layout property.
05:34The first item within this group is the application control bar represented by this group.
05:40Notice that this group has an includeIn attribute, which is set to only two view states.
05:45nrmalWithControlBar and disabledWithControlBar. If you look in the code
05:49for the application component, you'll see where that view state is set based on
05:54whether the application implements a control bar or not.
05:57I'm going to move down past that group because I want to keep the possibility of
06:01a control bar in the application and down here at line 103, I'll find a group
06:07with an ID of ContentGroup. This is a critical group element in the skin.
06:12It's called a required skin part.
06:15At runtime as the application starts up, it looks for this group, which is a
06:19part of the application skin, and any MXML objects that are declared in the
06:24application file are passed into this group container.
06:27So if you want to modify the layout and placement of objects that are declared
06:32in MXML from the application, you modify the properties of this group.
06:37I'm going to change it from a Group to VGroup. That will mean that objects are
06:42stacked vertically within the application.
06:45I can now add the horizontalAlign property and set it to a value of Center and
06:50the result will be that any objects declared in MXML in the application are now
06:55going to be aligned in the center of the VGroup.
06:58I'll save the changes of the skin and then come back to the application and look
07:02at it in Design Mode and now that label is centered on the screen.
07:06Notice that I didn't make any changes to the application itself to make that happen.
07:11Now I'm going to make the application a little more interesting to look at.
07:15I am going to wrap the VGroup, which is the contentGroup, inside another group.
07:20I'll give this group a height and width of 100%, so that it expands to fill the
07:25entire available space in the application, and I'll move the contentGroup inside that group.
07:33Then within the group, I'll add a vector graphic, a rectangle that will be laid
07:38behind the contentGroup.
07:39I will declare a rect object and I'll assign it to constraint properties of the
07:46top, bottom, right and left, setting them all to values of 10 pixels.
07:53I'll add a fill property to the rectangle and set it to an instance of the
07:57LinearGradient class.
08:00The LinearGradient will have an entries property with two colors.
08:05The first GradientEntry will have a color of #1E5C75, one of the standard colors I've
08:12been using in my application, and the second GradientEntry will have a color of 6DB6C6.
08:21I'm going to add some more effects to my rectangle.
08:23I am going to add a set of filters, a BevelFilter and a BlurFilter, and the
08:28result will be creating a nice border around this nested region.
08:33I'll create a Filters property and within the Filters property, I'll add an
08:37instance of the BlurFilter class and an instance of the BevelFilter.
08:42Each of these classes has specific properties that you can set to customize
08:46the appearance, but I'll use the BlurFilter and BevelFilter using their
08:49default properties.
08:51Now after that rectangle I am going to add the Logo again.
08:55The Logo was originally presented from the application file and now it's going to
08:59become a part of the skin.
09:01I'll create an instance of the HandPLogo FXP file.
09:06I'll give it an explicit width of 180 pixels and an explicit height of 111 and
09:12then I'll set both its left and top properties to 20 pixels each.
09:17Finally, I will set the VGroup's top property to a value of 100 and its gap to a value of 20.
09:24The top property will cause the first object in the VGroup or the contentGroup
09:29to appear 100 pixels below the top of its container, the group its nested in,
09:34and the gap will dictate how many pixels are between each object with in the contentGroup.
09:40My custom skin is now complete.
09:43I'll go back and review a couple of its elements.
09:45This group now wraps around this vector graphic, which creates the nested visual
09:50region, this FXP file, which is placed 20 pixels from the left and top of its
09:55container, and the contentGroup, which is where the MXML content declared in the
10:00application itself will be placed.
10:03I'll save the changes to the application skin, I'll come back to the application
10:07itself, save those changes and then I'll run the application, and here's my
10:11customized application with a custom skin.
10:14The label is placed 100 pixels from the top of its container because of the top
10:19property of the contentGroup.
10:22The FXG logo is placed 20 pixels from the left and top of its container and the
10:27BevelFilter and BlurFilter give this rectangle its unique look.
10:32Finally if you add control bar content to the application, the control bar will
10:36appear above the rectangle.
10:38This is now a completely separate skin component, which can be used in this
10:42application and many others.
10:44The idea of this separation of the skin from the implementation of an
10:49application or component is that it gives you the ability to easily incorporate
10:54vector graphics in the form of MXML graphics and FXG graphics and also to
11:00incorporate bitmap graphics if you need them.
11:02Any visual objects that you place in the application file in MXML will now be
11:07presented in the context of the custom skin and the custom skin can be reused on
11:12this and other applications.
Collapse this transcript
Using custom component skins
00:00Just as with Flex 4 application components you can also provide custom skins
00:05to any Flex-based component that's extended from the component named
00:10SkinnableComponent.
00:11These include buttons, checkboxes, panels, and all other sorts of containers and controls.
00:17In this exercise, I will show you how to create a custom skin for the button control.
00:21I will start by following the same workflow as I did with the application,
00:25creating a copy of the default skin for the Spark button control, and then I'll
00:30show you some ways of customizing it.
00:31I work with this application, UseCustomButtonSkin.mxml.
00:36It already uses a custom skin for the application itself and if you look at it
00:40in Design Mode you will see that it incorporates some vector graphics that are
00:44implemented with MXML and with FXG.
00:47There's also a button in the application with the label of Click me.
00:51I am going to right click on the button control and select Create Skin.
00:55Just as with the application skin, I am presented with this dialog box.
00:59I will set the Package to skins and then I'll give this file and name
01:03of CustomButtonSkin.
01:05The HostComponent is set to spark.components.Button.
01:09That's the fully qualified name and location of the button component in the Flex
01:13SDK and the skin is set to the default skin name.
01:16I will click Finish and that result in creating a file named
01:20CustomButtonSkin.mxml and if I go back to the application and look at it in
01:25Source Mode, I will see that the button component that I right clicked on now
01:29have to skin class set to the fully qualified name of my new custom skin.
01:35Now I'll go back to the new custom skin and look at it in Source Mode.
01:40Each time you declare a custom skin you will see this metadata tag, HostComponent,
01:45which references the fully qualified name of the component that uses the skin.
01:50The purpose of the metadata tag is to allow Flex to populate a variable named
01:55host component, with a reference to HostComponent that's using the skin.
01:59Scroll all the way down toward the bottom of the skin and you'll see a label
02:03control with an ID of label display.
02:06This label implements a skin part that's defined by the button control.
02:11At runtime the value that you pass into the Label property of the button is directly
02:15passed to this Label control because its ID matches the required ID of the
02:21labelDisplay's skin part.
02:23You can find information about the skin parts in the API documentation for the
02:28host component, in this case the Button control.
02:30Here is another interesting aspect of the skin file.
02:34You will find that each custom skin file declares a set of states. These are
02:38called skins States. If you look at the API documentation for the Button control
02:43you will see that these four states are defined as required.
02:47When the user interacts with the button by moving their cursor over the button
02:51clicking down, releasing, or when the button is disabled, Flex sets the current
02:56state of the Skin component automatically and you can write your vector graphic
03:01code to take advantage of this.
03:03So for example, if you look at the various vector graphics, you'll see in many
03:07cases that the colors are different for the different states.
03:11Here is a LinearGradient that has different colors for the default state and
03:15for the over and down states and here's a set of gradient entries where the
03:19alphas are different for the different states.
03:22And here's an example of a complete graphic that's either shown or hidden
03:26depending on the state.
03:27I will scroll over and show that it has an includedIn attribute of down.
03:31So that means that that graphic only appears when the user has their mouse
03:35button pressed down with the cursor over the component.
03:39Now to make this go a little bit faster, I've already created an alternative
03:43custom skin for the button control.
03:45I will open it now.
03:47It's in the Skins folder of the Project that's included with the Exercise Files
03:51and its name is EllipticalButtonSkin.
03:54Notice that this version of the skin has had all the comments removed, but it
03:58still has the HostComponent attribute and in this skin all of the rectangles
04:03have been replaced with Ellipses.
04:05The Ellipses implement the same set of colors as the original skin, but now the
04:10button will appear as an ellipse instead of a rectangle. I will go back to the
04:14application and I will modify the code as follows.
04:17I will go to the skinClass attribute that was added automatically when I
04:20generated the copy of the skin and I'll delete all of the code within the quotes
04:25and then press Ctrl+Space to bring up the Code Assist list.
04:28I will start typing e-l until I get to the EllipticalButtonSkin class and I will select it.
04:34Flash Builder automatically adds an import statement for the skinClass and also
04:39since the skinClass style using the fully qualified skinClass name.
04:44I will save and run the application and you'll see that the application's button
04:49is now presented not with the original rectangular button but with the newly
04:53skinned elliptical button.
04:55Once again as with the application you can incorporate either vector graphics
04:59declared in MXML, FXG graphics exported from Photoshop, Illustrator or
05:04Fireworks, or bitmap graphics that you embed into the application with the
05:09BitmapGraphic class.
05:11Regardless of how you style your components, the new Spark component skin
05:15architecture gives you the ability to separate the implementation of the
05:19component from its visual presentation.
Collapse this transcript
Assigning skins to multiple components
00:00When you create custom skins using the new Spark component architecture in Flex 4,
00:05you can assign the skins to components using the skinClass style.
00:09So far, I've shown you in other videos how to assign the skin to one instance
00:13of a component at a time, but using style declarations, you can assign skins
00:19more globally throughout the application.
00:21I'll show you how to do this in the application file MultipleButtons.mxml.
00:26I'll start in Design Mode showing you that only the first button in this
00:30application has a custom skin, the EllipticalButtonSkin, and if you wanted to
00:34assign the skin to all of the buttons, right now you'd have to assign it one at a time.
00:39But I'm going to instead use a cascading style sheet declaration to assign the
00:44skin to all buttons throughout the entire application.
00:47I'll place the cursor after the Declarations section of the application and I'll
00:52create a Style tag set.
00:54When I create the Style tag set, Flash Builder automatically adds the spark
00:58and the mx namespaces.
01:01Now I'm only going to assign the skin to the Spark versions of buttons.
01:05I'll type in the name of the component and then press Ctrl+Space and I'll make
01:11sure I choose the Button selector that starts with s rather than mx.
01:15Then I'll create a pair of braces.
01:19Now I'll assign the skin class style using the skin-class property name.
01:24I'll type in Skin and press Ctrl+Space and Flash Builder automatically completes the
01:29style name, using the hyphenated naming convention.
01:32Then I'll put in the colon.
01:34Next I'll declare a compiler directive named ClassReference.
01:39It looks like this.
01:40It looks like a function. You put in a pair of parentheses and then within
01:44the parentheses, you type in the fully qualified name of the skin class you want to use.
01:49I already have that skin class referenced in this code, in the Button control.
01:54So I'll select it and copy it to the clipboard, and then I'll paste it into
01:59place in the ClassReference compiler directive.
02:02Then I'll close the name of the class, close the directive and complete the
02:06style selector with a semicolon.
02:09This skin class style is now applied to all instances of the Button control
02:13throughout the application and you can use any other cascading style sheet
02:17selectors as you like, including comma delimited lists of type selectors,
02:22style name selectors, descendent selectors, or any other style syntax that
02:27you're familiar with.
02:28I now no longer need the skinClass attribute in that first Button.
02:32It's going to get its skin class from the CSS selector.
02:35I'll look at the application in Design Mode and show you that now all four
02:39Buttons in the application are using the same skin class.
02:43So the skin-class setting is nothing more than a cascading style sheet's style
02:48setting. You can set it as an in-line style as was done automatically when you
02:53generated a copy of the skin or if you want to apply the skin more globally,
02:57use the CSS selector, either in an embedded style sheet as I've done here or in an
03:02external style sheet that's linked into your application.
Collapse this transcript
Loading skins at runtime
00:00You apply custom skins to Spark- based components such as the application
00:04