[jdom-interest] Has any one done a JDOM <> JTreeModel adaptor

Richard Cook rpc at prismtechnologies.com
Wed May 23 07:05:22 PDT 2001


I'm not sure why you want to display the whole document as a tree. Maybe a
JtreeTable or similar would be better with attrs in the table. For mixed
content, you could provide some view which would depend on the application
(show the raw structure, simple concatenation, convert to html, ...)

getChild() doesn't have to return a JDOMNode() to give a readable String,
you might find you create new JDOMNodes each time you expand a node
(consider expand/contract/expand same node), so you might provide some
variation on singleton if this was the case, or you can just use a different
cell renderer to display the correct text and return the Element from
getChild

class ElementRenderer extends javax.swing.JLabel implements
javax.swing.tree.TreeCellRenderer {
   public java.awt.Component getTreeCellRendererComponent(
      javax.swing.JTree tree,   Object value,   boolean selected,
      boolean expanded,   boolean leaf,   int row,   boolean hasFocus)
   {
         setText(((Element) value).getName());
         return this;
   }
}

then
      /* JTree tree */
      tree.setCellRenderer(new ElementRenderer());

I'd consider using a real adapter to give a more ideal interface to the JDOM
document to make the treemodel or whatever easier to write.

If only someone was working on V2 of a Java XML book with a Jtree example
running through it.


-----Original Message-----
From: jdom-interest-admin at jdom.org [mailto:jdom-interest-admin at jdom.org]On
Behalf Of Matthew MacKenzie
Sent: Tuesday, May 22, 2001 9:36 PM
To: Richard Cook; adam flinton
Cc: JDOM-Interest
Subject: RE: [jdom-interest] Has any one done a JDOM <> JTreeModel adaptor

Richard,

How about something along these lines:

 public Object getChild(Object parent, int index) {
      int atts = ((Element)parent).getAttributes().size();
      int mcon = ((Element)parent).getMixedContent().size();
      if (index <= atts) {
          Attribute att =
(Attribute)((Element)parent).getAttributes().get(index);
          return new JDOMNode(att);
      }
      else {
          return new
JDOMNode(((Element)parent).getMixedContent().get(index-atts));
      }
   }
   public int getChildCount(Object parent) {
       int atts = ((Element)parent).getAttributes().size();
       int mcon = ((Element)parent).getMixedContent().size();
      return atts + mcon;
   }


To properly display a whole XML tree, I need to tree the attributes as
children, which means they have to be figured in to getChildCount.  I also
need to handle CDATA, Entity, Comment, etceteras..which necessitates
creating a JDOMNode that can properly wrap all of the objects (Attribute,
DocType, etc.) and return a human readable toString().  The other problem,
which I am not sure exists in JDOM, is the occurence of multiple text nodes
(String objects in JDOM).  It looks stupid in a tree to have them all
seperated:

element: Root
  element: Kid
    #attr: id=1
    #text: fubar
    #cdata: <h1>hello</h1>
    #text: was his name.

This is all do-able, and the JDOMNode idea is solid...I just read an example
of doing this very thing using a DOM node, the only difference is that they
didn't support attributes.

Is it against the XML spec to include attribute objects in
getMixedContent()? I think that would be very useful.


-Matt

-----Original Message-----
From: Richard Cook [mailto:rpc at prismtechnologies.com]
Sent: Tuesday, May 22, 2001 11:25 AM
To: Matthew MacKenzie; adam flinton
Subject: RE: [jdom-interest] Has any one done a JDOM <> JTreeModel
adaptor


Expect someone has done it properly. I posted this a few weeks back. The
tree model asks for children one by one so you can return what you want in
getChild(). I suspect there is lots of inefficiency because of the partial
lists built in getChildren().


Depends on what you need, but say you want to display the elements could you
not just implement TreeModel:

class JDOMTreeModel implements TreeModel {
   private Element root;
   public JDOMTreeModel(Element root) {
      this.root = root;
   }
   public Object getChild(Object parent, int index) {
      return ((Element) parent).getChildren().get(index);
   }
   public int getChildCount(Object parent) {
      return ((Element) parent).getChildren().size();
   }
   public int getIndexOfChild(Object parent, Object child) {
      return ((Element) parent).getChildren().indexOf(child);
   }
   public Object getRoot() {
      return root;
   }
   public boolean isLeaf(Object node) {
      return false; // whatever
   }
   // etc
}

then TreeModel mdl = new JDOMTreeModel(doc.getRootElement());
     JTree tree = new JTree(mdl)


saves serialising the JDOM tree or having to walk it. You can change the
cell renderer to display the element name rather than the toString(), or
wrap the "Element" in getChild() if necessary [ return new MyNode(((Element)
parent).getChildren().get(index)) ]. If you need attrs there'd be a bit more
work.

-----Original Message-----
From: jdom-interest-admin at jdom.org [mailto:jdom-interest-admin at jdom.org]On
Behalf Of Kiss Gábor
Sent: Thursday, May 03, 2001 8:11 AM
To: jdom-interest at jdom.org
Subject: [jdom-interest] jdom tree conversion to a JTree


How can I convert a jdom tree to a JTree?

Thanks

Kiss Gábor

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com




-----Original Message-----
From: jdom-interest-admin at jdom.org [mailto:jdom-interest-admin at jdom.org]On
Behalf Of Matthew MacKenzie
Sent: Tuesday, May 22, 2001 6:48 PM
To: adam flinton; JDOM Mailing List (E-mail)
Subject: RE: [jdom-interest] Has any one done a JDOM <> JTreeModel adaptor

I wrote the JTreeOutputter for simple display purposes, but lately I have
found the need to keep a JDOM in sync with the tree contents.  I have never
wrote a JTreeModel, is it hard?  I might be able to whip one together fast
depending on the API.

-Matt

-----Original Message-----
From: jdom-interest-admin at jdom.org
[mailto:jdom-interest-admin at jdom.org]On Behalf Of adam flinton
Sent: Tuesday, May 22, 2001 10:07 AM
To: JDOM Mailing List (E-mail)
Subject: [jdom-interest] Has any one done a JDOM <> JTreeModel adaptor


Dear All,

I was just wondering if anyone has done a JDOM tree <> TreeModel adaptor
such that I can use a JDOM Model to directly interface with a JTree.

i.e. I am looking @ doing a designer tool for XML-DBMS & was very impressed
by the **lack of code** within the JTreeOutputter however it strikes me as
silly to store in effect 2 trees which I then have to keep in sync.

Any thoughts?

TIA

Adam Flinton
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com




More information about the jdom-interest mailing list