" /> Peter deHaan: June 2007 Archives

« August 2006 | Main | July 2007 »

June 28, 2007

Setting a Flash data grid's background color based on a row's data (ActionScript 3.0 edition)

Hot on the heels of the previous post... Here is another section of the third [and final] quick start on the Flash ActionScript 3.0 DataGrid component.

In this section we cover setting a row's background color/skin based on the row's data. And again I apologize for what will most certainly be hard to read formatting.

Setting background color based on a row's data
When customizing the layout of a data grid, you may not always want row colors to be alternated based on their row index. In some cases, you may want to set a row's background color based on the value of a field, or on a certain set of criteria.

The following example first creates a DataGrid component instance and alternates row colors based on the value of the value of the data provider's rowColor property.

Note: The alternating background skins are applied using the setStyle() method in the drawBackground() method in the CustomRowColors class below. For this example to work you need to create two new skins in your library and make sure they are exported for ActionScript: CellRenderer_upSkinGreen and CellRenderer_upSkinRed. For more information, see the previous example, Alternating background colors in a data grid.

Example


// Import the required component classes.
import fl.controls.DataGrid;
import fl.data.DataProvider;

// Create and populate a new DataProvider object.
var dp:DataProvider = new DataProvider();
dp.addItem({label:"Item a", data:0, rowColor:"green"});
dp.addItem({label:"Item b", data:1, rowColor:"green"});
dp.addItem({label:"Item c", data:2, rowColor:"green"});
dp.addItem({label:"Item d", data:3, rowColor:"green"});
dp.addItem({label:"Item e", data:4, rowColor:"red"});
dp.addItem({label:"Item f", data:5, rowColor:"red"});
dp.addItem({label:"Item g", data:6, rowColor:"green"});
dp.addItem({label:"Item h", data:7, rowColor:"red"});
dp.addItem({label:"Item i", data:8, rowColor:"green"});
dp.addItem({label:"Item j", data:9, rowColor:"green"});

/* Create a new DataGrid component instance, add two data grid columns,
define the data provider and add the instance to the display list. This
code also sets the cellRenderer style on the data grid by using the
setStyle() method and specifies the custom cell renderer class to use. */
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn("label");
myDataGrid.addColumn("data");
myDataGrid.dataProvider = dp;
myDataGrid.width = 200;
myDataGrid.rowCount = dp.length;
myDataGrid.move(10, 10);
myDataGrid.setStyle("cellRenderer", CustomRowColors);
addChild(myDataGrid);

The CustomRowColors class is as follows:


package {
// Import the required component classes.
import fl.controls.listClasses.CellRenderer;
import fl.controls.listClasses.ICellRenderer;

/**
* This class sets the upSkin style based on the current item's rowColor value
* in the data provider.
* Make sure the class is marked "public" and in the case of our custom cell renderer,
* extends the CellRenderer class and implements the ICellRenderer interface.
*/
public class CustomRowColors extends CellRenderer implements ICellRenderer {

/**
* Constructor.
*/
public function CustomRowColors():void {
super();
}

/**
* This method returns the style definition object from the CellRenderer class.
*/
public static function getStyleDefinition():Object {
return CellRenderer.getStyleDefinition();
}

/**
* This method overrides the inherited drawBackground() method and sets the renderer's
* upSkin style based on the row's rowColor value in the data provider. For example,
* if the item's rowColor value is "green", the upSkin style is set to the
* CellRenderer_upSkinGreen linkage in the library. If the rowColor value is "red", the
* upSkin style is set to the CellRenderer_upSkinRed linkage in the library.
*/
override protected function drawBackground():void {
switch (data.rowColor) {
case "green" :
setStyle("upSkin", CellRenderer_upSkinGreen);
break;
case "red" :
setStyle("upSkin", CellRenderer_upSkinRed);
break;
default :
break;
}
super.drawBackground();
}
}
}

The drawBackground() method above checks the value of the current row's rowColor property and depending on the value, selects either the CellRenderer_upSkinGreen or CellRenderer_upSkinRed asset from the library.

If you wanted to select a skin style based on the value of the row's data property, you could use code similar to the following:


override protected function drawBackground():void {
/* If the data property in the data provider is less than 4, set the upSkin style is
set to the CellRenderer_upSkinRed linkage in the library. */
if (data.data < 4) {
setStyle("upSkin", CellRenderer_upSkinRed);
}
super.drawBackground();
}

If the value of the row's data property is less than 4, the row's upSkin will be set to CellRenderer_upSkinRed, otherwise the default skin will be used.

Alternating background colors in a Flash data grid (ActionScript 3.0 edition)

Somebody was asking me today how to alternate background colors for the DataGrid component in Flash CS3 today (using the new ActionScript 3.0 components). Well, as luck has it, I wrote a section on it in an upcoming Flash Quick Start for the DataGrid component (my third and final installment on DataGrid).

The wording/code may change, but here's the latest draft of the "Alternating background colors in a data grid" section. Oh, and don't worry, the formatting will be *much* better once the Developer Center people clean it up.

Alternating background colors in a data grid
Often when displaying large amounts of data in a data grid, it is easier to see the rows when they have alternating row colors. In order for the following example to work, you need to create two new symbols in the Flash document's library. The easiest way to do this is to duplicate an existing cell renderer skin in the library (such as CellRenderer_upSkin in the Library panel's Component Assets/CellRendererSkins folder, see figure 1) and modify the asset as desired. Once you've finished creating the desired skins for the alternating row color backgrounds, you'll need to set up a linkage and give the symbol a class name of "CellRenderer_upSkinGray" and "CellRenderer_upskinDarkGray".

figure1.jpg
Figure 1. The CellRenderer_upSkin symbol in the Flash document's Library panel.

Once both the CellRenderer_upSkinDarkGray and CellRenderer_upSkinGray assets have been created, right-click the CellRenderer_upSkinDarkGray asset in the Library panel and select Linkage to open the Linkage Properties dialog box. From this dialog box, click the Export for ActionScript check box, assign a class name of CellRenderer_upSkinDarkGray and leave the base class text field as flash.display.MovieClip. Click OK. Repeat these steps for the CellRenderer_upSkinGray asset, giving it a class name of CellRenderer_upSkinGray (see figure 2).

figure2.jpg
Figure 2. Setting the class name for the CellRenderer_upSkinGray asset in the library.

Example
The following example creates ten items in a data grid and alternates the data grid row's background colors based on the row's current index:

// Import the required component classes.
import fl.controls.DataGrid;
import fl.data.DataProvider;

// Create and populate a new DataProvider object.
var dp:DataProvider = new DataProvider();
dp.addItem({label:"Item a", data:0});
dp.addItem({label:"Item b", data:1});
dp.addItem({label:"Item c", data:2});
dp.addItem({label:"Item d", data:3});
dp.addItem({label:"Item e", data:4});
dp.addItem({label:"Item f", data:5});
dp.addItem({label:"Item g", data:6});
dp.addItem({label:"Item h", data:7});
dp.addItem({label:"Item i", data:8});
dp.addItem({label:"Item j", data:9});

/* Create a new DataGrid component instance, add two data grid columns,
define the data provider and add the instance to the display list. This
code also sets the cellRenderer style on the data grid by using the
setStyle() method and specifies the custom cell renderer class to use. */
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn("label");
myDataGrid.addColumn("data");
myDataGrid.dataProvider = dp;
myDataGrid.width = 200;
myDataGrid.rowCount = dp.length;
myDataGrid.move(10, 10);
myDataGrid.setStyle("cellRenderer", AlternatingRowColors);
addChild(myDataGrid);

The AlternatingRowColors class is as follows:

package {
    // Import the required component classes.
    import fl.controls.listClasses.CellRenderer;
    import fl.controls.listClasses.ICellRenderer;

/**
* This class sets the upSkin style based on the current item's index in a list.
* Make sure the class is marked "public" and in the case of our custom cell renderer,
* extends the CellRenderer class and implements the ICellRenderer interface.
*/
public class AlternatingRowColors extends CellRenderer implements ICellRenderer {

/**
* Constructor.
*/
public function AlternatingRowColors():void {
super();
}

/**
* This method returns the style definition object from the CellRenderer class.
*/
public static function getStyleDefinition():Object {
return CellRenderer.getStyleDefinition();
}

/**
* This method overrides the inherited drawBackground() method and sets the renderer's
* upSkin style based on the row's current index. For example, if the row index is an
* odd number, the upSkin style is set to the CellRenderer_upSkinDarkGray linkage in the
* library. If the row index is an even number, the upSkin style is set to the
* CellRenderer_upSkinGray linkage in the library.
*/
override protected function drawBackground():void {
if (_listData.index % 2 == 0) {
setStyle("upSkin", CellRenderer_upSkinGray);
} else {
setStyle("upSkin", CellRenderer_upSkinDarkGray);
}
super.drawBackground();
}
}
}

Save the previous code in the same directory as your Flash document and give it a filename of AlternatingRowColors.as.

The custom AlternatingRowColors class extends the CellRenderer class and implements the ICellRenderer interface. This class overrides the drawBackground() method from the CellRenderer class and determines which upSkin to use for the current data grid row's background based on the current row's index. If the row index is an even number, the data grid sets the upSkin style to CellRender_upSkinGray, otherwise the CellRenderer_upSkinDarkGray is used.

Note: The alternating background skins are applied using the setStyle() method in the drawBackground() method in the custom cell renderer above. For this example to work you need to create two new skins in your library and make sure they are exported for ActionScript: CellRenderer_upSkinGray and CellRenderer_upSkinDarkGray.

June 27, 2007

Adobe Products in the News (Jun 26, 2007)

Recent reviews on Acrobat, AIR, Creative Suite, Digital Edition, Flash and Premiere Express...

Adobe MAX 2007 Flex Sessions

For those of you attending, or considering, the Adobe MAX 2007 conference in Chicago (Sept. 30 - Oct. 3), the sessions have been posted. I was browsing through the MAX 2007 mini-site and was reading through several of the sessions and descriptions.

There are 64 sessions (!!) involving Flex, and 51 of those sessions discuss multiple different products (such as Adobe AIR, Flash, ColdFusion, etc.). Here's a quick breakdown of the sessions by skill:

  • General Audience: 17 sessions
  • Beginner: 15 sessions
  • Intermediate: 23 sessions
  • Advanced: 9 sessions

For more information, check out the Adobe MAX 2007 mini-site. If you arent able to make it out to Chicago, there will also be MAX Europe in Barcelona, Spain (Oct. 15 - 18), and MAX Japan in Tokyo, Japan (Nov. 1 - 2).

More information on the Flex topics at MAX North America 2007 after the jump.

  1. A Designer's Introduction to Flex [Advanced]
    Products: Flex
  2. Web Application Development -- A Virtual Trading Floor: Bringing Wall Street to the Classroom [General Audience]
    Products: ColdFusion, Flex
  3. Rich Internet Applications -- Adobe Flex Toolkit for Apex: Building Rich Internet Applications for Businesses [General Audience]
    Products: Flex, Adobe Integrated Runtime, Flash Player
  4. Rich Internet Applications -- Adobe Integrated Runtime (AIR) Tips and Tricks [Advanced]
    Products: Adobe Integrated Runtime, Flex
  5. Rich Internet Applications -- Adobe Platform Roadmap [General Audience]
    Products: Flash Player, Adobe Integrated Runtime, Flex
  6. Web Application Development -- An Introduction to ColdFusion Powered Flex [General Audience]
    Products: ColdFusion, Flex
  7. Enterprise and Collaboration -- Architecting Solutions Using LiveCycle and Flex [Advanced]
    Products: LiveCycle, Flex
  8. Rich Internet Applications -- Best Practices for Building Quality Flex Applications for the Browser and for Adobe Integrated Runtime (AIR) [Intermediate]
    Products: Flex, Adobe Integrated Runtime, Flash Player
  9. Rich Internet Applications -- Beyond the Basics of Flex Builder [Intermediate]
    Products: Flex, Adobe Integrated Runtime
  10. Rich Internet Applications -- Branded Experiences with Adobe Integrated Runtime (AIR) [General Audience]
    Products: Adobe Integrated Runtime, Flex
  11. Rich Internet Applications -- Building Accessible Applications with Flex [Intermediate]
    Products: Flex
  12. Rich Internet Applications -- Building Global Flex Applications [Intermediate]
    Products: Flex
  13. Rich Internet Applications -- Building Stunning Flex Applications with Flex Builder and Creative Suite 3 [Intermediate]
    Products: Flex, Creative Suite
  14. Rich Internet Applications -- Case Study: ESRI Geospatial Application Using Adobe Flex [General Audience]
    Products: Flex
  15. Rich Internet Applications -- Case Study: RIAs and E-commerce [General Audience]
    Products: Flex
  16. Rich Internet Applications -- Case Study: Yahoo! Web Messenger [General Audience]
    Products: Flex, Adobe Integrated Runtime
  17. Rich Internet Applications -- Continuous Integration with Flex, FlexUnit, and Ant [Intermediate]
    Products: Flex
  18. Rich Internet Applications -- Creating New Flex Components [Intermediate]
    Products: Flex
  19. Rich Internet Applications -- Customizing the Flex Framework [Advanced]
    Products: Flex
  20. Rich Internet Applications -- Data Visualization with Flex [Intermediate]
    Products: Flex
  21. Web Application Development -- Design Patterns in ActionScript 3.0 [Advanced]
    Products: ActionScript, Flash Player, Flex, Flash
  22. Rich Internet Applications -- Design-Led Innovation: Creating Disruptive Experiences [Beginner]
    Products: Flex, LiveCycle, Adobe Integrated Runtime
  23. Rich Internet Applications -- Enterprise RIAs the Adobe Consulting Way [Intermediate]
    Products: Flex, LiveCycle, Adobe Integrated Runtime
  24. Rich Internet Applications -- Flash Player Cross-Domain Security [Advanced]
    Products: Flash Player, Flex
  25. Web, Video and Interactive Design -- Flash Player Detection and Embedding: An Open Source Solution [Intermediate]
    Products: Flash, Flash Player, Flex
  26. Rich Internet Applications -- Flex 3: Developing Rich Client Applications [Beginner]
    Products: Flex, Adobe Integrated Runtime, Flash Player, Flash
  27. Rich Internet Applications -- Flex 3: Integrating with ColdFusion [Beginner]
    Products: Flex, ColdFusion, Adobe Integrated Runtime, Flash Player
  28. Rich Internet Applications -- Flex 3: Integration with Java [Beginner]
    Products: Flex, Adobe Integrated Runtime, Flash Player
  29. Rich Internet Applications -- Flex Best Practices [Intermediate]
    Products: Flex, Adobe Integrated Runtime, Flash Player
  30. Rich Internet Applications -- Flex Boot Camp [General Audience]
    Products: Flex, Adobe Integrated Runtime, Flash Player, Flash
  31. Rich Internet Applications -- Flex on Rails [Intermediate]
    Products: Flex, Adobe Integrated Runtime, Flash Player
  32. Rich Internet Applications -- Flex Roadmap [General Audience]
    Products: Flex, Adobe Integrated Runtime
  33. Rich Internet Applications -- Hands On: Building a Rich Internet Application with Flex 3 and .NET [Beginner]
    Products: Flex, Adobe Integrated Runtime, Flash Player
  34. Rich Internet Applications -- Hands On: Building a Rich Internet Application with Flex 3 and Java [Intermediate]
    Products: Flex, LiveCycle
  35. Rich Internet Applications -- Hands On: Building a Rich Internet Application with Flex 3 and PHP [Beginner]
    Products: Flex, Adobe Integrated Runtime, Flash Player
  36. Enterprise and Collaboration -- Hands On: Building Custom Applications with LiveCycle Workspace [Advanced]
    Products: LiveCycle, Flex
  37. Rich Internet Applications -- Hands On: Building your First Rich Internet Application With Flex 3 [Beginner]
    Products: Flex, Adobe Integrated Runtime, Flash Player
  38. Rich Internet Applications -- Hands On: Deploying Flex and HTML/JavaScript Applications to Adobe Integrated Runtime (AIR) [Beginner]
    Products: Adobe Integrated Runtime, Flex
  39. Web, Video and Interactive Design -- Hands On: Introduction to ActionScript 3.0 [Beginner]
    Products: Flash, Flex, ActionScript, Flash Player, Adobe Integrated Runtime
  40. Rich Internet Applications -- HTML and Script Bridging for Flex Applications in Adobe Integrated Runtime (AIR) [Intermediate]
    Products: Adobe Integrated Runtime, Flex
  41. Web Application Development -- If You Build It Will They Come? Designing Your Web Application for Smooth Web Analytics [Advanced]
    Products: Dreamweaver, Flash, Flex
  42. Rich Internet Applications -- Integrating Flex and Video [Intermediate]
    Products: Flex, Flash, Flash Media Server, Adobe Integrated Runtime
  43. Rich Internet Applications -- Integrating Flex with Ajax and the Browser [Intermediate]
    Products: Flex
  44. Rich Internet Applications -- Introduction to Adobe Integrated Runtime (AIR) [Beginner]
    Products: Adobe Integrated Runtime, Flex
  45. Rich Internet Applications -- Introduction to Flex [Beginner]
    Products: Flex, Adobe Integrated Runtime, Flash Player
  46. Rich Internet Applications -- Introduction to Flex Builder [Beginner]
    Products: Flex, Adobe Integrated Runtime, Flash Player
  47. Rich Internet Applications -- Introduction to LiveCycle Data Services for Flex Developers [Beginner]
    Products: LiveCycle, Flex
  48. Rich Internet Applications -- Local Database Access with Adobe Integrated Runtime (AIR) and Data Synchronization Strategies [Intermediate]
    Products: Adobe Integrated Runtime, LiveCycle, Flex
  49. Design Across Media -- MAX Community Project Deconstructed [General Audience]
    Products: Creative Suite, Adobe Integrated Runtime, Flex, ActionScript, Flash Player
  50. Rich Internet Applications -- Next Generation User Experiences: Real Case Studies [General Audience]
    Products: Flex, Adobe Integrated Runtime
  51. Rich Internet Applications -- Open Source Flex [Beginner]
    Products: Flex
  52. Web Application Development -- Optimizing ActionScript 3.0 Performance [Intermediate]
    Products: ActionScript, Flash Player, Flex, Flash
  53. Rich Internet Applications -- Optimizing Flex Applications [Advanced]
    Products: Flex, Adobe Integrated Runtime, Flash Player
  54. Rich Internet Applications -- Optimizing Search for Rich Internet Applications [Advanced]
    Products: Dreamweaver, Flash, Flex
  55. Rich Internet Applications -- Project San Dimas: Under the Hood of eBay's Adobe Integrated Runtime (AIR) project [General Audience]
    Products: Adobe Integrated Runtime, Flex
  56. Rich Internet Applications -- Taking a Flex Application from the Web to the Desktop with Adobe Integrated Runtime (AIR) [Intermediate]
    Products: Flex, Adobe Integrated Runtime
  57. Design Across Media -- The New Creatives [General Audience]
    Products: Creative Suite, Adobe Integrated Runtime, Flex, ActionScript, Flash Player
  58. Rich Internet Applications -- Top Adobe Integrated Runtime (AIR) Applications: Visualizing the Possibilities of a New Platform [General Audience]
    Products: Adobe Integrated Runtime, Flex
  59. Rich Internet Applications -- Using Adobe Flex to Develop a Rich Financial Services Portal [General Audience]
    \Products: Flex
  60. Rich Internet Applications -- Using Flash and Flex Together [Intermediate]
    Products: Flex, Flash
  61. Rich Internet Applications -- Working with Data in Flex 3 [Intermediate]
    Products: Flex, Adobe Integrated Runtime
  62. Rich Internet Applications -- Working with Persistent Data in Adobe Integrated Runtime (AIR) [Intermediate]
    Products: Adobe Integrated Runtime, Flex, LiveCycle
  63. Rich Internet Applications -- XD: Bringing the Desktop to the Web [Intermediate]
    Products: Flex
  64. Web, Video and Interactive Design -- XD: From Zero to Bike Race in Six Weeks: The Amgen Tour of California [General Audience]
    Products: Flash Media Server, Flex

June 25, 2007

New content on the Flash Developer Center

After a "brief" absense from my blog, we're back with a brief update...

Today we posted new items of interest (video, Quick Start tutorial, and article) on the Flash Developer Center:

Video interview: Flex Component Kit for Flash CS3
Flex SDK engineer Glenn Ruehle explains how the Flex Component Kit improves the workflow between Flex and Flash developers and designers.
(Note: There is also an excellent video by Steve Heintz, Sr. Product Manager for Adobe Flex Builder on the adjoining tab.)

Using the Button component in Flash CS3 Professional
Peter deHaan writes about some of the common ways to use the Button component in your Flash projects, including setting labels, resizing and positioning buttons, embedding fonts, and creating toggles.

Setting up a Flash project for local and network playback
Dan Carr shows you how to create Flash content for local and web delivery by understanding the various playback features and deployment options.

I also wanted to highlight an excellent article that I overlooked the other week:

Formatting text for localized Flash projects using CSS, HTML, and ActionScript by Dan Carr
Learn how to create Flash applications that display multiple languages using ActionScript and text formatting techniques.

I know this topic comes up quite a bit in forums, mailing lists, LiveDocs, etc. so it's nice to have a bit of additional coverage. Thanks Dan!

Anyways, expect a few more posts around this dusty blog in the future. I missed you guys.

As always, you can find a lot more information and tutorials at the following URLs:
Developer Center
Flash Developer Center
Flash Developer Center Quick Starts