jdom jtree : DefaultMutableTreeNode or DefaultTreeModel or both ?

Discussion in 'Java' started by Pavel, Jul 25, 2003.

  1. Pavel

    Pavel Guest

    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.
    Pavel, Jul 25, 2003
    #1
    1. Advertising

  2. Pavel

    Pavel Guest

    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.


    (Pavel) wrote in message news:<>...
    > 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.
    Pavel, Jul 25, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wendy S
    Replies:
    1
    Views:
    6,351
    Darren Davison
    Aug 5, 2003
  2. Bernd Oninger
    Replies:
    4
    Views:
    12,247
    GIMME
    Jun 21, 2004
  3. Arun Hallan

    JDom -- > JTree

    Arun Hallan, Dec 22, 2004, in forum: Java
    Replies:
    2
    Views:
    853
    The Abrasive Sponge
    Dec 23, 2004
  4. Tinker
    Replies:
    4
    Views:
    5,286
    Harry Bosch
    Oct 9, 2005
  5. Bernd Oninger
    Replies:
    3
    Views:
    2,871
    GIMME
    Jun 21, 2004
Loading...

Share This Page