GraphView.java 7.1 KB

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