/*
 * ScatterPlotExample.java
 *
 * Copyright 2005-2006                                
 * Performance Research Laboratory, University of Oregon
 */
package edu.uoregon.tau.vis;

import java.awt.Canvas;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyListener;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLException;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
import javax.swing.JPanel;

import edu.uoregon.tau.vis.VisRenderer.Vec;


/* </pre>
 *    
 * <P>CVS $Id: ScatterPlotExample.java,v 1.6 2007/12/07 02:05:22 amorris Exp $</P>
 * @author	Alan Morris
 * @version	$Revision: 1.6 $
 */
public class ScatterPlotExample extends JFrame {

	static class VisCanvas {

		private GLCanvas glCanvas;

		/**
		 * Creates a <tt>VisCanvas</tt> with the given <tt>VisRenderer</tt>.
		 * 
		 * @param visRenderer
		 *            <tt>VisRenderer</tt> to use.
		 */
		public VisCanvas(VisRenderer visRenderer) {

			GLCapabilities caps = new GLCapabilities(null);

			// FSAA (Full Screen Anti-Aliasing)
			if (visRenderer.getFSAA()) {
				caps.setSampleBuffers(true);
				caps.setNumSamples(4);
				caps.setHardwareAccelerated(true);
			}

			boolean tryStereo = true;
			// Fisher Price machines segfault if you try to use stereo
			String os = System.getProperty("os.name").toLowerCase();
			if (os.startsWith("mac os x")) {
				tryStereo = false;
			}

			// ask for stereo, if available
			try {
				caps.setStereo(tryStereo);
				glCanvas = new GLCanvas(caps);
			} catch (GLException gle) {
				caps.setStereo(false);
				glCanvas = new GLCanvas(caps);
			}

			glCanvas.setSize(200, 200);
			glCanvas.addGLEventListener(visRenderer);

			// for testing
			// canvas.addGLEventListener(new Gears.GearRenderer());

			// visRenderer.set

		}

		/**
		 * Adds a key listener to the underlying glCanvas.
		 * 
		 * @param keyListener
		 *            the key listener to add.
		 */
		public void addKeyListener(KeyListener keyListener) {
			glCanvas.addKeyListener(keyListener);
		}

		/**
		 * Returns the actual GLCanvas as a Canvas.
		 * 
		 * @return the actual GLCanvas as a Canvas.
		 */
		public Canvas getActualCanvas() {
			return glCanvas;
		}

		/**
		 * Returns the height of the <tt>Canvas</tt>.
		 * 
		 * @return the height of the <tt>Canvas</tt>.
		 */
		public int getHeight() {
			return glCanvas.getHeight();
		}

		/**
		 * Returns the width of the <tt>Canvas</tt>.
		 * 
		 * @return the width of the <tt>Canvas</tt>.
		 */
		public int getWidth() {
			return glCanvas.getWidth();
		}

	}// VisCanvas

    /**
	 * 
	 */
	private static final long serialVersionUID = -1659573170925191527L;
	private VisCanvas visCanvas;
	private VisRenderer visRenderer;

	// JSplitPane jSplitPane;

	private ScatterPlotExample() {

        // Create and set up the window.
        Container frame = this.getContentPane();//new JFrame("ScatterPlotExample");


		super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// setupMenus();
        // Create some values
        float values[][] = new float[100][4];
        for (int i = 0; i < 100; i++) {
            values[i][0] = i * (float) Math.sin(i); // value for the x axis
            values[i][1] = i * (float) Math.cos(i); // value for the y axis
            values[i][2] = i * i; // value for the z axis
            values[i][3] = i; // value for the color axis
        }

        // Create the visRenderer and register it with the canvas
       visRenderer = new VisRenderer();
        visRenderer.setVisCanvasListener(this);
        // Create the canvas
        visCanvas = new VisCanvas(visRenderer);
        visCanvas.getActualCanvas().setSize(600,600);
       
        


        // Create the scatterPlot
		ScatterPlot scatterPlot = new ScatterPlot();
		// PlotFactory.createScatterPlot("x axis", "y axis", "z axis",
		// "color axis", values, true, colorScale);
		scatterPlot.setValues(values);

        // Set the size
        scatterPlot.setSize(10, 10, 10);

        // point at the center of the scatterPlot
        visRenderer.setAim(new Vec(5, 5, 5));

        // Add the drawable objects to the visRenderer (the scatterPlot will draw the axes)
        visRenderer.addShape(scatterPlot);



        // Add everything to a JPanel and add the panel to the frame
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.add(visCanvas.getActualCanvas(), new GridBagConstraints(0, 0, 1, 1, 0.9, 1.0, GridBagConstraints.WEST, 
                GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 1, 1));
        panel.setPreferredSize(new Dimension(5, 5));
		panel.setSize(800, 800);
		frame.add(panel);
        //frame.p
        //frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.

    	ScatterPlotExample spe = new ScatterPlotExample();
		spe.setSize(800, 800);
		// spe.pack();
    	spe.setVisible(true);
    	
    	
    }

	public void createNewCanvas() {
		visCanvas = new VisCanvas(visRenderer);
		JPanel panel = new JPanel();
		GridBagConstraints gdb = new GridBagConstraints(0, 0, 1, 1, 0.9, 1.0, GridBagConstraints.WEST, 
                GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 1, 1);
		panel.add(visCanvas.getActualCanvas(),gdb);

		
	}
	
	

}
