import java.awt.*;
import java.awt.event.*;

import java.io.*;

public class Top40 extends Frame implements ActionListener {
  MenuItem open, load, save, custom, quit;
  Component theComp = null;

  public Top40 () {
    super ("Casey's Countdown");
    Menu file = new Menu ("File");
    open = file.add (new MenuItem ("Open Serialized Bean"));
    open.addActionListener (this);
    load = file.add (new MenuItem ("Load CentralPerk Class"));
    load.addActionListener (this);
    save = file.add (new MenuItem ("Save Bean"));
    save.addActionListener (this);
    save.setEnabled (false);
    file.addSeparator();
    custom = file.add (new MenuItem ("Customize Bean"));
    custom.addActionListener (this);
    custom.setEnabled (false);
    file.addSeparator();
    quit = file.add (new MenuItem ("Quit"));
    quit.addActionListener (this);
    MenuBar mb = new MenuBar();
    mb.add (file);
    setMenuBar (mb);
    enableEvents (AWTEvent.WINDOW_EVENT_MASK);
  }
  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      // Notify others we are closing
      super.processWindowEvent(e);
      System.exit(0);
    } else {
      super.processWindowEvent(e);
    }
  }
  public static void main (String args[]) {
    Frame f = new Top40();
    f.setSize(300, 300);
    f.show();
  }
  public void actionPerformed (ActionEvent e) {
    Object o = e.getSource();
    if (o == open) {
      loadBean();
    } else if (o == load) {
      newBean();
    } else if (o == save) {
      saveBean();
    } else if (o == custom) {
      customize();
    } else if (o == quit) {
      System.exit (0);
    }
  }
  private void loadBean () {
    setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    try {
      // Get File object for CentralPerk.ser

      // Stream to a FileInputStream

      // Stream to an ObjectInputStream

      // Read theComp

      // Close Everything

      // Add Bean to Center Quadrant

      // Call checkCustomize to setup menu

      // Save reference in theComp

      save.setEnabled (true);
    } catch (Exception e) {
      System.out.println ("Unable to load: CentralPerk.ser");
    } finally {
      setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
  }
  private void newBean () {
    try {
      setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
      // Instantiate Bean

      // Add Bean to Center Quadrant

      // Call checkCustomize to setup menu

      // Save reference in theComp

      save.setEnabled (true);
    } catch (Exception e) {
      System.out.println ("Unable to create");
    } finally {
      setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
  }
  private void saveBean () {
    setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    try {
      // Get File object for CentralPerk.ser

      // Stream to a FileOutputStream

      // Stream to an ObjectOutputStream

      // Write theComp

      // Close Everything

    } catch (Exception e) {
      System.out.println ("Unable to write");
    } finally {
      setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
  }
  private void customize () {
    try {
      // Get BeanInfo for theComp

      // Get the BeanDescriptor for the BeanInfo

      // Get the Customizer Class from the BeanDescriptor

      // Create an instance

      // Associate theComp with the Customizer

      final Dialog d = new Dialog (this, "Customizer", true);
      d.add ((Component)customizer, BorderLayout.CENTER);
      Button b = new Button ("Done");
      d.add (b, BorderLayout.SOUTH);
      b.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent e) {
            d.dispose();
          }
        }
      );
      d.pack();
      d.show();
    } catch (Exception e) {
      System.out.println ("Oops");
    }
  }
  private void checkCustomize(Class c) {
    try {
      BeanInfo bi = Introspector.getBeanInfo (c);
      BeanDescriptor bd = bi.getBeanDescriptor();
      custom.setEnabled (bd.getCustomizerClass() != null);
    } catch (IntrospectionException e) {
      System.out.println ("Oops");
      custom.setEnabled (false);
    }
  }
}
