KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > console > TreeView


1 package org.sape.carbon.services.console;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Set JavaDoc;
6
7 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
8 import javax.swing.tree.DefaultTreeModel JavaDoc;
9
10 import org.sape.carbon.core.util.reflection.ClassFinder;
11
12 /**
13  *
14  *
15  * Copyright 2002 Sapient
16  * @since carbon 1.0
17  * @author Greg Hinkle, January 2002
18  * @version $Revision: 1.6 $($Author: ghinkl $ / $Date: 2003/04/04 01:11:49 $)
19  */

20 public class TreeView extends javax.swing.JTree JavaDoc {
21     
22     /** Creates a new instance of TreeView */
23     public TreeView() {
24         
25     }
26     
27
28     
29     
30     public void loadPackages(Class JavaDoc toLoad, boolean interfaces, boolean classes) {
31         DefaultMutableTreeNode JavaDoc root = new DefaultMutableTreeNode JavaDoc(""); //new PackageNode("",false));
32
DefaultTreeModel JavaDoc model = new DefaultTreeModel JavaDoc(root);
33         
34         ClassFinder finder = new ClassFinder(toLoad,"sapient");
35         Set JavaDoc dataSet = finder.getClasses();
36         
37         
38         Iterator JavaDoc dataIter = dataSet.iterator();
39         while (dataIter.hasNext()) {
40             String JavaDoc className = (String JavaDoc) dataIter.next();
41             Class JavaDoc theClass = null;
42             try {
43                 theClass = Class.forName(className,false,this.getClass().getClassLoader());
44             } catch ( ClassNotFoundException JavaDoc cnfe) { cnfe.printStackTrace(); }
45             
46             if ((interfaces && theClass.isInterface()) ||
47                 (classes && !theClass.isInterface())) {
48                 TreeView.ClassNode classNode = new ClassNode(theClass);
49                 addNode(root, className, classNode);
50             }
51         }
52         
53         this.putClientProperty("JTree.lineStyle", "Angled");
54         
55         this.setModel(model);
56         //this.packageJTree.setCellRenderer(new SourceFilter.PkgCellRenderer());
57
this.repaint();
58     }
59     
60     public void addNode(DefaultMutableTreeNode JavaDoc base, String JavaDoc nodeNameLeft, ClassNode classNode) {
61         //System.out.println("Left: " + nodeNameLeft);
62
int pIndex = nodeNameLeft.indexOf((char) '.');
63         
64         if (pIndex < 0) {
65             // Last Node
66
DefaultMutableTreeNode JavaDoc node =
67                 new DefaultMutableTreeNode JavaDoc(classNode);
68             base.add(node);
69             
70         } else {
71             String JavaDoc pkgName = nodeNameLeft.substring(0, pIndex);
72             boolean nodeFound = false;
73             Enumeration JavaDoc children = base.children();
74             while (children.hasMoreElements()) {
75                 DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc) children.nextElement();
76                 if (node.getUserObject().toString().equals(pkgName)) {
77                     addNode(node, nodeNameLeft.substring(pIndex+1), classNode);
78                     nodeFound = true;
79                 }
80             }
81             if (!nodeFound) {
82                 DefaultMutableTreeNode JavaDoc node =
83                     new DefaultMutableTreeNode JavaDoc(pkgName);
84                 base.add(node);
85                 addNode(node,nodeNameLeft.substring(pIndex+1), classNode);
86                 
87                 
88             }
89         }
90     }
91
92     
93     
94     public static class ClassNode {
95         private Class JavaDoc theClass;
96         
97         public ClassNode(Class JavaDoc theClass) {
98             this.theClass = theClass;
99         }
100         
101         public Class JavaDoc getHoldingClass() {
102             return this.theClass;
103         }
104         
105         public String JavaDoc toString() {
106             String JavaDoc className = this.theClass.getName();
107             
108             return className.substring(className.lastIndexOf('.')+1,className.length());
109         }
110         
111     }
112     
113 }
114
Popular Tags