Geertjan's Blog

  • May 23, 2010

Best Lead In to Explain NetBeans FilterNode

Geertjan Wielenga
Product Manager
I'm working through some edits for the "Node and Explorer View" chapter of the upcoming NetBeans Platform 6.9 book by Jurgen Petri. I quickly want to share why I think this book will be so great, by means of a text I've just been working on, which is (to my mind) the best lead in to a discussion on org.openide.nodes.FilterNode that I have ever read:

"There may be situations where you are not really satisfied by an existing node hierarchy. You may want to construct a new node hierarchy, based on the original nodes, but with different behavior. For example, you may want to provide different icons or filter out child nodes, while wanting to keep the rest of the behavior of the original node.

Initially, you might attempt to expand the existing node, or the node's children, overriding the relevant methods to obtain the required behavior. In the first place, this approach would be cumbersome and inflexible, given that a flexible alternative exists. Secondly, however, this approach might not be possible in all cases. Think, for example, of nodes that are defined in modules over which you have no control.

The decorator pattern, familiar from other areas of Java, provides a useful approach, letting you create a filter for a node that does not quite meet your requirements. In dealing with Java streams, for example, you have a BufferedInputStream, which is an InputStream that owns another InputStream, the first InputStream delegating the actual reading to the second InputStream. The first InputStream implements additional behavior, namely the buffering of the data, reducing the number of real read activities. Thus, the additional behavior is added to the decorated InputStream at runtime, dynamically. Its behavior is changed, without requiring a subclass. In the same way, you can decorate existing nodes and children to change their behavior."

The whole book is full of helpful explanations such as the above. Pretty cool, isn't it!

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.