/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © 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 customClasses;

import ilog.cpl.IlpTree;

import ilog.tgo.IltSystem;
import ilog.tgo.datasource.IltDefaultDataSource;

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

import javax.swing.JMenuBar;

import shared.AbstractSample;

/**
 * Rogue Wave JViews TGO tree sample.
 * Show instances of custom business classes.
 */
public class Main extends AbstractSample {
  public static final String sampleLoggerName = "samples.tree.customClasses";
  public static final int sampleWidth = 250;
  public static final int sampleHeight = 300;

  public static final String sampleDataSourceFile = "treenodes.xml";
  public static final String sampleCSSFile = "tree.css";

  // JTGO components
  IltDefaultDataSource dataSource;
  // JTGO Tree component
  IlpTree treeComponent;

  /**
   * Execute the main part of the sample.
   */
  protected void doSample(Container container) {
    try {
      // ------------------------
      // Initialize JTGO
      // ------------------------
      // Read a deployment descriptor file to initialize JTGO services
      // Contains search paths, and the definition of the custom business
      // classes used in this sample
      if (isApplet())
        IltSystem.Init(this, "deploy.xml");
      else
        IltSystem.Init("deploy.xml");

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

      // ------------------------
      // Set up the JTGO data source and the tree component
      // ------------------------
      // Create a tree component
      treeComponent = new IlpTree();

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

      // Read an XML file into the datasource
      dataSource.parse(sampleDataSourceFile);

      // Load the style sheet for the component
      treeComponent.setStyleSheets(new String[] {sampleCSSFile});

      // Set the datasource to the component
      treeComponent.setDataSource(dataSource);

      // Add the component to the frame
      container.add(treeComponent);
    }
    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.
   */
  protected void reloadBusinessObjects() {
    reloadBusinessObjects(dataSource, sampleDataSourceFile);
  }

  /**
   * This method is called from the base class when the user asks to
   * reload the CSS styles.
   * It reloads the CSS file.
   */
  protected void reloadCSSStyles() {
    try {
      treeComponent.setStyleSheets(new String[] {sampleCSSFile});
    } catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }

  /**
   * 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 CSS styles
   */
  protected void populateMenuBar(JMenuBar menubar) {
    super.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);
  }
}