Universal Mind

17 09 2008

Universal Mind Logo
After a seemingly brief stay with Highwinds in Winter Park, FL, I’ve decided to become an independent consultant working with Universal Mind.  I’ve known these guys for a long time now and they are steadily growing as the premiere Rich Internet Application development house.

More here: www.UniversalMind.com

I will really miss my friends at Highwinds, but I left StrikeTracker in some very capable hands and I’m not going far.





StrikeTracker Unveiled

22 05 2008

This press release gives a great description of what we’re up to in the new product:

http://www.cnbc.com/id/24723705/





A Little Recognition

19 05 2008

I haven’t posted much about my new position at Highwinds yet, but there have been quite a few articles from local newspapers, the associated press, and industry magazinges about my latest project, StrikeTracker:

Streaming Media Magazine, May 19, 2008

More here on the Highwinds website





Salsa T-shirt for a Software Engineer

29 02 2008

I saw this shirt at Flava Invasion 3 in Atlanta… how appropriate is this for me?!?!?!

Salsa Day and Night





Scrolling Containers, IE, wmode=”transparent” PROBLEM

21 02 2008

This makes TWO Flex bugs in one day… damn I’m handy:

http://bugs.adobe.com/jira/browse/SDK-14738





Making Flash/Flex Video More Efficient

21 02 2008

Submitted an issue to the Flex Bug Tracking DB today:

http://bugs.adobe.com/jira/browse/SDK-14732

This is a result of some performance tuning an application that needs to connect to FMS for video using mx.controls.videoClasses.VideoDisplay

Read the issue for details.





Firefox and WMP Plugin Issue

15 02 2008

I have an issue that only occurs using the Firefox version of the WMP plugin. The intent is to change the video programatically. The following scenario causes 2 problems:

1) The video is not visible even though the player is. The audio stream is playing.

2) Subsequent attempts to set the content of the player cause the video to display, but the internal content stretches to the previous dimensions of the player. (I’m handling the PlayStateChange event.. see below) The first one I play is 360×240 and the second is 320×240.

On an html page I have the plugin created with javascript to handle cross-browser differences:

<script language="JavaScript" type="text/javascript" for="windowsMediaPlayerInstance" event="PlayStateChange(newState)">
  <!--
    onWmpPlayStateChange(newState);
  // -->
</script>

<div style="position: absolute; overflow: hidden; display: none;" id="windowsMediaPlayerHost">
  <script type="text/javascript" language="Javascript">
    <!--
      var player = createWindowsMediaPlayer();
      document.write(player);
    // -->
  </script>
</div>

The player HTML is returned from:

function createWindowsMediaPlayer()
{
  var target;

  if (isIE)  // isIE is provided by the AC_OETags.js script
  {
    target = "IE";
  }
  else
  {
    target = "FF";
  }

  if (isIE)
  {
    content = "<object id='windowsMediaPlayerInstance' classid='CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6' width='320' heigh='240'";
    content += " codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701' type='application/x-oleobject'>";
    content += " <param name='url' value=''>";
    content += " <param name='uiMode' value='none'>";
    content += " <param name='autoStart' value='true'>";
    content += " <param name='enableContextMenu' value='true'>";
    content += "</object>";
  }
  else
  {
    content = "<object id='windowsMediaPlayerInstance' type='application/x-ms-wmp' data='' width='320' heigh='240'>";
    content += " <param name='url' value=''>";
    content += " <param name='uiMode' value='none'>";
    content += " <param name='autoStart' value='true'>";
    content += " <param name='enableContextMenu' value='true'>";
    content += "</object>";
  }

  return content;
}

I set the content:

var player = document.getElementById("windowsMediaPlayerInstance");
player.URL= url;
player.FileName  = url;
player.autoSize  = "0";
player.autoStart = "true";
var host = document.getElementById("windowsMediaPlayerHost");
host.style.display = "block";

Here is how I handle the state change and look for the video to start playing so I can grab the imageSourceWidth and Height:

function onWmpPlayStateChange(newState)
{
  if (newState == 3)
  {
    if (sizeUpdated == false)
    {
      sizeUpdated = true;
      var player = document.getElementById("windowsMediaPlayerInstance");

      if (player == null)
      {
        alert("Error: The Windows Media player control could not be not found.");
      }
      else
      {
        var currentMedia = player.currentMedia;
        var w = currentMedia.imageSourceWidth;
        var h = currentMedia.imageSourceHeight;
        setPlayerSize(w, h);
        updateAppPlayerSize(player.width, player.height);
      }
    }
  }
}




DataGrid Item Renderers… Think outside the <mx:Box/>

22 01 2008

Ok… so I think I’m funny. This is more about thinking OF the <mx:Box/>

Let’s say you want to create a custom ItemRenderer for a Datagrid that has layout control like the HBox container. You cannot simply write a custom component and assign that to a DataGridColumn. You’ll find out that in order to get access to data being assigned to your formatted column at runtime, you need to implement IDropInListItemRenderer. The only components useful to DataGrid that already implement that interface are: Button, ComboBox, DataGridItemRenderer, DateField, Image, Label, ListBase, ListItemRenderer, NumericStepper, TextArea, and TextInput.

This is fairly easy to overcome. All you have to do to a container like HBox would be to define that interface to implement, create a getter and setter for the listData property, and handle the dataChange event so when this class is reused as a renderer in a list, we appropriately set the property that stores our DataGrisListData:

<mx:HBox
    implements="mx.controls.listClasses.IDropInListItemRenderer"
    dataChange="onDataChange">

private var _listData : BaseListData;
private var _dgListData : DataGridListData;

public function get listData() : BaseListData{
return _listData;
}

public function set listData(value : BaseListData) : void{
this._listData = value;
}

public function onDataChanged(event : Event) : void{
_dgListData = listData as DataGridListData;
}

When the item renderer is used at runtime and the data property is set to the item in the grid, you can access the appropriate datafield which you intend to format. In this case I’m setting a private [Bindable] variable that a label is listening to:

public override function set data(value : Object) : void{
    super.data = value;
    _theLabelText = value[_dgListData.dataField] as String;
    super.invalidateDisplayList();
}

Now you can use the HBox to do this:

<mx:DataGridColumn
    datafield="myfield"
    itemrenderer="myHBoxRenderer"/>

I’ve used this method to create an Excel-style Accounting Renderer. It will be posted shortly here in this blog.





Singleton Pattern in Cairngorm 2.1 with Actionscript 3

12 01 2007

So, I’m an experienced software engineer diving into the world of Rich Internet Applications (RIA) using Flex. After attending Adobe MAX 2006 in Las Vegas I was inspired to follow the Cairngorm bandwagon that even Adobe seems to have adopted as a gospel design pattern.

Call me OCD, but I analyze the daylights out of every new direction I take in programming. I don’t want to be associated with something that could possibly be dropped like a bad habit in the near future (f.y.i. In my mind so far, Cairngorm = Good). So let’s take a look at what Cairngorm is trying to accomplish, and an inherent flaw in ideology that I recently discovered.

Cairngorm is an adaptation of existing design patterns (mainly from J2EE and .NET) that are applied to the new RIA paradigm. The subject of this discussion will focus on one core pattern in particular that it has taken on as its own.

The Singleton Pattern

This veteran in the design pattern world is a tried and true way of insuring that one and ONLY one instance of an object is created and used at any given time during the life of an application. For Cairngorm this applies to some key components:

ModelLocator – Used to store the client-side model
ViewLocator – Stores/Manages view names
CairngormEventDispatcher – Used to broadcast user gesture events

These objects absolutely MUST be singletons. We can’t have two different ModelLocators running wild in our application. Traditionally we prevent this from happening with the Singleton Pattern.

An example (taken from java.sun.com)

public class MySingleton{
   private static MySingleton _instance = new MySingleton();
   private MySingleton(){
        // construct object . . .
   }
   public static MySingleton getInstance(){
        return _instance;
   }
   // Remainder of class definition . . .
}

The key component of this implementation of the Singleton is the Private Constructor. In Java for example, making the constructor private disallows any external objects from creating a new instance of our Singleton. You get a GOLD STAR if an alarm just went off in the Actionscript portion of your brain. Actionscript 3 DOES NOT ALLOW PRIVATE CONSTRUCTORS. Yikes! All is not lost. A loosely interpreted version of the Singleton Pattern is still possible as long as we adhere to the core principle of disallowing the instantiation of more than one of our would-be singletons.

But first… the faulty Cairngorm 2.1 documentation
This example of how Cairngorm suggests you implement the ModelLocator as a singleton:

[Bindable]
public class ShopModelLocator implements ModelLocator{
   private static var modelLocator :  ShopModelLocator;
   public static function getInstance() : ShopModelLocator{
        if ( modelLocator == null )
             modelLocator = new ShopModelLocator();
        return modelLocator;
   }
   public var products :  ICollectionView;
}

Right away you can see that this wasn’t written for AS3. Private constructor definitions are no longer allowed.

Here is an implementation in the Cairngorm Store (taken from IterationTwo):

[Bindable]
public class ShopModelLocator implements ModelLocator
{
   private static var modelLocator : ShopModelLocator;

   public static function getInstance() : ShopModelLocator
   {
        if ( modelLocator == null )
        {
             modelLocator = new ShopModelLocator();
        }

        return modelLocator;
    }

    //Constructor should be private but current AS3.0 does not allow it yet (?)...
    public function ShopModelLocator()
    {
         if ( modelLocator != null )
         {
              throw new Error( "Only one ShopModelLocator instance should be
              instantiated" );
         }

         shoppingCart = new ShoppingCart();
         productComparator = new Comparator();
         currencyFormatter = getInitialisedFormatter();
         assets = new CairngormStoreAssets();
    }

Here it is noted that the AS3 problem is known, and a workaround is provided. This is what set the alarm bells off in my head! A workaround… already? Is this not as stable as I thought?

Solutions
Here are a couple solutions I’ve discovered:

Creating a Public Yet Inaccessible Constructor
An excerpt from a blog post by Jeff Tapper

public function DataManager(pri:PrivateClass, wsdl:String){
}
/*
PrivateClass is used to make DataManager constructor private
*/
private class PrivateClass{
   public function PrivateClass(){
}

“Since AS3 doesn’t currently have private constructors (I’m still hoping this gets added into the language before its released), the constructor takes an instance of PrivateClass as an argument. Since PrivateClass is defined as a private class, its only available within this same file. This ensures that the constructor can not be called externally, essentially giving us a private constructor.” – Jeff Tapper

The “Have I Been Created” Flag
Inspired by a blog post by Michael at Createage.com

/*
*  Class is a Singleton
*/
public class Welcomer
{
   private static var instance:Welcomer;
   private static var create:Boolean;

/**
* Constructor – Singleton
*/
public function Welcomer()
{
   if ( !create )
   {
        throw new Error("Class cannot be instantiated");
   }
}

/**
* Returns the one single instance of this class
*/
public static function getInstance():Welcomer
{
   if (instance == null)
   {
   create = true;
   instance = new Welcomer();
   create = false;
   }

   return instance;
}

/**
* Returns a welcoming message with the provided name.
*/
public function welcome( name:String ):String
{
   return "Hello " + name;
}

In this case the constructor does a check to see if the “create” variable (stored in the singleton) is true or false. If true, then allow one to be instantiated; if false, then throw an error up to the culprit code.

Pros: This is logically easy to understand and works for AS3
Cons: Watch out if the next version of Actionscript becomes multithreaded. Checking a global variable is one of the things that raise eyebrows in determining thread-safety. AS3 is NOT multi-threaded and so, this works… for now. See this blog post that covers Multithreading and Actionscript.

Why is the Cairngorm Store not using the SINGLETON_EXCEPTION??
This is just a fun question that I pose to the Cairngorm Store folks: Why not throw the SINGLETON_EXCEPTION when you catch some rogue code trying to create another ModelLocator, etc.?

http://www.cairngormdocs.org/docs/cairngorm_2_1/com/adobe/cairngorm/CairngormMessageCodes.html

Tom Schober
Lone Palm Creative Solutions