author | wenzelm |
Sat, 24 Nov 2001 16:54:32 +0100 | |
changeset 12282 | f98beaaa7c4f |
parent 11873 | 38dc46b55d7e |
child 13973 | 9170772bf420 |
permissions | -rw-r--r-- |
3599 | 1 |
/*************************************************************************** |
2 |
Title: GraphBrowser/TreeNode.java |
|
3 |
ID: $Id$ |
|
4 |
Author: Stefan Berghofer, TU Muenchen |
|
11873
38dc46b55d7e
Moved font settings from TreeNode to TreeBrowser.
berghofe
parents:
9407
diff
changeset
|
5 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
3599 | 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 |
int starty,endy,number; |
|
37 |
String name,path; |
|
38 |
||
39 |
Vector leaves=new Vector(10,10); |
|
40 |
boolean unfold=false; |
|
41 |
||
42 |
public void insertNode(String n,String d,String p,int num,boolean u) { |
|
43 |
if (d.length()==0) { |
|
44 |
leaves.addElement(new TreeNode(n,p,num)); |
|
45 |
unfold=unfold||u; |
|
46 |
} else { |
|
47 |
unfold=unfold||u; |
|
48 |
String str1,str2; |
|
49 |
if (d.indexOf('/')<0) { |
|
50 |
str1=d;str2=""; |
|
51 |
} else { |
|
52 |
str1=d.substring(0,d.indexOf('/')); |
|
53 |
str2=d.substring(d.indexOf('/')+1); |
|
54 |
} |
|
55 |
||
56 |
Enumeration e1=leaves.elements(); |
|
57 |
TreeNode nd=null; |
|
58 |
||
59 |
while (e1.hasMoreElements()) { |
|
60 |
TreeNode nd2=(TreeNode)(e1.nextElement()); |
|
61 |
if (nd2.name.equals(str1)) { |
|
62 |
nd=nd2;break; |
|
63 |
} |
|
64 |
} |
|
65 |
if (nd==null) { |
|
66 |
nd=new TreeNode(str1,"",-1); |
|
67 |
leaves.addElement(nd); |
|
68 |
} |
|
69 |
nd.insertNode(n,str2,p,num,u); |
|
70 |
} |
|
71 |
} |
|
72 |
||
73 |
public TreeNode(String n,String p,int num) { |
|
74 |
name=n; |
|
75 |
path=p; |
|
76 |
number=num; |
|
77 |
} |
|
78 |
||
79 |
public TreeNode(String n,String p,int num,boolean u) { |
|
80 |
this(n,p,num); |
|
81 |
unfold=u; |
|
82 |
} |
|
83 |
||
84 |
public int getNumber() { return number; } |
|
85 |
||
86 |
public TreeNode lookup(int y) |
|
87 |
{ |
|
88 |
if (y>=starty && y<endy) return this; |
|
89 |
else |
|
90 |
if (unfold) |
|
91 |
for (int i=0;i<leaves.size();i++) |
|
92 |
{ |
|
93 |
TreeNode t=((TreeNode)leaves.elementAt(i)).lookup(y); |
|
94 |
if (t!=null) return t; |
|
95 |
} |
|
96 |
return null; |
|
97 |
} |
|
98 |
||
99 |
public boolean select() |
|
100 |
{ |
|
101 |
if (!leaves.isEmpty()) { |
|
102 |
if (unfold) collapse(); |
|
103 |
else unfold=true; |
|
104 |
return true; |
|
105 |
} |
|
106 |
return false; |
|
107 |
} |
|
108 |
||
109 |
public void collapse() |
|
110 |
{ |
|
111 |
unfold=false; |
|
112 |
/* |
|
113 |
for(int i=0;i<leaves.size();i++) |
|
114 |
((Tree)leaves.elementAt(i)).collapse(); |
|
115 |
*/ |
|
116 |
} |
|
117 |
||
118 |
void collapsedNodes(Vector v) { |
|
119 |
if (number>=0) v.addElement(new Integer(number)); |
|
120 |
Enumeration e1=leaves.elements(); |
|
121 |
while (e1.hasMoreElements()) |
|
122 |
((TreeNode)(e1.nextElement())).collapsedNodes(v); |
|
123 |
} |
|
124 |
||
125 |
public void collapsedDirectories(Vector v) { |
|
126 |
if (!unfold) { |
|
127 |
Vector v2=new Vector(10,10); |
|
128 |
v.addElement(new Directory(this,name,v2)); |
|
129 |
collapsedNodes(v2); |
|
130 |
} else { |
|
131 |
Enumeration e1=leaves.elements(); |
|
132 |
while (e1.hasMoreElements()) { |
|
133 |
TreeNode tn=(TreeNode)(e1.nextElement()); |
|
134 |
if (!tn.leaves.isEmpty()) |
|
135 |
tn.collapsedDirectories(v); |
|
136 |
} |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
public Dimension draw(Graphics g,int x,int y,TreeNode t) |
|
141 |
{ |
|
11873
38dc46b55d7e
Moved font settings from TreeNode to TreeBrowser.
berghofe
parents:
9407
diff
changeset
|
142 |
FontMetrics fm=g.getFontMetrics(g.getFont()); |
3599 | 143 |
int h=fm.getHeight(); |
9407 | 144 |
int e=(int) (h / 10) + 1; |
145 |
int down_x[]={x + e, x + h - e, x + (int)(h / 2)}; |
|
146 |
int down_y[]={y + e, y + e, y + (int)(3 * h / 4) - e}; |
|
147 |
int right_x[]={x + e, x + (int)(3 * h / 4) - e, x + e}; |
|
148 |
int right_y[]={y + e, y + (int)(h / 2), y + h - e}; |
|
3599 | 149 |
int dx=0; |
150 |
||
151 |
if (unfold) |
|
152 |
{ |
|
153 |
g.setColor(Color.green); |
|
154 |
g.fillPolygon(down_x,down_y,3); |
|
155 |
g.setColor(Color.black); |
|
6657 | 156 |
g.drawString(name,x+h+4,y+fm.getAscent()); |
3599 | 157 |
starty=y;endy=y+h; |
6657 | 158 |
dx=Math.max(dx,x+h+4+fm.stringWidth(name)); |
3599 | 159 |
y+=h+5; |
160 |
for(int i=0;i<leaves.size();i++) |
|
161 |
{ |
|
6657 | 162 |
Dimension d=((TreeNode)leaves.elementAt(i)).draw(g,x+h+4,y,t); |
3599 | 163 |
y=d.height; |
164 |
dx=Math.max(dx,d.width); |
|
165 |
} |
|
166 |
} |
|
167 |
else |
|
168 |
{ |
|
169 |
g.setColor(leaves.isEmpty() ? Color.blue : Color.red); |
|
170 |
g.fillPolygon(right_x,right_y,3); |
|
171 |
if (this==t && leaves.isEmpty()) |
|
172 |
{ |
|
173 |
g.setColor(Color.white); |
|
6657 | 174 |
g.fillRect(x+h+4,y,fm.stringWidth(name),h); |
3599 | 175 |
} |
176 |
g.setColor(Color.black); |
|
6657 | 177 |
g.drawString(name,x+h+4,y+fm.getAscent()); |
3599 | 178 |
starty=y;endy=y+h; |
6657 | 179 |
dx=Math.max(dx,x+h+4+fm.stringWidth(name)); |
3599 | 180 |
y+=h+5; |
181 |
} |
|
182 |
return new Dimension(dx,y); |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |