/*
 * 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 demo.editing.faces;

import javax.faces.context.FacesContext;

import ilog.views.faces.component.IlvBasicView;
import ilog.views.faces.component.IlvFacesComponentBase;
import ilog.views.faces.component.IlvViewHelper;
import ilog.views.faces.component.IlvViewHelperImpl;

/**
 * This class define a property sheet designed to work exclusively with the
 * selection manager of a view component.
 */
SuppressWarnings("unchecked")
public class PropertySheet extends IlvFacesComponentBase implements IlvViewHelper {

  /**
   * {@inheritDoc}
   */
  Override
  public String getFamily() {
    return "ilog.faces.PropertySheet";
  }

  /**
   * Returns the type of this component.
   * 
   * @return The type of this component.
   */
  public static String getComponentType() {
    return "ilog.faces.PropertySheet";
  }

  IlvViewHelper viewHelper = createViewHelper();

  /**
   * Creates the default <code>IlvViewHelper</code> used by this component. This
   * default implementation returns an <code>IlvViewHelperImpl</code>
   * 
   * @return The default <code>IlvViewHelper</code> implementation.
   */
  protected IlvViewHelper createViewHelper() {
    return new IlvViewHelperImpl(this);
  }

  /**
   * Returns the view component associated with this faces component.
   * 
   * @return The view component associated with this faces component.
   */
  Override
  public IlvBasicView getView() {
    return viewHelper.getView();
  }

  /**
   * Returns the ID of the view linked to this faces component.
   * 
   * @return The ID of the view linked to this faces component.
   */
  Override
  public String getViewId() {
    return viewHelper.getViewId();
  }

  /**
   * Sets the view component associated with this faces component.
   * 
   * @param view
   *          The view component associated with this faces component.
   */
  Override
  public void setView(IlvBasicView view) {
    viewHelper.setView(view);
  }

  /**
   * Sets the ID of the view linked to this faces component.
   * 
   * @param viewId
   *          The ID of the view.
   */
  Override
  public void setViewId(String viewId) {
    viewHelper.setViewId(viewId);
  }

  /**
   * {@inheritDoc}
   */
  Override
  public void updateModel(FacesContext ctx) {
    super.updateModel(ctx);
    viewHelper.updateModel(ctx);
  }

  // State Holder implementation ----------------------------------------------

  /**
   * {@inheritDoc}
   */
  Override
  public Object saveState(FacesContext context) {
    Object[] state = new Object[2];
    state[0] = super.saveState(context);
    state[1] = viewHelper.saveState(context);
    return state;
  }

  /**
   * {@inheritDoc}
   */
  Override
  public void restoreState(FacesContext context, Object stateObj) {
    Object[] state = (Object[]) stateObj;
    super.restoreState(context, state[0]);
    viewHelper.restoreState(context, state[1]);
  }

}