jdom jtree : DefaultMutableTreeNode or DefaultTreeModel or both ?

P

Pavel

Greetings to all -

I'm working on presenting the JDOM Document object in the swing JTree
component. There is a need to add, remove and modify the elements of
the Document.

I've started by extending the DefaultTreeModel (actually, by
implementing TreeModel first) but then realized that creating jdom
Element wrapper class that extends DefaultMutableTreeNode might be a
better idea because most of the logic connecting TreeNode with Element
will be in one place (subclass of DefaultMutableTreeNode).

I think that similar code already exists or at least the best practice
is established. Any suggestions are greatly appreciated.

Thanks
Pavel

P.S. Maybe JDOMMutableTreeNode class that extends
DefaultMutableTreeNode (or just implements MutableTreeNode) would be a
nice complement to the JDOM package. Or maybe it already exists and I
just overlooked it.
 
P

Pavel

I believe that I came up with an implementation of the
DefaultMutableTreeNode that supports add, remove and modify
capabilities and works with DefaultTreeModel.

The children of the DefaultMutableTreeNode should be initialized in
the constructor with the values of the respective Element's children.

/**
* Creates new instance from the user object and children option
specified.
*/
public XmlElementNode(Object userObject, boolean allowsChildren) {
super(userObject, allowsChildren);
setChildren((Element)userObject);
}

/**
* Initialize the node's children with CWHCIXmlElementNode instances
created
* from the userObject's children
*
* @param userObject
*/
protected void setChildren(Element userObject) {
super.children = new Vector();
Iterator iterator = getElement().getChildren().iterator();
while (iterator.hasNext()) {
Element element = (Element)iterator.next();
CWHCIXmlElementNode node = new CWHCIXmlElementNode(element);
node.setParent(this);
super.children.add(node);
}
}

That takes care about most of the functionality.

Further, by overwriting the following methods, the respective
functionality is take care of:

setUserObject(Object) : changing the Element's value

insert(MutableTreeNode newChild, int childIndex) : adding new Element

removeFromParent()
remove(MutableTreeNode node)
remove(int index) : removing Element

If anybody wants the actual code I can send it via an e-mail.

Pavel

P.S. I was surprised to find out that protected variable 'children'
was used all over the place in DefaultMutableTreeNode instead of the
method getChildren() that does not even exist. My initial assumption
was that children() method would be used so overwriting it would do
the trick. W/out looking at the source code it would be quite
difficult to write the functioning subclass.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top