YOUR FEEDBACK
José D'Andrade wrote: "...it may never be released..." Why? "...if Midori isn’t heir to Windows Mi...
AJAXWorld RIA Conference
$300 Savings Expire August 8
Register Today and SAVE!


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP COLDFUSION LINKS


Adobe Flex 2: Advanced DataGrid
Part 1: Destination-awareness formatters & renderers

More on Customizing the DataGridColumn
We'll put our DataGridColumn in the dataGridClasses subfolder as a sign of our respect for the well-thought-out directory structure of the Flex framework:

As mentioned above, the "dirty" job of locating and assigning the proper label function has been delegated to the helper class FormattingManager. This class, presented in Listing 4, should be put in our theriabook project.

Tada! And the winner is...the developer. Once we add theriabook.swc (with DataGrid, DataGridColumn, and FormattingManager) to the library path, the application code gets reduced to Listing 5.

Improving FormattingManager
In the previous examples we've used SwitchSymbolFormatter for both phone and ssn formatting. As soon as we start formatting numbers or currency values, it's natural to use NumberFormatter or CurrencyFormatter - descendants of mx.formatters.Formatter. In fact, Flex offers a dedicated formatter even for the phone formatting.

While SwitchSymbolFormatter derives from Object, all the rest of the formatters descend from Formatter. By encapsulating this specific of SwitchSymbolFormatter in the custom class MaskFormatter we'll help ourselves in basing the next version of FormattingManager entirely on Formatters as in Listing 6.

Look how this MaskFormatter simplifies our FormattingManager: we can replace all the private methods with an anonymous function, as shown in Listing 7. Please note that the reference to the appropriate formatter is preserved with the closure.

The testing application FormatStringDemo is in Listing 8. If you run it, the DataGrid dg will be formatted as shown in Figure 4:

Let's focus on the hard-coding that we allowed in case of the money value:

     case "money":
        formatter = new CurrencyFormatter();
        CurrencyFormatter(formatter).precision=2;
        break;

This hard-coding reflects, perhaps, the most "popular" case. But what if we want to have the full advantage of the properties of the corresponding formatter, such as precision, in case of CurrencyFormatter? To address these cases we're going to introduce one more fx:DataGridColumn property - formatData. Here's how it will be used in the application MXML:

    <fx:DataGridColumn dataField="SALARY" >
      <fx:formatData>
        <mx:Object formatString="money" precision="0"/>
      </fx:formatData>
    </fx:DataGridColumn>

The elegance of MXML lets us implement this extension with just a few lines of extra code in com.theriabook.controls.dataGridClasses.DataGridColumn:

    public function set formatData(fd :Object) : void{
      FormattingManager.setFormat(this, fd);
    }

Then, to accommodate the change on the FormattingManager side, we'll iterate through all the properties of the formatData object and attempt to assign them to the appropriate properties of the formatter with an emphasis on the word appropriate. The MXML compiler isn't going to help us check the properties of the unsealed <mx:Object> against the properties of the formatter. Accordingly, to protect ourselves from the no-such-property-exceptions, we surround property assignments with try/catch:

    public static function setFormat(
        dgc:mx.controls.dataGridClasses.DataGridColumn,         formatData:Object
    ):void {
      . . . . .
      if (!(formatData is String)) {
        for (var property:String in formatData) {
        try {
            formatter[property] = formatData[property];
          } catch (err:Error) {
            // Property does not match formatter type
          }
        }
      }
      . . . . .
    }

The complete listing of renewed FormattingManager is in Listing 9. While maintaining your own framework, you would transform this class to accommodate your requirements

Finally, Listing 10 has the sample application to test our changes, FormatDataDemo.

When you run the application it will produce the DataGrid shown in Figure 5.

We'll continue beefing up our custom DataGridColumn after a short detour into CheckBox and RadioButton controls.

CheckBox as a Drop-In Renderer
As we warned the reader at the beginning of this article, DataGrids rarely come alone. In this section we're going to suggest customizimg the CheckBox, which will help us illustrate custom DataGridColumns from a different perspective.

The state of a CheckBox control is managed by the Boolean property selected. At the same time, many business systems use either Y/N or, sometimes, 0/1 flags. As a result, translating business-specific values into selected and vice versa burdens the application code. Listing 11 presents the custom CheckBox, which supports application-specific on and off values along with the current value.

So, using this CheckBox, we could have written:

<fx:CheckBox value="Y" onValue="Y" offValue="N" />

to have selected CheckBox, or

<fx:CheckBox value="N" onValue="Y" offValue="N" />

to set selected to false.

DataGridColumn as ItemRenderer's Knowledge Base
Now let's get back to the DataGrid world. What if we wanted to use our CheckBox as the DataGrid item renderer? Here's the suggested use case example:

<fx:DataGridColumn dataField="BENE_DAY_CARE"
       itemRenderer="com.theriabook.controls.CheckBox" >
    </fx:DataGridColumn>

Obviously, we need to modify the CheckBox more to take care of the value within the data setter:

    override public function set data(item:Object):void
    {
      super.data = item;
      if( item!=null ) {
        value = item[DataGridListData(listData).dataField];
      }
    }

But how will we communicate the offValue and onValue properties to our CheckBox-turned-itemRenderer? Ideally, we would need something like:

    <fx:DataGridColumn dataField="BENE_DAY_CARE"
    itemRenderer="com.theriabook.controls.CheckBox" >
      <fx:extendedProperties>
        <mx:Object onValue="Y" offValue="N" />
      </fx:extendedProperties>
    </fx:DataGridColumn>


About Victor Rasputnis
Dr. Victor Rasputnis is a Managing Principal of Farata Systems. He's responsible for providing architectural design, implementation management and mentoring to companies migrating to XML Internet technologies. He holds a PhD in computer science from the Moscow Institute of Robotics. You can reach him at vrasputnis@faratasystems.com

About Yakov Fain
Yakov Fain is a managing principal of Farata Systems, consulting, training and product company. He has authored several Java books, dozens of technical articles. SYS-CON Books released his latest co-authored book , "Rich Internet Applications with Adobe Flex and Java: Secrets of the Masters" in Spring 2007. Sun Microsystems has nominated and awarded Yakov with the title Java Champion. He leads the Princeton Java Users Group. Yakov teaches Java and Flex 2 part time at New York University. He is an Adobe Certified Flex Instructor and an Editor-in-Chief of Flex Developers Journal.

About Anatole Tartakovsky
Anatole Tartakovsky is a Managing Principal of Farata Systems. He's responsible for creation of frameworks and reusable components. Anatole authored number of books and articles on AJAX, XML, Internet and client-server technologies. He holds an MS in mathematics. You can reach him at atartakovsky@faratasystems.com

CFDJ LATEST STORIES . . .
SQL Injection attacks are one of the easiest ways to hack into a website. One recent hack, using a script from verynx.cn, involves injecting sql into a web form that then appends some JavaScript code into fields in a database that then gets executed on the client side when a user views...
Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft. Mike is focused on the delivery of the Windows virtualization technology, including Windows Server 2008 Hyper-V, Microsoft Hyper-V Server and Virtual PC 2007. Mike also directs the tec...
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted to be...
Recursion Software released a private beta version of their Voyager mobile platform, with powerful interoperability for Android, Microsoft .NET and Compact Framework (CF), all Java editions (JME CDC, JSE and JEE), and more than 15 embedded operating systems. The Voyager platform is a p...
2008 is going to be an important year for Rich Internet Applications. Most organizations are delivering or planning to deliver Rich Internet Applications; however, at the same time, most IT managers are facing a dilemma: which Rich Internet Application technology and platform to use? T...
CFDynamics, a ColdFusion web host, has renewed an agreement with SmarterTools that will allow them to pass on immediate value to their customers. When a customers signs up for a dedicated hosting account they will now receive $750 worth of features including SmarterMail, SmarterStats a...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE