KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > dso > editors > tree > ClassFileNode


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package org.terracotta.dso.editors.tree;
5
6 import org.eclipse.jdt.core.IClassFile;
7 import org.eclipse.jdt.core.IType;
8 import org.eclipse.jdt.core.JavaModelException;
9
10 import org.terracotta.dso.PatternHelper;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Arrays JavaDoc;
14
15 import javax.swing.tree.TreeNode JavaDoc;
16
17 /**
18  * A TreeNode that represents a Java module (ICompilationUnit).
19  *
20  * @see JavaProjectNode
21  * @see org.eclipse.jdt.core.ICompilationUnit
22  */

23
24 public class ClassFileNode extends JavaProjectNode {
25   private IClassFile m_classFile;
26   private String JavaDoc m_signature;
27   private String JavaDoc m_moduleName;
28   private boolean m_showFields;
29   private boolean m_showMethods;
30   private boolean m_showTypes;
31   private TypeNode m_typeNode;
32   
33   public ClassFileNode(IClassFile classFile) {
34     this(classFile, true, true, true);
35   }
36   
37   public ClassFileNode(
38     IClassFile classFile,
39     boolean showFields,
40     boolean showMethods,
41     boolean showTypes)
42   {
43     super(classFile);
44     
45     m_classFile = classFile;
46     m_moduleName = internalGetType().getElementName()+".class";
47     m_signature = showMethods ? PatternHelper.getExecutionPattern(internalGetType()) :
48                                   PatternHelper.getWithinPattern(internalGetType());
49     m_showFields = showFields;
50     m_showMethods = showMethods;
51     m_showTypes = showTypes;
52   }
53   
54   public int getChildCount() {
55     return 1;
56   }
57   
58   public TreeNode JavaDoc getChildAt(int index) {
59     if(m_typeNode == null) {
60       m_typeNode = buildTypeNode();
61     }
62     
63     return m_typeNode;
64   }
65   
66   private TypeNode buildTypeNode() {
67     IType type = internalGetType();
68     return new TypeNode(type, m_showFields, m_showMethods, m_showTypes);
69   }
70   
71   public String JavaDoc getSignature() {
72     return m_signature;
73   }
74
75   public String JavaDoc[] getFields() {
76     ArrayList JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
77     int childCount = getChildCount();
78     
79     for(int i = 0; i < childCount; i++) {
80       list.addAll(Arrays.asList(((TypeNode)getChildAt(i)).getFields()));
81     }
82     
83     return list.toArray(new String JavaDoc[0]);
84   }
85   
86   public String JavaDoc toString() {
87     return m_moduleName;
88   }
89   
90   private IType internalGetType() {
91     try {
92       return m_classFile.getType();
93     } catch(JavaModelException jme) {
94       jme.printStackTrace();
95       return null;
96     }
97   }
98 }
99
Popular Tags