Wednesday, January 7, 2009

Ant and gui components ( Groovy Swing )

What to do if you want to extend ant scripts functionality with some gui features i.e. wizards for the beginners etc. ? I was facing the same problem - I needed some quick and nice solution so my choice was Swing and Java, well not directly - it's Groovy and SwingBuilder. It's even easier :)

Two things, we need to create our component in Groovy and integrate it with Ant's environment.

Component's key parts and structure ( Groovy ):

Some explanations:

We need to add the code preventing from the thread termination due to Ant policies. Ant executes target and ends its work by terminating its thread and all sub threads.

isAlive - flag which indicates whether component is alive or not



class Configurator extends SoftwareBuild {

boolean isAlive = true
SwingBuilder swing = null
JFrame frame = null

public void init(AntBuilder providerAntBuilder) {
if (swing == null || frame == null) {
swing = new SwingBuilder()
frame = swing.frame()
}
super.init(providerAntBuilder);
}


public void start() {

frame = swing.frame(title: 'Vine configurator', size: [800, 600],
defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE,
addShutdownHook { isAlive = false}) {

}

frame.show()

//To keep this tread alive - prevents from being closed by ant
while (isAlive) {
sleep(1000)
}

}

Invocation of our component from the ant script level ( Ant's build.xml ):




<target name="gui" description="Displays gui for Vine configuration">
<groovy>

ant.gui.Configurator configurator = new ant.gui.Configurator()
try {
// from the level of Ant's build script we're passing a reference to the ant environment
configurator.init(ant)
// displaying our swing component
configurator.start()
} catch (Exception e) {
e.printStackTrace()
}

</groovy>
</target>


Ant the effect of this work is ( based on the example from the Vine Toolkit ):



No comments:

Post a Comment