001    /*
002     * cs101 Graphical Console 
003     *
004     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
005     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
006     * CS101 homepage</a> or email <las@ai.mit.edu>.
007     *
008     * Copyright (C) 1998 Massachusetts Institute of Technology.
009     * Please do not redistribute without obtaining permission.
010     */
011    
012    package cs101.awt;
013    
014    import java.awt.*;
015    import java.awt.event.*;
016    import cs101.util.queue.Queue;
017    import cs101.util.queue.DefaultQueue;
018    import cs101.util.queue.EmptyQueueException;
019    
020    /**
021     * A graphical helper class for Java Console IO.  Contains appropiate
022     * console read and print methods.<p>
023     *
024     * Exists mostly to ease the differences between the numerous (and
025     * sometimes broken) IDE java consoles.<p>
026     *
027     * Copyright (c) 1998 Massachusetts Institute of Technolgoy
028     *
029     * @author   Todd C. Parnell, tparnell@ai.mit.edu
030     * @version  $Id: Console.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $
031     *
032     * @see cs101.io.Console 
033     */
034    public class Console extends Frame {
035    
036      /** Buffer for input lines not yet read. */
037      private Queue inputLines;
038      
039      private TextArea ta;
040      
041      /**
042       * Create and display a new Console.
043       */
044      public Console() {
045        super("CS101 Console");
046        
047        this.inputLines = new DefaultQueue();
048        
049        this.ta = new TextArea("", 24, 80, TextArea.SCROLLBARS_BOTH);
050        ta.setFont(new Font("Monospaced", Font.PLAIN, 10));
051        ta.addKeyListener(new ConsoleKeyAdapter(this));
052        
053        this.add("Center", this.ta);
054        this.pack();
055        this.show();
056            
057        this.addWindowListener(new WindowAdapter() {
058          public void windowClosing(ActionEvent e) {
059            Console.this.setVisible(false);
060          }
061        });
062      }
063      
064      /**
065       * Write a String to Console.  The String written is concatenated
066       * with a newline.
067       */
068      public void println(String s) {
069        this.print(s+"\n");
070      }
071      
072      /**
073       * Write a line to Console.  The String written is <I>not</I>
074       * concatenated with a newline.
075       */
076      public void print(String s) {
077        this.ta.append(s);
078      }
079      
080      /**
081       * Read a line from Console.  The String returned is not terminated
082       * with a newline.
083       *
084       * @return the String the user typed
085       */
086      public String readln() {
087        boolean success = false;
088        String temp = "";
089        while (!success) {
090          try {
091            temp = (String)this.inputLines.dequeue();
092            success = true;
093          } catch (EmptyQueueException eqe) {
094            try { Thread.sleep(100); }
095            catch (InterruptedException ie) {}
096          }
097        }
098        return temp;
099      }
100      
101      /**
102       * Insert a line into the user input queue.  Text inserted in this manner
103       * will appear to readln() calls.
104       */
105      void addInputLine(String newInputLine) {
106        this.inputLines.enqueue(newInputLine);
107      }
108      
109      
110      /** The KeyAdapter used by Console to record user input. */
111      class ConsoleKeyAdapter extends KeyAdapter {
112        
113        private Console myConsole;
114        private StringBuffer curInputLine;
115        
116        public ConsoleKeyAdapter(Console myConsole) {
117          this.myConsole = myConsole;
118          this.curInputLine = new StringBuffer();
119        }
120        
121        public void keyTyped(KeyEvent e) {
122          char c = e.getKeyChar();
123          if (c == '\u0008' && curInputLine.length() > 0) {
124            this.curInputLine.deleteCharAt(this.curInputLine.length() - 1);
125            return;
126          } 
127          if (c == '\n') {
128            // don't include newline with String
129            this.myConsole.addInputLine(this.curInputLine.toString());
130            this.curInputLine = new StringBuffer();
131          }
132          else if (e.isActionKey()) return;
133          else this.curInputLine.append(c);
134        }
135      } 
136    }
137    
138    /*
139     * $Log: Console.java,v $
140     * Revision 1.1.1.1  2002/06/05 21:56:32  root
141     * CS101 comes to Olin finally.
142     *
143     * Revision 1.3  2000/04/30 02:33:50  mharder
144     * Changed import statements to confirm to new cs101 packages.
145     *
146     * Revision 1.2  1999/02/08 20:52:13  tparnell
147     * fixed references to Queue
148     *
149     * Revision 1.1  1998/07/28 17:46:19  tparnell
150     * Initial revision.  Uses cs101.util.Queue as abstraction.
151     *
152     */
153    
154    
155    
156    
157    
158    
159    
160