return undefined;


Open Web: Fifteen year old third grader?

Posted in Flash, Flex, pointless blather by Ben on the May 6th, 2008

I don’t really want to get fully involved in the discussion being had by Ryan Stewart, Asa Dotzler and Simeon Bateman. However, in reading through each argument a few things Asa said struck me as particularly indicative of the fact that he, and I suppose maybe Mozilla as a whole, simply don’t get it. I am admittedly cherry picking here, but I couldn’t resist pulling out a few quotes.

Open is developing the protocols and specifications in a co-operative and participatory environment and then competing on implementations.

Replace “Open is” with “The colossal headache that is developing consistent experiences that are cross browser/platform and engaging to users is thanks to” and you’ve got a true statement. I use Firefox as my primary browser and think overall its a great application but unfortunately, telling a client their new site looks great in Firefox and like garbage in IE won’t exactly result in a lucrative career. Developing for “competing implementations” sucks, and its a large part of why Flash Player is so widely embraced and successful.

If I was in Adobe’s shoes, I’d give everything away, all of it. Hell, I’d pay people to develop on the Adobe platform and I’d encourage dozens of competing implementations of my platform across every type of device imaginable because, in the end, it’d be my platform and I’d decide how and when it evolved and to what ends.

See above for my thoughts on competing implementations.

So, all I can do in this battle for the future of the Web is to advocate for advances in real open Web standards from groups like ECMA, W3C, and WHATWG. It may be a bit slower to market, (hopefully not too much slower,)…

A bit slower? Seriously? Just a bit? Far as I can tell HTML 4.01 was finalized in December of 1999. Nineteen ninety freakin’ nine. Best I can tell the latest Flash Player back then was version 4. So in the 9 YEARS we’ve been stuck with HTML 4 there have been 5 full version releases of Flash Player. If thats a bit then I am just a bit shy of being a billionaire. Yes, Firefox has had a few releases in that time but it wasn’t adding any new capabilities, just improving its implementation of the same old spec.

So be patient everyone, for by 2011 you will almost certainly have a finalized HTML 5 spec and, not only that, your choice of no less than 4 major implementations of it to choose from!

ArrayCollection for AS2

Posted in AS2, Flash by Ben on the June 8th, 2007

Did you know that AS2 includes rudimentary implementations of collections and iterators? I didn't until a few months back, but they do come in handy from time to time. An added bonus is that they make me less sad about developing in AS2 and missing out on all that AS3 goodness when the project requires FP8 compatibility.

Anyhow, the default implementation still leaves a bit to be desired, especially for those of us who spend the majority of our time coding AS3. The built-in class not only bears the unfortunate name CollectionImpl (which implements the Collection interface, both of which reside in the mx.utils package), but it also lacks some methods I've grown to appreciate in AS3's ArrayCollection class. Those methods are as follows:

  • length (getter)
  • addItemAt()
  • contains()
  • getItemIndex()
  • removeAll()
  • removeItemAt()

The following class provides those methods. I know, nothing fancy or impressive, but useful (in my opinion) nonetheless. CollectionImpl isn't all bad though, it does provide us with most of the basic collection-related functionality you would expect, including the removeItem() method that was left off of AS3's ArrayCollection class. As such, this class inherits from CollectionImpl and attempts to fill in the blanks.

import mx.utils.Collection;
import mx.utils.CollectionImpl;

class com.returnundefined.utils.ArrayCollection extends CollectionImpl implements Collection
{
    private var _items:Array;
   
    public function ArrayCollection(arr:Array)
    {
        super();
        _items = (arr == null) ? new Array() : arr;
    }
   
    public function get length():Number
    {
        return _items.length;
    }
   
    public function addItemAt(item:Object, index:Number):Boolean
    {
        var result:Boolean = false;
       
        if(item != null)
        {
            _items.splice(index, 0, item);
            result = true;
        }
       
        return result;
    }
   
    public function contains(item:Object):Boolean
    {
        return getItemIndex> -1;
    }
   
    public function getItemIndex(item:Object):Number
    {
        var index:Number = -1;
        var len:Number = length;
        for(var i:Number = 0; i <len; i++)
        {
            if (_items[i] == item)
            {
                index = i;
                break;
            }
        }
        return index;
    }
   
    public function removeAll():Void
    {
        _items = new Array();
    }
   
    public function removeItemAt(index:Number):Boolean
    {
        return removeItem(getItemAt(index));
    }
}

Feel free to alter the package path here and insert this class into your library wherever you want.

Flex, .NET and insight

Posted in Flash by Ben on the October 17th, 2006

I just discovered Graeme Harker's Flex.NET blog (via Jesse Warden) and I must say I am impressed. His article concerning marketing Flex to developers is dead on, and I really hope Adobe listens to some of his suggestions.

As someone who has successfully convinced their .NET development shop employer to adopt Flex, I can attest to the fact that Adobe's insistence on literally ignoring the .NET community was one of the hurdles I had to overcome. As a developer that learned Flex in the context of a .NET environment, I can tell you that the lack of tutorials using anything but FDS was a real disappointment and a significant hindrance. If I were a .NET developer investigating Flex, rather than a Flash developer doing so, I probably would have given up thinking that Flex is intended as a front-end for Java stacks. At the very least put some Web Service tutorials out there!

Anyhow, this post was intended to point you to Graeme Harker. He seems to know his stuff and I look forward to working my way through his past articles.

Conditional selections based on sibling values in E4X

Posted in AS3, Flash by Ben on the August 18th, 2006

This may be obvious to others but I found it a bit counter intuitive and thought I would post a note as much for my own reference as for others. Consider the following code:

var x:XML =
<Plans>
    <Plan>
        <Industry>MFG</Industry>
        <Contact>Bob Cratchett</Contact>
        <PlanNumber>11111</PlanNumber>
    </Plan>
    <Plan>
        <Industry>AEROSPACE</Industry>
        <Contact>Bo Peep</Contact>
        <PlanNumber>22222</PlanNumber>
    </Plan>
    <Plan>
        <Industry>ENTERTAINMENT</Industry>
        <Contact>Scrooge McDuck</Contact>
        <PlanNumber>33333</PlanNumber>
    </Plan>
</Plans>;

trace(x.Plan.(Industry == "AEROSPACE").PlanNumber);

This will return the correct value (22222), but I thought it was a bit odd that the dot that precedes the condition does not actually take you down into the node, allowing you to still directly access the property you're after. I think this is a very smart implementation because otherwise it would be very difficult and convoluted to select a value based on a sibling value. It would likely require something similar to, albeit more complicated than, the methodology recently discussed by Mike Chambers.

E4X definitely rocks the house.

AS3 support in FlashDevelop

Posted in AS3, Flash, FlashDevelop, Flex by Ben on the August 6th, 2006

Over the past week or so I have been fortunate enough to get to help out the guys from FlashDevelop, as they added support for AS3. This has been really fun so far. Even though I have not used FlashDevelop very much, my limited exposure to it along with ringing endorsements from some well known and respected Flash developers makes it obvious that it is a great editor with a very bright future. This is reinforced by the talented, enthusiastic and very responsive people behind the program, and my hat certainly goes off to them for implementing a feature requested by the community so quickly.

This work gave me a chance to finally try my hand at C# and to dig deeper into regex than I've dared before. Thanks to Expresso and some knowledgeable, helpful co-workers it was quite a positive experience. I am really looking forward to continuing to work on this.

Anyhow, rather than crud up my main page every time there is an update to announce, I have created a page devoted to my tiny piece of the pie. I would expect that any new developments (like Javadoc info) will be announced on the FD site, but the instrinsic class files will always reside in the same place. Unless they move. But they probably won't.

Now go try FlashDevelop! I know I'm gonna :)

Smart Home functionality in Flex Builder 2

Posted in Flash, Flex by Ben on the July 26th, 2006

One of the simplest yet most important features I require in a code editor is what is generally referred to as 'smart home' functionality. This is where pressing the home key will move the cursor to the first non-whitespace character on the line, respecting whatever indentation may exist. Well, apparently this is something that did not make the cut for the initial launch of Flex Builder, but it is quite easy (and free) to accomplish.

  1. Install the awesome FDT ActionScript plugin
  2. 'Activate' the plugin by opening a file with it:

That's it! You can now go back to using FB as your AS editor (the one with the black dot in the image above; it will still be set as the default editor for .as files), but now you have smart home functionality!

The FDT plugin does only provide a 30 day trial before you are required to purchase it, but this will work even after the trial expires. You are not actually using FDT for anything, but something happens when you install and 'activate' it that gives Flex Builder this functionality. Hopefully others will find this as useful as I did.

QuickDateFormatter - efficient date formatting in AS3

Posted in Flash, Flex by Ben on the July 21st, 2006

I've known that AS3 included some formatter classes, including DateFormatter, for some time now but didn't have cause to use them until today. I'm not sure if its laziness or that I just don't understand some level of abstraction Adobe was striving for with their implementation, but I found it maddeningly complicated to use. I was expecting something like php's date(), which usually looks something like this:

$formattedDate = date($myDateString, 'd/m/Y');

Well, you can imagine my horror (exaggerate much?) when I discovered that the same functionality in AS3 looked like this:

var df:DateFormatter = new DateFormatter();
df.formatString = "MM/DD/YYYY";
var formattedDate:String = df.format(myUnformattedString);

My solution? QuickDateFormatter, whose usage looks like this:

var formattedDate:String = QuickDateFormatter.format(myUnformattedString, "MM/DD/YYYY");

Much quicker and cleaner, specially made for us lazy folk. I suppose to match the robustness of the built-in DateFormatter I would need to add some error handling but I am not really worried about that. This is meant for cases where you know the input is a valid date but you need to alter the formatting. You can grab the source here or simply copy and paste from below.

package com.fmr.utils
{
    import mx.formatters.DateFormatter;

    public class QuickDateFormatter
    {
        public static function format(str_dateString:String, str_dateFormat:String):String
        {
            var f:DateFormatter = new DateFormatter();
            f.formatString = str_dateFormat;
            return f.format(str_dateString);
        }
    }
}

Enjoy!

DataGrid, labelFunction and namespaces

Posted in Flash, Flex by Ben on the July 18th, 2006

In a previous post I talked about my preferred method in Flex 2 for dealing with XML data that contains namespaces. Another task that gets complicated by the presence of 'namespaced XML' is populating a DataGrid with that data. Despite the usefulness of the use namespace directive, I have been unable to populate a DataGrid just by using the dataField attribute, as can be done when your data does not contain namespaces.

<mx:DataGrid x="10" y="291">
    <mx:columns>
        <mx:DataGridColumn headerText="Column 1" dataField="col1"/>
        <mx:DataGridColumn headerText="Column 2" dataField="col2"/>
    </mx:columns>
</mx:DataGrid>

The method I employ uses the labelFunction attribute of DataGridColumn, something that is most commonly used to let you specify a function to handle the formatting of data you would like displayed in that column. This approach allows you to use a single function for both special formatting of data and to gain access to data that resides in a namespace. Here is the basic format of my labelFunction:

private function genericLabelFunction(obj_data:Object, obj_dataGridColumn:DataGridColumn):String
{
    use namespace DOCUMENT_METADATA_NAMESPACE;
    var q:QName = new QName(DOCUMENT_METADATA_NAMESPACE, obj_dataGridColumn.dataField);
    var str_data:String = obj_data[q];
                   
    switch(obj_dataGridColumn.dataField)
    {
        case "Status":
            return (str_data == "InProgress") ? "In Progress" : str_data;
            break;
        case "PlanDataDate":
            return str_data.substr(0, str_data.indexOf(" "))
            break;
        default:
            return str_data;
            break;
    }
}

Another item that has proven useful for dealing with namespaced XML is the QName class. Short for 'qualified name', the QName constructor allows you to specify a namespace and a local name, which it uses to address the data you are targeting. Once we establish our QName object, we use it to reference the desired property of the data object (obj_data) that is passed into our labelFunction. Also notice that the local name is determined by the dataField attribute of the DataGridColumn object that was passed into the function. We now have a variable (str_data) that should contain the data from the node in our XML that has the same name as specified by our dataField attribute.

As shown in the first two cases, you can do further formatting to this data if desired. The default case takes care of all non-special cases, for when you would like to display the data as it exists in the XML and were simply using the labelFunction to get around namespace issues. Finally, here is a simple example of a DataGrid that uses our genericLabelFunction.

<mx:DataGrid dataProvider="{ModelLocator.getInstance().clientPlanList}" id="planList_dg">
    <mx:columns>
        <mx:DataGridColumn headerText="Industry" dataField="Industry" labelFunction="genericLabelFunction"/>
        <mx:DataGridColumn headerText="Plan Date" dataField="PlanDataDate" labelFunction="genericLabelFunction"/>
        <mx:DataGridColumn headerText="Status" dataField="Status" labelFunction="genericLabelFunction"/>
        <mx:DataGridColumn headerText="Group Size" dataField="PlanSizeGroup" labelFunction="genericLabelFunction"/>
    </mx:columns>
</mx:DataGrid>

Flash Extension: Convert Bitmaps to Symbols

Posted in Flash, JSFL by Ben on the July 17th, 2006

A while back I worked on a project that required the conversion of about 100 images to one-frame movieclip symbols. No way was I going to do it by hand, so a friend and I developed this nifty little extension to avoid doing the monotonous task ourselves. I have seen a few similarly capable extensions out there but I think ours offers a few features the others didn't.

From the source: Allows you to convert selected bitmaps from your library to a symbol type of your choice. Other features include: slicing leading/trailing characters of the symbol names, adding a prefix and/or suffix to the symbol names, adding the symbols to a folder, and setting the registration point of the symbols.

Anyhow, if you find it useful post a comment and let us know. Zip includes MXP file and source.

Enjoy!

Dealing with (default) namespaces in Flex 2/AS3

Posted in Flash, Flex by Ben on the July 14th, 2006

Since the release of Beta 2, I have been playing with Flex 2 and AS3 whenever I've had spare moments. Most of my tinkering has been focused around calling SOAP web services as that is what is most applicable to my current work situation. A problem I kept running into was that the data returned contains default namespaces (i.e. no prefix, just xmlns="yaddayadda"), and at present there is a dearth of information on working with them. Long story short, you must specify which namespace you want to use for each and every operation, unless you use this approach:

namespace foo = "http://site.com/foo";
use namespace foo;

I really didn't like the idea of having those directives scattered about in my app, in every file that needed to work with my XML data. Today, guided by an initial tip from Brian, I've got it working. This is more or less a direct implementation of the examples shown in the docs applied to XML, rather than methods.

What follows is my initial take on how to approach the use of several namespaces in a Flex 2 application that uses the Cairngorm framework. I am a Flex and Cairngorm noob, so if someone has suggestions on a better structure, I am all ears. The approach is based around creating package-level namespace files, that you can then import into wherever you need them.

Here is a sample namespace file:

package com.fmr.nstest.business.namespaces
{
    public namespace CLIENT_MEASURES_NAMESPACE = "http://site.com/BackOffice/CMeasures";
}

I decided the most logical place to put these files was in a 'namespaces' package inside the 'business' package, which is where my Delegates and Services.mxml file reside. I am defining them in all caps since they are more or less constants. You can only define one namespace per file, however, because there can only be one externally accessible definition per file in AS3. Using these files is pretty straightforward as well. Simply import the file (it will not show up in the auto-complete list in Flex Builder) and then insert your use namespace directive where appropriate:

import com.fmr.nstest.business.namespaces.CLIENT_MEASURES_NAMESPACE;
...
use namespace CLIENT_MEASURES_NAMESPACE;

This approach is flexible in that if you put the use namespace directive before your actual class declaration starts, it can essentially be used as the default namespace for all the methods of your class. On the other hand, you could also import everything (*) from your namespaces package and place different use namespace directives in your methods so that they can address different sets of data.