KickJava   Java API By Example, From Geeks To Geeks.

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


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.ICompilationUnit;
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 import java.util.Vector JavaDoc;
15
16 import javax.swing.SwingUtilities JavaDoc;
17 import javax.swing.tree.TreeNode JavaDoc;
18
19 /**
20  * A TreeNode that represents a Java module (ICompilationUnit).
21  *
22  * @see JavaProjectNode
23  * @see org.eclipse.jdt.core.ICompilationUnit
24  */

25
26 public class CompilationUnitNode extends JavaProjectNode {
27   private ICompilationUnit m_cu;
28   private String JavaDoc m_signature;
29   private String JavaDoc m_moduleName;
30   private boolean m_showFields;
31   private boolean m_showMethods;
32   private boolean m_showTypes;
33   private IType[] m_types;
34   
35   public CompilationUnitNode(ICompilationUnit cu) {
36     this(cu, true, true, true);
37   }
38   
39   public CompilationUnitNode(
40     ICompilationUnit cu,
41     boolean showFields,
42     boolean showMethods,
43     boolean showTypes)
44   {
45     super(cu);
46     
47     m_cu = cu;
48     m_moduleName = cu.getResource().getName();
49     m_signature = showMethods ? PatternHelper.getExecutionPattern(cu.findPrimaryType()) :
50                                   PatternHelper.getWithinPattern(cu.findPrimaryType());
51     m_showFields = showFields;
52     m_showMethods = showMethods;
53     m_showTypes = showTypes;
54   }
55   
56   public int getChildCount() {
57     return children != null ? children.size() : 1;
58   }
59   
60   public TreeNode JavaDoc getChildAt(int index) {
61     if(children == null) {
62       children = new Vector JavaDoc();
63       children.setSize(ensureTypes().length);
64       SwingUtilities.invokeLater(new Runnable JavaDoc() {
65         public void run() {
66           nodeStructureChanged();
67         }
68       });
69     }
70
71     if(children.elementAt(index) == null) {
72       addChild(index);
73     }
74     
75     return super.getChildAt(index);
76   }
77   
78   private void addChild(int index) {
79     IType[] types = ensureTypes();
80     IType type = types[index];
81     TypeNode node;
82     
83     node = new TypeNode(type, m_showFields, m_showMethods, m_showTypes);
84     setChildAt(node, index);
85   }
86   
87   public String JavaDoc getSignature() {
88     return m_signature;
89   }
90
91   public String JavaDoc[] getFields() {
92     ArrayList JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
93     int childCount = getChildCount();
94     
95     for(int i = 0; i < childCount; i++) {
96       list.addAll(Arrays.asList(((TypeNode)getChildAt(i)).getFields()));
97     }
98     
99     return list.toArray(new String JavaDoc[0]);
100   }
101   
102   public String JavaDoc toString() {
103     return m_moduleName;
104   }
105
106   private IType[] ensureTypes() {
107     if(m_types == null) {
108       m_types = internalGetTypes();
109     }
110     return m_types;
111   }
112   
113   private IType[] internalGetTypes() {
114     try {
115       return (IType[])JavaElementComparable.sort(m_cu.getTypes());
116     } catch(JavaModelException jme) {
117       return new IType[0];
118     }
119   }
120 }
121
Popular Tags