When a CTreeNode object is expanded for the first time, each plug-in gets the opportunity to add child nodes to the CTreeNode being expanded.
In the plug-in root, add a class called ConsoleTreeHandler. In our example, this would be com.MyPlugin.ConsoleTreeHandler. Add a method called “getTreeNodeChildren()” to this class. The source code should look something like the following example:
public static CTreeNode[] getTreeNodeChildren(CTreeNode node) { // strictly speaking, this check for null should never be // necessary if (node == null) return new CTreeNode[0]; if (node instanceof SomeSpecificTreeNode) { CTreeNode[] theChildren = new CTreeNode[5]; theChildren[0] = new ChildNode(); theChildren[1] = new AnotherChildNode(); // and so on... return theChildren; } else if (node instanceof SomeOtherTreeNode) { // different set of children here. } // and if we're not interested in any other types. return new CTreeNode[0]. }
Item of interest for this operation:
This method could be declared public Object[] getTreeNodeChildren(CTreeNode node) and it would still get called. The CTreeNode method that handles this checks the return value for null and also checks each item returned in the array to ensure that it is an instance of a CTreeNode object. Declaring the method as in the example enforces to the implementer of the plug-in that the items returned must be items derived from the CTreeNode class.
The only arrangement that currently is done is that child nodes that cannot have children are placed before the child nodes that can have children. Nodes from plug-ins are placed after the nodes that the parent node already knows about.