Desktop Development - The Fun Way

Tutorial - Griffon: Building Desktop Applications with Groovy

Building desktop applications can be an enjoyable experience if you’re willing to throw in a bit of Groovy into the mix. Griffon is an application framework that follows the spirit of Grails to bring back the fun to desktop development.

Desktop application development, a term that isn’t heard much these days as web development nor concurrency and parallelism are. However that doesn’t mean it’s dead as some proclaim. There are indeed some industry sectors where a desktop application is the top alternative for solving a particular problem; in some other environments it’s the only choice for security reasons. Think of financial institutes, banks, the health industry, biological research, chemical laboratories, satellite operations and the military; just to name a few. All of them impose a particular set of restrictions where desktop applications excel over web applications, such as security, access to local resources, devices and port communications. The other thing that they have in common is Griffon. Yes, the Griffon framework has helped teams in all those industries and spaces to get the job done.

You may have heard of Griffon before but still wonder what it is. In short, it’s a desktop application platform for the JVM. It has strong roots in the Groovy community as the project is the brain child of the Groovy Swing team: Danno Ferrin, James Williams and myself. That being said, you may excuse me if I get a bit excited explaining some of Griffon’s features, as I’m very fond of the project. A key driving force behind the framework’s design is that it should be easy to pick up by Java developers. It should also enable quick coding cycles while keeping the source nice and tidy. Finally, the productivity gains and the fun factor must be immediately perceived.

For these reasons the team decided to follow in the steps of the Grails framework and its community. There are a lot of similarities between both frameworks. For instance, both have a command line interface that helps you get around with the usual tasks of creating, building, packaging and deploying applications. Both frameworks leverage the Groovy language as a glue for their respective software stacks. The tool integration is also quite good, as major IDEs and popular text editors offer good support for dealing with this kind of projects.

But enough of the theory, let’s get some practice! The rest of this article will be devoted to building a simple address book application. We will definitely not build a full-fledged application in the few pages that we have to spare but it’s my hope that all the things to be discussed will give you enough pointers to get you going with the framework.

Setup and Configuration

The first step is to download and configure Griffon on your computer; there are several choices for doing so. If you pick the universal installer from the download page it will unpack the binaries and configure the path environments for you, particular on Windows platforms. Or if you’re on Linux machines you can give it a try to either the RPM or Debian-based packages. ZIP or TGZ packages may work as your last resort. Simply download the package, uncompress it on a directory of your choosing – preferably one without spaces. Next configure an environment variable GRIFFON_HOME pointing to the directory where the Griffon binary distribution was unpacked. Lastly make sure that the PATH environment variable contains a reference to GRIFFON_HOME/bin. If all goes well, invoking griffon command with the --version flag turned on should display a similar output as the following one

$ griffon --version

---------------------------------------------

Griffon 0.9.5

---------------------------------------------

Build: 15-Mar-2012 12:56 PM

Groovy: 1.8.6

Ant: 1.8.2

Slf4j: 1.6.4

Spring: 3.1.0.RELEASE

JVM: 1.6.0_29 (Apple Inc. 20.4-b02-402)

OS: Mac OS X 10.6.8 x86_64

 

Alright. Time to get down to business...

Initial Steps 

First things first, how do we create an application? Typically you may choose the Maven-based approach and select an appropriate archetype to bootstrap a project. Or you just can simply create a new directory, fetch some Ant scripts and be done with it. Or let your trusty IDE decide. Choices, choices, choices. The griffon command line tool is here to help. Every Griffon application starts in the same way, by invoking the following command

$ griffon create-app addressbook

$ cd addressbook

You’ll notice a flurry of lines in the output. Go ahead and inspect the contents of the newly created application if you want. The create-app command initializes the application by creating several directories and some files. One of these directories is of particular importance, its name is griffon-app. Within this directory you’ll find another set of directories that help keeping the source code organized. Figure 1 shows the expanded content of the griffon-app directory as created moments ago.

Figure 1:  Contents of the address book application

As you can appreciate, Griffon makes use of the MVC pattern to arrange the elements that comprise an application. When an application is created, you’ll also get an initial MVC group whose name matches the name of the application. Inside each MVC member there’s enough code to make the application run. Yes, believe it or not, the application is ready to be launched. Go back to the console and execute the following command

$ griffon run-app

This should compile the application sources, package resources, roundup dependencies and launch the application. In a matter of seconds you should see a window popping up, like the one shown in Figure 2.

Figure 2: Addressbook application running for the first time

Granted, it doesn’t loom like much, but we haven’t written a single line of code yet! Listing 1 shows what you’ll find inside the file griffon-app/views/addressbook/AddressbookView.groovy when you open it.

Listing 1 - AddressbookView

 

package addressbook
application(title: 'addressbook',
  preferredSize: [320, 240],
  pack: true,
  //location: [50,50], 
  locationByPlatform:true,
  iconImage: imageIcon('/griffon-icon-48x48.png').image,
  iconImages: [imageIcon('/griffon-icon-48x48.png').image,
               imageIcon('/griffon-icon-32x32.png').image,
               imageIcon('/griffon-icon-16x16.png').image]) {
    // add content here
    label('Content Goes Here') // delete me
}

 

What we see here is a Swing-based Domain Specific Language (or DSL for short) based on a popular Groovy feature: builders. In our particular case we’re dealing with SwingBuilder. Builders are but a collection of nodes and rules that know how to build hierarchical structures. It so happens that a Swing UI is comprised of a tree of components. In the View we can observe a top level node named “application” and some properties being applied to it. Next we see a child node named “label” with a single text entry. You may recognize the code structure with what’s shown by Figure 2. That’s the power of the Swing DSL. The code and the UI resemble each other a lot, it’s quite easy to follow how components are structured when reading the DSL.

           

Pages

Andres Almiray
Andres Almiray

What do you think?

JAX Magazine - 2014 - 03 Exclucively for iPad users JAX Magazine on Android

Comments

Latest opinions