/*
 * 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 blinking;

import java.awt.Container;
import java.util.logging.Level;

import javax.swing.JMenuBar;

import ilog.cpl.IlpNetwork;
import ilog.tgo.IltSystem;
import ilog.tgo.datasource.IltDefaultDataSource;
import shared.AbstractSample;

/**
 * Perforce JViews TGO blinking network component sample.
 */
public class Main extends AbstractSample {
  public static final String sampleLoggerName = "samples.network.blinking";
  public static final int sampleWidth = 769;
  public static final int sampleHeight = 647;

  public static final String sampleDataSourceFile = "network.xml";
  public static final String sampleConfigurationFile = "network.css";

  // JTGO components
  IlpNetwork networkComponent;
  IltDefaultDataSource dataSource;

  /**
   * Execute the main part of the sample.
   */
  Override
  protected void doSample(Container container) {
    try {
      // ------------------------
      // Initialize JTGO
      // ------------------------
      // Read a deployment descriptor file to initialize JTGO services
      IltSystem.Init("deploy.xml");

      // Initialize context and resources
      initContext(IltSystem.GetDefaultContext());

      // Create a datasource
      dataSource = new IltDefaultDataSource();

      // Read an XML file into the datasource
      dataSource.parse("network.xml");

      // Create a network component
      networkComponent = new IlpNetwork();

      // Load network component configuration
      reloadCSSStyles();

      // Connect the data source to the network component
      networkComponent.setDataSource(dataSource);

      // Add the component to the frame
      container.add(networkComponent);
    } catch (Exception e) {
      log.log(Level.SEVERE, "Exception caught while running sample");
      e.printStackTrace();
    }
  }

  /**
   * This method is called from the base class when the user asks to reload the
   * business objects. It clears the data source, then reloads the data file.
   */
  Override
  protected void reloadBusinessObjects() {
    reloadBusinessObjects(dataSource, sampleDataSourceFile);
  }

  /**
   * This method is called when the user asks to reload the CSS styles.
   */
  Override
  protected void reloadCSSStyles() {
    String[] css = new String[] { IlpNetwork.DefaultConfigurationFileName, sampleConfigurationFile };
    try {
      networkComponent.setStyleSheets(css);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * This method is called from the base class to create the menu bar. Call base
   * class version that also adds actions to reload business objects and styles
   */
  Override
  protected void populateMenuBar(JMenuBar menubar) {
    populateMenuBar(menubar, true, true);
  }

  public Main() {
    // Create panel with a menubar
    super(sampleLoggerName, true);
  }

  /**
   * Application entry point.
   */
  public static void main(String[] args) {
    createAndShowGUI(new Main(), sampleWidth, sampleHeight);
  }
}