The book of inspiration

October 21, 2009

Extending ScriptCoreLib

Filed under: jsc — zproxy @ 5:25 pm

In this post I am going to show you how to implement yet another .net class for java. It is going to be Windows.Forms.Treeview.

This will be included in the next jsc release.

demobutton.png

capture_21102009_192835

It is actually a regular .net windows forms application (see c# source code)!

capture_21102009_202040

Now that we know what we are going to be implementing lets check first that it does not already exist. The project we need to open is ScriptCoreLibJava.Windows.Forms.

capture_21102009_134340

I can see TextBox but I cannot see TreeView. Let’s continue and add it then. The very first thing we do is to define an internal class which is marked to be implementing the TreeView class. At this time we do not define any members to it yet.

capture_21102009_134626

Now it is time to create a test project.

capture_21102009_135300

Before we can use this new project we should do a debug build. Some referenced assemblies will be rebuilt in doing this.

capture_21102009_135346

We are only interested in java and thus we proceed to disable javascript generation. This is done by commenting out a line in the post build event.

capture_21102009_135623

We should now check if the project template can be translated to java by launching a release build.

capture_21102009_135806

The first build will take some time. Go check your emails:)

You should now have a java version of your application. There are actually two bat files. Both of them will launch the java version of the application. The fusion version will use the jar file which have been embedded in the .net exe file.

capture_21102009_140303

capture_21102009_140628

At this point I chose to this project into the svn.

To continue let’s switch back to debug build and add a treeview to the Form1 with some nodes.

capture_21102009_141127

This was all done in the designer. To see which API we must now go and actually implement to make it work for java we need to open the generated code.

capture_21102009_141252

As we can see we will also need to add TreeNode and TreeNodeCollection.

capture_21102009_141656

Now it is time to investigate the generated InitializeComponent code to see which API we must implement. It seems we will need some constructors and properties.

capture_21102009_142148

After this step we will switch to release mode and launch a build to catch any missing API calls. It seems we got zero errors. This means we can continue with implementation. For that we need to use JTree. We need to add a native stub class as it does not yet exist.

capture_21102009_142740

capture_21102009_143135

Adding stubs is still a semi manual job. I have set up a web service which will generate C# source code to serve as stubs for ScriptCoreLib. There are a lot of native stubs to be added and the service currently does not support nested types within interfaces, which is not allowed in c# mind you. There is a trick to allow nested stubs in interfaces but currently I will comment out the classes that do not fit the current support.

capture_21102009_143854

Now that we actually have the native stubs defined our next job is to make that JTree appear where we would want our TreeView to appear. We will omit nodes at first.

capture_21102009_144219

Let’s switch to release build mode and try to build this application for java runtime.

capture_21102009_144332

If all works we will have a white box where our nodes should be.

But we are not that lucky.

capture_21102009_144555

Aha! We forgot to initialize the Nodes property.

capture_21102009_144819

After next rebuild we get a nice surprise where instead of a white box there are some example nodes. Whats up with that? :)

capture_21102009_145011

After reading some documentation it became clear that I would need to create a hidden root node.

capture_21102009_154643

Basically I got it working. Now I need to improve the Clear method and add event support.

A few hours later and it is looking really nice :)

capture_21102009_192835

About these ads

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 62 other followers

%d bloggers like this: