A while back, I started playing around with Google App Engine (Java) and the lightweight, Groovy , Gaelyk. There’s also a great library written for using the low level datastore in App Engine called Objectify.
I decided to write a little Groovified wrapper for Objectify (which already does most of the work) and turn it into a plugin for Gaelyk. It adds a little syntactic sugar, and although it doesn’t have everything, it is working nicely for me so far. Check out the Obgaektify site to learn more.Archive for the ‘Groovy’ Category
Directory Recursing Thumbnail Generator
Put this script in a particular directory (or change the directory specified in startDir variable
and it will recurse through all sub-directories, creating thumbnails of the images (jpg, png, gif, or bmp).
If you change the deleteOriginal variable to true, it will delete the original image for you afterwards.
import javax.imageio.*
import java.awt.*;
import java.awt.image.*;
def startDir = new File(".")
def deleteOriginal = false
def thumbWidth = 200
def thumbHeight = 150
startDir.eachFileRecurse{file->
if(file.name =~ /\.(JPG|jpg|PNG|png|GIF|gif|BMP|bmp)$/)
{
def imgType = file.name.replaceAll(/[^\.]*\.(JPG|jpg|PNG|png|GIF|gif|BMP|bmp)$/, "\$1")
def imgPath = file.getCanonicalPath().replaceAll(/\.(JPG|jpg|PNG|png|GIF|gif|BMP|bmp)$/, "_Thumb.\$1")
println("$imgType, $imgPath")
//load each image
img = ImageIO.read(file)
println (file.name + " " + img.width + "x" + img.height)
//output scaled thumbnail image
width = thumbWidth
height = thumbHeight
if(img.width < img.height){width = thumbHeight; height = thumbWidth;}
BufferedImage thumbImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D gThumbImg = thumbImg.createGraphics();
gThumbImg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
gThumbImg.drawImage(img, 0, 0, width, height, null);
ImageIO.write(thumbImg, "$imgType", new File(imgPath))
if(deleteOriginal)
{
file.delete()
}
}
}
Unicode Files in Groovy
It took a little while for me to track down how to read and write Unicode-encoded files using Groovy. Here’s the answer:
/*
Given a file called test.txt that's filled with text in UTF-8 format,
here's how to use Groovy to read it in, preserve the encoding, and
output it to a file called testout.txt, also in UTF-8 formatting.
*/
def inF = new File("test.txt")
def reader = inF.newReader("UTF-8")
def outF = new File("testout.txt")
outF.withWriter("UTF-8"){writer->
reader.eachLine{line->
writer.write(line)
}
}
Dynamic Swing
//Groovy script that demonstrates dynamically adding and removing text fields from a Swing form
import groovy.swing.SwingBuilder
swing = new SwingBuilder()
addButton = swing.button(text: 'Add Field', actionPerformed: {addField()})
remButton = swing.button(text: 'Remove Field', actionPerformed: {removeField()})
frame = swing.frame(title:"Test Dynamic Fields", size: [200,400], defaultCloseOperation:javax.swing.WindowConstants.EXIT_ON_CLOSE)
{
panel = panel()
{
vbox = vbox(id:'vbox')
{
hbox()
{
widget(addButton)
widget(remButton)
}
}
}
}
fields = []
fields << swing.textField(text: fields.size())
vbox.add(fields[0])
//frame.pack()
frame.show()
def addField()
{
field = swing.textField(text: fields.size())
fields << field
vbox.add(field)
vbox.revalidate()
}
def removeField()
{
if(fields.size() > 0)
{
field = fields[-1]
vbox.remove(field)
fields.remove(fields.size() - 1)
}
vbox.revalidate()
}





