Groovy Console Bling-Bling - No Fluff Just Stuff

Groovy Console Bling-Bling

Posted by: Danno Ferrin on February 18, 2009

Groovy 1.6 is about to hit the inter-webs, so I thought it was an appropriate time for me to emerge from a twitter induced blogging hiatus. But what to blog about? How about one of the funner items I added to the new release. AST Transformation have changed the way I think of annotations, the bind node in SwingBuilder has brought my GUIs closer to MVC nirvana, and Grapes have been a tasty experiment. But when it comes to plain old gratuitous fun, output visualizations in the Groovy Console will help you see your results in a new light.

GroovyConsoleViz.png

Yes, you probably have seen something like this before, but it's such a neat Idea we had to copy re-implement it.

How does it work? Well, first and formost you have to turn it on. By default it is disabled, so it is less astonishing that way. It's in the menu in View->Visualize Script Results, and we remember what the last seting was when you exit as well. The built in transforms are fairly benign, if the script returns a java.awt.Image, a javax.swing.Icon, or a java.awt.Component that has no parent we display the object instead of the text. Everything else is still text. But that's not really interesting. It is also user extensible! If you have a file ~/.groovy/OutputTransforms.groovy the Groovy Console will insert an empty list named transforms, and you just need to add an appropriate closure into that variable and GroovyConsole will take the results and process those first.

The closure itself is fairly simple. The result of the script is passed in. If you want to transform it you return a non-null transformation. If you don't want to transform it you return null. And the output doesn't need to be a visual component either. You could make all numbers register as percentages:

transforms << { it -> if (it instanceof Number) "${it / 100}%" }

But visual results are much more fun. Here is a simple transform that will make all maps render in tables (it is also the transform that is displayed in this post):

transforms << {it ->
  if (it instanceof Map) {
    def jt = new javax.swing.JTable(
        it.collect{k, v->[k, v?.inspect()] as Object[]} as Object[][], 
        ['Key', 'Value'] as Object[])
    jt.preferredViewportSize = jt.preferredSize
    return new javax.swing.JScrollPane(jt)
  }
}

Don't forget to have fun!

Danno Ferrin

About Danno Ferrin

Danno Ferrin has been programming in Java since the Netscape 2.0 beta and started his career programming in NetDynamics. After surviving three different startups he is currently working as a government contracter at Intelligent Software Solutions. His day job involves writing Swing applications in Java, but on his own time he writes Swing applications in Groovy.

In the open source Java world he is an emeritus commiter at the Apache Tomcat and Ant projects and currently is a committer for the Groovy project and a Despot (Project lead) for the Griffon project.

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 »