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

import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;

/**
 * JSF Component to store a variable name and value, for transmission to
 * JavaScript code.
 */
SuppressWarnings("deprecation")
public class JavaScriptVariableComponent extends UIOutput {
  static final String VARNAME_PROPERTY = "varName"; //$NON-NLS-1$
  static final String VARVALUE_PROPERTY = "varValue";//$NON-NLS-1$
  private String varName;
  private String varValue;

  /**
   * @return the varName
   */
  public String getVarName() {
    javax.faces.el.ValueBinding v = getValueBinding(VARNAME_PROPERTY);
    if (v != null) {
      Object rtn = v.getValue(FacesContext.getCurrentInstance());
      return rtn.toString();
    }
    return varName;
  }

  /**
   * @param varName
   *          the varName to set
   */
  public void setVarName(String varName) {
    this.varName = varName;
  }

  /**
   * @return the varValue
   */
  public String getVarValue() {
    javax.faces.el.ValueBinding v = getValueBinding(VARVALUE_PROPERTY);
    if (v != null) {
      Object rtn = v.getValue(FacesContext.getCurrentInstance());
      return rtn.toString();
    }
    return varValue;
  }

  /**
   * @param varValue
   *          the varValue to set
   */
  public void setVarValue(String varValue) {
    this.varValue = varValue;
  }

  Override
  public Object saveState(FacesContext context) {
    Object[] state = new Object[4];
    state[0] = super.saveState(context);
    state[1] = getVarName();
    state[2] = getVarValue();
    return state;
  }

  Override
  public void restoreState(FacesContext context, Object stateObj) {
    Object[] state = (Object[]) stateObj;
    super.restoreState(context, state[0]);
    setVarName((String) state[1]);
    setVarValue((String) state[2]);
  }
}