ActionScript 3.0

A fun way to open a native window in AIR with MATE

Sometimes when coding it happens that you stumble on something which just strikes you as funny (sometimes for apparently no reason whatsoever).
One such experience happened today when I was coding an AIR client with MATE and I had to open a new NativeWindow.
It should be noted that I for this implementation am experimenting with ridding myself of using Managers (no other motivation than curiosity about will come out in terms of code design and architecture).

Well, the thing that amused me was to use a PropertySetter. This is due to the fact that you have to set the “visible” property in order to actually show the window. However, you also have to pass an object of the type “NativeWindowInitOptions” to the constructor.

The code eventually can be written like this…


<s>

	<s />

	
		
	

</s>



	

		
			
		

	



ActionScript 3.0, Adobe Flex

MATE CacheSetter.. a quick and dirty introduction

One of the newer classes in the MATE Flex Framework yet to be documented is the CacheSetter.
It’s nothing overly fancy, but it does exactly what it promises… it allows you to set the Cache from an ActionTag.

Let’s examine the code below… it’s a standard MATE EventMap with a couple of eventhandlers which offers to exit an AIR application when the User clicks anywhere on it… the applications for such a feature might be quite limited, but that’s what I could think of at 4:40 AM in Bangkok after almost 2 hours of being teleconferencing with the Flex Team sitting in California and it proves the point with the CacheSetter close to a minimal implementation.

NativeSystemEventMap.mxml




	
		
	

	

		
			
		

		
			
		

	


For the sake of completion, I will also bring the code to the AIR application itself…


<s>

	
		
	

</s>

One of the more interesting and intricate details (and eventually why this exercise is interesting), is to see that it uses the Class reference as the cacheKey and that you eventually would be able to use any class or hashable object and chain as the cacheKey. If the cacheKey is NOT set explicitly it will default to use the class type of the Generator attribute, but if you are working with MATE the Cache class is one of the classes it makes the most sense to take an extra look at…

CacheSetter (Source)
http://code.google.com/p/mate-framework/source/browse/trunk/src/com/asfusion/mate/actions/CacheSetter.as

Cache (Source)
http://code.google.com/p/mate-framework/source/browse/trunk/src/com/asfusion/mate/core/Cache.as

Cache (Documentation
http://mate.asfusion.com/api_docs/com/asfusion/mate/core/Cache.html

ActionScript 3.0

ActionScript API for Adobe Wave Service

While I was experimenting with the new Adobe Wave Service from Adobe… I decided to take some of the experimental code and create a reusable library.
It has a basic implementation handling authentication as well as broadcasting notifications.

2009-07-09_0612

I have named the project “asadobewave” which roughly stands for ActionScript Adobe Wave and I have made it available via the Google Code under the Mozilla license.

Check it out…
http://code.google.com/p/asadobewave/

It has been moved to here…
http://code.google.com/p/as3wavelib/

Uncategorized

Invoking the Flex Compiler from Adobe AIR… with Merapi and the Flex Compiler API

As the first step in a larger effort trying to automate my Flex Application development, I have been creating a simple Adobe AIR application that through the AIR bridge from the Merapi Project invoke the Flex compiler.
The implementation is only intended as the first experiment, so currently the use case for the App is a bit farfetched… however, the offspring of this experiment and the potential implications are very strong in terms of workflow optimizations.

Anyways, lets get on with it…
I have to warn you in advance, this is not the simplest possible implementation, but originally it was not my intention to post about it. A better sample would have excluded all the didn’t relate to the central elements which in this case is the Flex Compiler API and getting in touch with it through Merapi. Anyways… after this disclaimer… let’s get on with it…

There are a number of prerequisites in order to get started.

First you need to download and familiarize yourself with the Merapi Project.

Then you need to get the Flex Compiler. The easiest way is to grab it from the Flex SDK which was installed alongside Flex Builder / Flash Builder.

You can find the JAR file in following location:
{Flash Builder Installation Directory}/sdks/{SDK Version}/lib/flex-compiler-oem.jar

On my machine the location looks like this:
Applications/Flash Builder Plugin 4 Beta 1/sdks/4.0.0/lib/flex-compiler-oem.jar

Setting up the appropriate Java project is an exercise I will leave for you to accomplish on your own as out of scope of this post to go through that.

After setting up the project, the first we need to create the Java application which will be running in the background and listening for messages from Adobe AIR.
This application will instantiate the Merapi AIR bridge and through this listen for commands in the form of Compile message types.

It could look something like this…

import java.io.File;
import merapi.Bridge;
import flex2.tools.oem.Application;

public class FxsdkJavAirApplication
{
	public static void main(String[] args)
	{
        Bridge.open();
        new CompileHandler();
	}

	protected static void compile(CompileMessage message)
	{
		try
		{
			String inputFile = message.inputFile;
			String outputFile = message.outputFile;

			Application application = new Application(new File(inputFile));

			application.setOutput(new File(outputFile));
			application.setProgressMeter(new FxsdkJavAirProgressMeter(message));

			long result = application.build(true);
			if (result &gt; 0)
			{
				System.out.println("COMPILE OK");
			}
			else
			{
				System.out.println("COMPILE FAILED");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}

The message which will be using to pass through the bridge is called CompileMessage and inherits from Merapi’s message class and we are using the Application class from the Compiler API to actually do the building of the SWF from the MXML file.

import merapi.messages.Message;

public class CompileMessage extends Message
{
	public static final String COMPILE = "compile";

	public String inputFile = null;
	public String outputFile = null;
	public int progress;

	public CompileMessage()
	{
		super();
	}
}

Seeing that I copied most of the Java code from the HelloWorld sample of the Merapi project, the calling back to the Adobe AIR application is implemented in the ProgressMeter seeing that this was a very easy and convenient way of getting the callback to function. It should not be regarded as a recommended way of doing it, this is merely an experiment and should not be taken on account for recommended ways of implementing callbacks.

import merapi.messages.IMessage;
import flex2.tools.oem.ProgressMeter;

public class FxsdkJavAirProgressMeter implements ProgressMeter
{
	private CompileMessage message;
	private long before, after;

	FxsdkJavAirProgressMeter(IMessage message)
	{
		super();
		this.message = (CompileMessage)message;
	}

	public void start()
	{
		before = System.currentTimeMillis();

		System.out.print("begin...");
	}

	public void end()
	{
		after = System.currentTimeMillis();

		System.out.println("done");
		System.out.println("Elapsed Time: " + (after - before) + "ms");

		this.message.send();
	}

	public void percentDone(int n)
	{
		System.out.print(n + "...");

		this.message.progress = n;
		this.message.send();
	}
}

One of the key elements in the establishing the bridge between Adobe AIR and Java is the message handler invoked by the Java application.
This eventually registers a message type and will assume the role of the observer in the messaging flow.

import merapi.handlers.MessageHandler;
import merapi.messages.IMessage;

public class CompileHandler extends MessageHandler
{
    public CompileHandler()
    {
        super( CompileMessage.COMPILE );
    }

    public void handleMessage( IMessage message )
    {
    	if( message instanceof CompileMessage )
    	{
    		CompileMessage compileMessage = (CompileMessage)message;
    		FxsdkJavAirApplication.compile(compileMessage);
    	}
    }
}

These 4 classes are all the constitute the Java application based on the existing Flex Compiler API and the classes from the Merapi Project.
Now in the same order, I will go through the Flex part of the application.

First we have the application itself, in this case it is the container of the form and the Mate EventMap and to spice thinks up, a PreferenceMap which wraps the logic relating to managing user preferences.


<s>

	

	
		
		
	

</s>

The message which corresponds to the message in the Java application looks something like this.

package org.hello.fxsdkjavair.simplecompile
{
	import merapi.messages.Message;

	[Bindable]
	[RemoteClass(alias="CompileMessage")]
	public class CompileMessage extends Message
	{
		public static const COMPILE:String = "compile";

		public var inputFile:String = null;
		public var outputFile:String = null;
		public var progress:int;

		public function CompileMessage()
		{
			super( COMPILE );
		}
	}
}

Then we have the form which contains the code allowing us to manage the inputfile and the outputfile paths.


<s>

	
		
	

	<s>
		<s />
	</s>

	
		
		
	

	<s>
		<s>
			<s>
			</s>
		</s>
		<s />
		<s />
		<s />
		
	</s>

	

	<s>
		
		
	</s>

</s>

For your reference, I will just be adding the EventMap and the PreferenceMap as well…

First we have the EventMap which is a core element of Mate based applications, again… this as well is outside the scope of this post to discuss further.




	
		
	

	

		
			
		

		
			
		

		
			
		

	


This is the preferencemap, again it might be total overkill to put in a sample such as this… but seeing that it is a basic part of any application I do these days, I saw no reason to exempt it. Remembering the user’s preferences is paramount when creating user experiences which seeks to satisfy the increasingly high expectations of users today.




	
		
	

	

		
			
		

	


This is far from a complete or perhaps even suitable sample to illustrate the interoperation with both Merapi and the Flex Compiler API, but I decided to put it out here as part of a collaborative effort of getting my hands dirty with extending Flash Builder and Flash Catalyst.
If you have any questions regarding this sample, please don’t hesitate to contact me or comment on the post.

I will soon make the source code available under the “unsponsored” framework.

ActionScript 3.0, Adobe Flex, Tools

FC / FB Workflow Optimizer… sources available

I have made the sources available for the Workflow Optimizer… its a crude implementation but it does the job.
It uses Flex, AIR, as3preferenceslib, as3corelib and the Mate Flex Framework.

Don’t hesitate to comment on the ideas or concepts.. but don’t comment the code: it’s not written with any other priority than functionality…

Check it out…
http://code.google.com/p/workflowoptimizer/

ActionScript 3.0

Mate… Lightweight Schedule Extension

Some time ago I was in need of something to be run at regular intervals.
Among the many implementations I already had in my framework, I thought that including either my Workflow package (An ActionScript port of the Windows Workflow Foundation 3) or my processing package (eventually just a scheduling application block which allows processes to be managed Windows Service style.

I wanted something much more lightweight, so I decided to implement a Schedule Handler.
It extends the AbstractHandlers class, so all I had to do was initiate a timer and then run the sequence when the timer elapsed… more lightweight then that is hard to imagine.


package org.hellogroup.mate.actionLists
{
	import com.asfusion.mate.actionLists.AbstractHandlers;
	import com.asfusion.mate.actionLists.Scope;

	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class Schedule extends AbstractHandlers
	{
		private var timer:Timer = null;

		public function Schedule()
		{
			super();

			this.timer = new Timer( 1000 );
			this.timer.addEventListener( TimerEvent.TIMER, this.handleTimer );
		}

		[Bindable]
		public function get delay() : Number
		{
			return timer.delay;
		}

		public function set delay( value:Number ) : void
		{
			this.timer.delay = value;
		}

		override public function initialized( document:Object, id:String ) : void
		{
			super.initialized( document, id );

			this.timer.start();
		}

		private function handleTimer( event:TimerEvent ) : void
		{
			this.runSequence( new Scope( event, true, this.map ), this.actions );
		}
	}
}

The class is included in the downloads in my previous post…
https://blog.petermolgaard.com/2009/05/30/mate-stopanddispatchhandlers-extension/

ActionScript 3.0

Mate… StopAndDispatchHandlers Extension

Some time ago I found myself in need of a couple of features not included in Mate, such as a StopHandler which would be interruptable, but instead of just returning from the entire ActionList, would dispatch an event which could be handled by another handler.

It was named StopAndDispatchHandlers and extends the standard StopHandlers.

First I created a somewhat abstract stophandlers class which overrides the run function and handles some of the more intricate details relating to the lastReturnEquals and lastReturn members of the scope object.


package org.hellogroup.mate.actions
{
	import com.asfusion.mate.actionLists.IScope;
	import com.asfusion.mate.actions.StopHandlers;

	public class StopHandlers extends com.asfusion.mate.actions.StopHandlers
	{
		public function StopHandlers()
		{
			super();
		}

		override protected function run( scope:IScope ):void
		{
			super.run( scope );

			if( lastReturnEquals == null &amp;&amp; scope.lastReturn == null )
			{
				scope.stopRunning();
			}
		}
	}
}

Here is the concrete implementation which allows for a stophandler to dispatch an event.


package org.hellogroup.mate.actions
{
	import com.asfusion.mate.actionLists.IScope;
	import org.hellogroup.mate.actions.StopHandlers;
	import flash.events.Event;

	public class StopAndDispatchHandlers extends StopHandlers
	{
		private var _nextEvent:Event = null;

		public function StopAndDispatchHandlers()
		{
			super();
		}

		public function get nextEvent() : Event
		{
			return _nextEvent;
		}

		public function set nextEvent( value:Event ) : void
		{
			_nextEvent = value;
		}

		override protected function run( scope:IScope ):void
		{
			super.run( scope );

			scope.dispatcher.dispatchEvent( nextEvent );
		}
	}
}

You can download the compiled binary from here…
http://petermolgaard.com/projects/MateSchedule.swc.zip

…and the entire sourcecode as a Flex project from here…
http://petermolgaard.com/projects/MateSchedule.fxpl.zip