Package controller

Source Code of controller.SchemeController$commandListener

package controller;

//----- JDK Imports ------------------------------------------------------------
import java.awt.Cursor;
import java.io.IOException;
import java.net.URL;
import javax.swing.JOptionPane;
import javax.swing.text.Style;

//----- SISC Imports -----------------------------------------------------------
import sisc.interpreter.AppContext;
import sisc.interpreter.Context;
import sisc.interpreter.Interpreter;
import sisc.interpreter.SchemeException;

//----- Phoenix Imports --------------------------------------------------------
import view.SchemeWindow;

/**
* Video Phoenix
* Version 0.2.0
* Copyright (c) 2007 Lunderskov, Ian; Pan, Jiabei; Rebelsky, Samuel;
* Whisenhunt, Heather; Young, Ian; Zuleta Benavides, Luis
* All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* @author  Lunderskov, Ian; Pan, Jiabei; Rebelsky, Samuel; Whisenhunt, Heather;
*          Young, Ian; Zuleta Benavides, Luis
* @author  Glimmer Labs
* @version 0.2.0
*/
@SuppressWarnings("serial")
public class SchemeController
{
  /*--------*-------------------------------------------------------------------
   * Fields *
   *--------*/

  // one window for this controller
  public SchemeWindow window;

  // this controller's instance of Phoenix
  public PhoenixController phoenix;

  // SISC interpreter
  public Interpreter schemeHandler;

  // latest command received from SchemeWindow
  private static String command = "";

  // latest result received from schemeHandler
  private static String result = "";

  // whether messages/results should be printed to the window
  private static boolean display = true;

  // the (stoppable) infinite loop that listens for and executes commands
  public static Thread cmdListener = null;

  // whether command listener loop should be kept alive
  public static boolean alive = true;


  /*--------------*-------------------------------------------------------------
   * Constructors *
   *--------------*/

  /**
   * Create a new SchemeController
   *
   * @param phoenix  PhoenixController for this SchemeController
   */
  public SchemeController(PhoenixController phoenix)
  {
    // load SISC
    AppContext ctx = new AppContext();
    this.phoenix = phoenix;
    this.window = new SchemeWindow(this);

    // load heap
    try
    {
      ctx.addHeap(AppContext.openHeap(new URL("file://"
        + PhoenixController.getFile("sisc.shp"))));
    } // try
    catch (Exception e)
    {
      e.printStackTrace();
    } // catch (Exception)

    // create handler and command listener loop
    schemeHandler = Context.enter(ctx);
    SchemeController.cmdListener = new Thread(new commandListener());
    SchemeController.cmdListener.start();

    // print welcome message
    this.printStyledText("Welcome to Phoenix!\n", "output");
    this.printStyledText("Phoenix version 0.2.0, Copyright (c) 2007 Lunderskov,"
      + " Ian; Pan, Jiabei; Rebelsky, Samuel; Whisenhunt, Heather; Young, Ian;"
      + " Zuleta Benavides, Luis\n");
    this.printStyledText("Video Phoenix comes with ABSOLUTELY NO WARRANTY.\n");
    this.printStyledText("This is free software, and you are welcome"
      + " to redistribute it subject to certain terms and conditions; for"
      + " details see Help > About Phoenix.\n");
  } // SchemeController(PhoenixController)

  // listen for new commands
  private class commandListener implements Runnable
  {
    public void run()
    {
      // as long as the thread has not been terminated
      while (SchemeController.alive)
      {
        if (SchemeController.command == "")
        {
          Thread.yield();
        } // if (SchemeController.command == "")
        else
        {
          // non-empty command indicates that a new command has been entered
          try
          {
            // disable components for execution in progress
            window.inputArea.setEnabled(false);
            window.runButton.setEnabled(false);
            window.killButton.setEnabled(true);
            window.setTitle("Interaction Window - Busy");
            window.setCursor(new Cursor(Cursor.WAIT_CURSOR));
            // evaluate the command
            SchemeController.result = schemeHandler.eval(
              SchemeController.command).toString();
            if (SchemeController.display)
            {
              printStyledText(SchemeController.result + "\n", "output");
            } // if (SchemeController.display)
          } // try
          catch (SchemeException se)
          {
            SchemeController.command = "";
            String message = "";

            // user stopped execution
            if (se == null)
            {
              message = "Execution terminated.";
            } // if (se == null)
            else 
            {
              message = se.getMessageText();

              // these two messages indicate that the exception is thrown
              // on the Java side (i.e. thread was interrupted)
              if (message.contains("SchemeException")
                  || (message.contains("NullPointerException")))
              {
                message = "Execution terminated.";
              } // if (message.contains("SchemeException")) ...
            } // else

            // exception thrown on Java side
            printStyledText("Error: " + message + "\n", "scheme_error");

            // reset variables
            window.setInputText("");
            SchemeWindow.savedInput = "";
          } // catch (SchemeException)

          // IOExceptions thrown by bad Scheme commands
          catch (IOException ioe)
          {
            printStyledText("Error: Bad Scheme Syntax.\n", "scheme_error");
          } // catch (IOException)

          // re-enable components when execution is complete
          window.inputArea.setEnabled(true);
          window.runButton.setEnabled(true);
          window.killButton.setEnabled(false);
          window.setTitle("Interaction Window");
          window.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
          if ((!SchemeController.command.contains("(show"))
              && (!SchemeController.command.contains("(help")))
          {
            window.moveToFront();
          } // if (!SchemeController.command.contains("(show ")) ...
          window.inputArea.requestFocus();
          SchemeController.command = "";
        } // else
      } // while (SchemeController.alive)
    } // run()
  } // class commandListener


  /*---------*------------------------------------------------------------------
   * Methods *
   *---------*/

  /**
   * Evaluate a Scheme expression
   *
   * @param input  Command to be evaluated
   * @param disp   Whether the output should be displayed
   */
  public static void schemeCall(String input, boolean disp)
  {
    SchemeController.display = disp;
    SchemeController.command = input;
  } // schemeCall(String, boolean)

  /**
   * Print text in regular style
   *
   * @param str  String to be displayed
   */
  public void printStyledText(String str)
  {
    this.printStyledText(str, this.window.doc.getStyle("regular"));
  } // printStyledText(String)

  /**
   * Print text in a predefined style
   *
   * @param str    String to be displayed
   * @param style  Name of style in which text will be printed
   */
  public void printStyledText(String str, String style)
  {
    this.printStyledText(str, this.window.doc.getStyle(style));
  } // printStyledText(String, String)

  /**
   * Print text in a predefined style
   *
   * @param str    String to be displayed
   * @param style  Style in which text will be printed
   */
  public void printStyledText(String str, Style style)
  {
    try
    {
      // insert the string into the text window's styled document
      this.window.doc.insertString(this.window.doc.getLength(), str, style);

      // make sure new text is inserted at the end of the current text
      this.window.outputArea.setCaretPosition(this.window.doc.getLength());
    } // try
    catch (Exception e)
    {
      e.printStackTrace();
    } // catch (Exception)
  } // printStyledText(String, Style)

  /**
   * Show a dialog to read input from the user
   *
   * @param title  Title of the dialog
   * @param msg    Prompt message
   * @param def    Default text in the input box
   */
  public String readInput(String title, String msg, String def)
  {
    String input = (String)JOptionPane.showInputDialog(
      phoenix.getWindow(), msg, title,
      JOptionPane.PLAIN_MESSAGE, null, null, def);
    if (input == null)
    {
      return "";
    } // if (input == null)
    else
    {
      return input;
    } // else
  } // readInput(String, String, String)
} // class SchemeInterpreter
TOP

Related Classes of controller.SchemeController$commandListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.