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