/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.print.PageFormat;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import ilog.views.chart.IlvChart;
import ilog.views.chart.data.IlvDefaultDataSet;
import ilog.views.chart.print.IlvChartFlowObject;
import ilog.views.chart.renderer.IlvBarChartRenderer;
import ilog.views.util.IlvProductUtil;
import ilog.views.util.print.IlvFlow;
import ilog.views.util.print.IlvPrintableDocument;
import ilog.views.util.print.IlvPrintingController;
import ilog.views.util.print.IlvUnit;

/**
 * A simple class that illustrate how chart and test can be mixed in a flow.
 */
public class PrintFlowExample extends JFrame {
  /** The printing controller that handles the printing process. */
  IlvPrintingController controller;

  /**
   * Initializes a new <code>PrintFlowExample</code> object.
   */
  public PrintFlowExample() {
    super("Printing Charts Together With Text");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new BorderLayout());
    IlvChart chart = createChart();

    // The toolbar to holds printing controls.
    JToolBar toolbar = new JToolBar(JToolBar.HORIZONTAL);
    toolbar.add(new AbstractAction("Print Setup") {
      Override
      public void actionPerformed(ActionEvent evt) {
        printSetup();
      }
    });
    toolbar.add(new AbstractAction("Print Preview") {
      Override
      public void actionPerformed(ActionEvent evt) {
        printPreview();
      }
    });

    // Creates the document.
    IlvPrintableDocument document = new IlvPrintableDocument("ChartInFlow", PageFormat.PORTRAIT);
    // Creates the printing controller, and associates it with our document.
    controller = new IlvPrintingController(document);
    // Gets the flow from the document.
    IlvFlow flow = document.getFlow();
    // Creates a new TextStyle to format the paragraphs and texts.
    IlvFlow.TextStyle style = new IlvFlow.TextStyle();
    // Initialize a new paragraph. Object of this paragraph are aligned with
    // a center alignment, and text is rendered using a Dialog-20 font.
    style.setAlignment(IlvFlow.TextStyle.CENTER_ALIGNMENT);
    style.setFont(new Font("Dialog", Font.PLAIN, 20));
    // apply the style
    flow.setTextStyle(style);
    // add a title.
    flow.add("A Chart in a Flow");
    // creates a new paragraph, keeping the current text style.
    flow.newLine();
    // Adds a chart to the flow. The chart is center justified, and covers
    // 60% of the page width, with a aspect ratio of 0.75 between its width
    // and height.
    IlvChartFlowObject printChart = new IlvChartFlowObject(chart, flow, 60, 0, .75f);
    flow.add(printChart, IlvFlow.BOTTOM_ALIGNMENT);
    // Creates a new paragraph: first set its text style then create it with
    // newLine().
    style.setAlignment(IlvFlow.TextStyle.LEFT_ALIGNMENT);
    style.setFont(new Font("Dialog", Font.PLAIN, 12));
    flow.setTextStyle(style);
    flow.newLine();
    // A the text.
    String text = "A flow consists on a list of printable objects of different types  (text, charts, manager, etc...) that are printed sequencially in a document."
        + " Flow objects are instances of the ilog.views.printing.IlvFlow class and are directly obtained from the printable document by means of the "
        + "IlvPrintableDocument.getFlow() method (Note that theres only one flow per document). A flow object is responsible for creating the document "
        + "pages and performing their layout, positionning the printable object according to various printing parameters that may affect the pages format.";
    flow.add(text);
    // Creates a new paragraph. The text style is changed to specify a new
    // font. This paragraph contains a text object and a chart.
    style.setFont(new Font("Dialog", Font.PLAIN, 10));
    flow.setTextStyle(style);
    flow.newLine();
    flow.newLine();
    text = "Note: A flow may also mix chart and text in the same line";
    flow.add(text);
    // Creates the chart. The chart is centered on the text baseline.
    printChart = new IlvChartFlowObject(chart, flow, new IlvUnit.Dimension(6, 5, IlvUnit.CM));
    flow.add(printChart, IlvFlow.CENTER_BASELINE);

    getContentPane().add(toolbar, BorderLayout.NORTH);
    getContentPane().add(chart, BorderLayout.CENTER);
    setSize(400, 300);
  }

  /**
   * Show the preview panel.
   */
  private void printPreview() {
    controller.printPreview(this);
  }

  /**
   * Show the chart setup dialog.
   */
  private void printSetup() {
    controller.setupDialog(this, true, true);
  }

  /**
   * Returns a configured IlvChart instance.
   */
  protected IlvChart createChart() {
    IlvChart chart = new IlvChart();
    chart.setAntiAliasing(true);
    IlvBarChartRenderer r = new IlvBarChartRenderer();
    chart.addRenderer(r, new IlvDefaultDataSet("DataSet 1", new double[] { 5, 1, 0, 9, 6, 11, 13, 5, 3, 9 }));
    return chart;
  }

  public static final void main(String[] args) {
    // This sample uses JViews Charts features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Perforce JViews Charts Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(IlvProductUtil.JViews_Charts_Deployment);

    SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        PrintFlowExample frame = new PrintFlowExample();
        frame.setVisible(true);
      }
    });
  }
}