/**
 * 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.
 */

package net.sourceforge.chart2d.demo;


import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import net.sourceforge.chart2d.Chart2D;


/**
 * Shared support code for local demo launchers.
 */
public final class DemoFrameSupport {

  private DemoFrameSupport() {}


  public static void normalizeChartSizes (
    JTabbedPane panes, boolean dynamicSizeCalc, Dimension chartSize,
    Dimension paneSize) {

    if (dynamicSizeCalc) {
      int maxWidth = 0;
      int maxHeight = 0;
      for (int i = 0; i < panes.getTabCount(); ++i) {
        Chart2D chart2D = (Chart2D)panes.getComponentAt (i);
        chart2D.pack();
        Dimension size = chart2D.getSize();
        maxWidth = maxWidth > size.width ? maxWidth : size.width;
        maxHeight = maxHeight > size.height ? maxHeight : size.height;
      }
      Dimension maxSize = new Dimension (maxWidth, maxHeight);
      System.out.println (maxSize);
      for (int i = 0; i < panes.getTabCount(); ++i) {
        Chart2D chart2D = (Chart2D)panes.getComponentAt (i);
        chart2D.setSize (maxSize);
        chart2D.setPreferredSize (maxSize);
      }
      System.out.println (panes.getPreferredSize());
    }
    else {
      for (int i = 0; i < panes.getTabCount(); ++i) {
        Chart2D chart2D = (Chart2D)panes.getComponentAt (i);
        chart2D.setSize (chartSize);
        chart2D.setPreferredSize (chartSize);
      }
      panes.setPreferredSize (paneSize);
    }
  }


  public static JFrame buildFrame (
    Component content, String title, WindowAdapter closeHandler) {

    JFrame frame = new JFrame();
    frame.getContentPane().add (content);
    frame.setTitle (title);
    frame.addWindowListener (closeHandler);
    frame.pack();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation (
      (screenSize.width - frame.getSize().width) / 2,
      (screenSize.height - frame.getSize().height) / 2);
    return frame;
  }
}
