package G4ip;

import pizza.util.Hashtable;
import java.awt.*;
import G4ip.Form.*;


/** A label with a surrounding box.
  */
class MyLabel extends Label {
  public MyLabel() { super(); }
  public MyLabel(String s) { super(s); }

  public void paint(Graphics g) {
    Dimension d = this.size();
    g.setColor(Color.gray);
    g.drawRect(0,0,d.width-1,d.height-1);
  }
}


/** Draws a frame on the screen containing a proof.
  */
public class ProofDisplay extends Frame {
  Button ok;
  Hashtable<int,Sequent> proof;      // contains the indexed sequents 

  /** The proof is represented in the hashtable.
    * @param proof is a hashtable; the structure of the proof (or tree) 
    * is mapped onto a sequence of integers; each integer is a key for
    * a sequent stored in the hashtable.
    */
  public ProofDisplay(Hashtable<int,Sequent> iproof) {
    super("Proof");
    ok = new Button("Close");
    proof = iproof;

    setLayout(new BorderLayout());
    // SOUTH
    Panel p = new Panel();
    p.add(ok);
    add("South",p);

    //CENTER
    Panel display = new Panel();
    printproof(display,1);
    add("Center", display);

    pack();
    show();
  }

  /** Recovers the structure of the proofs from the hashtable representation.
    */
  public void printproof(Panel p,int index) {
    MyLabel l = new MyLabel(proof.get(index).toString());
    l.setAlignment(MyLabel.CENTER);
    p.setLayout(new BorderLayout());
    p.add("South",l); 
    // axiom (do nothing)
    if ((proof.get(2*index) == null) && (proof.get(2*index+1) == null)) { 
      return;    
    }
    // rule with a single premise
    if ((proof.get(2*index) != null) && (proof.get(2*index+1) == null)) { 
      Panel np = new Panel();
      printproof(np,2*index);
      p.add("Center",np);
      return;  
    }
    // rule with two premises
    if ((proof.get(2*index) != null) && (proof.get(2*index+1) != null)) { 
      Panel np1 = new Panel();
      Panel np2 = new Panel();
      printproof(np1,2*index);
      printproof(np2,2*index+1);
      p.add("West",np1);
      p.add("East",np2);
      return;
    }
    
  }

  /** If either the "Close" or the "Window-destroy" button
    * is pressed, then close the window.
    */
  public boolean handleEvent(Event e)  {
    if (e.id == Event.ACTION_EVENT && e.target == ok) {
      this.finalize(); 
      return true;
    }
    if (e.id == Event.WINDOW_DESTROY) {
      this.finalize(); 
      return true;
    }
    return false;
  }

  /** Closes the window.
    */
  public void finalize()
    { this.dispose();  }
  
}


