/*
 * 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 javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import ilog.views.IlvManagerView;
import ilog.views.IlvPoint;
import ilog.views.IlvTransformer;
import ilog.views.diagrammer.IlvDiagrammer;
import ilog.views.diagrammer.faces.IlvFacesDiagrammerUtil;
import ilog.views.diagrammer.faces.component.IlvFacesDiagrammerView;
import ilog.views.diagrammer.faces.dhtml.interactor.IlvDiagrammerObjectSelectedFinder;
import ilog.views.faces.dhtml.interactor.IlvObjectSelectInteractor;
import ilog.views.faces.dhtml.interactor.IlvObjectSelectedFinder;
import ilog.views.sdm.IlvSDMView;
import ilog.views.util.servlet.IlvMenuFactory;
import ilog.views.util.servlet.model.IlvMenu;

/**
 * This bean contains the logic of the JSF Diagrammer Editing sample.
 */
public class EditingBean {
  /**
   * This action performs a layout on all nodes.
   * 
   * @param diagrammer
   *          The diagrammer
   * @param params
   *          The parameters
   */
  public void layoutAllNodes(IlvDiagrammer diagrammer, String[] params) {
    diagrammer.layoutAllNodes();
  }

  private IlvMenuFactory menuFactory = new MenuFactory();

  /**
   * Bean method to return the menu factory.
   * 
   * @return the menu factory.
   */
  public IlvMenuFactory getMenuFactory() {
    return menuFactory;
  }

  private IlvObjectSelectedFinder finder = new IlvDiagrammerObjectSelectedFinder() {
    /**
     * {@inheritDoc}
     * 
     * @see ilog.views.diagrammer.faces.dhtml.interactor.IlvDiagrammerObjectSelectedFinder#computeObjectSelected(ilog.views.IlvManagerView,
     *      ilog.views.IlvPoint, ilog.views.IlvTransformer)
     */
    Override
    public Object computeObjectSelected(IlvManagerView managerView, IlvPoint pointClicked, IlvTransformer transformer) {
      IlvSDMView sdmView = (IlvSDMView) managerView;
      IlvDiagrammer diagrammer = IlvFacesDiagrammerUtil.getDiagrammer(sdmView);
      Object obj = super.computeObjectSelected(managerView, pointClicked, transformer);
      if (diagrammer.isLink(obj)) {
        return null;
      }
      return obj;
    }
  };

  /**
   * Bean method to return the object finder.
   * 
   * @return the object finder.
   */
  public IlvObjectSelectedFinder getFinder() {
    return finder;
  }

  private boolean clicked;

  /**
   * @return true if the endAction was executed
   */
  public boolean isClicked() {
    return clicked;
  }

  /**
   * Bean method to trace a page action.
   * 
   * @param evt
   */
  public void endAction(ActionEvent evt) {
    clicked = true;
    System.out.println("Done something"); //$NON-NLS-1$
  }

  /**
   * Selects the graphic object specified by this event.
   * 
   * @param event
   *          The value change event
   * @throws Exception
   */
  public void selectNodeOrLink(ValueChangeEvent event) throws Exception {
    Object node = event.getNewValue();
    if (node != null && event.getSource() instanceof IlvObjectSelectInteractor) {
      // The source of the event is the interactor
      IlvObjectSelectInteractor source = (IlvObjectSelectInteractor) event.getSource();
      // Retrieve the JSF view connected to the interactor
      IlvFacesDiagrammerView jsfDiagrammer = (IlvFacesDiagrammerView) source.getView();
      try {
        // Retrieve the IlvDiagrammer wrapped by the JSF component.
        IlvSDMView view = (IlvSDMView) jsfDiagrammer.getView();
        // Select the clicked object
        view.getSDMEngine().deselectAllObjects();
        view.getSDMEngine().setSelected(node, true);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  IlvMenu staticMenu = null;

  /**
   * Bean method to return a static menu.
   * 
   * @return the static menu.
   */
  public IlvMenu getMenu() {
    if (staticMenu == null) {
      staticMenu = new MenuFactory().createMenu(null, null, "staticMenu"); //$NON-NLS-1$
    }
    return staticMenu;
  }
}