How To: Create Plugins using BlueJ

I thought this may be helpful for people creating simple plugins, and want an easier interface like BlueJ to do it in.

Dowload BlueJ from: http://www.bluej.org/

First, Add the Servoy Jar (j2db.jar) to your BlueJ Library:
Open BlueJ, click on ‘BlueJ’ from the top menu, and Select ‘Preferences’.
Select the ‘Libraries’ tab
Click the ‘Add’ button, then find and select ‘j2db.jar’ (should be insider Servoy folder, and inside the ‘lib’ folder)
Quit BlueJ and ReOpen to complete. Go back into Library preferences and make sure the status is ‘loaded’

Download the Zip file and extract to a folder (included in this post). This includes all source files and folders.

While in BlueJ, select ‘Project > Open Project’. Select the top level folder from the zip file. It should be ‘PluginTest’.

Navigate in BlueJ to the com.ddc.plugins.plugintest folder. Compile the java files.

*Here is where you would make changes to the files to fit your own needs

To create the jar for the Servoy Plugin, select ‘Project > Create jar file’ . You do not need to select any of the options from the pop-up window. Just select ‘Continue’. Name the file plugintest.jar, and save it inside of the Servoy/plugins folder.

Launch Servoy (quit and relaunch if it was previously opened), and you should see a plugin named “PluginTest” with the getHelloString method in it.

PluginTest.zip (9.67 KB)

This looks pretty awesome Scott!
It definitely takes the complexity of an environment out of the equation. Time to learn Java without learning first how to use the IDE and/or commandline.

Thanks for this heads up AND example project!

Somehow I feel we gonna see much more plugins in the near future ;)

goldcougar:
Download the Zip file and extract to a folder (included in this post).

Where I can get the example project?

Txs,
Miguel

Hi Miguel,

The example project is attached to scott’s post.
It’s the PluginTest.zip file.

:oops:

Silly of me, never noticed that you must be logged in to view attachments!

Txs Robert ,
Miguel

BTW: Congrats on the Polyglot solution. Looks very good, I will check it out!

BlueJ looks very easy to use. I had no trouble getting your plugintest to compile and it loads as a Servoy plugin. However, I’m not clear which parts of PluginTestProvider and PluginTest are required for any plugin and which bits are specific to HelloWorld.

In essence, I have a few JARs that contain methods which I want to call within Servoy. Using BlueJ, how do I start with JARs to achieve the same result? Do I just add them as libraries? or do I need to create methods in my project to call the methods in the JARs?

For most things (where you are returning regular data types), you only need to modify PluginTestProvider. Any method you create that you want to show up in servoy should look like js_methodName(params), like the get HelloString. Then you modify the getParameterNames, getSample, and getToolTip to add your methods in the if, then, else brackets.
For applications where you want to return a self created datatype (so you have some class called MyDataType, and you want to return that into servoy, you need to put this in the getAllReturnedTypes method

return new Class[] {MyDataType.class};
``` . Then you are able to create public methods or public variables (inside MyDataType) that will be availabe to you by reference that MyDataType from Servoy. This is really useful if you have a plugin method that returns a lot of stuff back. I use that in my Credit Card Auth plugin because there are about 10 things returned and it is easier to return my data type, and then access the returned values through the data type (rather than some delimited string or array)...I hope all that makes sence. 

For your second question, to just use other jars (not return them), There arew 2 ways to do this, one is mentioned at , <a class="postlink" href="http://forum.servoy.com/viewtopic.php?t=6120">http://forum.servoy.com/viewtopic.php?t=6120</a> which is where you posted. The down side of that way is that you cannot see the method calls from Servoy...you have to just remember and type them.

What I do is to create a method in PluginTestProvider that calls upon the Jar to do whatever it needs to do. Then you just have to reference your jars in BlueJ, like in the example, and then just but the Jars into the Servoy/Lib folder, upload your plugin, and it should work.

Thanks +++++ Scott, very useful advice indeed! I’m in unfamiliar territory, and this has got me on the road. I’ve now got a basic plugin working with my JARs.

To handle the dual issues of java vector types and wanting to return more than one variable from calls to the external JARs, I’ve managed to avoid creating a new data type by
declaring an array in the plugin,
converting the vector and other data to strings
placing them as elements in the array
returning the array to Servoy

public String[] js_getErrorText(int sessionId, int errorCode)
{
   String[] s = new String[2];
   int rval = 0;
   Vector result = new Vector();

   rval =  Eclaim.getInstance().getErrorText(sessionId, errorCode, result);
   // then convert the int 'rval' and the vector 'result' to strings and put in the array
   s[0] = Integer.toString(rval);
   s[1] = result.get(0).toString();
   return s; 
}

If there are errors or you have suggestions about the code, please let me know how to improve it.

Scott, I’d still be interested to see a small example of the use of the self created data type, if you have something handy.

Here is a quick, but silly example where I modified the plugin to return back a custom object. In this example, the PluginTestReturn object has a public variable, ‘name’, holding the name passed into the method, and also has a method to return the length of the name.
Here is an example Seroy method i used with it:

var myObject = plugins.PluginTest.getHelloCustomReturn("Servoy Guy")
plugins.dialogs.showInfoDialog( "Hello String",  myObject.name,  "ok")
plugins.dialogs.showInfoDialog( "String Length",  myObject.getStringLength(),  "ok")

They way you did it above with an array is perfectly fine. I only prefer this metod because I, and the other people on our team, don’t have to know what order the method returns things back into the array. Instead a custom object is created and returned…then anyone can access its public variables or methods through the return type (you’ll notice an arrow next to PluginTest, which below will show PluginTestReturn, and all of the variables and methods) Hope this helps.

PluginTest.zip (11.3 KB)

Thanks Scott!
I really like that refinement, much easier to remember myObject.name than myArray[26]!

With your example, it was a breeze to get it working. BTW, what’s the HashCode function used for?

I’m really not sure why servoy shows the hashCode function. In general the hashCode method is from the java.lang.object class, and normally used in part to compare two objects. So, I suppose you could implement the hashCode method in the PluginTestReturn class (it should return an integer), and then I guess you could compare to different PluginTestReturn objects…if you ever needed to do that.

So, then this should return true.

var myObject = plugins.PluginTest.getHelloCustomReturn("Servoy Guy") 
var myObject2 = plugins.PluginTest.getHelloCustomReturn("Servoy Guy") 
return myObject==myObject2

We may need to ask the Servoy gods about that one.
The other way may be…

var myObject = plugins.PluginTest.getHelloCustomReturn("Servoy Guy") 
var myObject2 = plugins.PluginTest.getHelloCustomReturn("Servoy Guy") 
return myObject.hashCode()==myObject2.hashCode()

But I’m just guessing.

goldcougar:
I’m really not sure why servoy shows the hashCode function.

When you don’t have any js_ methods in your class it will show all methods that are in the class.
So just add a method with the js_ prefix and you loose the hashCode function in Servoy.

Just a “small” issue. One needs to install the JDK, minimal version 1.5.