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

import javax.faces.event.ValueChangeEvent;

import ilog.views.diagrammer.IlvDiagrammer;
import ilog.views.diagrammer.faces.IlvFacesDiagrammerUtil;
import ilog.views.diagrammer.faces.component.IlvFacesDiagrammerView;
import ilog.views.diagrammer.faces.dhtml.component.IlvFacesDHTMLDiagrammerView;
import ilog.views.faces.dhtml.event.FacesViewActionEvent;
import ilog.views.sdm.IlvSDMEngine;
import ilog.views.sdm.IlvSDMView;
import ilog.views.sdm.model.IlvSDMNode;
import ilog.views.sdm.renderer.IlvRendererUtil;
import ilog.views.sdm.renderer.IlvSubGraphRenderer;
import ilog.views.servlet.IlvImageMapAreaGenerator;

/**
 * This class defines the logic of the JViews Diagrammer Faces Gallery sample.
 * <br/>
 * This beans provides a customized handle of the select node or link interactor
 * to select the node and create a data model filled with its properties to be
 * displayed in a data table.</li>
 * </ul>
 * 
 * @proofreadall
 */
public class GalleryBean {

  // JV-6638, forces sub graphers to update its positions once expanded.
  static {
    IlvSubGraphRenderer.setDefaultUpdatingPositionsOnExpand(true);
  }

  /** The diagrammer view */
  private IlvFacesDiagrammerView diagrammerView = new IlvFacesDHTMLDiagrammerView();

  /** The image map generator */
  private IlvImageMapAreaGenerator imapGenerator = new GalleryImageMapGenerator();

  /** The contextual pop-up menu factory */
  private GalleryMenuFactory menuFactory = new GalleryMenuFactory();

  /** The selection property accessor **/
  private GalleryPropertyAccessor propertyAccessor = new GalleryPropertyAccessor();

  /**
   * Expand or collase a subgraph.<br/>
   * 
   * @param event
   *          The value change event.
   */
  public void expandCollapseSubGraph(ValueChangeEvent event) throws Exception {
    IlvDiagrammer diagrammer = diagrammerView.getDiagrammer();
    expandCollapseSubGraph(diagrammer, (IlvSDMNode) event.getNewValue());
  }

  /**
   * Expand or collase a subgraph.<br/>
   * 
   * @param event
   *          The view action event
   */
  public void expandCollapseSubGraph(FacesViewActionEvent event) throws Exception {
    IlvDiagrammer diagrammer = IlvFacesDiagrammerUtil.getDiagrammer((IlvSDMView) event.getGraphicComponent());
    expandCollapseSubGraph(diagrammer, (IlvSDMNode) event.getObject());
  }

  /**
   * Expand or collase a subgraph.<br/>
   * 
   * @param diagrammer
   *          The diagrammer
   * @param node
   *          The node to expand or collapse.
   */
  private void expandCollapseSubGraph(IlvDiagrammer diagrammer, IlvSDMNode node) {
    if (diagrammer != null && node != null) {
      IlvSDMEngine engine = diagrammer.getEngine();
      IlvSubGraphRenderer renderer = (IlvSubGraphRenderer) IlvRendererUtil.getRenderer(engine,
          IlvRendererUtil.SubGraph);
      renderer.setExpanded(engine, node, !renderer.isExpanded(engine, node), false);

    }
  }

  public IlvImageMapAreaGenerator getImapGenerator() {
    return imapGenerator;
  }

  public void setImapGenerator(IlvImageMapAreaGenerator imapGenerator) {
    this.imapGenerator = imapGenerator;
  }

  public GalleryMenuFactory getMenuFactory() {
    return menuFactory;
  }

  public void setMenuFactory(GalleryMenuFactory menuFactory) {
    this.menuFactory = menuFactory;
  }

  public GalleryPropertyAccessor getPropertyAccessor() {
    return propertyAccessor;
  }
}