
This dialog is intended as an error witness informing the user that something unexpected happened but that the application did not crash. Being a dialog with a single action you might wonder why spend all the effort in making an MVC group for it? There are a couple of reasons why this would be a good thing actually:
- Both the message and title can be parameterized. Maybe bindings can be used to our advantage.
- Showing the dialog at the correct place with the proper modal behavior requires more than just setting visible = true on a dialog.
griffon create-mvc dialog
In my case this command will generate the following files
- griffon-app/models/bomb/DialogModel.groovy
- griffon-app/views/bomb/DialogView.groovy
- griffon-app/controllers/bomb/DialogController.groovy
Do you notice something missing in the code? Where's the import statement for groovy.beans.Bindable? Just like Groovy will automatically import some packages like java.io and java.util Griffon does something similar, through additional default imports, which are of course configurable, even per artifact type. Good, let's turn our gaze to the View now
The View is actually a JOptionPane so it is not a dialog per se (yet). Notice again the usage of default imports in action (we needn't import javax.swing.JOptionPane). The View does have a binding to one of the Model's properties (the message property), it's very likely that we'll see the other property at play with the Controller. Oh, in case you wonder what does that nuvolaIcon node do it basically renders an specific Icon from the NuvolaIcons plugin. Time to look at the final piece of the MVC group, the Controller
This controller makes sure that the show action is executed synchronously inside the UI thread using an annotation that generates specific byte code through the usage of Groovy's AST transformation framework (have a look at section 9.3 of the Griffon Guide to learn more about the different options). The action makes sure to locate a suitable parent Window for the dialog it will create, that is, if the passed in window turns out to be invalid.
Perfect, now we can wire this dialog with the uncaught exception handler shown in the previous post. Locate griffon-app/conf/Events.groovy and edit it, make sure it looks like the following snippet
The handler makes use of a helper method that instantiates the group and cleans up after it has been discarded. This method has been suggested a few times in the past and will be featured in a future Griffon release (see GRIFFON-345), but for now we have to write our own.
A group such as this one can come in handy for more than just one application. It probably is a good idea to package it in a plugin, perhaps one that supplies different types of dialogs, for example: AboutDialog, ErrorDialog (like this one), etc. I'll cover how such a plugin can be created in a future post. Full code for the application shown here can be browsed/forked/downloaded from here.
Keep on Groovying!