1. Introduction :

If you are an AIR fanatic like I am, you might have heard of a project called Merapi. The Merapiproject.net is an open source project to create an easy link between Java applications and AIR. They have just entered their first alpha. The source isn’t available ( yet ),but you can download a .swc file and get started !

In this post I’ll explain how to transfer custom objects from one runtime to the other using Merapi. I’m using Eclipse for both my Java and Flex projects. There a lot of examples on how to set this up so you can have a good working environment.

2. Time to get started :

The application I’ll show you here doesn’t do anything fancy. I’ll just create a custom class called Shizzle in Java, and send it over to Flex and starting using it immediately. Here are some screenshots on how my projects are organized :


So let’s get started with the Java side.

3. Java != coffee

This is the main class for the Java project.

Merapi.java

package be.dietlev;
import javax.swing.JButton;
import javax.swing.JFrame;
import merapi.Bridge;
import merapi.messages.Message;
import java.awt.event.*;
 
public class Merapi implements ActionListener{
 
	// ---------------------------- variables ---------------------
	Bridge bridge;
 
	// ---------------------------- constructor ---------------------
	public Merapi()
	{
		bridge = Bridge.getInstance();
	}
 
	public static void main(String[] args) {
		Merapi m =  new Merapi();
 
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setTitle("Merapi Demo");
 
		JButton actionbutton = new JButton("sendmessage");
		actionbutton.addActionListener(m);
 
		f.add("Center", actionbutton);
		f.pack();
		f.setVisible(true);
	}
 
	// ---------------------------- private functions ---------------------
	private void sendMessage()
	{
		try
		{
			Shizzle shiz = new Shizzle();
			Message mess = new Message("testMessage", null, shiz);
			bridge.sendMessage(mess);
		}
		catch( Exception ex )
		{
			System.out.println("Exception : " + ex);
		}
	}	
 
	// ---------------------------- public functions ---------------------
	public void actionPerformed(ActionEvent arg0) {
		sendMessage();
	}
 
}

And this is my shizzle class.

Shizzle.java

package be.dietlev;
 
public class Shizzle {
 
	// ---------------------------- variables ---------------------
	private String helloWorld;
 
	// ---------------------------- constructor ---------------------
	public Shizzle()
	{
		helloWorld = "Helloooooooo World!";
	}
 
	// ---------------------------- private functions ---------------------
 
	// ---------------------------- public functions ---------------------
 
	// ---------------------------- getters & setters ---------------------
	public String getHello() {
		return helloWorld;
	}
 
	public void setHello(String hello) {
		this.helloWorld = hello;
	}
}

The code is pretty straightforward. First create an instance of the Merapi Bridge class. When you click the button, dispatch a Message with the custom class Shizzle. In the Shizzle class, make sure you have those getters & setters for every variable you make. These will be used by Merapi internally.

4. Flex !

main.mxml

 
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
 
	<mx:Script>
		<![CDATA[
 
			// ---------------------------- imports ---------------------
			import mx.core.UIComponent;
			import be.dietlev.Shizzle;
			import mx.rpc.events.ResultEvent;
			import merapi.Bridge;
 
			// ---------------------------- variables ---------------------
			private var shizzle : Shizzle;
			private var ui : UIComponent;
 
			// ---------------------------- constructor ---------------------
			private function init() : void
			{
 
				ui = new UIComponent();
				addChild(ui);
 
				shizzle = new Shizzle();
				Bridge.instance.addEventListener(ResultEvent.RESULT, onResult);
			}
 
			// ---------------------------- private functions ---------------------			
			private function onResult( evt : ResultEvent ) : void
			{
				shizzle = evt.result.data as Shizzle;
				if ( shizzle != null )
				{
					removeChild(ui);
					ui = new UIComponent();
					ui.addChild(shizzle);
					addChild(ui);
					trace(shizzle.shizzleMe());
				}
			}
 
		]]>
	</mx:Script>
 
</mx:WindowedApplication>

and finally the Shizzle class on the Flex side.

Shizzle.as

package be.dietlev
{
	// ---------------------------- imports ---------------------
	import flash.net.registerClassAlias;
	import flash.text.TextField;
	import flash.display.MovieClip;
 
	public class Shizzle extends MovieClip
	{
 
		// ---------------------------- variables ---------------------
		private var __helloWorld : String;
		private var __tf :  TextField;
 
		// ---------------------------- constructor ---------------------
		public function Shizzle() : void
		{
			registerClassAlias("be.dietlev.Shizzle", Shizzle);
			__tf = new TextField();
			addChild(__tf);
		}
 
		// ---------------------------- public functions ---------------------
		public function shizzleMe() : String
		{
			return __helloWorld;
		}
 
		// ---------------------------- getters &amp; setters ---------------------
		public function get helloWorld() : String
		{
			return __helloWorld;
		}
		public function set helloWorld( value : String ) : void
		{
			__helloWorld = value;
			__tf.text = __helloWorld;
		}
 
	}
}

5. The explanation

As I said, the Java side of this project is really simple. Hit the button and it’ll dispatch the custom Shizzle class.

In main.mxml, you have to create the bridge instance as well and attach the event listener. When it received an event, it’ll go to the onResult function. Take the data that has been sent over and typecast it to a Shizzle object. Now here is the cool part :

In Shizzle.as, this line makes sure the class will be filled correctly with the given data :

registerClassAlias("be.dietlev.Shizzle", Shizzle);

So at this point, everything is working as it should ! Congratulations, you have just created your first Merapi application.

6. Ok, some more details

I did have some problems to get this running. Here are some final pointers :

  • If you use private variables, create public getters & setters
  • Make sure the classes you try to link have the same package names
  • Create a new instance of the class before you attach data to it ( I instantiated shizzle in main.mxml for the first time when the application launched )
  • Debug your .mxml so you see your traces ^^

That’s all for today. Good luck with the projects !


Technorati Tags: , , , , ,