May 23, 2006

More Monkey

I've written a couple more Eclipse Monkey scripts that I hope might be useful to others. They aren't meant as examples of anything clever using Monkey - for that, you'd be better looking elsewhere.

There are some bits of Java code editing typing that I see often, so, although they don't take long, I've written a couple of monkey scripts to help.

To install the scripts, install eclipse monkey, for each script, copy the script (including the "---" lines at the beginning and end), then from the "Monkey" menu in eclipse, select "Paste new script".

Convert method parameter declaration into method call parameter list

Converts:

String string, int integer, int[] nums, List list

into:

string, integer, nums, list

The script:

--- Came wiffling through the eclipsey wood ---
/*
 * Menu: Strips types out of parameter list
 Converts:
 	String foo, int x
 into: 
 	foo, x
 * Kudos: Ivan Moore
 * License: EPL
 */

function main() {
	systemClipboard = Packages.java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
	contents = systemClipboard.getContents(null);
	msg = contents.getTransferData(Packages.java.awt.datatransfer.DataFlavor.stringFlavor);
	parts = msg.split(',');
	result = "";
	for(partNumber in parts){
		part = parts[partNumber].trim();
		if(partNumber > 0){
			result += ", ";
		}
		result += part.split(" ")[1]
	}
	newContents  = new Packages.java.awt.datatransfer.StringSelection(result);
	systemClipboard.setContents(newContents, newContents);
}
--- And burbled as it ran! ---

Convert method parameter declaration into local variable declarations.

Converts:

String string, int integer, int[] nums, List list

into:

		String string = null;
		int integer = -1;
		int[] nums = null;
		List list = null;

The script:

--- Came wiffling through the eclipsey wood ---
/*
 * Menu: Paramater list INTO declarations
 Converts:
 	String foo, int x
 into: 
 	String foo = null;
	int x = -1;
 * Kudos: Ivan Moore
 * License: EPL
 */

function main() {
	systemClipboard = Packages.java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
	contents = systemClipboard.getContents(null);
	msg = contents.getTransferData(Packages.java.awt.datatransfer.DataFlavor.stringFlavor);
	parts = msg.split(',');
	declarations = "";
	for(partNumber in parts){
		if(partNumber > 0){
			declarations += "\t\t";
		}
		part = parts[partNumber].trim();
		declarations += part + " = " + suitableValue(part) + ";\n";
	}
	newContents  = new Packages.java.awt.datatransfer.StringSelection(declarations);
	systemClipboard.setContents(newContents, newContents);
}

function suitableValue(part){
	type = part.split(" ")[0];
	if(type == "long") return "-1L";
	if(type == "boolean") return "false";
	if(type == "int" || type == "short" || type == "byte") return "-1";
	if(type == "double") return "-1.0";
	if(type == "float") return "-1.0F";
	return "null";
}
--- And burbled as it ran! ---

How to run the scripts

Once monkey and the scripts are installed:

  • Copy the method parameters (e.g. String string, int integer, int[] nums, List list).
  • Select the appropriate monkey menu item (e.g. "Paramater list INTO declarations")
  • Paste, and you get the results as described above

What I'd like

Can you associate a keyboard shortcut to each monkey script? That would be really useful. The point of any script as tiny as these shown is to eliminate the jarring flow-destroying few seconds of manual editing. Therefore, to be of most use, they might be better available from a keyboard shortcut. Maybe.

If you know of a better way of doing these, then please comment.

Posted by ivan at 9:03 PM Copyright (c) 2004-2008 Ivan Moore | Comments (1)

May 8, 2006

Jar file viewing

I've come across a couple of teams where at least one person is missing an easy to use jar viewing tool.

For one of these people, working on windows, he renames somefile.jar to somefile.zip and uses the built-in compressed folder viewer. Someone else, working on a mac, extracts the jar somewhere to see the contents.

Existing solutions

I used to use WinZip for viewing jars, but the nagging to buy a license has driven me to look for an open source alternative - I now use 7-zip.

Writing a zip/jar viewer

I don't know why there aren't more zip/jar viewing tools available - it seems such a simple thing that I thought I'd have a go a writing one in a few evenings and seeing how it went.

Tech choices

To get something written quickly, I've chosen Python as the language, wxPython for the GUI, Boa Constructor as an IDE with GUI builder (for wxPython GUIs), and py2exe to produce a windows executable so that whatever is produced can be easily used from Windows (via "open with" or by association with a "file type").

Experiences from this mission

I tried Boa Constructor a few years ago. It's improved a lot - it's now really very good. It has code completion :-) If you want to put together a windows GUI quickly, it's free and works.

wxPython comes with demos - I found them great for working out how to do stuff. However, getting the (very simple) layout to work took much longer than I had expected.

I found it difficult to implement something satisfactory for "opening" a selected file from the zip/jar - ideas in a comment please.

py2exe is great - really simple to use. Unfortunately, the executables and associated files produced are often quite large - as in this case.

The finished product - simple and not great

It's available from my often unavailable server under an MIT style license. To produce an executable version of the zip/jar viewer, install python, wxPython and py2exe and run "python setup.py py2exe" - that'll produce a folder called "dist" that contains the executable "ZippApp.exe" and associated files. If you copy those files to another machine, the executable can then be run without having to install python, wxPython or anything else.

To specify the zip/jar to view it supports:
- command line argument - to make the executable easy to use from Windows, e.g. "open with"
- drag-and-drop a file onto it
- typing the file path and pressing enter in text input

Double clicking on a file in the list executes "start" with the file name - a simple way to get an aappropriate application opened. This is currently implemented rather badly though - I couldn't work out how to do it better in the limited time I've spent on it. The problems with the current implementation are: sometimes you want to choose something other than what "start" will do to your file, and the current implementation leaves temp files lying around even after you've exited the application, for which I can only appologize as that's really quite horrible.

This code is around 150 lines (including code generated by Boa Constructor and the py2exe script) - quite short, so please have a read and tell me of improvements.

I haven't added any "extract" or "add" behaviour - I've just done what I wanted in order to see what it takes to write a jar/zip viewer. The missing features are "just work" and not so interesting from a learning point of view. If you add them, please post a link to your improved version.

I hope this little example is enough of a taster to encourage you to try writing a desktop application using the technologies featured.

Comments

From Mark Jenner:

Hi Ivan,

I just came across this on the Server side and thought it might help you with your Jar viewing post. Haven't tried it myself, but it sounds interesting, especially for digging into the gubbins of a and ear file.

http://www.theserverside.com/news/thread.tss?thread_id=40514

Posted by ivan at 9:34 PM Copyright (c) 2004-2008 Ivan Moore | Comments (4)

May 1, 2006

ACCU 2006

ACCU 2006

Last week I went to the highly enjoyable ACCU conference in Oxford.

In Roger Orr's entertaining talk Producing Better Bugs: a satirical look at bugs in software he mentioned two sayings which I haven't heard before, that I really liked.

There's never enough time to do it right, but always enough time to do it twice.

(no attribution) and:

Prediction is very difficult, especially about the future.

from Niels Bohr.

Posted by ivan at 7:12 PM Copyright (c) 2004-2008 Ivan Moore