1/*************************************************************************** 2 Title: GraphBrowser/TreeNode.java 3 Author: Stefan Berghofer, TU Muenchen 4 Options: :tabSize=4: 5 6 This class contains methods for storing and manipulating directory 7 trees (e.g. collapsing / uncollapsing directory branches). 8***************************************************************************/ 9 10package GraphBrowser; 11 12import java.awt.*; 13import java.util.*; 14 15 16public class TreeNode 17{ 18 int starty,endy,number; 19 String name,path; 20 21 Vector leaves=new Vector(10,10); 22 boolean unfold=false; 23 24 public void insertNode(String n,String d,String p,int num,boolean u) { 25 if (d.length()==0) { 26 leaves.addElement(new TreeNode(n,p,num)); 27 unfold=unfold||u; 28 } else { 29 unfold=unfold||u; 30 String str1,str2; 31 if (d.indexOf('/')<0) { 32 str1=d;str2=""; 33 } else { 34 str1=d.substring(0,d.indexOf('/')); 35 str2=d.substring(d.indexOf('/')+1); 36 } 37 38 Enumeration e1=leaves.elements(); 39 TreeNode nd=null; 40 41 while (e1.hasMoreElements()) { 42 TreeNode nd2=(TreeNode)(e1.nextElement()); 43 if (nd2.name.equals(str1)) { 44 nd=nd2;break; 45 } 46 } 47 if (nd==null) { 48 nd=new TreeNode(str1,"",-1); 49 leaves.addElement(nd); 50 } 51 nd.insertNode(n,str2,p,num,u); 52 } 53 } 54 55 public TreeNode(String n,String p,int num) { 56 name=n; 57 path=p; 58 number=num; 59 } 60 61 public TreeNode(String n,String p,int num,boolean u) { 62 this(n,p,num); 63 unfold=u; 64 } 65 66 public int getNumber() { return number; } 67 68 public TreeNode lookup(int y) 69 { 70 if (y>=starty && y<endy) return this; 71 else 72 if (unfold) 73 for (int i=0;i<leaves.size();i++) 74 { 75 TreeNode t=((TreeNode)leaves.elementAt(i)).lookup(y); 76 if (t!=null) return t; 77 } 78 return null; 79 } 80 81 public boolean select() 82 { 83 if (!leaves.isEmpty()) { 84 if (unfold) collapse(); 85 else unfold=true; 86 return true; 87 } 88 return false; 89 } 90 91 public void collapse() 92 { 93 unfold=false; 94 /* 95 for(int i=0;i<leaves.size();i++) 96 ((Tree)leaves.elementAt(i)).collapse(); 97 */ 98 } 99 100 void collapsedNodes(Vector v) { 101 if (number>=0) v.addElement(new Integer(number)); 102 Enumeration e1=leaves.elements(); 103 while (e1.hasMoreElements()) 104 ((TreeNode)(e1.nextElement())).collapsedNodes(v); 105 } 106 107 public void collapsedDirectories(Vector v) { 108 if (!unfold) { 109 Vector v2=new Vector(10,10); 110 v.addElement(new Directory(this,name,v2)); 111 collapsedNodes(v2); 112 } else { 113 Enumeration e1=leaves.elements(); 114 while (e1.hasMoreElements()) { 115 TreeNode tn=(TreeNode)(e1.nextElement()); 116 if (!tn.leaves.isEmpty()) 117 tn.collapsedDirectories(v); 118 } 119 } 120 } 121 122 public Dimension draw(Graphics g,int x,int y,TreeNode t) 123 { 124 FontMetrics fm=g.getFontMetrics(g.getFont()); 125 int h=fm.getHeight(); 126 int e=(int) (h / 10) + 1; 127 int down_x[]={x + e, x + h - e, x + (int)(h / 2)}; 128 int down_y[]={y + e, y + e, y + (int)(3 * h / 4) - e}; 129 int right_x[]={x + e, x + (int)(3 * h / 4) - e, x + e}; 130 int right_y[]={y + e, y + (int)(h / 2), y + h - e}; 131 int dx=0; 132 133 if (unfold) 134 { 135 g.setColor(Color.green); 136 g.fillPolygon(down_x,down_y,3); 137 g.setColor(Color.black); 138 g.drawString(name,x+h+4,y+fm.getAscent()); 139 starty=y;endy=y+h; 140 dx=Math.max(dx,x+h+4+fm.stringWidth(name)); 141 y+=h+5; 142 for(int i=0;i<leaves.size();i++) 143 { 144 Dimension d=((TreeNode)leaves.elementAt(i)).draw(g,x+h+4,y,t); 145 y=d.height; 146 dx=Math.max(dx,d.width); 147 } 148 } 149 else 150 { 151 g.setColor(leaves.isEmpty() ? Color.blue : Color.red); 152 g.fillPolygon(right_x,right_y,3); 153 if (this==t && leaves.isEmpty()) 154 { 155 g.setColor(Color.white); 156 g.fillRect(x+h+4,y,fm.stringWidth(name),h); 157 } 158 g.setColor(Color.black); 159 g.drawString(name,x+h+4,y+fm.getAscent()); 160 starty=y;endy=y+h; 161 dx=Math.max(dx,x+h+4+fm.stringWidth(name)); 162 y+=h+5; 163 } 164 return new Dimension(dx,y); 165 } 166} 167 168 169