FW: [jdom-interest] Building a JTree out of a Dom Document.
Mark Ayad
mayad at ogilvy.net
Tue Sep 18 08:01:12 PDT 2001
-----Original Message-----
From: Mark Ayad [mailto:mayad at ogilvy.net]
Sent: Monday, September 03, 2001 4:27 PM
To: Tasso Angelidis
Subject: RE: [jdom-interest] Building a JTree out of a Dom Document.
I have an answer your in luck..
// java classes
import java.util.List;
import java.util.Iterator;
import java.io.*;
// swing classes
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
// awt classes
import java.awt.*;
import java.awt.event.*;
// xml classes
import org.jdom.*;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class BigTree
{
/**************************************
/*
/* Declerations
/*
/**************************************/
/** */
private JFrame mainFrame = null;
/** */
private JScrollPane treeScroll = null;
/** */
private DefaultMutableTreeNode rootNode;
/** */
private DefaultTreeModel treeModel;
/** */
private JTree tree;
/** jdom sax driver */
private static String saxDriverClass = null;
/** jdom root elemnt */
private Element rootElement = null;
/**************************************
/*
/* Constructor
/*
/**************************************/
/** default constructor */
public BigTree()
{
// a simple tree test
// buildSimpleTree();
// build an xml tree
buildXmlTree();
getMainFrame();
}
/**************************************
/*
/* Helper Methods
/*
/**************************************/
/**
* build a simple jtree
*/
private void buildSimpleTree()
{
String p1Name = new String("Parent 1");
String p2Name = new String("Parent 2");
String c1Name = new String("Child 1");
String c2Name = new String("Child 2");
DefaultMutableTreeNode p1, p2;
// create the trees root node
rootNode = new DefaultMutableTreeNode("The Root Node");
// construct a tree model
treeModel = new DefaultTreeModel(rootNode);
// construct a tree based on a tree model
tree = new JTree(treeModel);
// allow the tree to be editable
tree.setEditable(true);
// show the root handles on the tree
tree.setShowsRootHandles(true);
// apply a look and feel to the tree
tree.putClientProperty("JTree.lineStyle", "Angled");
// create a tree node
p1 = addObject(null, p1Name);
// create a tree node
p2 = addObject(null, p2Name);
// add a child node to the p1 node
addObject(p1, c1Name);
}
/**
* build a jtree based on an xml document
*/
private void buildXmlTree()
{
initXml();
// create the trees root node
rootNode = new DefaultMutableTreeNode("document");
// construct a tree model
treeModel = new DefaultTreeModel(rootNode);
// construct a tree based on a tree model
tree = new JTree(treeModel);
// allow the tree to be editable
tree.setEditable(false);
// show the root handles on the tree
tree.setShowsRootHandles(true);
// apply a look and feel to the tree
tree.putClientProperty("JTree.lineStyle", "Angled");
// if a Jdom root element exists proceed
if ( rootElement != null)
{
recurseXml(rootElement,rootNode);
}
}
/**
* recurse an xml tree from a given jdom element
*/
private void recurseXml(Element element, DefaultMutableTreeNode parentNode)
{
if(element.hasChildren())
{
List elementList = element.getChildren();
Iterator listIterator = elementList.iterator();
while (listIterator.hasNext())
{
Element currentElement = (Element) listIterator.next();
DefaultMutableTreeNode newNode = new
DefaultMutableTreeNode(currentElement.getName());
treeModel.insertNodeInto(newNode
,parentNode,parentNode.getChildCount());
// recurse make the parent node the new node
recurseXml(currentElement,newNode);
}
}
}
/**
* build an xml document read for parsing
*/
private void initXml()
{
try
{
SAXBuilder builder = null;
if (saxDriverClass == null) builder = new SAXBuilder();
else builder = new SAXBuilder("");
Document doc = builder.build("dtdxml.xml");
rootElement = doc.getRootElement();
}
catch(Exception e)
{
System.out.println("initXml Exception: " + e);
}
}
/** add child to the currently selected node. */
public DefaultMutableTreeNode addObject(Object child)
{
DefaultMutableTreeNode parentNode = null;
TreePath parentPath = tree.getSelectionPath();
// if the parent is null then by default the parent is the root node
if (parentPath == null) parentNode = rootNode;
else
{
parentNode = (DefaultMutableTreeNode)
(parentPath.getLastPathComponent());
}
return addObject(parentNode, child, true);
}
/** */
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode
parent,Object child)
{
return addObject(parent, child, false);
}
/** */
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode
parent,Object child,boolean shouldBeVisible)
{
// create a new child node
DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
// if the parent is null then by default the parent is the root node
if (parent == null) parent = rootNode;
// add the child to the tree model
treeModel.insertNodeInto(childNode, parent, parent.getChildCount());
// make sure that the added node is visible
if (shouldBeVisible) tree.scrollPathToVisible(new
TreePath(childNode.getPath()));
// return the child node
return childNode;
}
/*##########################################################
/*
/* Component Constructors
/*
/*##########################################################
/**************************************
/*
/* Component Getters
/*
/**************************************/
/** create or return the my tree frame instance */
private JFrame getMainFrame()
{
if (mainFrame == null)
{
mainFrame = new JFrame("Big Tree");
mainFrame.setSize(400, 300);
mainFrame.getContentPane().add(getTreeScroll());
mainFrame.addWindowListener(new MyWindowListener());
mainFrame.show();
}
return mainFrame;
}
/** create or return the my tree scroll instance */
private JScrollPane getTreeScroll()
{
if (treeScroll == null)
{
treeScroll = new JScrollPane();
treeScroll.getViewport().add(tree);
}
return treeScroll;
}
/*##########################################################
/*
/* Event Handling
/*
/*##########################################################
/** */
public class MyWindowListener implements WindowListener, ActionListener
{
/**************************************
/*
/* Inner Class
/* Implement the WindowListener interface
/*
/**************************************/
/**
Invoked when a window is activated.
@param e the received event
*/
public void windowActivated(WindowEvent e)
{
//System.out.println("debug window got focus");
}
/**
Invoked when a window has been closed.
@param e the received event
*/
public void windowClosed(WindowEvent e)
{
// Do nothing.
// This method is required to comply with the WindowListener interface.
}
/**
Invoked when a window is in the process of being closed.
The close operation can be overridden at this point.
@param e the received event
*/
public void windowClosing(WindowEvent e)
{
// The window is being closed. Shut down the system.
System.exit(0);
}
/**
Invoked when a window is deactivated.
@param e the received event
*/
public void windowDeactivated(WindowEvent e)
{
//System.out.println("debug window lost focus");
}
/**
Invoked when a window is de-iconified.
@param e the received event
*/
public void windowDeiconified(WindowEvent e)
{
//System.out.println("debug de-iconified the window");
}
/**
Invoked when a window is iconified.
@param e the received event
*/
public void windowIconified(WindowEvent e)
{
//System.out.println("debug iconified the window");
}
/**
Invoked when a window has been opened.
@param e the received event
*/
public void windowOpened(WindowEvent e)
{
// Do nothing.
// This method is required to comply with the WindowListener interface.
}
/**************************************
/*
/* Implement the ActionListener interface
/*
/**************************************/
public void actionPerformed(java.awt.event.ActionEvent e)
{
System.out.println("window action event");
}
}
/*##########################################################
/*
/* Testing
/*
/*##########################################################
/**************************************
/*
/* Main
/*
/**************************************/
/** program entry point */
public static void main(java.lang.String[] args)
{
BigTree objBigTree = new BigTree();
}
}
-----Original Message-----
From: jdom-interest-admin at jdom.org
[mailto:jdom-interest-admin at jdom.org]On Behalf Of Tasso Angelidis
Sent: Tuesday, September 18, 2001 3:17 PM
To: 'Matthew MacKenzie'; philip.nelson at omniresources.com; Tasso
Angelidis; jdom-interest at jdom.org
Subject: RE: [jdom-interest] Building a JTree out of a Dom Document.
Call me ignorant, where are the contribs or the files for the outputter...
Thanks.
-----Original Message-----
From: Matthew MacKenzie [mailto:matt at xmlglobal.com]
Sent: Monday, September 17, 2001 8:32 PM
To: philip.nelson at omniresources.com; TassoA at trustmarque.ca;
jdom-interest at jdom.org
Subject: Re: [jdom-interest] Building a JTree out of a Dom Document.
On Monday 17 September 2001 16:51, philip.nelson at omniresources.com wrote:
> There is a JTree outputter in the jdom-contrib area though I don't know if
> it's up to date.
It should still work. Also, if one were to search the list archives they
might find an announcement that someone wrote a TreeModel for JDOM. Last I
checked, jdom-contrib still just output to a tree node, following the
Outputter model.
>
> Brett's second edition of "Java and XML" has an example in the Advanced
> JDOM chapter (and the sax chapter if I remember right)
> _______________________________________________
> To control your jdom-interest membership:
>
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourho
>st.com
--
Matthew MacKenzie
XML Global
<quote>
If at first you don't succeed, redefine success.
</quote>
_______________________________________________
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