/**
 * Chart2D, a java library for drawing two dimensional charts.
 * Copyright (C) 2001 Jason J. Simas
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


import net.sourceforge.chart2d.*;
import net.sourceforge.chart2d.demo.DemoFrameSupport;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.Method;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;


/**
 * A combined Chart2D demo with top-level tabs for LB, LL, and Pie charts.
 */
@SuppressWarnings ("removal")
public class CombinedChart2DFrameDemo extends JApplet {


  private static final String DEMO_VERSION = "2.0";
  private static final String[] LB_TAB_LABELS = {
    "Bar", "Bar with Regions", "Bar with Trend Line", "Bar with Cat Coloring",
    "Bar True Stacked", "Line", "Line with Regions", "Filled Line True Stacked",
    "Line with Large Dataset", "Scatter / Dot", "Line and Dot", "Standard Overlay"
  };
  private static final String[] LL_TAB_LABELS = {
    "Bar", "Bar with Regions", "Bar with Trend Line", "Bar with Cat Coloring",
    "Bar True Stacked", "Line", "Line with Regions", "Filled Line True Stacked",
    "Line with Large Dataset", "Scatter / Dot", "Line and Dot", "Standard Overlay"
  };
  private static final String[] DEMO_METHOD_SUFFIXES = {
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"
  };

  private JFrame frame = null;
  private static boolean isApplet = true;


  public static void main (String[] args) {

    isApplet = false;
    CombinedChart2DFrameDemo demo = new CombinedChart2DFrameDemo();
    demo.init();
    demo.start();
  }


  public void init() {

    JTabbedPane topLevelPanes = new JTabbedPane();
    topLevelPanes.addTab ("LB", buildDemoTabs (
      new LBChart2DFrameDemo(), "getChart2DDemo", LB_TAB_LABELS));
    topLevelPanes.addTab ("LL", buildDemoTabs (
      new LLChart2DFrameDemo(), "getChart2DDemo", LL_TAB_LABELS));
    topLevelPanes.addTab ("Pie", buildPieTabs());

    frame = DemoFrameSupport.buildFrame (
      topLevelPanes, "CombinedChart2DFrameDemo " + DEMO_VERSION,
      new WindowAdapter() {
        public void windowClosing (WindowEvent e) {
          destroy();
    } } );

    if (!isApplet) start();
  }


  public void start() {
    frame.setVisible (true);
  }


  public void destroy() {

    if (frame != null) frame.dispose();
    if (!isApplet) System.exit (0);
  }


  private JTabbedPane buildDemoTabs (
    Object demo, String methodPrefix, String[] tabLabels) {

    JTabbedPane panes = new JTabbedPane();
    for (int i = 0; i < tabLabels.length; ++i) {
      panes.addTab (
        tabLabels[i],
        invokeChartBuilder (demo, methodPrefix + DEMO_METHOD_SUFFIXES[i]));
    }
    normalizeChartSizes (panes);
    return panes;
  }


  private Chart2D invokeChartBuilder (Object demo, String methodName) {

    try {
      Method method = demo.getClass().getDeclaredMethod (methodName, new Class[0]);
      method.setAccessible (true);
      return (Chart2D)method.invoke (demo, new Object[0]);
    }
    catch (Exception e) {
      throw new RuntimeException ("Unable to build chart with " + methodName, e);
    }
  }


  private void normalizeChartSizes (JTabbedPane panes) {

    Dimension maxSize = new Dimension (561, 214);
    for (int i = 0; i < panes.getTabCount(); ++i) {
      Chart2D chart2D = (Chart2D)panes.getComponentAt (i);
      chart2D.setSize (maxSize);
      chart2D.setPreferredSize (maxSize);
    }
    panes.setPreferredSize (new Dimension (566 + 5, 280 + 5));
  }


  private JTabbedPane buildPieTabs() {

    JTabbedPane panes = new JTabbedPane();
    PieChart2D chart2D = createPieDemoChart();
    Dimension size = new Dimension (561, 214);
    chart2D.setSize (size);
    chart2D.setPreferredSize (size);
    panes.addTab ("LOC per Class", chart2D);
    panes.setPreferredSize (new Dimension (566 + 5, 280 + 5));
    return panes;
  }


  private PieChart2D createPieDemoChart() {

    Object2DProperties object2DProps = new Object2DProperties();
    object2DProps.setObjectTitleText ("LOC per Class");

    Chart2DProperties chart2DProps = new Chart2DProperties();
    chart2DProps.setChartDataLabelsPrecision (-3);

    LegendProperties legendProps = new LegendProperties();
    String[] legendLabels =
      {"PieChart2D", "LBChart2D", "LLChart2D", "GraphChart2D", "Chart2D", "Object2D"};
    legendProps.setLegendLabelsTexts (legendLabels);

    Dataset dataset = new Dataset (6, 1, 1);
    dataset.set (0, 0, 0, .419f);
    dataset.set (1, 0, 0, .284f);
    dataset.set (2, 0, 0, .284f);
    dataset.set (3, 0, 0, .714f);
    dataset.set (4, 0, 0, .193f);
    dataset.set (5, 0, 0, .241f);

    MultiColorsProperties multiColorsProps = new MultiColorsProperties();
    PieChart2DProperties pieChart2DProps = new PieChart2DProperties();

    PieChart2D chart2D = new PieChart2D();
    chart2D.setObject2DProperties (object2DProps);
    chart2D.setChart2DProperties (chart2DProps);
    chart2D.setLegendProperties (legendProps);
    chart2D.setDataset (dataset);
    chart2D.setMultiColorsProperties (multiColorsProps);
    chart2D.setPieChart2DProperties (pieChart2DProps);

    if (!chart2D.validate (false)) chart2D.validate (true);

    return chart2D;
  }
}






