/*******************************************************************************
 *                      Copyright (c) 1999 Justin Couch
 *                               Java Source
 *
 * Raw J3D Tutorial
 *
 * Version History
 * Date        Version  Programmer
 * ----------  -------  ------------------------------------------
 * 01/08/1998  1.0.0    Justin Couch
 *
 ******************************************************************************/

// no package

// Standard imports
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.Vector3f;
import javax.vecmath.AxisAngle4f;

// Application specific imports
// none

/**
 * Test frame class for the dealing with J3D experimentation
 * <P>
 * Basic window consists of a menubar and a J3D window
 *
 * @author Justin Couch
 * @version 1.0.0
 */
public class J3dAWTFrame extends Frame
  implements ActionListener, WindowListener
{
  private MenuItem close_menu;
  private Canvas3D canvas;

  // The universe to hold everything in
  private UniverseManager universe;

  // The geometry information
  private ExampleGeometry geometry;

  /**
   * Construct the test frame with a menubar and 3D canvas
   */
  public J3dAWTFrame()
  {
    super("Java3D Tester");

    GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = env.getDefaultScreenDevice();
    GraphicsConfiguration config = device.getBestConfiguration(template);

    canvas = new Canvas3D(config);

    // add the canvas to this frame. Since this is the only thing added to
    // the main frame we don't care about layout managers etc.
    add(canvas, BorderLayout.CENTER);

    constructWorld();


    MenuBar menubar = new MenuBar();

    // File menu
    Menu file_menu = new Menu("File");
    menubar.add(file_menu);


    close_menu = new MenuItem("Exit");
    close_menu.addActionListener(this);
    file_menu.add(close_menu);

    Menu geometry_menu = new Menu("Geometry");
    createGeometryMenu(geometry_menu);
    menubar.add(geometry_menu);

    setMenuBar(menubar);
    addWindowListener(this);

    setSize(600, 600);
  }

  /**
   * Construct everything that we want in the basic test world
   */
  private void constructWorld()
  {
    // create the basic universe
    universe = new UniverseManager();

/*
    // create a light grey coloured background
    Background bg = new Background(0.5f, 0.5f, 0.5f);
    BoundingSphere bounds = new BoundingSphere();
    bounds.setRadius(1000);
    bg.setApplicationBounds(bounds);
    universe.addWorldObject(bg);
*/

    Camera cam = new Camera();
    Vector3f loc = new Vector3f(0, 0, 10.0f);
    cam.setLocation(loc);
    cam.setHeadLight(true);
    universe.addCamera(cam);

    cam.setCanvas(canvas);

    // add some geometry
    geometry = new ExampleGeometry();

    universe.addWorldObject(geometry);
    universe.makeLive();
  }

  /**
   * Create the geometry menu
   *
   * @param menu The menu that this all gets added to
   */
  private void createGeometryMenu(Menu menu)
  {
    // guaranteed this is always non-null
    String[] items = geometry.getAvailableItems();

    for(int i = 0; i < items.length; i++)
    {
      MenuItem menu_item = new IntAction(items[i], i);
      menu_item.addActionListener(this);
      menu.add(menu_item);
    }
  }

  /**
   * An mouse action has occurred. Used to process menu item selection.
   *
   * @param evt The event that caused this method to be called.
   */
  public void actionPerformed(ActionEvent evt)
  {
    Object src = evt.getSource();

    if(src == close_menu)
      System.exit(0);
    else if(src instanceof IntAction)
    {
      IntAction item = (IntAction)src;
      geometry.selectItem(item.getValue());
    }
  }

  /**
   * Ignored
   */
  public void windowActivated(WindowEvent evt)
  {
  }

  /**
   * Ignored
   */
  public void windowClosed(WindowEvent evt)
  {
  }

  /**
   * Exit the application
   *
   * @param evt The event that caused this method to be called.
   */
  public void windowClosing(WindowEvent evt)
  {
    System.exit(0);
  }

  /**
   * Ignored
   */
  public void windowDeactivated(WindowEvent evt)
  {
  }

  /**
   * Ignored
   */
  public void windowDeiconified(WindowEvent evt)
  {
  }

  /**
   * Ignored
   */
  public void windowIconified(WindowEvent evt)
  {
  }

  /**
   * Ignored
   */
  public void windowOpened(WindowEvent evt)
  {
  }

  /**
   * Start the application....
   */
  public static void main(String[] args)
  {
    Frame frame = new J3dAWTFrame();
    frame.setVisible(true);
  }
}
