1 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 ; 13 import java.util.Arrays ; 14 import java.util.Vector ; 15 16 import javax.swing.SwingUtilities ; 17 import javax.swing.tree.TreeNode ; 18 19 25 26 public class CompilationUnitNode extends JavaProjectNode { 27 private ICompilationUnit m_cu; 28 private String m_signature; 29 private String 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 getChildAt(int index) { 61 if(children == null) { 62 children = new Vector (); 63 children.setSize(ensureTypes().length); 64 SwingUtilities.invokeLater(new Runnable () { 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 getSignature() { 88 return m_signature; 89 } 90 91 public String [] getFields() { 92 ArrayList <String > list = new ArrayList <String >(); 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 [0]); 100 } 101 102 public String 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 |