/**********************************************************************
 * Copyright (c) 2005, 2006 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * $Id: MethodTimePieChart.java,v 1.1 2006/07/07 23:08:07 nmehrega Exp $
 * 
 * Contributors: 
 * IBM - Initial API and implementation
 **********************************************************************/

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.ApplicationFrame;

/**
 * EclipseCon 2006
 * This class uses a 3D pie chart to graph method execution times
 * 
 * @author Navid Mehregani
 */
public class MethodTimePieChart extends ApplicationFrame {

	private static DefaultPieDataset _dataset = new DefaultPieDataset();
	
    /**
     * Default constructor.
     *
     * @param title  the frame title.
     */
    public MethodTimePieChart() {
    	      
        super("Method Execution Time");
        setContentPane(createChartPanel());
    }
    
    
    /**
     * Used to create the chart 
     * 
     * @param dataset  the dataset for the chart
     * 
     * @return The chart that's created 
     */
    private static JFreeChart createChart() {
        
    	/* Create a 3D pie chart */
        JFreeChart chart = ChartFactory.createPieChart3D(
            "Method Execution Time (in seconds)",  // chart title
            _dataset,             	  			   // dataset to use
            false,               	  			   // include legend
            true,
            false
        );

        /* Customize the chart */
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setSectionOutlinesVisible(false);
        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        plot.setNoDataMessage("No data available");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        return chart;
        
    }
    
    /**
     * Used to create a panel for the demo
     * 
     * @return A panel.
     */
    public static JPanel createChartPanel() {
    	ChartPanel chartPanel = null;
    	
    	try
    	{
	        JFreeChart chart = createChart();
	        chartPanel = new ChartPanel(chart);
	        chartPanel.setPreferredSize(new Dimension(700, 430));
    	} catch (Exception e)
    	{
    		System.out.println("CAUGHT AN EXCEPTION: " + e.getMessage()); // TODO
    	}
        return chartPanel;
    }
    
    /**
     *  Used to get the chart's dataset 
     */
    public DefaultPieDataset getDataset()
    {
    	synchronized(_dataset)
    	{
    		return _dataset;
    	}
    }
    
    /**
     * Include the execution time of the given method on the pie chart 
     * 
     * @param value  The value of the method on the chart
     * @param methodName  The name of the method 
     */  
    public void includeMethod(double value, String methodName)
    {
    	synchronized(_dataset)
    	{
    		_dataset.setValue(methodName, value);
    	}
    	
    }   

}
