Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

Monday, May 16, 2011

Spock It Like You Mean It!

This post was just put up on the blog of one of our architects, Anatoly Polinsky.  Anatoly has a playful take on Spock, a Groovy testing framework, and specifically, abotu connecting Spock with Spring and DbUnit.


So here we go.. Yesterday night we hacked our way into The Ancient Database where besides the data about ancients themselves, we found incredible stats about many different living species of all the planets ancients traveled to.
So what do we do now? Well, we need a way to query/read this data. So we asked Sam to develop a reader ( ancients called it a ‘DAO’ ) to find a number of living species on each planet since the “beginning of time”.
So she did, and called this “species reader” a ‘SpeciesDao’.
We, of course, trust Sam. But in ancient technology, trust is always based on challenging the assumption by testing all the possible permutations. So we are calling Mr. Spock to help us out…
Sam of course used a well known ancient technique ( code name: Spring Framework ) to create a “species reader”, so Mr. Spock will use all the goodies of this technique to inject all the dependencies ( e.g. data source, a reader itself, etc.. ) for a test.

Read more...



Tuesday, December 28, 2010

Painless Java Desktop Application Development with Griffon, MigLayout, and IntelliJ

As Java consultants and developers, a very large percentage our projects are web applications.
We have become accustomed to depending on MVC web frameworks, and almost take for granted that at some level, we will be handling HTTPServletRequests, providing HTTPServletResponses, and deploying our applications to either a servlet container or a full J2EE server.

Every once in a while, however, the need arises to write an old-fashioned, desktop, thick-ui, non-web-centric application. If you have ever written a Java Swing application, you may have painful memories of dealing with things like BorderLayouts, GridBags, Event Listeners and Event Dispatching Threads, JFrames, and the like.

As a web developer, when you think about building your desktop application, you might start to think about how you will structure your application, and how you will build and package it. Will you have to re-package your code and all of your dependencies in one giant uber-jar?
Will you make it a web-start application? Fortunately, some folks have been thinking about, and working on these issues.

Griffon is a Groovy-based MVC framework for writing desktop applications. It is written by the Groovy Swing team, and is heavily influenced by Grails. If you have worked with Grails, then Griffon will be a very familiar environment. Griffon answers all of the above questions, and more.

Using the convention-over-configuration and command-line-driven scaffolding paradigms made popular by Ruby on Rails, Django, and Groovy on Grails, among others, Griffon generates the project structure, the model, view, and controller objects, and all of the configuration for you. You then customize these to produce your application.

Working in Groovy, of course, makes this a much more pleasurable experience, dispensing with the ceremony of Java development, and letting you write high-level, fairly terse, yet expressive code, and enabling you to accomplish quite a bit very quickly. The prime example of this in the Griffon framework is the SwingBuilder object - a Groovy Builder that provides a DSL for building Swing user interfaces. Views in Griffon are SwingBuilder scripts, and greatly simplify the task of creating Swing UIs.

Griffon uses the Gant build system (as does Grails), and by default, packages your application with all its dependencies as an uber-jar (with the correct manifest entries to make it 'executable'), as an applet, and as a java-webstart application.

When working with Swing, even with the SwingBuilder, one potentially still has to deal with the frustration of laying out the user interface by using a LayoutManager. GridBagLayout is flexible (too flexible?), but about as complicated as it can get. BorderLayout is fairly simple, but the simplicity comes at a cost - you typically wind up nesting lots of BorderLayouts in other BorderLayouts to get the desired behavior. Over the years, alternate layout managers have appeared, and some are quite useful, most notably the JGoodies Form Layout and RiverLayout. I tried MigLayout with my Griffon project, and found it to be very flexible and easy to use.

Here is a screenshot of the main UI of my application:
ServiceTester

As mentioned previously, the View is a Groovy SwingBuilder script. The first part of the script sets up an 'application' - the application's "MainFrame", which Griffon interprets according to the runtime environment - for a stand-alone application, it will build a JFrame, and for an applet it will build a JApplet:

package servicetester

import java.awt.FlowLayout
import net.miginfocom.swing.MigLayout

application(title: 'Service Tester', minimumSize: [297, 200], maximumSize: [297,200], location: [50, 50], pack: true, locationByPlatform: true) {
Note that minimumSize, maximumSize, location, etc. are set as properties here. In straight Java Swing code, these would have been at least one line of code each, and several would have involved building Dimension objects.

Next is setup of the actions and the menu bar that uses the actions, in this case, a simple 'File' menu with a 'Quit' menu item to exit the application.
Note the use of Groovy closures for associating the Action object with implementation of the action.

actions {

    action(id: 'exitAction',
            name: 'Quit',
            closure: { System.exit(1) },
            mnemonic: 'Q',
            accelerator: shortcut('Q'),
            shortDescription: 'Quit'
    )
  }

  menuBar(id: 'menuBar') {

    menu(text: 'File', mnemonic: 'F') {
      menuItem(exitAction)
    }

  }

I chose to use the MigLayout for the main panel of the view. There are lots of ways to initialize a MigLayout, I used the "fill" parameter to the constructor, which tells it to grow to occupy all available space, which makes sense here because it is the top-level component in the frame.
panel(border: emptyBorder(12), layout: new MigLayout('fill')) {

As you can see in the screenshot, there are three groupings of controls - the group of fields above the separator (the input fields), the group of fields below the separator (the output fields), and the "button bar" at the bottom (only one button). To achieve these groupings, I added two panels, each with MigLayout initialized with "fill", as with the main frame, and a third panel with a FlowLayout for the button bar.
These MigLayouts are given constraints of "growy" and "wrap":

panel(layout: new MigLayout('fill'), 
          constraints: "growy, wrap") {
"growy" means "grow vertically", and "wrap" means that the next component should be placed below, instead of to the right.
With this configuration, the MigLayout in the main frame gives the button bar it's preferred size, and resizes the other two panels as the main window is resized, giving equal weight to them.

The two MigLayout panels are very similar. Each has sets of label / input field pairs, which look like:
label(text: 'AMQ Broker URL:')
      textField(
              id: 'brokerUrl',
              columns: 40,
              text: bind(target: model, targetProperty: 'brokerUrl', value: model.brokerUrl),
              enabled: bind {model.enabled},
              constraints: 'wrap'
      )
The "text:bind" property binds the value entered in the textField to a property in the model - the value entered by the user is placed into the model.

One big "gotcha" here is that bi-directional binding is not supported. It is possible to bind in each direction, but not both directions at the same time. As one might expect, lots of people are asking for this capability, so I wouldn't be surprised to see it show up soon. I ran into this when trying to set the initial value of the field from the value in the model. The answer for this case is to use the "value" property as shown above - it gives the field it's initial value, and does not participate in the binding mechanism, so it doesn't present the same problem as bi-directional binding. If you do truly need bi-directional binding, at the moment, you'll have to roll your own.

Here is the full source code for this View.

Due to its highly-developed sense of structure and general lack of need for configuration, working on a Griffon application using the command line and a text editor is very straightforward. However, IDE support is always nice. Eclipse does not directly support Griffon, but does support Groovy. Both NetBeans and IntelliJ have direct support for Griffon projects. IntelliJ 9 and 10 have direct support for creating, importing, testing, running, and debugging Griffon applications, and I found these features to work quite well.

I found the combination of Groovy, Griffon, MigLayout, and IntelliJ to be an extremely productive environment for creating Swing applications, providing much of the application infrastructure, allowing me to dispense with ceremony and concentrate on writing the parts of the application that make it unique. Before embarking on a large or mission-critical Griffon project, however, you do need to be aware that Griffon is still in Beta (0.9.2-beta-3 was just released on Dec. 21st, 2010), and so is still in a bit of a state of flux.

If you are interested in reading about Griffon, there's documenatation at at the Griffon home page, and the Griffon In Action book is available from Manning.

Monday, January 4, 2010

Where is Java Headed?

Welcome to 2010!

As a way to start the new year, Joel Confino, one of our architects and future blog contributor, sat down for an interview to discuss his opinions on Java's future course. As you will hear in this interview, there are more options as Java is maturing.



We will be exploring some of these alternatives through 2010 on our blog, on our podcasts (Chariot Techcasts), and at our events, especially ETE 2010 (www.phillyemergingtech.com).

Wednesday, November 18, 2009

Groovy and Grails seminar - G2 and Springsource Tool Suite

Here is another interview from our Groovy and Grails seminar. Brent Baxter had been conducting some research into G2 and the Springsource Tool Suite (STS). I asked Brent how they worked together and if this was advantageous to a developer.



As you can see here, we certainly try to gather reflections from our attendees when we have them "captive" at a seminar or conference. People are beginning to run in the other direction when they see me with the Chariot video camera in my hand...

Tuesday, November 17, 2009

Wrap up from our Grails/Groovy Seminar

Last week Chariot Solutions hosted a one-day seminar around Groovy and Grails. Or Grails and Groovy if that is your preference.

Chariot has been trying to have two events a year, the larger Emerging Technologies for the Enterprise in the spring, and a smaller event in the fall. We have tried to use the fall event as a way to see how much support there is in the development community for newer technologies. We did this with Spring, Ruby, Cloud, SOA and now Groovy. We found the Groovy and Grails community to be very vocal and enthusiastic.

Over the next week, we will be posting videos from the seminar to give you a better understanding of why Chariot will be paying a little more attention to this framework and language.

Thursday, October 22, 2009

SpringOne 2GX - More reporting, this time on Groovy and Grails

We were able to conduct a short interview with Graeme Rocher and Jeff Brown, Grails leads, at SpringOne 2GX. They review some of the new features that will be available in Grails 1.2.



And if you are looking to learn more about Grails:
http://grails.org/Developer+-+Groovy+Server+Pages

Monday, October 19, 2009

Here we are at SpringOne/2GX

The opening night festivities have kicked off this year's SpringOne and 2GX (Groovy/Grails). The keynote is happening right now and we will be giving a recap during the day tomorrow.

While we are here, we hope to also interview (video or audio) a some of the committers we know to get an update on their projects. If you have anything you would like us to ask and who you might want us to interview, please post a comment.

Our team who will be attending session over the next three days will be giving their thoughts on what they have found to be most valuable to them.