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