August 4th, 2008Merapi :: Custom Class Mapping

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 & 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: air adobe, eclipse, flex, java, merapi, merapiproject




September 4th, 2008 at 9:29 pm
I’m alllllmost there. Except I can’t see your main.mxml code. Thanks for the post though!
January 29th, 2009 at 7:58 pm
Hi Guys, good post
but I had to change some of the code to get it run.
In the java Shizzle class you’re using a hello property (through the getter/setter)
But in the As3- Shizzle class you’re using a helloWorld property.
I think they should be same (so either hello or helloWorld)
i.e.
[JAVA snippet]
// ——– getters & setters ———————
public String getHelloWorld() {
return helloWorld;
}
public void setHelloWorld(String hello) {
this.helloWorld = hello;
}
[AS3 snippet]
// —-getters & setters ———————
public function get helloWorld() : String
{
return __helloWorld;
}
public function set helloWorld( value : String ) : void
{
__helloWorld = value;
__tf.text = __helloWorld;
}
February 6th, 2009 at 11:09 am
The code listed above works but what Patrick posted also works. Thanks for the alternative code.
March 11th, 2009 at 5:33 am
I am not a coder but as I understand this it will migrate a app from java to air platform. If my understanding is correct I am very excited to see this as I have always been an avid windows user and want desperately to move over to mac but the lack of interchangeable programs has stopped me. Ad I see this it is one step closer to the day when that might be possible. I hope I am not making an idiot of myself because of misunderstanding.
April 23rd, 2009 at 8:04 pm
Hi Dear author,
Thanks very much for this helpful post!
However would you please do me a favor to send a copy of Merapi release to my email box which you may be able to see?
Thanks a lot in advance!!
//bow
June 5th, 2009 at 10:47 am
a nice post again,thanks
December 4th, 2009 at 10:22 pm
This looks useful
thanks for posting it (i may have to borrow a few lines to get my code working - thanks
January 26th, 2010 at 8:52 pm
I tried both the author’s and patrick’s codes and either seem to work for me, Sarah.