[antlr-interest] On trees and JavaBeans, part 2: tree creation
scott at javadude.com
Tue Apr 19 20:59:50 PDT 2005
I should mention regarding performance, the Carrier/Payload approach has
exactly the same execution overhead as the ASTModel approach. Both involve
one added level of indirection from the real data, causing an extra method
call when asking for children, data, whatever...
See http://javadude.com/misc/treesample.zip for an example of how JTree was
really intended to be used.
The example has a gui (don't bother looking at its code -- it was a quick
hack converted from an old VAJ sample I had). The app simply looks at that
UI as "an existing tree data structure", and presents its data on the screen
in a Jtree. (It ends up showing the layout structure of the UI).
The examples adapt it in to ways:
1) Using a TreeModel to adapt the entire tree at once. This requires one
instance for adaptation, and if you look at the code, it's pretty simple.
2) Using a TreeNode to adapt every component in the UI. This doubles the
number of objects in play, and if you look at the code, it's slightly more
complex (though not much) than the TreeModel approach. Every time we ask for
children they must be wrapped in a new adapter. (We could cache the
instances to reuse them, but that adds significant complexity to the app,
taking it even further from the simple #1.)
These examples are *exactly* what I'm talking about. #2 is the current
Carrier approach. The payload is the component data (we don't copy it), and
the carriers are all of the ComponentTreeNode instances.
#1 is the approach I'm recommending. Just by having the TreeModel interface,
I can create one adapter that manages the entire tree.
Suppose I want to write a tree parser that analyzes a GUI like the one I'm
presenting. Perhaps it wants to look at all components to be sure they have
an assigned name, and that containers are all using layout managers. We can
use either of the above approaches to do so.
Ter -- this is an example where ANTLR isn't creating the tree, but I want to
walk it. It's slightly contrived, but it's the type of concept that I really
need ANTLR for. (The prime example for me is the eclipse-generated Java
More information about the antlr-interest