Posts Tagged swing

From Java to Groovy

Recently I’ve been playing with some old Java Swing applications and I found myself wishing that I could replace all the anonymous inner-classes with some clean Groovy closures. The job of converting a java source file to a groovy source file is pretty simple and eminently scriptable. Here’s my take on a python script to do just that:

from optparse import OptionParser
from subprocess import Popen, PIPE
import os

def rename(src, dest, use_svn):
    if use_svn:
        output = Popen(['svn', 'rename', src, dest], stdout=PIPE).communicate()
        print output[0]
    else:
        print 'mv', src, dest
        os.rename(src, dest)

def process_file(src, use_svn):
    if src.endswith('.java'):
        print 'Processing', src
        fd = open(src, 'r')
        lines = fd.readlines()
        fd.close()
        fd = open(src, 'w')
        for line in lines:
            text = line.rstrip()
            if text.endswith(';'):
                text = text[:-1]
            fd.write(text)
            fd.write('\n')
        fd.close()
        root = os.path.dirname(src)
        filename = os.path.basename(src)
        dest = os.path.join(root, filename[:-5] + '.groovy')
        rename(src, dest, use_svn)

def process_dir(dir_path, use_svn):
    for root, dirs, files in os.walk(dir_path):
        for filename in files:
            src = os.path.join(root, filename)
            process_file(src, use_svn)

if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option('-s', '--svn', action='store_true', dest='use_svn', help='use svn to rename files')
    (options, args) = parser.parse_args()
    if len(args) > 0:
        target = args[0]
        if os.path.isfile(target):
            process_file(target, options.use_svn)
        else:
            process_dir(target, options.use_svn)
    else:
        process_dir(os.getcwd(), options.use_svn)

This means that I can now replace this:

public void actionPerformed(ActionEvent e) {
    executor.execute(new Runnable() {
        public void run() {
            controller.showBuckets();
        }
    });
}

with this:

public void actionPerformed(ActionEvent e) {
    executor.execute { controller.showBuckets() }
}

Now that’s refactoring ;-)

Tags: , , , ,

S3DropBox 1.2 is now released

I love feedback … including bugs (thanks Cam). The S3DropBox will now only permit the creation of a bucket when Amazon S3 returns a HTTP 404 status code in response to a HTTP HEAD request for the bucket prior to creation. The new version can now be downloaded from http://code.google.com/p/s3dropbox/.

Tags: , , , , , , ,

S3DropBox 1.1 has been released

This release now permits the S3DropBox to be used in corporate environments (ie. behind authenticated proxies), including being able to handle NTLM authentication with proxies. HTTPS support has been enabled, although HTTP is still used by default. Check out the new release here.

Tags: , , , , , , ,