/* * cs101 Graphical Console * * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. * For more information, see the * CS101 homepage or email . * * Copyright (C) 1998 Massachusetts Institute of Technology. * Please do not redistribute without obtaining permission. */ package cs101.awt; import java.awt.*; import java.awt.event.*; import cs101.util.queue.Queue; import cs101.util.queue.DefaultQueue; import cs101.util.queue.EmptyQueueException; /** * A graphical helper class for Java Console IO. Contains appropiate * console read and print methods.

* * Exists mostly to ease the differences between the numerous (and * sometimes broken) IDE java consoles.

* * Copyright (c) 1998 Massachusetts Institute of Technolgoy * * @author Todd C. Parnell, tparnell@ai.mit.edu * @version $Id: Console.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $ * * @see cs101.io.Console */ public class Console extends Frame { /** Buffer for input lines not yet read. */ private Queue inputLines; private TextArea ta; /** * Create and display a new Console. */ public Console() { super("CS101 Console"); this.inputLines = new DefaultQueue(); this.ta = new TextArea("", 24, 80, TextArea.SCROLLBARS_BOTH); ta.setFont(new Font("Monospaced", Font.PLAIN, 10)); ta.addKeyListener(new ConsoleKeyAdapter(this)); this.add("Center", this.ta); this.pack(); this.show(); this.addWindowListener(new WindowAdapter() { public void windowClosing(ActionEvent e) { Console.this.setVisible(false); } }); } /** * Write a String to Console. The String written is concatenated * with a newline. */ public void println(String s) { this.print(s+"\n"); } /** * Write a line to Console. The String written is not * concatenated with a newline. */ public void print(String s) { this.ta.append(s); } /** * Read a line from Console. The String returned is not terminated * with a newline. * * @return the String the user typed */ public String readln() { boolean success = false; String temp = ""; while (!success) { try { temp = (String)this.inputLines.dequeue(); success = true; } catch (EmptyQueueException eqe) { try { Thread.sleep(100); } catch (InterruptedException ie) {} } } return temp; } /** * Insert a line into the user input queue. Text inserted in this manner * will appear to readln() calls. */ void addInputLine(String newInputLine) { this.inputLines.enqueue(newInputLine); } /** The KeyAdapter used by Console to record user input. */ class ConsoleKeyAdapter extends KeyAdapter { private Console myConsole; private StringBuffer curInputLine; public ConsoleKeyAdapter(Console myConsole) { this.myConsole = myConsole; this.curInputLine = new StringBuffer(); } public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if (c == '\u0008' && curInputLine.length() > 0) { this.curInputLine.deleteCharAt(this.curInputLine.length() - 1); return; } if (c == '\n') { // don't include newline with String this.myConsole.addInputLine(this.curInputLine.toString()); this.curInputLine = new StringBuffer(); } else if (e.isActionKey()) return; else this.curInputLine.append(c); } } } /* * $Log: Console.java,v $ * Revision 1.1.1.1 2002/06/05 21:56:32 root * CS101 comes to Olin finally. * * Revision 1.3 2000/04/30 02:33:50 mharder * Changed import statements to confirm to new cs101 packages. * * Revision 1.2 1999/02/08 20:52:13 tparnell * fixed references to Queue * * Revision 1.1 1998/07/28 17:46:19 tparnell * Initial revision. Uses cs101.util.Queue as abstraction. * */