/**
* Simple tree test.
* @author [email="sebastian.haufe@ebenius.com"]Sebastian Haufe[/email]
* @version 1.0 / 19.12.2003
*/
public class TreeTest {
public static void main(String[] args) {
final MyTreeModel model;
JTree tree = new JTree(model = new MyTreeModel());
tree.setPreferredSize(new Dimension(400, 60));
model.addChild("Child 1");
model.addChild("Child 2");
model.addChild("Child 3");
JFrame frame = new JFrame("Test Frame");
frame.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
JButton button = new JButton("Change Text");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.changeChild("A damned long text to draw in the tree.", 2);
}
});
frame.getContentPane().add(button, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.show();
}
}
/**
* Simple flat tree model.
* @author [email="sebastian.haufe@ebenius.com"]Sebastian Haufe[/email]
* @version 1.0 / 19.12.2003
*/
class MyTreeModel implements TreeModel {
private EventListenerList listenerList = new EventListenerList();
private List children = new LinkedList();
/*
* @see javax.swing.tree.TreeModel#getRoot()
*/
public Object getRoot() {
return "Root";
}
/*
* @see javax.swing.tree.TreeModel#getChild(java.lang.Object, int)
*/
public Object getChild(Object parent, int index) {
return parent == "Root" ? children.get(index) : null;
}
/*
* @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object)
*/
public int getChildCount(Object parent) {
return parent == "Root" ? children.size() : 0;
}
/*
* @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
*/
public boolean isLeaf(Object node) {
return node != "Root";
}
/*
* @see javax.swing.tree.TreeModel#valueForPathChanged(javax.swing.tree.TreePath, java.lang.Object)
*/
public void valueForPathChanged(TreePath path, Object newValue) {
// do nothing -> not editable
}
/*
* @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object, java.lang.Object)
*/
public int getIndexOfChild(Object parent, Object child) {
return parent == "Root" && child != null ? children.indexOf(child) : -1;
}
/*
* @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
*/
public void addTreeModelListener(TreeModelListener l) {
listenerList.add(TreeModelListener.class, l);
}
/*
* @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.TreeModelListener)
*/
public void removeTreeModelListener(TreeModelListener l) {
listenerList.remove(TreeModelListener.class, l);
}
/**
* Adds a child string to the bottom of the list of children.
* @param child The child to add
*/
public void addChild(String child) {
if (children.contains(child))
return;
children.add(child);
int index = children.indexOf(child);
fireTreeNodesInserted(this, new Object[] { "Root" }, new int[] { index }, new Object[] { child });
}
/**
* Changes the text of the child at the specified index.
* @param child The child to add
* @param index The index to add the child at
*/
public void changeChild(String child, int index) {
children.set(index, child);
fireTreeNodesChanged(this, new Object[] { "Root" }, new int[] { index }, new Object[] { child });
}
/**
* [b]Lazily copied from DefaultTreeModel[/b]
* @see javax.swing.tree.DefaultTreeModel#fireTreeNodesChanged(java.lang.Object, java.lang.Object[], int[], java.lang.Object[])
*/
protected void fireTreeNodesChanged(Object source, Object[] path, int[] childIndices, Object[] children) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
TreeModelEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TreeModelListener.class) {
// Lazily create the event:
if (e == null)
e = new TreeModelEvent(source, path, childIndices, children);
((TreeModelListener) listeners[i + 1]).treeNodesChanged(e);
}
}
}
/**
* [b]Lazily copied from DefaultTreeModel[/b]
* @see javax.swing.tree.DefaultTreeModel#fireTreeNodesInserted(java.lang.Object, java.lang.Object[], int[], java.lang.Object[])
*/
protected void fireTreeNodesInserted(Object source, Object[] path, int[] childIndices, Object[] children) {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
TreeModelEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == TreeModelListener.class) {
// Lazily create the event:
if (e == null)
e = new TreeModelEvent(source, path, childIndices, children);
((TreeModelListener) listeners[i + 1]).treeNodesInserted(e);
}
}
}
}