Useful FDT Templates Paul
One of the most useful features in FDT is templates. It’s something that FlashBuilder is still missing, and while templates are pretty simple in concept, they are one of the key features that keep me using FDT over its free counterpart.
Templates are basically code completion shortcuts that allow you to generate boilerplate code quickly and easily. This can be anything from generating a for loop up to generating entire classes. They are especially useful when you’re under that tight deadline before your presentation on Mobile Broadband deals or trying to realize your next great game idea. In this article I’ll share with you some of the more useful ones I’ve written that speed up my dev time significantly. Not only do they speed things up, they can help enforce consistency and reduce the little typos that introduce errors and bugs.
A Better Trace
One of the pre-packaged templates in FDT is systrace, which just generates a call to the trace method and puts your cursor in the message body. Surely we can do better than that!
mt:
trace("${enclosing_type}.${enclosing_method}(",[${enclosing_method_arguments}],")");
This template will generate traces that look like:
trace("MyClass.saveUser(",[userid, userdata],")");
By wrapping the method arguments in an array, the contents of the array will each have their toString methods called and automatically be concatenated in to one string. This is a huge time saver, and allows you to do a quick-and-dirty check of what’s flowing in to your methods.
Generating Event Listeners
cl:
addEventListener( Event.COMPLETE, ${cursor} );
ml:
addEventListener( MouseEvent.CLICK, ${cursor} );
After typing in the event handler method’s name, I follow up with CTRL+1, which will auto generate the method and automatically create the correct event parameter based on what type of event listener you added. You can add similar templates to take care of all the common events you work with.
Declaring Event Names
More than once I’ve been bitten by copying and pasting an event name declaration and ending up with code that looks like this:
public static const ADD_USER : String = "addUser"; public static const REMOVE_USER : String = "addUser";
Any time a user is added, the handlers for both ADD_USER and REMOVE_USER will get fired!
et:
public static const ${param} : String = "${param}";
Using this event name template makes sure duplicate names will never happen by ensuring that the string constant behind your event parameter is unique, since it shares its name with its compile-time checked constant
Public/Private/Protected Functions
puf:
public function ${cursor}() : void
{
}
ptf:
protected function ${cursor}() : void
{
}
prf:
private function ${cursor}() : void
{
}
These are pretty obvious, and it’s strange that they don’t ship with FDT by default.
Singletons
This generates a singleton based on a technique described over at Daniel Rinehart’s blog.
singleton:
package ${enclosing_package}
{
public class ${enclosing_type}
{
public static var instance : ${enclosing_type};
public static function getInstance() : ${enclosing_type}
{
if( instance == null ) instance = new ${enclosing_type}( new SingletonEnforcer( ) );
return instance;
}
public function ${enclosing_type}( pvt : SingletonEnforcer )
{
if (pvt == null)
{
throw new Error("${enclosing_type} is a singleton class, use getInstance() instead");
}
${cursor}
}
}
}
internal class SingletonEnforcer {}
These are just a few ways to use templates to speed up your day-to-day workflow. If you have any of your own, please, leave them in a comment!
You can download the templates here.
To install on Windows open FDT and go to: Window > Preferences
In the preferences window: FDT > Editor > Templates > Import
On a Mac open FDT and go to Application Menu (FDT 3 or Eclipse) > Preferences
In the preferences window: FDT > Editor > Templates > Import


December 3rd, 2009 at 6:47 pm
Hi Paul,
Can I add your templates file to http://github.com/Powerflasher/FDT-Resources/tree/master/templates/ ?
Thanks for share.
December 4th, 2009 at 1:30 pm
eidiot: Go for it! Glad to help.
December 4th, 2009 at 3:49 pm
Usefull template!!!
But I prefer singleton without internal class!
public class Singleton {
private static var __instance : Singleton ;
public function Singleton (_target :*= null) {
if (Singleton .__instance == null) super(_target);
else strace( “[Singleton] – Can’t Instantiate More than Once” );
}
public static function getInstance() : Singleton {
if(Singleton .__instance == null) Singleton .__instance = new Singleton( );
return Singleton.__instance;
}
}
April 4th, 2010 at 1:32 am
[...] ※こちらのサイトでも「Public/Private/Protected Functions」が公開されていたのですが、「puf ptf prf」というショートカットが個人的に使いにくかったので、普通に「public…」などと打つと候補が出てくるようにしました。 [...]
June 16th, 2010 at 4:28 am
[...] http://blog.hydrotik.com/2007/11/19/fdt-30-code-templates/ http://www.breaktrycatch.com/useful-fdt-templates/ http://www.stevensacks.net/2010/04/30/setting-up-fdt-to-look-and-behave-like-flashdevelop/ [...]
June 19th, 2010 at 6:01 am
hi there.. nice blog. Just found it on bing Subrscribed!
November 5th, 2010 at 11:14 am
[...] Useful FDT Templates [...]