/*
 * Copyright (c) Ronald A Chernich 2004. The copyright holder grants the right
 * to use and copy this work provided this copyright message is retained.
 *
 * This program was written to be compatible with Java 1.1.x for maximum
 * compatibility with browser environments.
 */
import java.awt.*;
import java.util.*;
import java.applet.Applet;


/**
 * Applet wrapper for the command line cam table generating utility "CamCalc".
 */
public class CamTable
  extends Applet
{
  private int m_inc;
  private Font m_font;
  private String m_msg;
  private CamCalc m_calc;

  /**
   * Load the CamCalc args and create the calculator
   */
  public synchronized void init ()
  {
    m_font = new Font("Monospaced", Font.PLAIN, 12);
    try {
      String st = null;
      st = getParameter("angle");
      double angle = Double.parseDouble(st);
      st = getParameter("bc");
      double bc = Double.parseDouble(st);
      st = getParameter("lift");
      double lift = Double.parseDouble(st);
      st = getParameter("fr");
      double fr = Double.parseDouble(st);
      st = getParameter("rpm");
      int rpm = Integer.parseInt(st);
      st = getParameter("inc");
      m_inc = Integer.parseInt(st);
      st = getParameter("met");
      boolean isMetric = "true".equals(st);

      m_calc = new CamCalc(angle, lift, fr, bc, rpm);
      m_calc.setIncrement(m_inc);
      m_calc.setMetric(isMetric);
    }
    catch (Exception ex) {
      m_msg = ex.getMessage();
    }
  }

  public String getAppletInfo () {
    return "Calculates lift values of a harmonic cam.";
  }

  /**
   * Render the applet's drawing ares from the Cam model report strings.
   */
  public void paint (Graphics g)
  {
    if (m_msg != null) {
      formatString(m_msg, g, 12, 60);
    }
    else {
      g.setFont(m_font);
      int y = 18;
      String st = m_calc.getInDataSummary();
      y = formatString(st, g, 6, y);
      st = m_calc.getOutDataSummary();
      y = formatString(st, g, 6, y + 12);

      st = "Generated by CamCalc version " + m_calc.VERSION;
      y = formatString(st, g, 32, y + 24);
    }
  }

  /**
   * Breaks up the passed string into lines and renders them to the
   * graphics env, expanding tabs if present.
   * @param st String to render
   * @param g Graphics environment
   * @param x Horiz offset to start each line
   * @param y Vert offset for first line
   * @return Vert offset of next blank line
   */
  private int formatString (String st, Graphics g, int x, int y)
  {
    StringTokenizer tok = new StringTokenizer(st, "\n");
    while (tok.hasMoreTokens()) {
      g.drawString(expandTabs(tok.nextToken(), 8), x, y);
      y += 12;
    }
    return y;
  }

  /**
   * Expand embedded tab chars on specified column boundaries.
   * @param st String to expand
   * @param cols Column spacing for tabs.
   */
  private String expandTabs (String st, int cols)
  {
    if (st.indexOf("\t") < 0) {
      return st;
    }

    StringTokenizer tok = new StringTokenizer(st, "\t");
    StringBuffer sb = new StringBuffer();
    while (tok.hasMoreTokens()) {
      sb.append(tok.nextToken());
      int cnt = cols - (sb.length() % cols);
      while (cnt-- >= 0) {
        sb.append(' ');
      }
    }    
    return sb.toString();
  }


} // endof CamTable
