/**
 * This simple extension of the java.awt.Frame class
 * contains all the elements necessary to act as the
 * main window of an application.
 *
 * @author Robert Cohen (rfc)
 * @version JDSL 2
 */

import java.awt.*;

public class AuctionFrame extends Frame
{
  
  public AuctionFrame(AuctionSimulator sim)
    {
      this.sim = sim;
      // This code is automatically generated by Visual Cafe when you add
      // components to the visual environment. It instantiates and initializes
      // the components. To modify the code, only use code syntax that matches
      // what Visual Cafe can generate, or Visual Cafe may be unable to back
      // parse your Java file into its visual environment.
      
      //{{INIT_CONTROLS
      setLayout(null);
      setSize(478,301);
      setVisible(false);
      title.setText("Auction");
      add(title);
      title.setFont(new Font("Dialog", Font.BOLD, 24));
      title.setBounds(192,12,100,36);
      item.setText("Item:");
      item.setAlignment(java.awt.Label.RIGHT);
      add(item);
      item.setBounds(36,60,43,24);
      highbid.setText("High Bid:");
      highbid.setAlignment(java.awt.Label.RIGHT);
      add(highbid);
      highbid.setBounds(17,84,62,24);
      add(itemDesc);
      itemDesc.setBounds(96,60,174,24);
      add(bidDesc);
      bidDesc.setBounds(96,84,174,24);
      time.setText("Time");
      time.setAlignment(java.awt.Label.RIGHT);
      add(time);
      time.setBounds(312,60,43,12);
      remaining.setText("Remaining");
      remaining.setAlignment(java.awt.Label.RIGHT);
      add(remaining);
      remaining.setBounds(288,72,67,24);
      add(clock);
      clock.setBounds(372,50,50,50);
      yourBid.setText("Your Bid:");
      yourBid.setAlignment(java.awt.Label.RIGHT);
      add(yourBid);
      yourBid.setBounds(108,180,62,24);
      startBtn.setLabel("Start");
      add(startBtn);
      startBtn.setBackground(java.awt.Color.lightGray);
      startBtn.setBounds(120,216,48,26);
      withdrawBtn.setLabel("Withdraw");
      add(withdrawBtn);
      withdrawBtn.setBackground(java.awt.Color.lightGray);
      withdrawBtn.setBounds(180,216,72,26);
      withdrawBtn.setEnabled(false);
      resetBtn.setLabel("Reset");
      add(resetBtn);
      resetBtn.setBackground(java.awt.Color.lightGray);
      resetBtn.setBounds(264,216,48,26);
      quitBtn.setLabel("Quit");
      add(quitBtn);
      quitBtn.setBackground(java.awt.Color.lightGray);
      quitBtn.setBounds(324,216,48,26);
      add(msg);
      msg.setFont(new Font("Dialog", Font.ITALIC, 12));
      msg.setBounds(84,264,336,24);
      add(bidField);
      bidField.setBounds(180,177,190,30);
      numBidders.setText("Bidders:");
      numBidders.setAlignment(java.awt.Label.RIGHT);
      add(numBidders);
      numBidders.setBounds(17,108,62,24);
      add(biddersDesc);
      biddersDesc.setBounds(96,108,174,24);
      setTitle("Auction Simulator");
      //}}
      
      //{{INIT_MENUS
      //}}
      
      //{{REGISTER_LISTENERS
      SymWindow aSymWindow = new SymWindow();
      this.addWindowListener(aSymWindow);
      SymAction lSymAction = new SymAction();
      quitBtn.addActionListener(lSymAction);
      startBtn.addActionListener(sim);
      withdrawBtn.addActionListener(sim);
      resetBtn.addActionListener(sim);
      bidField.addActionListener(sim);
      clock.addCountDownTimerListener(sim);
      //}}
    }
  
  public AuctionFrame(AuctionSimulator sim, String title)
    {
      this(sim);
      setTitle(title);
    }
  
  /**
    * Shows or hides the component depending on the boolean flag b.
    * @param b  if true, show the component; otherwise, hide the component.
    * @see java.awt.Component#isVisible
    */
  public void setVisible(boolean b)
    {
      if(b)
	{
	  setLocation(50, 50);
	}	
      super.setVisible(b);
    }
  
  public void addNotify()
    {
      // Record the size of the window prior to calling parents addNotify.
      Dimension d = getSize();
      
      super.addNotify();
      
      if (fComponentsAdjusted)
	return;
      
      // Adjust components according to the insets
      setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height);
      Component components[] = getComponents();
      for (int i = 0; i < components.length; i++)
	{
	  Point p = components[i].getLocation();
	  p.translate(getInsets().left, getInsets().top);
	  components[i].setLocation(p);
	}
      fComponentsAdjusted = true;
    }
  
  // Used for addNotify check.
  boolean fComponentsAdjusted = false;
  
  //{{DECLARE_CONTROLS
  java.awt.Label title = new java.awt.Label();
  java.awt.Label item = new java.awt.Label();
  java.awt.Label highbid = new java.awt.Label();
  java.awt.Label itemDesc = new java.awt.Label();
  java.awt.Label bidDesc = new java.awt.Label();
  java.awt.Label time = new java.awt.Label();
  java.awt.Label remaining = new java.awt.Label();
  //java.awt.Canvas clock = new java.awt.Canvas();
  CountDownTimer clock = new CountDownTimer(25);
  java.awt.Label yourBid = new java.awt.Label();
  java.awt.Button startBtn = new java.awt.Button();
  java.awt.Button withdrawBtn = new java.awt.Button();
  java.awt.Button resetBtn = new java.awt.Button();
  java.awt.Button quitBtn = new java.awt.Button();
  java.awt.Label msg = new java.awt.Label();
  java.awt.TextField bidField = new java.awt.TextField();
  java.awt.Label numBidders = new java.awt.Label();
  java.awt.Label biddersDesc = new java.awt.Label();
  //}}
  
  AuctionSimulator sim;
  
  //{{DECLARE_MENUS
  //}}
  
  class SymWindow extends java.awt.event.WindowAdapter
  {
    public void windowClosing(java.awt.event.WindowEvent event)
      {
	Object object = event.getSource();
	if (object == AuctionFrame.this)
	  AuctionFrame_WindowClosing(event);
      }
  }
  
  void AuctionFrame_WindowClosing(java.awt.event.WindowEvent event)
    {
      quit();
    }
  
  private void quit() {
    System.exit(0);
  }
  
  public void setItem(String s) {
    itemDesc.setText(s);
  }
  
  public void setHighBid(double d) {
    bidDesc.setText(String.valueOf(d));
  }
  
  public void setNumBidders(int n) {
    biddersDesc.setText(String.valueOf(n));
  }
  
  public void setMsg(String message) {
    msg.setText(message);
  }
  
  public void setBid(String str) {
    bidField.setText(str);
  }
  
  public String getName() {
    return "Your Name";
  }
  
  public double getBid() {
    String str = bidField.getText();
    setBid("");
    try {
      return Double.parseDouble(str);
    } catch (NumberFormatException e) {
      return 0; 
    }
  }
  
  public void clear() {
    setItem("");
    setHighBid(0);
    setNumBidders(0);
    setMsg("");
  }
  
  
  class SymAction implements java.awt.event.ActionListener
  {
    public void actionPerformed(java.awt.event.ActionEvent event)
      {
	Object object = event.getSource();
	if (object == quitBtn)
	  quitBtn_ActionPerformed(event);
      }
  }
  
  
  void quitBtn_ActionPerformed(java.awt.event.ActionEvent event)
    {
      // to do: code goes here.
      
      quitBtn_ActionPerformed_Interaction1(event);
    }
  
  void quitBtn_ActionPerformed_Interaction1(java.awt.event.ActionEvent event)
    {
      try {
	this.quit();
      } catch (Exception e) {
      }
    }
}