October 25, 2011

Grails Goodness: Use a Different jQuery UI Theme with Resources Plugin

The resources plugin is a great way to manage resources in our Grails application. We define our resources like Javascript and CSS files with a simple DSL. The plugin will package the resources in the most efficient way for us in the final application.

The jQuery UI library has support for theming. We can use the default theme(s), but we can also create our own custom theme with for example the jQuery UI ThemeRoller site.

If we use the jQuery UI plugin and want to use a different than the default theme we must change our configuration for the resources plugin. We override the theme that is set by default and point it to our new custom theme. We can change grails-app/conf/Config.groovy or a separate resources artifact file. We add an overrides section and use the same id attribute value as set by the jQuery UI plugin. The url attribute points to the location of the custom jQuery UI ThemeRoller CSS file.

// File: grails-app/conf/Config.groovy
grails.resources.modules = {
    core {
        dependsOn 'jquery-ui'
    // Define reference to custom jQuery UI theme
    overrides {
        'jquery-theme' {
            resource id: 'theme', url: '/css/custom-theme/jquery-ui.custom.css'

October 24, 2011

Groovy Goodness: Customize Groovy Console Visual Output

The Groovy Console ($ groovyConsole) is a great tool to run Groovy scripts and experiment with Groovy. Normally when we run a script the return result (if not null) is shown as a string value. We can customize the way the value is shown by creating a new file with the name OutputTransforms.groovy in our GROOVY_HOME directory. Normally this directory is located in your user directory with the name .groovy.

In OutputTransforms.groovy we must fill the internal script variable transforms of type ArrayList with a closure where we do the actual customization of the return result. The closure has one parameter which is the return result of the script. We can check for example the type and then transform the result to a different output result.

The Groovy Console already contains some transformations for script results of type java.awt.Image, javax.swing.Icon and java.awt.Component with no parent. To see the customized visual output we must make sure we enable this in the View menu and select Visualize Script Results.

The following sample code show that we return a String result that starts with <html> and ends with </html> as a formatted JLabel. Other String values are prepended with the default text Groovy Console says:.

// File: ~/.groovy/OutputTransforms.groovy
import javax.swing.JLabel
import javax.swing.ImageIcion

transforms << { result ->
    if (result instanceof String) {
        if (result ==~ /<html>.*<\/html>/) {
            return new JLabel(result)
        } else if (result == 'mrhaki') {
            return new ImageIcon('/Users/mrhaki/Pictures/blog/haki-logo-black-64.png')
        } else {
            return "Groovy Console says: $result"

October 20, 2011

Groovy Goodness: Format Dates with TimeZone

Since Groovy 1.8.3 we can use an extra TimeZone parameter with the format() method of the Date class. This can be used to print a date/time for a particular timezone.

import static java.util.Calendar.*

def timeZone = TimeZone.getTimeZone('Europe/Amsterdam')
def otherTimeZone = TimeZone.getTimeZone('Australia/Canberra')

def cal = Calendar.instance
cal.set(year: 2011, month: OCTOBER, date: 20, hourOfDay: 12, minute: 30)

def date = cal.time
def dateFormat = 'yyyy/MM/dd HH:mm'

assert date.format(dateFormat, timeZone) == '2011/10/20 12:30'
assert date.format(dateFormat, otherTimeZone) == '2011/10/20 21:30'

October 19, 2011

Groovy Goodness: Default Groovy Script File Extensions

When we run a Groovy script file from the command line with the groovy command we can use the complete filename to refer to the script file. But we can also leave out the filename extension. By default the following extensions are used to search for the Groovy script file:

  • .groovy
  • .gvy
  • .gy
  • .gsh

So suppose we have a Groovy script file named sample.gsh we can use the following commands to run the file:

$ groovy sample.gsh
$ groovy sample

(With this post we hit Groovy Goodness post 250. ;-) )

October 18, 2011

Groovy Goodness: Run Remote Scripts via URL

Since Groovy 1.8.3 we can run remote Groovy scripts. We can use an URL to refer to the Groovy script that we want to execute. This can be very useful to build a library of Groovy scripts and publish them on a web server or a code repository like Github. Then we can run those scripts by referring the scripts by URL.

// File:
// http://www.mrhaki.com/samples/remotesample.groovy

printMessage 'Remote Groovy script @ http://www.mrhaki.com/samples/remotesample.groovy'

['Groovy', 'rocks'].each {
    print "${it.toUpperCase()} "
println '!'

def printMessage(message) {
    def LINE_LENGTH = message.size() + 4
    println '*' * LINE_LENGTH
    println "* $message *"
    println '*' * LINE_LENGTH

On a command line we run $ groovy http://www.mrhaki.com/samples/remotesample.groovy and we get the following output:

$ groovy http://www.mrhaki.com/samples/remotesample.groovy
* Remote Groovy script @ http://www.mrhaki.com/samples/remotesample.groovy *