Archives for September 2006

UML, Stub Code and ASDocs

I have recently started using Enterprise Architect (EA) for creating UML diagrams for Actionscript and Flex projects. This is an outstanding product that is reducing my development time and helping me to create self documenting code following AsDoc formatting rules. If you have never created a UML diagram or used UML software, the concept can seem a little overwhelming but let me assure you, your time spent learning how to do so will be time very well spent.

In it's simplest form UML software allows a person to visually layout and architect a project before ever doing any actual coding. You can create your classes, document the properties and functions you intend to use, organize them and relate them (for either implementing or extending). At this point you are probably wondering why you would want to do that when you could just fire up your IDE of choice and start coding. Before I started using Enterprise Architect I only had one really good reason, it makes you think through the solution and plan before you start coding, so you can attack the project with a well defined plan. Now I have another reason, that I think is just as important as the first, EA can generate all your UML'd classes in either AS2 or AS3 in ASDoc friendly format. All your stub code is generated in seconds, perfectly formatted and compilable. This is an absolutely huge time and effort saver. By using UML you can create a well structured plan for your project and using the code generation in EA allows you to export all that planning effort into perfectly formatted Actionscript. I have created a sample project to demonstrate just how nicely this solution works and hopefully show why you may want to consider this approach for your projects. I will walk you through how to create a simple UML diagram in EA, how to export the diagram into AS3 files, and how to run the AS3 code through ASDoc to create project documentation.

You can download all my source files for this sample here.

Step 1 - Configure EA for Actionscript projects

Download the trial version of Enterprise Architect. Once you have it installed a few adustments need to be made. First, select the 'Tools/Options' menu and click the 'Source Code Engineering' node, set the 'Default Language for Code Generation' to 'Actionscript'. Click on the 'Actionscript' node and set the 'Default Version' to whichever AS version you are working in. Second, the templates that generate the AS code need some tweaks (just a few minor things that EA appears to have missed). You have two options, import the template updates I created or do it manually. To import my adjustements, download this file, unzip it, in EA, open the 'Tools' menu and select the 'Import Reference Data' item. Browse to the 'actionscript_code_templates.xml' file, select and open it, select the 'Actionscript_Code_Template' and click 'Import'. To do it manualy open the 'Settings' menu and select the 'Code Generation Templates' item and then make these two changes.

  • Select the 'Class' template, change the entire section to
    %ImportSection%\n
    %ClassNotes%
    %ClassDeclaration%
    %ClassBody%
    This puts the import statements after the package declaration but before the class declaration in the as files.
  • Select the 'Operation Declaration' template, find this block of code (near the top)
    %PI=" "%
    %opStatic == "T" ? "static"%
    %if genOptActionscriptVersion=="3.0" and opTag:"namespace"!=""%
    %opTag:"namespace"%
    %else%
    %CONVERT_SCOPE(opScope)%
    %endIf%
    
    and change it to this
    %PI=" "%
    %opStatic == "T" ? "static"%
    %if genOptActionscriptVersion=="3.0" and opTag:"namespace"!=""%
    %opTag:"namespace"%
    %elseIf elemType=="Interface"%
    %else%
    %CONVERT_SCOPE(opScope)%
    %endIf%
    
    This ensures that functions in Interfaces can not be declared Public, Private etc, which will throw a compiler error.

Step 2 - Create a UML diagram

Open EA and create a new project. In the project browser you can setup your package structure. For this sample I will keep it really simple and add a few packages 'com.dgrigg.vo', 'com.dgrigg.view'. The next step is creating the diagram. You can drag Class and Interface (to name a few) elements onto the diagram to begin creating your class structure. As you add new elements, you are prompted to enter the class properties. Enter information in the 'Notes' field to explain what the class is for. A few things to note when you are creating elements.

  • As you create Classes, Interfaces etc. and add Operations and Attributes (functions and properties) to each, be sure to enter a description in the 'Notes' field, this will get exported in your code and also be used by ASDocs.
  • When you create a new class you must create a constructor for it in the Operators list, for some reason EA does not do this for Actionscript projects.
  • If you add an Operator that has return value, be sure to enter a return line in the 'Initial Code' field on the 'Behavior' tab. This will set a 'default' return value in the code and allow ASDoc to properly execute and also not throw any compiler errors.
  • If the element you are working with needs to import '.as' files, right click on the element in the 'Project Browser', select 'Generate Code' and enter your import statements in the second 'Import(s)/Header(s)' field, click the 'Save' button.

When you are all done your diagram it will look something like this.

Step 3 - Generate the AS file

In the Project Browser right click on the 'com' folder, or whatever you root package folder is, and select 'Code Engineering/Generate Source Code'. Set the path you want to export to, check the 'Include all Child Packages' options, and click the 'Generate' button. Below is a sample of a generated AS file, straight from EA with no editing.

///////////////////////////////////////////////////////////
//  ProductList.as
//  Macromedia ActionScript Implementation of the Class ProductList
//  Generated by Enterprise Architect
//  Created on:      14-Sep-2006 5:08:46 PM
//  Original author: Derrick Grigg
///////////////////////////////////////////////////////////

package com.dgrigg.view
{
	import com.dgrigg.vo.ProductVO;
	import flash.text.TextField;
	import mx.controls.DataGrid;
	import flash.events.Event;
	import com.dgrigg.vo.ProductVO;
	import com.dgrigg.view.IView;

	/**
	 * Displays a DataGrid of products, lets the user select and view a product.
	 * 
	 * @see com.dgrigg.vo.ProductVO
	 * @author Derrick Grigg
	 * @version 1.0
	 * @updated 14-Sep-2006 7:38:22 PM
	 */
	public class ProductList implements IView
	{
	    /**
	     * displays the list of products
	     */
	    private var products_dg: DataGrid;
	    /**
	     * display the selected product's name
	     */
	    private var name_txt: TextField;
	    /**
	     * display the selected products' description
	     */
	    private var description_txt: TextField;
	    /**
	     * display the selected product's sku
	     */
	    private var sku_txt: TextField;

	    /**
	     * Constructor
	     */
	    public function ProductList()
	    {
	    }

	    /**
	     * add a product to the displayed product list
	     * 
	     * @param product    product to add to the product list
	     */
	    public function addProduct(product:ProductVO): Boolean
	    {
	    	return true;
	    }

	    public function getSelectedProduct(): ProductVO
	    {
	    	return new ProductVO();
	    }

	    /**
	     * handle the event that is triggered when a user selected a row in the data grid
	     * 
	     * @param event    event dispatched from DataGrid
	     */
	    public function handleSelectedProduct(event:Event): void
	    {
	    }

	}//end ProductList

}

Step 4 - Generate the ASDoc files

Download and install the ASDoc tool from Adobe. I have created a little bat file I use with ASDoc just to make is easier to recreate the files whenever I need to without having to remember my settings, you can download it here. Run ASDoc to create your files. Click here to see the final ASDocs files.

Conclusion

If you develop Actionscript projects of any size and/or complexity, and you need some documentation done along the way, the steps I have outlined above are any excellent way to quickly generate both the stub code you need to begin your development and some project documentation, which as we all know is a hassle to create but invaluable to have. Enjoy

Free Flash online whiteboard

Last night I was searching for an online whiteboard application, preferably free, to use for a quick meeting. A quick Google search returned exactly what I was after. GE recently launched a free, Flash based, online whiteboard called Imagination Cubed. Exactly what I needed. I remembered a few years ago that GE had something similiar to this with just a pen and some different tips you could draw with. It looks like they went back to the 'drawing board' and came up with something much better.

Flash based whiteboard

For a free whiteboard application you really can not go wrong with GE's whiteboard. You can draw basic shapes, add text, save it, print it and best of all collaborate on it with up to two other users. It has a chat window built in so you can chat with your collaborators while you work away. My only complaint is that there is no eraser function (but hey it's free). This is an excellent example of the types of internet based applications that can be 'easily' (I use the term loosely) built using Flash versus other technologies.

XIFF Actionscript 3 for Flex 2

I recently started a new project which I will be writing more about in the upcoming months. One of the initial issues that popped up was the lack of an Actionscript 3 version of the XIFF library for accessing an XMPP server. Task one, work with the development leader, Nick Veloff (check out his portfolio, he's done some amazing work), to port the Actionscript 2 version of XIFF to Actionscript 3. Following is Nick's post on the new version.


Spice up your Flex Builder 2 projects with XMPP, using an newly ported ActionScript 3 version of XIFF

If you would like to skip to the Flex demo of the messenger just go here.

A few months ago I started working on a complex RIA that required an instant messenger type component. We decided to use Flex Builder 2 and ActionScript 3 to developer the application. I quickly realized that with ActionScript 3 being so new (in terms of official release) that there was no real standardized way to add the component. So I started exploring different options, weighing the benefits and drawbacks of various technologies. Licensing cost, scalability, reusability, maintenance and standardization were among the main criteria I used to come to an effective solution.

Where I landed was a bit of a hybrid. I decided to utilize Jabber for the textual component of the software.

Jabber is best known as "the Linux of instant messaging" ? an open, secure, ad-free alternative to consumer IM services like AIM, ICQ, MSN, and Yahoo? from Jabber

Combine this with Flash Media Server for the video chat and you have a solid messaging platform. (FMS integration is out of the scope of this blog but I am sure I will be documenting my experience with it at a later date.)

In order to satisfy the business needs of this project I needed to combine

-the built in Flash XML socket functionality;

-a standardized xml specification called xmpp (Jabber);

-an open source server that supports this specification (Wildfire server in
my case);

-and an open source xmpp api called XIFF.

The only rub, XIFF was currently in beta and written in ActionScript 2.

XIFF is a Flash API for instant messaging and presence using the XMPP protocol. The main developer, Sean Voisen had a solid, object oriented library from which to start an AS3 port.

I decided it made more sense to take an existing well written library and port it than start from scratch. Given that this library is protected by a limited GNU license it also meant I would be giving something back to the Flash community, which, has been an immeasurable asset to me over the years.

I teamed up with a super talented developer, Derrick Grigg and got to work. Let me tell you, converting a large library of AS2 code to AS3 is no small task. The rules for AS3 have definitely changed. All the methods that pulled null results and thus silently failed are silent no more! You?ll quickly see the reason for ?try?catch?finally? as there is nothing silent about a failure in AS3. The support for runtime errors is huge and ultimately makes a library like this much more stable. The prototype decoration that solved for multiple inheritance issues? not supported (I say that, knowing full well there is a way to do it? it's just not pretty). AS3 loves using the inheritance chain the way it was intended.

So now that the new AS3 library is (almost) available, here are some summary stats..

Features:

XIFF includes an extension architecture that makes it easy to add functionality for additional protocol extensions, or even your own special-needs extensions. There are quite a few extensions already included in the library, giving it support for the following functionality:

? XML-RPC over XMPP (JEP-0009)

? Multi-user conferencing (JEP-0045)

? Service browsing (JEP-0030)

? XHTML message support (JEP-0071)

Compatibility:

The following are servers that are known to work with XIFF and Flash:

? Wildfire (http://www.jivesoftware.org/wildfire)
? Jive Messenger (http://www.jivesoftware.org)
? Antepo OPN Server (http://www.antepo.com)
? OpenIM 1.2.1 (http://www.open-im.net/en/)
? JabberD 1.4.3 (http://www.jabber.org)
? Jabber, Inc. XCP (http://www.jabber.com)

With the feature and compatibility set, here are some common uses and ideas.

Possible uses:

? Adding instant messaging to your projects. This includes nifty features like presence information, multi-user conferencing support, XHTML message support, Flex data enabled roster handling and so on.

? Live communication of any kind, including live tech support, admin communication, and anything that requires a live response from an admin side.

?Creative advertising? use your imagination.

? The general ability to be able to ?push? content to your visitor, rather than depending on them to ?request? it.

This library is currently in alpha. As soon as there has been a bit more testing we will move it to beta and release the source. Check out a Flex demo of the messenger here.

//nick

velloff.com

multi-user chat

multi-user chat

single user chat

personal messaging