/*
 * 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.IlpNetwork;
import ilog.cpl.datasource.IlpDefaultDataSource;
import ilog.cpl.graphic.IlpGraphicComponent;
import ilog.tgo.faces.network.component.IltFacesNetworkView;
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("network.customization");
  }

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

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

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

  /**
   * Creates the IlpNetwork 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 = IltFacesNetworkView.DefaultConfigurationFileName;
    IlpNetwork network = new IlpNetwork(config, getContext());

    new IlpDefaultDataSource(network.getContext());

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

    return network;
  }

  /**
   * 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());
    }
  }
}