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

import java.net.URL;

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

import ilog.views.IlvGraphic;
import ilog.views.IlvManager;
import ilog.views.IlvManagerView;
import ilog.views.faces.dhtml.event.FacesMenuActionEvent;
import ilog.views.util.servlet.IlvMenuFactory;

/**
 * The bean that provides the logic of the JViews Graphic Framework Faces code
 * fragments.
 */
public class FrameworkBean {

  private IlvManagerView managerView;
  private IlvMenuFactory menuFactory = new MenuFactory();

  /**
   * Creates a new <code>FrameworkBean</code> instance.
   */
  public FrameworkBean() {
    initManagerView();
  }

  /**
   * Initializes the manager view with the current ivl file.
   */
  protected void initManagerView() {
    if (managerView == null)
      managerView = new IlvManagerView();
    URL ivlURL;
    try {
      ivlURL = FacesContext.getCurrentInstance().getExternalContext().getResource("/data/lou.ivl"); //$NON-NLS-1$
      IlvManager manager = managerView.getManager();
      manager.setFileName(ivlURL);
      managerView.fitTransformerToContent();
      managerView.setKeepingAspectRatio(true);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Selects the graphic object specified by this event.
   * 
   * @param event
   *          The value change event
   */
  public void selectObject(ValueChangeEvent event) {
    Object value = event.getNewValue();
    if (value != null && value instanceof IlvGraphic)
      selectObject((IlvGraphic) value);
  }

  /**
   * Selects the graphic object specified by this event.
   * 
   * @param event
   *          The menu event
   */
  public void selectObject(FacesMenuActionEvent event) {
    Object value = event.getObject();
    if (value != null && value instanceof IlvGraphic)
      selectObject((IlvGraphic) value);
  }

  /**
   * Selects the graphic object specified by this event.
   * 
   * @param g
   *          The graphic object to select.
   */
  public void selectObject(IlvGraphic g) {
    managerView.getManager().deSelectAll(false);
    managerView.getManager().setSelected(g, true, false);
  }

  /**
   * Returns the manager view.
   * 
   * @return The manager view.
   */
  public IlvManagerView getView() {
    return managerView;
  }

  /**
   * Returns the menu factory.
   * 
   * @return The menu factory
   */
  public IlvMenuFactory getMenuFactory() {
    return menuFactory;
  }

}