1/*************************************************************************** 2 Title: GraphBrowser/GraphBrowser.java 3 Author: Stefan Berghofer, TU Muenchen 4 Options: :tabSize=4: 5 6 This is the graph browser's main class. It contains the "main(...)" 7 method, which is used for the stand-alone version, as well as 8 "init(...)", "start(...)" and "stop(...)" methods which are used for 9 the applet version. 10 Note: GraphBrowser is designed for the 1.1.x version of the JDK. 11***************************************************************************/ 12 13package GraphBrowser; 14 15import java.awt.*; 16import java.applet.*; 17import java.io.*; 18import java.util.*; 19import java.net.*; 20import awtUtilities.*; 21 22public class GraphBrowser extends Applet { 23 GraphView gv; 24 TreeBrowser tb=null; 25 String gfname; 26 27 static boolean isApplet; 28 static Frame f; 29 30 public GraphBrowser(String name) { 31 gfname=name; 32 } 33 34 public GraphBrowser() {} 35 36 public void showWaitMessage() { 37 if (isApplet) 38 getAppletContext().showStatus("calculating layout, please wait ..."); 39 else { 40 f.setCursor(new Cursor(Cursor.WAIT_CURSOR)); 41 } 42 } 43 44 public void showReadyMessage() { 45 if (isApplet) 46 getAppletContext().showStatus("ready !"); 47 else { 48 f.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 49 } 50 } 51 52 public void viewFile(String fname) { 53 try { 54 if (isApplet) 55 getAppletContext().showDocument(new URL(getDocumentBase(), fname), "_blank"); 56 else { 57 String path = gfname.substring(0, gfname.lastIndexOf('/') + 1); 58 Reader rd; 59 BufferedReader br; 60 String line, text = ""; 61 62 try { 63 rd = new BufferedReader(new InputStreamReader((new URL(fname)).openConnection().getInputStream())); 64 } catch (Exception exn) { 65 rd = new FileReader(path + fname); 66 } 67 br = new BufferedReader(rd); 68 69 while ((line = br.readLine()) != null) 70 text += line + "\n"; 71 72 if (fname.endsWith(".html")) { 73 /**** convert HTML to text (just a quick hack) ****/ 74 75 String buf=""; 76 char[] text2,text3; 77 int i,j=0; 78 boolean special=false, html=false; 79 char ctrl; 80 81 text2 = text.toCharArray(); 82 text3 = new char[text.length()]; 83 for (i = 0; i < text.length(); i++) { 84 char c = text2[i]; 85 if (c == '&') { 86 special = true; 87 buf = ""; 88 } else if (special) { 89 if (c == ';') { 90 special = false; 91 if (buf.equals("lt")) 92 text3[j++] = '<'; 93 else if (buf.equals("gt")) 94 text3[j++] = '>'; 95 else if (buf.equals("amp")) 96 text3[j++] = '&'; 97 } else 98 buf += c; 99 } else if (c == '<') { 100 html = true; 101 ctrl = text2[i+1]; 102 } else if (c == '>') 103 html = false; 104 else if (!html) 105 text3[j++] = c; 106 } 107 text = String.valueOf(text3); 108 } 109 110 Frame f=new TextFrame(fname.substring(fname.lastIndexOf('/')+1),text); 111 f.setSize(500,600); 112 f.show(); 113 } 114 } catch (Exception exn) { 115 System.err.println("Can't read file "+fname); 116 } 117 } 118 119 public void PS(String fname,boolean printable) throws IOException { 120 gv.PS(fname,printable); 121 } 122 123 public boolean isEmpty() { 124 return tb==null; 125 } 126 127 public void initBrowser(InputStream is, boolean noAWT) { 128 try { 129 Font font = noAWT ? null : new Font("Helvetica", Font.PLAIN, 12); 130 TreeNode tn = new TreeNode("Root", "", -1, true); 131 gv = new GraphView(new Graph(is, tn), this, font); 132 tb = new TreeBrowser(tn, gv, font); 133 gv.setTreeBrowser(tb); 134 Vector v = new Vector(10,10); 135 tn.collapsedDirectories(v); 136 gv.collapseDir(v); 137 138 ScrollPane scrollp1 = new ScrollPane(); 139 ScrollPane scrollp2 = new ScrollPane(); 140 scrollp1.add(gv); 141 scrollp2.add(tb); 142 scrollp1.getHAdjustable().setUnitIncrement(20); 143 scrollp1.getVAdjustable().setUnitIncrement(20); 144 scrollp2.getHAdjustable().setUnitIncrement(20); 145 scrollp2.getVAdjustable().setUnitIncrement(20); 146 Component gv2 = new Border(scrollp1, 3); 147 Component tb2 = new Border(scrollp2, 3); 148 GridBagLayout gridbag = new GridBagLayout(); 149 GridBagConstraints cnstr = new GridBagConstraints(); 150 setLayout(gridbag); 151 cnstr.fill = GridBagConstraints.BOTH; 152 cnstr.insets = new Insets(5,5,5,5); 153 cnstr.weightx = 1; 154 cnstr.weighty = 1; 155 cnstr.gridwidth = 1; 156 gridbag.setConstraints(tb2,cnstr); 157 add(tb2); 158 cnstr.weightx = 2.5; 159 cnstr.gridwidth = GridBagConstraints.REMAINDER; 160 gridbag.setConstraints(gv2,cnstr); 161 add(gv2); 162 } catch (IOException exn) { 163 System.err.println("\nI/O error while reading graph file."); 164 } catch (ParseError exn) { 165 System.err.println("\nParse error in graph file:"); 166 System.err.println(exn.getMessage()); 167 System.err.println("\nSyntax:\n<vertexname> <vertexID> <dirname> [ + ] <path> [ < | > ] [ <vertexID> [ ... [ <vertexID> ] ... ] ] ;"); 168 } 169 } 170 171 public void init() { 172 isApplet=true; 173 gfname=getParameter("graphfile"); 174 try { 175 InputStream is=(new URL(getDocumentBase(), gfname)).openConnection().getInputStream(); 176 initBrowser(is, false); 177 is.close(); 178 } catch (MalformedURLException exn) { 179 System.err.println("Invalid URL: "+gfname); 180 } catch (IOException exn) { 181 System.err.println("I/O error while reading "+gfname+"."); 182 } 183 } 184 185 public static void main(String[] args) { 186 isApplet=false; 187 try { 188 GraphBrowser gb=new GraphBrowser(args.length > 0 ? args[0] : ""); 189 if (args.length > 0) { 190 InputStream is=new FileInputStream(args[0]); 191 gb.initBrowser(is, args.length > 1); 192 is.close(); 193 } 194 if (args.length > 1) { 195 try { 196 if (args[1].endsWith(".ps")) 197 gb.gv.PS(args[1], true); 198 else if (args[1].endsWith(".eps")) 199 gb.gv.PS(args[1], false); 200 else 201 System.err.println("Unknown file type: " + args[1]); 202 } catch (IOException exn) { 203 System.err.println("Unable to write file " + args[1]); 204 } 205 } else { 206 f=new GraphBrowserFrame(gb); 207 f.setSize(700,500); 208 f.show(); 209 } 210 } catch (IOException exn) { 211 System.err.println("Can't open graph file "+args[0]); 212 } 213 } 214} 215 216