001: package examples.swingdemos;
002:
003: /*
004: * This code is based on an example provided by Richard Stanford,
005: * a tutorial reader.
006: */
007:
008: import java.awt.*;
009:
010: import javax.swing.*;
011: import javax.swing.event.*;
012: import javax.swing.tree.*;
013:
014: public class DynamicTree extends JPanel {
015: protected DefaultMutableTreeNode rootNode;
016: protected DefaultTreeModel treeModel;
017: protected JTree tree;
018: private Toolkit toolkit = Toolkit.getDefaultToolkit();
019:
020: public DynamicTree() {
021: rootNode = new DefaultMutableTreeNode("Root Node");
022: treeModel = new DefaultTreeModel(rootNode);
023: treeModel.addTreeModelListener(new MyTreeModelListener());
024:
025: tree = new JTree(treeModel);
026: tree.setEditable(true);
027: tree.getSelectionModel().setSelectionMode(
028: TreeSelectionModel.SINGLE_TREE_SELECTION);
029: tree.setShowsRootHandles(true);
030:
031: JScrollPane scrollPane = new JScrollPane(tree);
032: setLayout(new GridLayout(1, 0));
033: add(scrollPane);
034: }
035:
036: /** Remove all nodes except the root node. */
037: public void clear() {
038: rootNode.removeAllChildren();
039: treeModel.reload();
040: }
041:
042: /** Remove the currently selected node. */
043: public void removeCurrentNode() {
044: TreePath currentSelection = tree.getSelectionPath();
045: if (currentSelection != null) {
046: DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection
047: .getLastPathComponent());
048: MutableTreeNode parent = (MutableTreeNode) (currentNode
049: .getParent());
050: if (parent != null) {
051: treeModel.removeNodeFromParent(currentNode);
052: return;
053: }
054: }
055:
056: // Either there was no selection, or the root was selected.
057: toolkit.beep();
058: }
059:
060: /** Add child to the currently selected node. */
061: public DefaultMutableTreeNode addObject(Object child) {
062: DefaultMutableTreeNode parentNode = null;
063: TreePath parentPath = tree.getSelectionPath();
064:
065: if (parentPath == null) {
066: parentNode = rootNode;
067: } else {
068: parentNode = (DefaultMutableTreeNode) (parentPath
069: .getLastPathComponent());
070: }
071:
072: return addObject(parentNode, child, true);
073: }
074:
075: public DefaultMutableTreeNode addObject(
076: DefaultMutableTreeNode parent, Object child) {
077: return addObject(parent, child, false);
078: }
079:
080: public DefaultMutableTreeNode addObject(
081: DefaultMutableTreeNode parent, Object child,
082: boolean shouldBeVisible) {
083: DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(
084: child);
085:
086: if (parent == null) {
087: parent = rootNode;
088: }
089:
090: treeModel.insertNodeInto(childNode, parent, parent
091: .getChildCount());
092:
093: // Make sure the user can see the lovely new node.
094: if (shouldBeVisible) {
095: tree.scrollPathToVisible(new TreePath(childNode.getPath()));
096: }
097: return childNode;
098: }
099:
100: class MyTreeModelListener implements TreeModelListener {
101: public void treeNodesChanged(TreeModelEvent e) {
102: DefaultMutableTreeNode node;
103: node = (DefaultMutableTreeNode) (e.getTreePath()
104: .getLastPathComponent());
105:
106: /*
107: * If the event lists children, then the changed
108: * node is the child of the node we've already
109: * gotten. Otherwise, the changed node and the
110: * specified node are the same.
111: */
112: try {
113: int index = e.getChildIndices()[0];
114: node = (DefaultMutableTreeNode) (node.getChildAt(index));
115: } catch (NullPointerException exc) {
116: }
117:
118: System.out
119: .println("The user has finished editing the node.");
120: System.out.println("New value: " + node.getUserObject());
121: }
122:
123: public void treeNodesInserted(TreeModelEvent e) {
124: }
125:
126: public void treeNodesRemoved(TreeModelEvent e) {
127: }
128:
129: public void treeStructureChanged(TreeModelEvent e) {
130: }
131: }
132: }
|