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