import java.awt.*;
import conveyor;

/*
  Date October 12, 1996
  Version 1.0b1
  Does: Demonstrates a mini-applet that sends communicates with the Ticker
        Tape to update what the ticker is displaying. It does this through
        a text entry field and a "Do It" button.

  Coder: John Criswick, Ottawa, criswick@conveyor.com, www.conveyor.com

  Code Updates at: http://www.conveyor.com/conveyor-java.html

  Copyright (c) 1996, John R. Criswick. All rights reserved.

  Right to use: As this is fairly simple Java code, I grant right to
        use and modification. Of course, I warrant or guarantee nothing
        at all as a result of anyone using this code.

*/

public class belt extends java.applet.Applet {
  Button  		doIt;
  TextField		stringField;

  public String getAppletInfo() {
    return new String("belt 1.0b1 Oct.12.96 by John Criswick, Ottawa, criswick@conveyor.com");
  }

  /**
    * This applet consists of a text field and a button
    */
  public void init() {

    System.out.println(getAppletInfo());

	setLayout(null);

    doIt = new Button("Do It");
    doIt.reshape(36, 20, bounds().width - 48, bounds().height - 20);
    add(doIt);

    stringField = new TextField(100);
    stringField.reshape(8, 16, 100, 24);
    add(stringField);

  }

  /**
    * Make sure things are positioned approximately reasonable!
    */
  public void reshape(int x, int y, int width, int height) {
    super.reshape(x, y, width, height);
    if (doIt != null) {
      doIt.reshape(bounds().width - 48, (bounds().height - 20)/2, 36, 20);
    }
    if (stringField != null) {
      stringField.reshape(8, (bounds().height - (getGraphics().getFontMetrics().getHeight() + 4))/2, 160, getGraphics().getFontMetrics().getHeight() + 4);
    }
  }

  /* Pipe all paint calls through to update
  */
  public void paint(Graphics g) {
    update(g);
  }

  /* Just paint the background white and do a black frame
  */
  public final synchronized void update (Graphics g) {
    Color  oldColour;

    oldColour = g.getColor();

    g.setColor(Color.white);
    g.fillRect(0, 0, bounds().width, bounds().height);
    g.setColor(Color.black);
    g.drawRect(0, 0, bounds().width-1, bounds().height-1);

    g.setColor(oldColour);

  }

  /**
    * This is where the string gets sent to the conveyor Ticker Tape.
    * The Do It button sends an action message out when the user
    * clicks it.
    *
    * We know, from the HTML example used, that the name of the
    * ticker tape is "criswick", we get a reference to the ticker
    * tape applet through getAppletContext() and then coerce that
    * to type conveyor so that we may call the DisplayThisText()
    * method in the ticker tape applet.
    *
    */
  public boolean action(Event evt, Object what) {
    if (what.equals("Do It")) {
      System.out.println("Doing it = "+stringField.getText());
      conveyor  theTicker = (conveyor) getAppletContext().getApplet("criswick");
      theTicker.DisplayThisText(stringField.getText());
      return (true);
    }
    return (super.action(evt, what));
  }
}
