KickJava   Java API By Example, From Geeks To Geeks.

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


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.IField;
7 import org.eclipse.jdt.core.IMethod;
8 import org.eclipse.jdt.core.IType;
9 import org.eclipse.jdt.core.JavaModelException;
10
11 import org.terracotta.dso.PatternHelper;
12
13 import java.util.Vector JavaDoc;
14
15 import javax.swing.tree.TreeNode JavaDoc;
16
17 /**
18  * A TreeNode that represents a Java type (yeah, a class).
19  *
20  * @see JavaProjectNode
21  */

22
23 public class TypeNode extends JavaProjectNode {
24   private IType m_type;
25   private String JavaDoc m_name;
26   private String JavaDoc m_signature;
27   private boolean m_showFields;
28   private IField[] m_fields;
29   private boolean m_showMethods;
30   private IMethod[] m_methods;
31   private boolean m_showTypes;
32   private IType[] m_types;
33   
34   public TypeNode(IType type) {
35     this(type, true, true, true);
36   }
37   
38   public TypeNode(
39     IType type,
40     boolean showFields,
41     boolean showMethods,
42     boolean showTypes)
43   {
44     super(type);
45     
46     m_type = type;
47     m_name = type.getElementName();
48     m_signature = showMethods ? PatternHelper.getExecutionPattern(type) :
49                                   PatternHelper.getFullyQualifiedName(type);
50     m_showFields = showFields;
51     m_showMethods = showMethods;
52     m_showTypes = showTypes;
53   }
54
55   public int getChildCount() {
56     if(children == null) {
57       children = new Vector JavaDoc();
58       children.setSize(determineChildCount());
59     }
60     
61     return children.size();
62   }
63   
64   private int determineChildCount() {
65     int result = 0;
66     
67     if(m_showFields) {
68       result += ensureFields().length;
69     }
70     if(m_showMethods) {
71       result += ensureMethods().length;
72     }
73     if(m_showTypes) {
74       result += ensureTypes().length;
75     }
76     
77     return result;
78 }
79   
80   public TreeNode JavaDoc getChildAt(int index) {
81     if(children.elementAt(index) == null) {
82       addChildren();
83     }
84     
85     return super.getChildAt(index);
86   }
87   
88   private void addChildren() {
89     int count = 0;
90     
91     if(m_showFields) {
92       IField[] fields = ensureFields();
93       
94       for(int i = 0; i < fields.length; i++, count++) {
95         setChildAt(new FieldNode(fields[i]), i);
96       }
97     }
98     
99     if(m_showMethods) {
100       IMethod[] methods = ensureMethods();
101       
102       for(int i = 0, j = count; i < methods.length; i++, count++, j++) {
103         setChildAt(new MethodNode(methods[i]), j);
104       }
105     }
106
107     if(m_showTypes) {
108       IType[] types = ensureTypes();
109       TypeNode node;
110       
111       for(int i = 0, j = count; i < types.length; i++, j++) {
112         node = new TypeNode(types[i], m_showFields, m_showMethods, m_showTypes);
113         setChildAt(node, j);
114       }
115     }
116   }
117   
118   public String JavaDoc getSignature() {
119     return m_signature;
120   }
121   
122   public String JavaDoc[] getFields() {
123     String JavaDoc typeName = PatternHelper.getFullyQualifiedName(m_type);
124     IField[] fields = internalGetFields();
125     String JavaDoc[] result = new String JavaDoc[fields.length];
126     
127     for(int i = 0; i < fields.length; i++) {
128       result[i] = typeName+"."+fields[i].getElementName();
129     }
130     
131     return result;
132   }
133   
134   public String JavaDoc toString() {
135     return m_name;
136   }
137   
138   private IField[] ensureFields() {
139     if(m_fields == null) {
140       m_fields = internalGetFields();
141     }
142     return m_fields;
143   }
144   
145   private IField[] internalGetFields() {
146     try {
147       return (IField[])JavaElementComparable.sort(m_type.getFields());
148     } catch(JavaModelException jme) {
149       return new IField[0];
150     }
151   }
152
153   private IMethod[] ensureMethods() {
154     if(m_methods == null) {
155       m_methods = internalGetMethods();
156     }
157     return m_methods;
158   }
159   
160   private IMethod[] internalGetMethods() {
161     try {
162       return (IMethod[])JavaElementComparable.sort(m_type.getMethods());
163     } catch(JavaModelException jme) {
164       return new IMethod[0];
165     }
166   }
167
168   private IType[] ensureTypes() {
169     if(m_types == null) {
170       m_types = internalGetTypes();
171     }
172     return m_types;
173   }
174   
175   private IType[] internalGetTypes() {
176     try {
177       return (IType[])JavaElementComparable.sort(m_type.getTypes());
178     } catch(JavaModelException jme) {
179       return new IType[0];
180     }
181   }
182 }
183
Popular Tags