added applet mode, terminal window
[fleet.git] / src / edu / berkeley / fleet / FleetApplet.java
1 package edu.berkeley.fleet;
2 import javax.swing.*;
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.applet.*;
6 import java.io.*;
7 import java.util.*;
8
9 public class FleetApplet extends Applet {
10
11     public static void main(String[] s) throws Exception {
12         Frame f = new Frame();
13         f.show();
14
15         PipedOutputStream po1 = new PipedOutputStream();
16         PipedInputStream pi1 = new PipedInputStream(po1);
17
18         PipedOutputStream po2 = new PipedOutputStream();
19         final PipedInputStream pi2 = new PipedInputStream(po2);
20
21         JPanel top = new JPanel();
22         top.setLayout(new BorderLayout());
23         final JTextArea text = new JTextArea(100, 10);
24         top.add(new JScrollPane(text), BorderLayout.CENTER);
25
26         JButton button = new JButton("interpret");
27         top.add(button, BorderLayout.SOUTH);
28
29         Term term = new Term(pi1, po2);
30         term.setMinimumSize(new Dimension(Integer.MAX_VALUE, 10));
31
32         JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
33                                         top,
34                                         term
35                                         );
36         f.add(jsp);
37
38         Log.log = new PrintWriter(new OutputStreamWriter(po1));
39
40         new Thread() {
41             public void run() {
42                 try {
43                     while(true)
44                         System.err.println(pi2.read());
45                 } catch (Exception e) {
46                     e.printStackTrace();
47                 }
48             }
49         }.start();
50         f.pack();
51
52         text.setFont(new Font("monospaced", 0, 20));
53         StringBuffer in = new StringBuffer();
54         BufferedReader br =
55             new BufferedReader(new InputStreamReader(FleetApplet.class.getClassLoader().getResourceAsStream("test.fleet")));
56         while(true) {
57             String str = br.readLine();
58             if (str==null) break;
59             in.append(str+"\n");
60         }
61         text.setText(in.toString());
62         button.addActionListener(new ActionListener() {
63                 public void actionPerformed(ActionEvent e) {
64                     try {
65                         System.err.println("launching");
66                         FleetParser.go(new StringReader(text.getText()));
67                         System.err.println("launched");
68                     } catch (Exception ex) {
69                         ex.printStackTrace();
70                     }
71                 }
72             });
73
74         jsp.setDividerLocation(0.5);
75         f.pack();
76         f.show();
77         f.setSize(800, 600);
78         f.doLayout();
79     }
80
81 }