As I read in the Groovy user mailinglist that some people complained missing information about how to integrate Groovy into Java applications, I decided to give one example of how we integrated Groovy as extension language for our graphical developer tool.
First a bit of background regarding the application to be scripted:
It is a little SDE GUI for starting different build targets for different components in a workarea. As we noticed that, depending on some individual tasks or different roles a developer takes in a team, extension and customisation would be a nice feature, we integrated Groovy as scripting language used to plug in individual features at the user site.
We created two "extension points" in the GUI: An empty "user" menu ready to be filled with items and an empty panel at the bottom of the GUI being able to be filled with e.g. custom buttons.
There is also a specific script output window, where the script can place messages or other textual output. The opening of this window is part of the API (see point 2).
To keep the Groovy integration at one location, we created the class ScriptConnector, which is the adaptor between Groovy and the application.
It calls the Groovy engine, maintains the binding, provides some API methods to be called inside the Groovy script, leading to better separation which keeps the script clean from the application's intera.
BTW: One requirement was, that errors in the Groovy integration should not break the rest of the application, but should only affect the customised parts, so exceptions are caught and shown as 'warnings' in a dialog window.
The plugin feature is provided by one dedicated plugin script which is customisable/extensible by the user. He can use all features the Groovy language provides, so external scripts and programs can be integrated via this script.
Let us have a look at the main class first, so you will see it all from startup on.
Please be aware that the shown source is a simplified form of our productive code.
Now let's look at the ScriptConnector, as this is the important place of Groovy integration:
This is only an (senseless) example of how to create custom buttons and menu items, but in combination with the connector class it will give you an idea of how an application can be customised/scripted with Groovy as scripting language.
I hope this spontaneous little article could give you a help in Groovy application integration and give a slight idea of what Groovy could do for you.