001: package kawa;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import gnu.mapping.*;
006: import gnu.text.Path;
007:
008: import gnu.expr.Language;
009: import kawa.standard.Scheme;
010:
011: /** A Frame containing a Kawa read-eval-print loop.
012: * @author Albert Ting <alt@artisan.com> (original base)
013: * @author Per Bothner (extensive changes).
014: */
015:
016: public class GuiConsole extends Frame implements ActionListener {
017: private static String CLOSE = "Close";
018: private static String EXIT = "Exit";
019: private static String NEW = "New";
020: private static String NEW_SHARED = "New (Shared)";
021: private static String PURGE_MESSAGE = "Purge Buffer";
022:
023: static int window_number = 0;
024:
025: Language language;
026: Environment environment;
027: Future thread;
028:
029: gnu.text.QueueReader in_r;
030: OutPort out_p, err_p;
031:
032: MessageArea message = null;
033:
034: public static void main(String[] args) {
035: Language language = Scheme.getInstance();
036: new GuiConsole();
037: }
038:
039: public GuiConsole() {
040: this (Language.getDefaultLanguage(), Environment.getCurrent(),
041: false);
042: }
043:
044: public GuiConsole(Language language, Environment penvironment,
045: boolean shared) {
046: super ("Kawa");
047: this .language = language;
048:
049: in_r = new gnu.text.QueueReader();
050: message = new MessageArea(in_r);
051: window_number++;
052: kawa.repl.exitIncrement();
053:
054: out_p = new OutPort(message.getStdout(), true, Path
055: .valueOf("<msg_stdout>"));
056: err_p = new OutPort(message.getStderr(), true, Path
057: .valueOf("<msg_stderr>"));
058: InPort in_p = new GuiInPort(in_r, Path.valueOf("<msg_stdin>"),
059: out_p, message);
060:
061: this .setLayout(new BorderLayout(0, 0));
062:
063: this .add("Center", message);
064:
065: setupMenus();
066: //pack();
067: setLocation(100 * window_number, 50 * window_number);
068: setSize(700, 500);
069: setVisible(true);
070:
071: thread = new Future(new kawa.repl(language), penvironment,
072: in_p, out_p, err_p);
073: Environment env = thread.getEnvironment();
074: if (shared)
075: env.setIndirectDefines();
076: this .environment = env;
077: thread.start();
078: }
079:
080: void close() {
081: in_r.appendEOF();
082: dispose();
083: // Give thread chance to finish and clean up
084: try {
085: Thread.sleep(100);
086: } catch (InterruptedException ex) {
087: }
088: // Thread.stop is deprecated in JDK 1.2, but I see no good
089: // alternative. (Thread.destroy is not implemented!)
090: thread.stop();
091: kawa.repl.exitDecrement();
092: }
093:
094: private void setupMenus() {
095: MenuBar menubar;
096: Menu fileMenu;
097: Menu utilitiesMenu;
098:
099: MenuItem menuItem;
100:
101: WindowListener windowExitCmd = new WindowAdapter() {
102: public void windowClosing(WindowEvent e) {
103: close();
104: }
105: };
106:
107: // Create the menubar
108: menubar = new MenuBar();
109: fileMenu = new Menu("File");
110: utilitiesMenu = new Menu("Utilities");
111:
112: menubar.add(fileMenu);
113: menubar.add(utilitiesMenu);
114:
115: menuItem = new MenuItem(NEW);
116: menuItem.addActionListener(this );
117: fileMenu.add(menuItem);
118:
119: menuItem = new MenuItem(NEW_SHARED);
120: menuItem.addActionListener(this );
121: fileMenu.add(menuItem);
122:
123: menuItem = new MenuItem(CLOSE);
124: menuItem.addActionListener(this );
125: fileMenu.add(menuItem);
126:
127: menuItem = new MenuItem(EXIT);
128: menuItem.addActionListener(this );
129: this .addWindowListener(windowExitCmd);
130: fileMenu.add(menuItem);
131:
132: menuItem = new MenuItem(PURGE_MESSAGE);
133: menuItem.addActionListener(this );
134: utilitiesMenu.add(menuItem);
135:
136: this .setMenuBar(menubar);
137: }
138:
139: public void actionPerformed(ActionEvent e) {
140: String cmd = e.getActionCommand();
141:
142: if (cmd.equals(NEW))
143: new GuiConsole(language, Environment.getGlobal(), false);
144: else if (cmd.equals(NEW_SHARED))
145: new GuiConsole(language, environment, true);
146: else if (cmd.equals(EXIT))
147: System.exit(0);
148: else if (cmd.equals(CLOSE))
149: close();
150: else if (cmd.equals(PURGE_MESSAGE)) {
151: message.deleteOldText();
152: } else
153: OutPort.outDefault().println("Unknown menu action: " + cmd);
154: }
155: }
|