The plugin workflow is very similar to Grails
- create a plugin project
- configure dependencies as needed
- add relevant source code and artifacts
- package the plugin
$ griffon create-plugin dialogs
$ cd dialogs
$ cd dialogs
A Griffon plugin project has pretty much the same structure as an application but you can't run it as one (another difference with Grails where you can actually perform this action). The project also includes a very special filed called the plugin descriptor, which in our case is named DialogsGriffonPlugin.groovy. This file describes what the plugin requires in order to be installed in an application (like Griffon version, dependencies, UI toolkit, OS platform) and pointers to additional documentation. Open the file in your favorite editor and make the following changes
DialogsGriffonPlugin.groov The dialog built in the last post requires that the NuvolaIcons plugin be installed too. We can force the installation of the plugin by setting a dependency on it. Now, every time a developer installs the dialogs plugin the nuvolaicons plugin will follow if it's not installed already.
Step #2 is creating the addon descriptor. This task can be done by invoking the following command
$ griffon create-addon dialogs
The convention is that the addon's name follows the plugin's but it's not enforced, you can pick any name you want. This command will perform the following tasks for you
- create the addon descriptor file named DialogsGriffonAddon.groovy
- add addon install instructions to scripts/_Install.groovy
- addd addon removal instructions to scripts/_Uninstall.groovy
- addd compile dependency on the addon's jar to scripts/_Events.groovy
Step #3. Remember how we created the MVC group before? Right, by means of a griffon command
$ griffon create-mvc griffon.plugins.dialogs.ErrorDialog
This time we have chosen a name with a package. The command should create the following files
- griffon-app/models/griffon/plugins/dialogs/ErrorDialogModel.groovy
- griffon-app/views/griffon/plugins/dialogs/ErrorDialogView.groovy
- griffon-app/controllers/griffon/plugins/dialogs/ErrorDialogController.groovy
There's a definition for the MVC group we just created. Awesome! Recall that addons can deliver application event handlers. Also recall from the previous post that we registered an application event handler in order to listen to uncaught exception events. What follows is that we can add the event handler to the addon, making it an integral part of it, thus the application developer no longer has to write a custom handler in griffon-app/conf/Events.groovy. WIN! The following snippet shows the final code for the addon descriptor
Pay close attention to the name of the event we choose this time: UncaughtExceptionThrown. The addon cannot know in advance the type of exception it should handle, so we declare our intention to handle any exception that might have been caught by GriffonExceptionHandler.
The next step (#4 if you're keeping count) is to fill in the details of the ErrorDialog group. You can copy the snippets from the previous post into their respective new places taking into account the changes in the package and class names.
The last step is to package the plugin and test it on an application. Packaging plugins in Griffon is done in the same way as in Grails, with one additional change: specify a -nodoc if you want a speedy build and avoid javadoc construction
$ griffon package-plugin -nodoc
The command should have created a file named griffon-dialogs-0.1.zip. You can then go an install this plugin in an application, like this
$ griffon create-app sample
$ cd sample
$ griffon install-plugin /path/to/griffon-dialogs-0.1.zip
$ cd sample
$ griffon install-plugin /path/to/griffon-dialogs-0.1.zip
The output of the last command should result in something similar to this
Now, edit the application by adding a few conditions that will make it fail. Run the application and trigger those failing conditions. You should see the ErrorDialog appear as soon as an exception reaches GriffonExceptionHandler. That concludes today's Griffon howto.
Keep on Groovying!