/* * Console.java * * Created on June 24, 2007, 3:24 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ //package browser; import java .io.*; abstract public class Console { protected BufferedReader stdin; protected PrintWriter stdout; protected PrintWriter stderr; protected String prompt = "-> "; abstract protected String execute(String cmmd) throws AppError; public Console() { stdout = new PrintWriter( new BufferedWriter( new OutputStreamWriter(System.out)), true); stderr = new PrintWriter( new BufferedWriter( new OutputStreamWriter(System.out)), true); stdin = new BufferedReader( new InputStreamReader(System.in)); } public void controlLoop() { boolean more = true; String cmmd = " "; String result = " "; String done = "done"; stdout.println("type \"help\" for commands"); while(more) { try { stdout.print(prompt); stdout.flush(); // force the write cmmd = stdin.readLine(); if (cmmd.equals("quit")) { more = false; } else if (cmmd.equals("help")) { help(); } else if (cmmd.equals("about")) { about(); } else { // an application-specific command? result = execute(cmmd); stdout.println(result); } } catch(AppError exp) { more = handle(exp); } catch (IOException exp) { stderr.println("IO error, " + exp); } catch (Exception exp) { stderr.println("Serious error, " + exp); more = false; } } // while stdout.println("bye"); } // controlLoop // overidables: protected void help() { stdout.println("Console Help Menu:"); stdout.println(" about: displays application information"); stdout.println(" help: displays this message"); stdout.println(" quit: terminate application"); } protected void about() { stdout.println("Console Framework"); stdout.println("copyright (c) 2001, all rights reserved\n"); } protected boolean handle(AppError exp) { stderr.println("Application error, " + exp); return true; } }