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

import ilog.cpl.graph.IlpGraphView;
import ilog.cpl.model.IlpAttributeValueHolder;
import ilog.cpl.model.IlpRepresentationObject;

import ilog.tgo.faces.graph.component.IltFacesGraphView;
import ilog.tgo.faces.graph.dhtml.interactor.IltFacesGraphInteractor;

import java.util.ArrayList;

import javax.faces.context.FacesContext;
import javax.faces.el.VariableResolver;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

/**
 * <p>This bean defines an action listener for action events which is called
 * when the <code>selectInteractor</code> is activated.
 */
public class MyInteractionListener implements ActionListener {
  // The bean that manages the attribute list
  private MyInteraction bean = null;

  /**
   * <p>Method implements an <code>ActionEvent</code> listener, it will
   * be called when the interactor is activated. The listeners are
   * invoked in the order they are declared, so, the default interaction 
   * will already be triggered when this method is invoked.
   * 
   * <p>Note that JSF automatically instantiates listeners declared with 
   * action listener tags such as:
   * <pre>
   *   <f:actionListener type="demo.MyInteraction"/>
   * </pre>
   * 
   * @param event  The action event with interaction information.
   */
  public void processAction(ActionEvent event) {
    // get the IltFacesGraphInteractor component
    IltFacesGraphInteractor component = (IltFacesGraphInteractor)event.getSource();

    // get the IlpGraphView from JSF component
    IlpGraphView gView = ((IltFacesGraphView)component.getView()).getGraphView();

    // get the selected object
    IlpRepresentationObject selectedObj = gView.getSelectedRepresentationObject();
    
    if (null == bean) {
      // find the bean that manages the attribute list
      FacesContext context = FacesContext.getCurrentInstance();
      VariableResolver vRes = context.getApplication().getVariableResolver();
      bean = (MyInteraction)vRes.resolveVariable(context, "customInteraction");
    }
    
    // clear attribute rendered flag
    bean.setAttributeSheetRendered(false);
    
    if (null != selectedObj) {
      // fill data model
      DataModel dModel = createDataModel(selectedObj);
      bean.setDataModel(dModel);
      bean.setAttributeSheetRendered(dModel.getRowCount() > 0);
    }
  }
  
  /**
   * <p>Creates a data model from a node or link. 
   * 
   * <p>The data model is a <code>ListDataModel</code> filled with a few 
   * attributes from the given node or link, paired as attribute name
   * and attribute value.
   * 
   * @param node  The node or link used to build the data model.
   * 
   * @return The data model filled with corresponding attributes.
   */
  private DataModel createDataModel(IlpRepresentationObject node) {
    ArrayList list = new ArrayList();
    // name attribute
    createPair(list, "name", node);
    // type
    createPair(list, "type", node);
    // family
    createPair(list, "family", node);
    // alarmCount
    createPair(list, "alarmCount", node);
    // newAlarmCount
    createPair(list, "newAlarmCount", node);

    return new ListDataModel(list);
  }

  /**
   * <p>Creates a pair <code>name</code>, <code>value</code> with the 
   * given attribute name and value, adding it into the array.
   * 
   * @param list        The array where the pair should be added to.
   * @param attribName  The attribute name to be used.
   * @param node        The representation object holding the attributes.
   */
  private void createPair(ArrayList list, String attribName, IlpRepresentationObject node) {
    Object value = node.getAttributeValue(attribName);
    if (null != value &&
        !IlpAttributeValueHolder.VALUE_NOT_SET.equals(value)) {
      list.add(new Pair(attribName, value.toString()));
    }
  }

  /**
   * <p>A simple (name, value) pair.
   * 
   * <p>This object defines a basic attribute of a node or link. A list 
   * of properties will be used to fill the data model of the data table
   * in order to display the properties of a link or node.
   */
  public class Pair {
    String name;
    String value;

    public Pair(String name, String value) {
      this.name = name;
      this.value = value;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public String getValue() {
      return value;
    }

    public void setValue(String value) {
      this.value = value;
    }
  }
}