Tuesday, January 24, 2006
The more things change, the more they stay the same.
I have spent the past 7 months or so reacquainting myself with JAVA based development after a two year run building a .NET credit derivatives trading system in C#. Now back squarely in the JAVA world and working on a number of projects for several global trading organizations, some of my old gripes with Swing-based (and in general, JAVA-based) applications have come back to the fore. This time around I find myself motivated to use SWT for my GUI development, since, like the VM, SWT based applications are by definition more tightly coupled with the native platform so there is a better chance of producing good looking applications faster -- at least that is part of the promise of SWT over Swing.
Having read through much of the positioning statements on SWT vs. Swing readily available with a web search, I have found that one of the biggest challenges in SWT development is the lack of comprehensive, non-trivial coding examples that don't use JFace. This is completely understandable, given the relative maturity of SWT as compared with Swing. As JFace becomes more cleanly separated from Eclipse and hence more usable for standalone work this situation will improve dramatically I suspect. But for now, while I have looked into the design and implementation of JFace, for standalone development projects I am avoiding it ... for the time being.
This brings me to my point. I am working on an application framework for my own development projects, and I don't want to spend the time tweaking a Swing-based framework just to get it to look half decent. Partly too, I chose SWT to prove to myself that SWT is mature enough to use in sophisticated development efforts (other than Eclipse, mind you), or rule it out. Recently, I couldn't find a simple example that demonstrated how TreeItems could be selectively added and dynamically removed from an SWT Tree, presumably because of the following design principal of SWT:
A parent is set by passing it to the constructor of TreeItem and can't be modified from either end of the relationship. Because of the way these relationships are maintained, removing a single item from a Tree is awkward: You must call removeAll() to empty the Tree and rebuild its contents, minus the items you wish to remove. ... from SWT/JFace in Action
While this makes it easier for the implementers of the widget to ensure it's internal integrity, it makes it harder to use an SWT Tree in contexts where you dynamically want to add and remove nodes based on user control. Not impossible mind you, just harder. So, my contribution is a hopefully ever-growing list of snippets. The first example, Tree and TreeItem, creates a tree with a single root element in it, and by right-clicking, the user may insert new nodes before, after or into the selected node. The example does not allow deleting the root element, or giving it any siblings, so the first action selected must be to insert into the root node. From there, you can freely add and remove nodes.
The Tree example may be straightforwardly generalized to implement insert/remove functionality on subclasses of these SWT widgets, and thereby reuse this functionality across projects.

