Pages

Monday, 7 September 2009

Taking Boiler Plate out of Examples

I was reviewing the GeoTools example code today with Michael Bedward - preparing for an a tutorial at FOSS4G this year.

One of the things that always comes up in example code; is the requirement to "show" the result. As a result a the majority of our example code was actually examples of how create quick and dirty swing user interfaces.

So our day was spent stripping out boilerplate user interface code to a jar appropriately tilted "gt-swing" so that examples can stay focused on being examples.

Here is a small example of a wizard page for connecting to a shape file:


Here is before:


File file;


JFileChooser chooser = new JFileChooser();

chooser.setDialogTitle("Open Shapefile for Reprojection");

chooser.setFileFilter(new FileFilter() {

public boolean accept(File f) {

return f.isDirectory() || f.getPath().endsWith("shp")

|| f.getPath().endsWith("SHP");

}

public String getDescription() {

return "Shapefiles";

}

});

int returnVal = chooser.showOpenDialog(null);

if (returnVal != JFileChooser.APPROVE_OPTION) {

System.exit(0);

}

file = chooser.getSelectedFile();


And here is after:


File file = JFileDataStoreChooser.showOpenFile("shp", parent );


The other thing that has happened in cleaning up examples is a real focus on the style interfaces. Specifically going through and making sure all the new concepts introduced are accessible, and ensuring that deprecated methods are not used in example code.

Documentation harmed in the making of todays blog:
Post a Comment