/////////////////////////////////////////////////////////////////////
//
// File: travio.java
//
// Purpose: Interface related classes (for easier IO). These classes
// are meant to be used in later programs, not just hw1.
//
// Authors: txe Travis Emmitt (emmitt@virginia.ed)
//
// Modifications:
// 13-SEP-1998 txe Initial creation
// 14-SEP-1998 txe Added comments, made InputHandler abstract
//
/////////////////////////////////////////////////////////////////////
import java.io.*;
import java.applet.*;
import java.awt.*;
/////////////////////////////////////////////////////////////////////
// TravIO and TravIO_Applet make it easy to make the code interface
// independent. TravIO is for standalone applications; it uses
// the System.* methods to write text to stdout, read text from
// the keyboard, and exit the program.
/////////////////////////////////////////////////////////////////////
class TravIO {
public void Println (String string) {
System.out.println (string);
}
public String Readln () {
try {
DataInputStream in = new DataInputStream (System.in);
return in.readLine().trim();
}
catch (Exception e) {
Println ("Error: couldn't read string from keyboard");
return null;
}
}
public int ReadInt () {
try {
DataInputStream in = new DataInputStream (System.in);
String string = in.readLine();
return Integer.parseInt (string.trim());
}
catch (Exception e) {
Println ("Warning: Invalid integer value, defaulting to -1");
return -1; // arbitrary; fine for now
}
}
public void Exit () {
System.exit (0);
}
}
/////////////////////////////////////////////////////////////////////
// TravIO_Applet provides the same methods as TravIO, but this time
// for applets. Specifically, it's to be used with BasicIOApplet,
// which is defined further below.
/////////////////////////////////////////////////////////////////////
class TravIO_Applet extends TravIO {
private BasicIOApplet applet;
public TravIO_Applet (BasicIOApplet a) {
applet = a; // need access to applet
}
public void Println (String string) {
applet.SetOutputString (string);
applet.repaint ();
}
public String Readln () {
return applet.GetInputString().trim();
}
public int ReadInt () {
String string = applet.GetInputString ();
try {
return Integer.parseInt (applet.GetInputString ());
}
catch (Exception e) {
Println ("Warning: Invalid integer value, defaulting to -1");
return -1; // arbitrary; fine for now
}
}
public void Exit () { // Note: this deletes the widgets, but
applet.removeAll (); // leaves the last (goodbye) message on
} // the screen, which is appropriate.
}
/////////////////////////////////////////////////////////////////
// BasicIOApplet is an applet with one input text field and one
// output line. It adds some new routines which are needed to
// allow TravIO_Applet access to the input and output fields.
// Also, BasicIOApplet requires the user to specify an input
// handler; this handler's HandleInput routine is called whenever
// the user types something into the input text field.
/////////////////////////////////////////////////////////////////
class BasicIOApplet extends Applet {
protected String output_string;
protected TextField text_field;
protected InputHandler handler;
public void paint (Graphics g) {
g.drawString (output_string, 10, 50);
}
public boolean action (Event e, Object arg) {
if (e.target instanceof TextField) {
// Make sure a handler exists //
if (handler == null) {
output_string = "NULL handler, exiting";
repaint ();
removeAll ();
return false;
}
handler.HandleInput ();
}
return true;
}
// The following specifies an InputHandler and then starts
// execution by providing an input box.
public void StartHandler (InputHandler h) {
handler = h;
add (text_field = new TextField (20));
repaint ();
}
// This sets the output string, so that the next time repaint()
// is called, the new string will be printed.
public void SetOutputString (String string) {
output_string = string;
}
// This gets the current String value of the input field, and
// then clears the field.
public String GetInputString () {
String string = text_field.getText ();
text_field.setText ("");
return string;
}
}
/////////////////////////////////////////////////////////////////////
// This is a skeleton for an input handler, made an abstract class.
// HandleInput() is meant to be called repeatedly by the standalone
// application or applet. It basically reads in input and then
// displays output and/or exits the program.
/////////////////////////////////////////////////////////////////////
abstract class InputHandler {
abstract public void HandleInput ();
}
////////////////////////////////////////////////////////////////////