Tuesday, January 23, 2007

Build Faster with This Turnkey SWT Project Template

The Eclipse Standard Widget Toolkit (SWT) is growing in popularity, as evidenced by its use in applications like Azureus and RSSOwl. In this 10-minute solution, you will learn how to make use of an SWT project template that can greatly reduce the time required to build small and medium-sized applications. In particular, the template offers the following features:


* A UI that Works Out of the Box—The project already has a MainWindow class that comes complete with a menubar, toolbar, client area, status bar, and about dialog. The Open toolbar button and File menu item have code that opens a file dialog box, and the File | Exit menu option closes the app. (See Figure 1.)

Figure 1. The basic template application, awaiting your modifications, is shown.
  • Executables—The project has an Ant task that makes it trivial to build executable JARs, Windows .exe's, and Windows installers.
  • Better Help—Context-sensitive SWT Javadocs are integrated into the template.
  • Preferences—The MainWindow saves its window size and location in a preferences file that your application can use.
  • TextFile—The project includes a class that makes reading and writing text files trivial.
  • Cursors—A default (arrow) cursor and a waiting (hourglass) cursor have been added to the MainWindow class.
  • Portable—You will be able to take your project to other computers and it will just work because the swt.jar and associated .dll are included in the project.

To give you an idea of the time savings you can expect to see using the template, consider a utility that I wrote recently. The application allows the user to select a directory for source code analysis. The application then parses all source code files in the directory and its sub-tree, and compiles information on the number of lines of code, comments, and white space for each file. From the time I began working on the application, to the time that the application was being installed on a colleague's computer, 50 minutes had passed. Of this, just under 40 minutes were spent on the tree-walking and source code parsing logic. Only 10 minutes were spent working on the user interface, building the executables, and creating an installer—tasks that could easily have taken hours to complete without the template.

The specific environment used to develop the SWT Project Template was Eclipse 3.2 (which includes Ant), JDK 6.0 and Windows XP (however, the basic steps can be adapted for use on *NIX and MacOS systems). Optional, but recommended, components include Jigloo GUI Builder 3.9, Launch4j 3.0, and NSIS 2.0.