lib/browser/GraphBrowser/GraphView.java
changeset 3599 89cbba12863d
child 6541 d3ac35b2bfbf
equal deleted inserted replaced
3598:28b6670e415a 3599:89cbba12863d
       
     1 /***************************************************************************
       
     2   Title:      GraphBrowser/GraphView.java
       
     3   ID:         $Id$
       
     4   Author:     Stefan Berghofer, TU Muenchen
       
     5   Copyright   1997  TU Muenchen
       
     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.*;
       
    15 import java.io.*;
       
    16 import java.util.*;
       
    17 import awtUtilities.*;
       
    18 
       
    19 public class GraphView extends ScrollCanvas {
       
    20 	Graph gra,gra2;
       
    21 	GraphBrowser parent;
       
    22 	Vertex v=null;
       
    23 	Vector collapsed=new Vector(10,10);
       
    24 	Vector collapsedDirs=new Vector(10,10);
       
    25 	TreeBrowser tb;
       
    26 	long timestamp;
       
    27 	Vertex highlighted=null;
       
    28 
       
    29 	public void setTreeBrowser(TreeBrowser br) {
       
    30 		tb=br;
       
    31 	}
       
    32 
       
    33 	public GraphBrowser getBrowser() { return parent; }
       
    34 
       
    35 	public Graph getGraph() { return gra; }
       
    36 
       
    37 	public Graph getOriginalGraph() { return gra2; }
       
    38 
       
    39 	public GraphView(Graph gr,GraphBrowser br) {
       
    40 		gra2=gr;
       
    41 		parent=br;
       
    42 		gra=(Graph)(gra2.clone());
       
    43 	}
       
    44 
       
    45 	public void PS(String fname,boolean printable) throws IOException {
       
    46 		gra.PS(fname,printable);
       
    47 	}
       
    48 
       
    49 	public void paintCanvas(Graphics g)
       
    50 	{
       
    51 		set_size(gra.max_x-gra.min_x,gra.max_y-gra.min_y);
       
    52 		gra.draw(g);
       
    53 	}
       
    54 
       
    55 	public boolean mouseMove(Event evt,int x,int y) {
       
    56 		x+=gra.min_x;
       
    57 		y+=gra.min_y;
       
    58 
       
    59 		Vertex v2=gra.vertexAt(x,y);
       
    60 		Graphics g=getGraphics();
       
    61 		g.translate(-gra.min_x,-gra.min_y);
       
    62 		if (highlighted!=null) {
       
    63 			highlighted.drawBox(g,Color.lightGray);
       
    64 			highlighted=null;
       
    65 		}
       
    66 		if (v2!=v) {
       
    67 			if (v!=null) v.removeButtons(g);
       
    68 			if (v2!=null) v2.drawButtons(g);
       
    69 			v=v2;
       
    70 		}
       
    71 		return true;
       
    72 	}
       
    73 
       
    74 	/*****************************************************************/
       
    75 	/* This method is called if successor / predecessor nodes (whose */
       
    76 	/* numbers are stored in Vector c) of a certain node should be   */
       
    77         /* displayed again                                               */
       
    78 	/*****************************************************************/
       
    79 
       
    80 	void uncollapse(Vector c) {
       
    81 		collapsed.removeElement(c);
       
    82 		collapseNodes();
       
    83 	}
       
    84 
       
    85 	/*****************************************************************/
       
    86 	/* This method is called by class TreeBrowser when directories   */
       
    87 	/* are collapsed / uncollapsed by the user                       */
       
    88 	/*****************************************************************/
       
    89 
       
    90 	public void collapseDir(Vector v) {
       
    91 		collapsedDirs=v;
       
    92 
       
    93 		collapseNodes();
       
    94 	}
       
    95 
       
    96 	/*****************************************************************/
       
    97 	/*                      Inflate node again                       */
       
    98 	/*****************************************************************/
       
    99 
       
   100 	public void inflateNode(Vector c) {
       
   101 		Enumeration e1;
       
   102 
       
   103 		e1=collapsedDirs.elements();
       
   104 		while (e1.hasMoreElements()) {
       
   105 			Directory d=(Directory)(e1.nextElement());
       
   106 			if (d.collapsed==c) {
       
   107 				tb.selectNode(d.getNode());
       
   108 				return;
       
   109 			}
       
   110 		}
       
   111 
       
   112 		collapsed.removeElement(c);
       
   113 		e1=gra2.getVertices();
       
   114 		while (e1.hasMoreElements()) {
       
   115 			Vertex vx=(Vertex)(e1.nextElement());
       
   116 			if (vx.getUp()==c) vx.setUp(null);
       
   117 			if (vx.getDown()==c) vx.setDown(null);
       
   118 		}
       
   119 
       
   120 		collapseNodes();
       
   121 		relayout();
       
   122 	}
       
   123 
       
   124 	public void relayout() {
       
   125 		parent.showWaitMessage();
       
   126 		highlighted=null;
       
   127 		gra.layout(getGraphics());
       
   128 		v=null;
       
   129 		update(getGraphics());
       
   130 		parent.showReadyMessage();
       
   131 	}
       
   132 
       
   133 	public void focusToVertex(int n) {
       
   134 		Vertex vx=gra.getVertexByNum(n);
       
   135 		if (vx!=null) {
       
   136 			focus_to(vx.getX()-gra.min_x,vx.getY()-gra.min_y);
       
   137 			highlighted=vx;
       
   138 			Graphics g=getGraphics();
       
   139 			g.translate(-gra.min_x,-gra.min_y);
       
   140 			vx.drawBox(g,Color.white);
       
   141 		}
       
   142 	}
       
   143 
       
   144 	/*****************************************************************/
       
   145 	/*             Create new graph with collapsed nodes             */
       
   146 	/*****************************************************************/
       
   147 
       
   148 	public void collapseNodes() {
       
   149 		Enumeration e1=collapsed.elements();
       
   150 		gra=(Graph)(gra2.clone());
       
   151 
       
   152 		while (e1.hasMoreElements()) {
       
   153 			Vector v1=(Vector)(e1.nextElement());
       
   154 			Vector v2=gra.decode(v1);
       
   155 			if (!v2.isEmpty()) gra.collapse(v2,"[. . . .]",v1);
       
   156 		}
       
   157 
       
   158 		e1=collapsedDirs.elements();
       
   159 
       
   160 		while (e1.hasMoreElements()) {
       
   161 			Directory d=(Directory)(e1.nextElement());
       
   162 			Vector v=gra.decode(d.getCollapsed());
       
   163 			if (!v.isEmpty())
       
   164 				gra.collapse(v,"["+d.getName()+"]",d.getCollapsed());
       
   165 		}
       
   166 	}
       
   167 
       
   168 	public boolean mouseDown(Event evt,int x,int y) {
       
   169 		Vector code=null;
       
   170 		Vertex v2;
       
   171 		x+=gra.min_x;
       
   172 		y+=gra.min_y;
       
   173 
       
   174 		if (v!=null) {
       
   175 			int num=v.getNumber();
       
   176 			v2=gra2.getVertexByNum(num);
       
   177 			if (v.leftButton(x,y)) {
       
   178 				if (v.getUp()!=null) {
       
   179 					code=v.getUp();
       
   180 					v2.setUp(null);
       
   181 					v=null;
       
   182 					uncollapse(code);
       
   183 					relayout();
       
   184 					focusToVertex(num);
       
   185 				} else {
       
   186 					Vector vs=v2.getPreds();
       
   187 					code=gra2.encode(vs);
       
   188 					v.setUp(code);v2.setUp(code);
       
   189 					v=null;
       
   190 					collapsed.insertElementAt(code,0);
       
   191 					collapseNodes();
       
   192 					relayout();
       
   193 					focusToVertex(num);
       
   194 				}
       
   195 			} else if (v.rightButton(x,y)) {
       
   196 				if (v.getDown()!=null) {
       
   197 					code=v.getDown();
       
   198 					v2.setDown(null);
       
   199 					v=null;
       
   200 					uncollapse(code);
       
   201 					relayout();
       
   202 					focusToVertex(num);
       
   203 				} else {
       
   204 					Vector vs=v2.getSuccs();
       
   205 					code=gra2.encode(vs);
       
   206 					v.setDown(code);v2.setDown(code);
       
   207 					v=null;
       
   208 					collapsed.insertElementAt(code,0);
       
   209 					collapseNodes();
       
   210 					relayout();
       
   211 					focusToVertex(num);
       
   212 				}
       
   213 			} else if (v.getInflate()!=null) {
       
   214 				inflateNode(v.getInflate());
       
   215 				v=null;
       
   216 			} else {
       
   217 				if (evt.when-timestamp < 400 && !(v.getPath().equals("")))
       
   218 					parent.viewFile(v.getPath());
       
   219 				timestamp=evt.when;
       
   220 			}
       
   221 		}
       
   222 		return true;
       
   223 	}
       
   224 
       
   225 	public boolean mouseExit(Event evt,int x,int y) {
       
   226 		Graphics g=getGraphics();
       
   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 		return true;
       
   235 	}
       
   236 }