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

import java.net.MalformedURLException;
import java.net.URL;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import ilog.views.IlvGraphic;
import ilog.views.IlvManagerView;
import ilog.views.diagrammer.IlvDiagrammer;
import ilog.views.diagrammer.datasource.IlvDiagrammerDataSource;
import ilog.views.diagrammer.datasource.IlvXMLDataSource;
import ilog.views.diagrammer.faces.dhtml.component.IlvFacesDiagrammerPropertyAccessor;
import ilog.views.faces.dhtml.event.FacesViewActionEvent;
import ilog.views.faces.internalutil.IlvResourceUtil;
import ilog.views.sdm.IlvSDMEngine;
import ilog.views.sdm.IlvSDMView;
import ilog.views.sdm.model.IlvSDMNode;
import ilog.views.sdm.servlet.IlvSDMImageMapAreaGenerator;
import ilog.views.util.servlet.IlvMenuFactory;

/**
 * This bean is the logic of the application.
 */
public class DiagrammerBean {

  private IlvDiagrammer diagrammer;

  private IlvDiagrammerDataSource dataSource;

  private IlvMenuFactory menuFactory = new MenuFactory();

  // the property accessor that allows to get and set values on the diagrammer
  // model from a JSF client
  private IlvFacesDiagrammerPropertyAccessor propertyAccessor = new IlvFacesDiagrammerPropertyAccessor() {
    // filter x, y properties
    Override
    protected boolean acceptProperty(IlvDiagrammer diagrammer, Object object, String propertyName) {
      return (!propertyName.equals("x") && !propertyName.equals("y"));
    }
  };

  private IlvSDMImageMapAreaGenerator imapGenerator = new IlvSDMImageMapAreaGenerator() {
    Override
    public String generateHREF(IlvManagerView view, IlvGraphic graphic) {
      IlvSDMEngine engine = ((IlvSDMView) view).getSDMEngine();
      IlvSDMNode obj = (IlvSDMNode) engine.getObject(graphic);
      return "javascript:alert('Object ID: " + obj.getID() + "')";
    }
  };

  /**
   * Creates a data source from an XML file.
   * 
   * @return The data source.
   */
  protected IlvDiagrammerDataSource createDataSource() {
    IlvXMLDataSource ds = null;
    URL url;
    try {
      ds = new IlvXMLDataSource();
      url = new URL(IlvResourceUtil.getAbsoluteURL("/data/molecule.xml"));
      ds.setDataURL(url);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
    return ds;
  }

  /**
   * Selects a SDM node.
   * 
   * @param event
   *          The value change event.
   */
  public void onSelectNodeOrLink(ValueChangeEvent event) {
    IlvSDMNode node = (IlvSDMNode) event.getNewValue();
    onSelectNodeOrLink(node);
  }

  /**
   * Selects a SDM node.
   * 
   * @param event
   *          The view action event.
   */
  public void onSelectNodeOrLink(FacesViewActionEvent event) {
    IlvSDMNode node = (IlvSDMNode) event.getObject();
    onSelectNodeOrLink(node);
  }

  /**
   * Selects a SDM node.
   * 
   * @param node
   *          The node selected (can be <code>null</code>.
   */
  protected void onSelectNodeOrLink(IlvSDMNode node) {
    if (node != null) {
      try {
        diagrammer.deselectAll();
        diagrammer.setSelected(node, true);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  /**
   * Returns the property accessor.
   * 
   * @return The property accessor.
   */
  public IlvFacesDiagrammerPropertyAccessor getPropertyAccessor() {
    return propertyAccessor;
  }

  /**
   * Returns a <code>IlvDiagrammerDataSource</code> instance.
   * 
   * @return A diagrammer data source.
   */
  public IlvDiagrammerDataSource getDataSource() {
    if (dataSource == null)
      dataSource = createDataSource();
    return dataSource;
  }

  /**
   * Returns an SDM image map generator.
   * 
   * @return An image map generator
   */
  public IlvSDMImageMapAreaGenerator getImapGenerator() {
    return imapGenerator;
  }

  /**
   * Returns a contextual menu factory.
   * 
   * @return A menu factory.
   */
  public IlvMenuFactory getMenuFactory() {
    return menuFactory;
  }

  /**
   * Returns a <code>IlvDiagrammer</code> instance.
   * 
   * @return A diagrammer.
   */
  public IlvDiagrammer getDiagrammer() {
    if (diagrammer == null) {
      diagrammer = new IlvDiagrammer();
      try {
        URL data = FacesContext.getCurrentInstance().getExternalContext().getResource("/data/genealogy.idpr");
        diagrammer.setDataFile(data);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return diagrammer;
  }

}