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