/*
 * 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.
 */
package css;

import ilog.views.gantt.IlvGanttModel;
import ilog.views.gantt.model.general.IlvGeneralActivity;
import ilog.views.gantt.model.general.IlvGeneralConstraint;
import ilog.views.gantt.model.general.IlvGeneralReservation;
import ilog.views.gantt.model.general.IlvGeneralResource;
import ilog.views.schedule.IlvResourceDataChart;
import ilog.views.util.IlvProductUtil;
import ilog.views.util.styling.IlvStylable;
import shared.AbstractExample;
import shared.data.SimpleEngineeringProject;
import shared.swing.CSSComboBox;
import shared.swing.CustomizerPanel;
import shared.swing.ExampleFrame;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;


/**
 * This example illustrates how to style a Resource Data chart with CSS.
 * @proofread
 */
public class ResourceDataCSSExample extends AbstractExample {

  /**
   * The directory containing the data files (css+xml).
   */
  static final String DATA_DIRECTORY = "data";

  /**
   * The name of the default data file.
   */
  static final String DEFAULT_DATA_FILE = "data.xml";

  /**
   * The sample CSS files. Used in applets.
   */
  static final String[] CSS_FILES = {
    "summer"
  };

  /**
   * The chart.
   */
  private IlvResourceDataChart chart;

  {
    // This sample uses JViews Gantt features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Perforce JViews Gantt Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(
        IlvProductUtil.JViews_Gantt_Deployment);
  }

  /**
   * Initializes the example's user interface in the specified container.
   *
   * @param container The container that the example is running in. This will
   * be the <code>contentPane</code> of the <code>JApplet</code> or the
   * <code>JFrame</code>, depending on whether the example is run as an applet
   * or as an application.
   */
  Override
  public void init(Container container) {
    super.init(container);
    container.setLayout(new BorderLayout());

    // Create the chart.
    chart = createChart();
    setHelpID(chart, "Chart.helpID");

    // Create and bind the Gantt data model.
    final IlvGanttModel model = createGanttModel();
    chart.setGanttModel(model);

    // Add the chart to the ui.
    chart.setPreferredSize(new Dimension(550, 230));
    container.add(chart, BorderLayout.CENTER);

    JComponent cssPanel = createCSSCustomizer();
    container.add(cssPanel, BorderLayout.LINE_END);
    // Override the default behavior which maintains a constant zoom factor
    // instead to maintain fit-to-content
    container.addComponentListener(new ComponentAdapter() {
      Override
      public void componentResized(ComponentEvent e) {
        chart.setVisibleInterval(model.getRootActivity().getStartTime(),
            model.getRootActivity().getTimeInterval().getDuration());
      }
    });
    
  }

  /**
   * Creates the Resource Data chart.
   *
   * @return The resource data chart.
   */
  protected IlvResourceDataChart createChart() {
    IlvResourceDataChart chart = new IlvResourceDataChart();
    // Set the debug mask for CSS.
    chart.setStyleSheetDebugMask(IlvStylable.BAD_CLASS_MASK
                                 | IlvStylable.BAD_PROP_MASK
                                 | IlvStylable.WARNING_PROP_MASK
                                 | IlvStylable.FAILED_CONVERSIONS_MASK);
    // The style sheets provided with the sample enable the chart legend to
    // be movable. We set the default state to be the same. This prevents
    // the user from dragging the legend to float above the chart, disable
    // styling, and then be unable to move the legend out of the way.
    chart.getLegend().setMovable(true);
    return chart;
  }

  /**
   * Creates and returns the Gantt data model.
   *
   * @return The Gantt data model.
   */
  protected IlvGanttModel createGanttModel() {
    return new SimpleEngineeringProject(new IlvGeneralActivity.Factory(),
                                        new IlvGeneralResource.Factory(),
                                        new IlvGeneralConstraint.Factory(),
                                        new IlvGeneralReservation.Factory());
  }


  // =========================================
  // CSS
  // =========================================

  /**
   * Creates and returns a customizer for applying and viewing style sheets.
   *
   * @return The CSS customizer.
   */
  protected JComponent createCSSCustomizer() {
    CustomizerPanel panel = new CustomizerPanel();
    panel.addHeading("Style Sheet");

    // Create a JComboBox containing all the available .css files.
    CSSComboBox cssCombo = new CSSComboBox(this,
                                           chart,
                                           DATA_DIRECTORY,
                                           getCSSFiles());

    panel.add(cssCombo);

    return panel;
  }

  /**
   * This method returns the list of CSS files used in an applet.
   *
   * @return The list of CSS files.
   */
  protected String[] getCSSFiles() {
    return CSS_FILES;
  }

  /**
   * This method is overridden to disable the view
   * customizer for this demo.
   *
   * @return <code>null</code> to disable the view customizer.
   */
  protected JComponent createViewCustomizer() {
    return null;
  }


  // =========================================
  // Example Application
  // =========================================

  /**
   * Returns the title of the sample.
   *
   * @return The title of the example.
   */
  Override
  public String getTitle() {
    return "Resource Data Chart CSS Sample";
  }

  /**
   * Application mainline.
   *
   * @param args The command line arguments.
   */
  public static void main (String[] args) {
    ExampleFrame.createAndShowGUI(ResourceDataCSSExample.class);
  }

}