KickJava   Java API By Example, From Geeks To Geeks.

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


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.Flags;
7 import org.eclipse.jdt.core.IClassFile;
8 import org.eclipse.jdt.core.ICompilationUnit;
9 import org.eclipse.jdt.core.IPackageFragment;
10 import org.eclipse.jdt.core.IPackageFragmentRoot;
11 import org.eclipse.jdt.core.IType;
12 import org.eclipse.jdt.core.JavaModelException;
13
14 import org.terracotta.dso.PatternHelper;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 import javax.swing.SwingUtilities JavaDoc;
21 import javax.swing.tree.TreeNode JavaDoc;
22
23 /**
24  * A TreeNode that represents a Java package root.
25  *
26  * @see JavaProjectNode
27  * @see org.eclipse.jdt.core.IPackageFragment
28  */

29
30 public class PackageFragmentNode extends JavaProjectNode {
31   private IPackageFragment m_fragment;
32   private String JavaDoc m_signature;
33   private String JavaDoc m_fragmentName;
34   private boolean m_showFields;
35   private boolean m_showMethods;
36   private boolean m_showTypes;
37   private ICompilationUnit[] m_cus;
38   private IClassFile[] m_classFiles;
39   
40   public PackageFragmentNode(IPackageFragment fragment) {
41     this(fragment, true, true, true);
42   }
43   
44   public PackageFragmentNode(
45     IPackageFragment fragment,
46     boolean showFields,
47     boolean showMethods,
48     boolean showTypes)
49   {
50     super(fragment);
51     
52     m_fragment = fragment;
53     m_fragmentName = m_fragment.getElementName();
54     m_signature = showMethods ? PatternHelper.getExecutionPattern(fragment) :
55                                    PatternHelper.getWithinPattern(fragment);
56     m_showFields = showFields;
57     m_showMethods = showMethods;
58     m_showTypes = showTypes;
59   }
60   
61   public int getChildCount() {
62     return children != null ? children.size() : 1;
63   }
64   
65   private int determineChildCount() {
66     return isSourceFragment() ?
67         ensureCompilationUnits().length :
68         ensureClassFiles().length;
69   }
70   
71   public TreeNode JavaDoc getChildAt(int index) {
72     if(children == null) {
73       children = new Vector JavaDoc();
74       children.setSize(determineChildCount());
75       SwingUtilities.invokeLater(new Runnable JavaDoc() {
76         public void run() {
77           nodeStructureChanged();
78         }
79       });
80     }
81
82     if(children.elementAt(index) == null) {
83       addChild(index);
84     }
85     
86     return super.getChildAt(index);
87   }
88
89   private void addChild(int index) {
90     ProjectNode node;
91     
92     if(isSourceFragment()) {
93       ICompilationUnit[] cus = ensureCompilationUnits();
94       ICompilationUnit cu = cus[index];
95         
96       node = new CompilationUnitNode(cu, m_showFields, m_showMethods, m_showTypes);
97       setChildAt(node, index);
98     }
99     else {
100       IClassFile[] classFiles = ensureClassFiles();
101       IClassFile classFile = classFiles[index];
102         
103       node = new ClassFileNode(classFile, m_showFields, m_showMethods, m_showTypes);
104       setChildAt(node, index);
105     }
106   }
107   
108   private boolean isSourceFragment() {
109     try {
110       return m_fragment.getKind() == IPackageFragmentRoot.K_SOURCE;
111     } catch(JavaModelException jme) {
112       return false;
113     }
114   }
115   
116   public String JavaDoc getSignature() {
117     return m_signature;
118   }
119   
120   public String JavaDoc[] getFields() {
121     ArrayList JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
122     int childCount = getChildCount();
123     
124     for(int i = 0; i < childCount; i++) {
125       list.addAll(Arrays.asList(((JavaProjectNode)getChildAt(i)).getFields()));
126     }
127     
128     return list.toArray(new String JavaDoc[0]);
129   }
130   
131   public String JavaDoc toString() {
132     return m_fragmentName;
133   }
134   
135   private ICompilationUnit[] ensureCompilationUnits() {
136     if(m_cus == null) {
137       m_cus = internalGetCompilationUnits();
138     }
139     
140     return m_cus;
141   }
142   
143   private ICompilationUnit[] internalGetCompilationUnits() {
144     try {
145       ICompilationUnit[] cus = m_fragment.getCompilationUnits();
146       return (ICompilationUnit[])JavaElementComparable.sort(cus);
147     } catch(JavaModelException jme) {
148       jme.printStackTrace();
149       return new ICompilationUnit[0];
150     }
151   }
152   
153   private IClassFile[] ensureClassFiles() {
154     if(m_classFiles == null) {
155       m_classFiles = internalGetClassFiles();
156     }
157     
158     return m_classFiles;
159   }
160   
161   private IClassFile[] internalGetClassFiles() {
162     try {
163       IClassFile[] classFiles = m_fragment.getClassFiles();
164       ArrayList JavaDoc<IClassFile> list = new ArrayList JavaDoc<IClassFile>();
165       IType type;
166       int flags;
167       
168       for(int i = 0; i < classFiles.length; i++) {
169         type = classFiles[i].getType();
170         flags = type.getFlags();
171         
172         if(!type.isInterface() &&
173            !type.isAnonymous() &&
174            !type.isMember() &&
175            !type.isEnum() &&
176            !type.isAnnotation() &&
177            !Flags.isPrivate(flags) &&
178            !Flags.isProtected(flags) &&
179            !Flags.isStatic(flags))
180         {
181           list.add(classFiles[i]);
182         }
183       }
184
185       classFiles = list.toArray(new IClassFile[0]);
186       return (IClassFile[])JavaElementComparable.sort(classFiles);
187     } catch(JavaModelException jme) {
188       jme.printStackTrace();
189       return new IClassFile[0];
190     }
191   }
192 }
193
Popular Tags