Griffon: Building dialogs with MVC - No Fluff Just Stuff

Griffon: Building dialogs with MVC

Posted by: Andres Almiray on March 24, 2011

In the previous post, Griffon: Dealing with uncaught exceptions, a screenshot of a dialog appeared at the end, which is reproduced here for your viewing pleasure


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:
  1. Both the message and title can be parameterized. Maybe bindings can be used to our advantage.
  2. Showing the dialog at the correct place with the proper modal behavior requires more than just setting visible = true on a dialog.
We can leave the first task to bindings between the Model and View portions. The second task can easily be achieved by the Controller. Alright, let's begin then. Assuming you're already working with a Griffon application then the following command should be enough to create the new group

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
We'll review each file in that order, starting with the Model. We said that a dialog of this kind might want to parameterize its tile and message, so let's add two properties to the model
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!
Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »