//------------------------------------------------------------------------------
// LabelDialog.java:
//		Implementation of "control creator" class LabelDialog
//------------------------------------------------------------------------------
import java.awt.*;
import DialogLayout;
import java.applet.*;

public class LabelDialog
{
  Container    m_Parent       = null;
  boolean      m_fInitialized = false;
  DialogLayout m_Layout;

  // Control definitions
  //--------------------------------------------------------------------------
  Button        IDOK;
  Button        IDCANCEL;
  Label         IDC_STATIC1;
  TextField     IDC_EDIT1;


  // Constructor
  //--------------------------------------------------------------------------
  public LabelDialog (Container parent)
  {
    m_Parent = parent;
  }

  // Initialization.
  //--------------------------------------------------------------------------
  public boolean CreateControls()
  {
    // CreateControls should be called only once
    //----------------------------------------------------------------------
    if (m_fInitialized || m_Parent == null)
      return false;

    // m_Parent must be extended from the Container class
    //----------------------------------------------------------------------
    if (!(m_Parent instanceof Container))
      return false;

    // Since a given font may not be supported across all platforms, it
    // is safe to modify only the size of the font, not the typeface.
    //----------------------------------------------------------------------
    Font OldFnt = m_Parent.getFont();
    if (OldFnt != null)
      {
	Font NewFnt = new Font(OldFnt.getName(), OldFnt.getStyle(), 12);

	m_Parent.setFont(NewFnt);
      }

    // All position and sizes are in dialog logical units, so we use a
    // DialogLayout as our layout manager.
    //----------------------------------------------------------------------
    m_Layout = new DialogLayout(m_Parent, 186, 95);
    m_Parent.setLayout(m_Layout);
    m_Parent.addNotify();

    Dimension size   = m_Layout.getDialogSize();
    Insets    insets = m_Parent.insets();
		
    m_Parent.resize(insets.left + size.width  + insets.right,
		    insets.top  + size.height + insets.bottom);

    // Control creation
    //----------------------------------------------------------------------
    IDOK = new Button ("OK");
    m_Parent.add(IDOK);
    m_Layout.setShape(IDOK, 36, 64, 50, 14);

    IDCANCEL = new Button ("Cancel");
    m_Parent.add(IDCANCEL);
    m_Layout.setShape(IDCANCEL, 103, 64, 50, 14);

    IDC_STATIC1 = new Label ("Enter Label Text:", Label.LEFT);
    m_Parent.add(IDC_STATIC1);
    m_Layout.setShape(IDC_STATIC1, 65, 15, 65, 10);

    IDC_EDIT1 = new TextField ("");
    m_Parent.add(IDC_EDIT1);
    m_Layout.setShape(IDC_EDIT1, 15, 31, 155, 14);

    m_fInitialized = true;
    return true;
  }
}

class LDialog extends Dialog implements Runnable
{
  private LabelDialog ldlg = null;
  private DLSim theApplet = null;
  private Labelable lbable = null;

  LDialog(DLSim theApplet, Labelable lbable)
  {
    super(theApplet.myFrame, "Set Label Text", true);
    this.theApplet = theApplet;
    this.lbable = lbable;
    setFont(new Font("Arial", Font.PLAIN, 12));
    ldlg = new LabelDialog(this);
    ldlg.CreateControls();
  }

  public void run()
  {
    try {
      Thread.sleep(100);
    } catch (Exception e) {}
    show();
  }

  public boolean action(Event evt, Object what)
  {
    Object target = evt.target;
    if (target instanceof Button) {
      Button button = (Button)target;
      if (button.getLabel().compareTo("OK") == 0) {
	lbable.putLabel(ldlg.IDC_EDIT1.getText());
	theApplet.pegboard.repaint(lbable.bx(), lbable.by(),
				   4*DLobj.WID, DLobj.HT);
      }
      hide();
    }
    return true;
  }

  public boolean handleEvent(Event evt)
  {
    switch (evt.id)
      {
      case Event.WINDOW_DESTROY:
	dispose();
	return true;
      default:
	return super.handleEvent(evt);
      }			 
  }

}

class CDialog extends Dialog implements Runnable
{
  private LabelDialog cdlg = null;
  private DLSim theApplet = null;
  private DLclock theClock = null;

  CDialog(DLSim theApplet, DLclock theClock)
  {
    super(theApplet.myFrame, "Enter Clock Cycle", true);
    this.theApplet = theApplet;
    this.theClock = theClock;
    setFont(new Font("Arial", Font.PLAIN, 12));
    cdlg = new LabelDialog(this);
    cdlg.CreateControls();
    cdlg.IDC_STATIC1.setText("Enter Clock Cycle Length (in ms):");
    cdlg.m_Layout.setShape(cdlg.IDC_STATIC1, 25, 15, 125, 10);
  }

  public void run()
  {
    try {
      Thread.sleep(100);
    } catch (Exception e) {}
    show();
  }

  public boolean action(Event evt, Object what)
  {
    Object target = evt.target;
    if (target instanceof Button) {
      Button button = (Button)target;
      if (button.getLabel().compareTo("OK") == 0) {
	try {
	  int newSpeed = Integer.parseInt(cdlg.IDC_EDIT1.getText());
	  theClock.speed = newSpeed;
	  theApplet.pegboard.repaint(theClock.bx, theClock.by,
				     theClock.WIDTH, theClock.HTH);
	} catch (NumberFormatException e) {}
      }
      hide();
    }
    return true;
  }

  public boolean handleEvent(Event evt)
  {
    switch (evt.id)
      {
      case Event.WINDOW_DESTROY:
	dispose();
	return true;
      default:
	return super.handleEvent(evt);
      }			 
  }

}
