author | berghofe |
Tue, 16 Oct 2001 19:54:24 +0200 | |
changeset 11811 | 38721b2c6f57 |
parent 6541 | d3ac35b2bfbf |
child 11872 | 4f24fd4dbcf5 |
permissions | -rw-r--r-- |
3599 | 1 |
/*************************************************************************** |
2 |
Title: GraphBrowser/GraphView.java |
|
3 |
ID: $Id$ |
|
4 |
Author: Stefan Berghofer, TU Muenchen |
|
11811
38721b2c6f57
PS method now calculates layout using default font metrics.
berghofe
parents:
6541
diff
changeset
|
5 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
3599 | 6 |
|
7 |
This class defines the window in which the graph is displayed. It |
|
8 |
contains methods for handling events such as collapsing / uncollapsing |
|
9 |
nodes of the graph. |
|
10 |
***************************************************************************/ |
|
11 |
||
12 |
package GraphBrowser; |
|
13 |
||
14 |
import java.awt.*; |
|
6541 | 15 |
import java.awt.event.*; |
3599 | 16 |
import java.io.*; |
17 |
import java.util.*; |
|
18 |
import awtUtilities.*; |
|
19 |
||
6541 | 20 |
public class GraphView extends Canvas implements MouseListener, MouseMotionListener { |
21 |
Graph gra, gra2; |
|
22 |
GraphBrowser browser; |
|
23 |
Vertex v = null; |
|
24 |
Vector collapsed = new Vector(10,10); |
|
25 |
Vector collapsedDirs = new Vector(10,10); |
|
3599 | 26 |
TreeBrowser tb; |
27 |
long timestamp; |
|
6541 | 28 |
Vertex highlighted = null; |
29 |
Dimension size; |
|
30 |
boolean parent_needs_layout; |
|
3599 | 31 |
|
32 |
public void setTreeBrowser(TreeBrowser br) { |
|
33 |
tb=br; |
|
34 |
} |
|
35 |
||
6541 | 36 |
public GraphBrowser getBrowser() { return browser; } |
3599 | 37 |
|
38 |
public Graph getGraph() { return gra; } |
|
39 |
||
40 |
public Graph getOriginalGraph() { return gra2; } |
|
41 |
||
42 |
public GraphView(Graph gr,GraphBrowser br) { |
|
43 |
gra2=gr; |
|
6541 | 44 |
browser=br; |
3599 | 45 |
gra=(Graph)(gra2.clone()); |
6541 | 46 |
parent_needs_layout = true; |
47 |
size = new Dimension(0, 0); |
|
48 |
addMouseListener(this); |
|
49 |
addMouseMotionListener(this); |
|
3599 | 50 |
} |
51 |
||
52 |
public void PS(String fname,boolean printable) throws IOException { |
|
11811
38721b2c6f57
PS method now calculates layout using default font metrics.
berghofe
parents:
6541
diff
changeset
|
53 |
Graph gra3 = (Graph)gra.clone(); |
38721b2c6f57
PS method now calculates layout using default font metrics.
berghofe
parents:
6541
diff
changeset
|
54 |
gra3.layout(null); |
38721b2c6f57
PS method now calculates layout using default font metrics.
berghofe
parents:
6541
diff
changeset
|
55 |
gra3.PS(fname,printable); |
3599 | 56 |
} |
57 |
||
6541 | 58 |
public void paint(Graphics g) { |
3599 | 59 |
gra.draw(g); |
6541 | 60 |
if (highlighted!=null) highlighted.drawBox(g,Color.white); |
61 |
size = new Dimension(gra.max_x-gra.min_x, gra.max_y-gra.min_y); |
|
62 |
if (parent_needs_layout) { |
|
63 |
parent_needs_layout = false; |
|
64 |
getParent().doLayout(); |
|
65 |
} |
|
3599 | 66 |
} |
67 |
||
6541 | 68 |
public Dimension getPreferredSize() { |
69 |
return size; |
|
70 |
} |
|
71 |
||
72 |
public void mouseMoved(MouseEvent evt) { |
|
73 |
int x = evt.getX() + gra.min_x; |
|
74 |
int y = evt.getY() + gra.min_y; |
|
3599 | 75 |
|
76 |
Vertex v2=gra.vertexAt(x,y); |
|
77 |
Graphics g=getGraphics(); |
|
78 |
g.translate(-gra.min_x,-gra.min_y); |
|
79 |
if (highlighted!=null) { |
|
80 |
highlighted.drawBox(g,Color.lightGray); |
|
81 |
highlighted=null; |
|
82 |
} |
|
83 |
if (v2!=v) { |
|
84 |
if (v!=null) v.removeButtons(g); |
|
85 |
if (v2!=null) v2.drawButtons(g); |
|
86 |
v=v2; |
|
87 |
} |
|
88 |
} |
|
89 |
||
6541 | 90 |
public void mouseDragged(MouseEvent evt) {} |
91 |
||
3599 | 92 |
/*****************************************************************/ |
93 |
/* This method is called if successor / predecessor nodes (whose */ |
|
94 |
/* numbers are stored in Vector c) of a certain node should be */ |
|
95 |
/* displayed again */ |
|
96 |
/*****************************************************************/ |
|
97 |
||
98 |
void uncollapse(Vector c) { |
|
99 |
collapsed.removeElement(c); |
|
100 |
collapseNodes(); |
|
101 |
} |
|
102 |
||
103 |
/*****************************************************************/ |
|
104 |
/* This method is called by class TreeBrowser when directories */ |
|
105 |
/* are collapsed / uncollapsed by the user */ |
|
106 |
/*****************************************************************/ |
|
107 |
||
108 |
public void collapseDir(Vector v) { |
|
109 |
collapsedDirs=v; |
|
110 |
||
111 |
collapseNodes(); |
|
112 |
} |
|
113 |
||
114 |
/*****************************************************************/ |
|
115 |
/* Inflate node again */ |
|
116 |
/*****************************************************************/ |
|
117 |
||
118 |
public void inflateNode(Vector c) { |
|
119 |
Enumeration e1; |
|
120 |
||
121 |
e1=collapsedDirs.elements(); |
|
122 |
while (e1.hasMoreElements()) { |
|
123 |
Directory d=(Directory)(e1.nextElement()); |
|
124 |
if (d.collapsed==c) { |
|
125 |
tb.selectNode(d.getNode()); |
|
126 |
return; |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
collapsed.removeElement(c); |
|
131 |
e1=gra2.getVertices(); |
|
132 |
while (e1.hasMoreElements()) { |
|
133 |
Vertex vx=(Vertex)(e1.nextElement()); |
|
134 |
if (vx.getUp()==c) vx.setUp(null); |
|
135 |
if (vx.getDown()==c) vx.setDown(null); |
|
136 |
} |
|
137 |
||
138 |
collapseNodes(); |
|
139 |
relayout(); |
|
140 |
} |
|
141 |
||
142 |
public void relayout() { |
|
6541 | 143 |
browser.showWaitMessage(); |
3599 | 144 |
highlighted=null; |
145 |
gra.layout(getGraphics()); |
|
146 |
v=null; |
|
6541 | 147 |
parent_needs_layout = true; |
3599 | 148 |
update(getGraphics()); |
6541 | 149 |
browser.showReadyMessage(); |
3599 | 150 |
} |
151 |
||
152 |
public void focusToVertex(int n) { |
|
153 |
Vertex vx=gra.getVertexByNum(n); |
|
154 |
if (vx!=null) { |
|
6541 | 155 |
ScrollPane scrollp = (ScrollPane)(getParent()); |
156 |
Dimension vpsize = scrollp.getViewportSize(); |
|
157 |
||
158 |
int x = vx.getX()-gra.min_x; |
|
159 |
int y = vx.getY()-gra.min_y; |
|
160 |
int offset_x = Math.min(scrollp.getHAdjustable().getMaximum(), |
|
161 |
Math.max(0,x-vpsize.width/2)); |
|
162 |
int offset_y = Math.min(scrollp.getVAdjustable().getMaximum(), |
|
163 |
Math.max(0,y-vpsize.height/2)); |
|
164 |
||
3599 | 165 |
Graphics g=getGraphics(); |
166 |
g.translate(-gra.min_x,-gra.min_y); |
|
6541 | 167 |
if (highlighted!=null) highlighted.drawBox(g,Color.lightGray); |
3599 | 168 |
vx.drawBox(g,Color.white); |
6541 | 169 |
highlighted=vx; |
170 |
scrollp.setScrollPosition(offset_x, offset_y); |
|
3599 | 171 |
} |
172 |
} |
|
173 |
||
174 |
/*****************************************************************/ |
|
175 |
/* Create new graph with collapsed nodes */ |
|
176 |
/*****************************************************************/ |
|
177 |
||
178 |
public void collapseNodes() { |
|
179 |
Enumeration e1=collapsed.elements(); |
|
180 |
gra=(Graph)(gra2.clone()); |
|
181 |
||
182 |
while (e1.hasMoreElements()) { |
|
183 |
Vector v1=(Vector)(e1.nextElement()); |
|
184 |
Vector v2=gra.decode(v1); |
|
185 |
if (!v2.isEmpty()) gra.collapse(v2,"[. . . .]",v1); |
|
186 |
} |
|
187 |
||
188 |
e1=collapsedDirs.elements(); |
|
189 |
||
190 |
while (e1.hasMoreElements()) { |
|
191 |
Directory d=(Directory)(e1.nextElement()); |
|
192 |
Vector v=gra.decode(d.getCollapsed()); |
|
193 |
if (!v.isEmpty()) |
|
194 |
gra.collapse(v,"["+d.getName()+"]",d.getCollapsed()); |
|
195 |
} |
|
196 |
} |
|
197 |
||
6541 | 198 |
public void mouseClicked(MouseEvent evt) { |
199 |
Vector code = null; |
|
3599 | 200 |
Vertex v2; |
6541 | 201 |
int x = evt.getX() + gra.min_x; |
202 |
int y = evt.getY() + gra.min_y; |
|
3599 | 203 |
|
204 |
if (v!=null) { |
|
205 |
int num=v.getNumber(); |
|
206 |
v2=gra2.getVertexByNum(num); |
|
207 |
if (v.leftButton(x,y)) { |
|
208 |
if (v.getUp()!=null) { |
|
209 |
code=v.getUp(); |
|
210 |
v2.setUp(null); |
|
211 |
v=null; |
|
212 |
uncollapse(code); |
|
213 |
relayout(); |
|
214 |
focusToVertex(num); |
|
215 |
} else { |
|
216 |
Vector vs=v2.getPreds(); |
|
217 |
code=gra2.encode(vs); |
|
218 |
v.setUp(code);v2.setUp(code); |
|
219 |
v=null; |
|
220 |
collapsed.insertElementAt(code,0); |
|
221 |
collapseNodes(); |
|
222 |
relayout(); |
|
223 |
focusToVertex(num); |
|
224 |
} |
|
225 |
} else if (v.rightButton(x,y)) { |
|
226 |
if (v.getDown()!=null) { |
|
227 |
code=v.getDown(); |
|
228 |
v2.setDown(null); |
|
229 |
v=null; |
|
230 |
uncollapse(code); |
|
231 |
relayout(); |
|
232 |
focusToVertex(num); |
|
233 |
} else { |
|
234 |
Vector vs=v2.getSuccs(); |
|
235 |
code=gra2.encode(vs); |
|
236 |
v.setDown(code);v2.setDown(code); |
|
237 |
v=null; |
|
238 |
collapsed.insertElementAt(code,0); |
|
239 |
collapseNodes(); |
|
240 |
relayout(); |
|
241 |
focusToVertex(num); |
|
242 |
} |
|
243 |
} else if (v.getInflate()!=null) { |
|
244 |
inflateNode(v.getInflate()); |
|
245 |
v=null; |
|
246 |
} else { |
|
6541 | 247 |
if (evt.getWhen()-timestamp < 400 && !(v.getPath().equals(""))) |
248 |
browser.viewFile(v.getPath()); |
|
249 |
timestamp=evt.getWhen(); |
|
3599 | 250 |
} |
251 |
} |
|
252 |
} |
|
253 |
||
6541 | 254 |
public void mouseExited(MouseEvent evt) { |
3599 | 255 |
Graphics g=getGraphics(); |
256 |
g.translate(-gra.min_x,-gra.min_y); |
|
257 |
if (highlighted!=null) { |
|
258 |
highlighted.drawBox(g,Color.lightGray); |
|
259 |
highlighted=null; |
|
260 |
} |
|
261 |
if (v!=null) v.removeButtons(g); |
|
262 |
v=null; |
|
263 |
} |
|
6541 | 264 |
|
265 |
public void mouseEntered(MouseEvent evt) {} |
|
266 |
||
267 |
public void mousePressed(MouseEvent evt) {} |
|
268 |
||
269 |
public void mouseReleased(MouseEvent evt) {} |
|
3599 | 270 |
} |