/*
 * 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.net.URL;

import ilog.cpl.IlpEquipment;
import ilog.cpl.datasource.IlpDefaultDataSource;
import ilog.cpl.graphic.IlpGraphicComponent;
import ilog.tgo.faces.equipment.component.IltFacesEquipmentView;
import ilog.tgo.resource.IltSettings;

/**
 * This is the context object that provides the objects needed for this sample
 * to function properly. It also handles the initialization of such objects.
 */
public class SampleContext extends AbstractSampleContext {

  public SampleContext() {
    super("equipment.image.renderer");
  }

  //////////////////////////////////////////////////////////////////////////////
  // Context Objects Accessors
  //////////////////////////////////////////////////////////////////////////////

  /**
   * Returns the IlpEquipment that is used by the sample.
   */
  public IlpEquipment getEquipment() {
    return (IlpEquipment) component;
  }

  //////////////////////////////////////////////////////////////////////////////
  // Sample Dependent
  //////////////////////////////////////////////////////////////////////////////

  /**
   * Creates the IlpEquipment that will be used by the sample. Note that the
   * default Faces configuration file is passed.
   */
  Override
  protected IlpGraphicComponent createComponent() {

    // Get the default configuration file name
    String config = IltFacesEquipmentView.DefaultConfigurationFileName;
    IlpEquipment equipment = new IlpEquipment(config, getContext());

    new IlpDefaultDataSource(equipment.getContext());

    try {
      URL project = context.getURLAccessService().getFileLocation("project.itpr");
      equipment.setProject(project);
    } catch (Exception e) {
      logger.severe(
          "Cannot set the default project on the equipment component with this exception:\n" + e.getLocalizedMessage());
    }

    return equipment;
  }

  /**
   * Initalizes the samples settings and customizations.
   */
  Override
  protected void initializeSample() {

    // Initialize samples' custom types
    try {
      IltSettings settings = new IltSettings(getContext());
      String[] settingsArray = { "settings.css" };
      settings.setStyleSheets(settingsArray);
    } catch (Exception e) {
      logger.severe("Exception caught while loading sample settings:\n" + e.getLocalizedMessage());
    }
  }
}