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

import ilog.views.faces.dhtml.IlvDHTMLConstants;
import ilog.views.faces.dhtml.event.FacesMethodBindingActionListener;
import ilog.views.gantt.IlvActivity;
import ilog.views.gantt.IlvGanttModel;
import ilog.views.gantt.IlvHierarchyChart;
import ilog.views.util.IlvResourceUtil;
import ilog.views.util.servlet.IlvMenuFactory;
import ilog.views.util.servlet.event.JavaScriptActionListener;
import ilog.views.util.servlet.model.IlvMenu;
import ilog.views.util.servlet.model.IlvMenuItem;
import ilog.views.util.servlet.model.IlvMenuSeparator;

/**
 * A simple menu factory.
 */
public class DemoFactory implements IlvMenuFactory, IlvDHTMLConstants {

  protected final static String SELECT_MENU_MODEL_ID = "select";

  protected final static String EXPAND_MENU_MODEL_ID = "expand";

  protected final static String SCROLL_MENU_MODEL_ID = "scroll";

  protected final static String IMAGES_DIR = "data/images/";

  /**
   * Creates menu item to set the expand/collapse row, the select row interactor
   * or scroll interactor, depending on the current interactor set on the client
   * view.
   * 
   * @param root
   *          The root menu
   * @param menuModelId
   *          The memu model ID.
   */
  protected void createInteractorItems(IlvMenu root, String menuModelId) {
    boolean enabled = !EXPAND_MENU_MODEL_ID.equals(menuModelId);
    IlvMenuItem toggleRow = new IlvMenuItem(
        IlvResourceUtil.getCurrentLocaleString(DemoFactory.class, "expandCollapseMenuMsg"));
    toggleRow.setImage(IMAGES_DIR + "arrowpm.gif");
    toggleRow.setActionListener(new JavaScriptActionListener("expandButton.doClick()"));
    toggleRow.setEnabled(enabled);
    root.addChild(toggleRow);

    enabled = !SELECT_MENU_MODEL_ID.equals(menuModelId);
    IlvMenuItem selectRow = new IlvMenuItem(
        IlvResourceUtil.getCurrentLocaleString(DemoFactory.class, "selectRowMenuMsg"));
    selectRow.setImage(IMAGES_DIR + "arrow.gif");
    selectRow.setActionListener(new JavaScriptActionListener("selectButton.doClick()"));
    selectRow.setEnabled(enabled);
    root.addChild(selectRow);

    enabled = !SCROLL_MENU_MODEL_ID.equals(menuModelId);
    IlvMenuItem scroll = new IlvMenuItem(
        IlvResourceUtil.getCurrentLocaleString(DemoFactory.class, "scrollViewMenuMsg"));
    scroll.setImage(IMAGES_DIR + "pan.gif");
    scroll.setActionListener(new JavaScriptActionListener("scrollButton.doClick()"));
    scroll.setEnabled(enabled);
    root.addChild(scroll);

  }

  /**
   * Creates menu items with icons and javascript action attached to zoom in,
   * zoom out or zoom to fit.
   * 
   * @param root
   *          The root menu
   */
  protected void createZoomItems(IlvMenu root) {
    IlvMenuItem zoomIn = new IlvMenuItem(IlvResourceUtil.getCurrentLocaleString(DemoFactory.class, "zoomInMenuMsg"));
    zoomIn.setImage(IMAGES_DIR + "zoom.gif");
    zoomIn.setActionListener(new JavaScriptActionListener("gantt.zoomIn()"));
    root.addChild(zoomIn);

    IlvMenuItem zoomOut = new IlvMenuItem(IlvResourceUtil.getCurrentLocaleString(DemoFactory.class, "zoomOutMenuMsg"));
    zoomOut.setImage(IMAGES_DIR + "unzoom.gif");
    zoomOut.setActionListener(new JavaScriptActionListener("gantt.zoomOut()"));
    root.addChild(zoomOut);

    IlvMenuItem zoomFit = new IlvMenuItem(
        IlvResourceUtil.getCurrentLocaleString(DemoFactory.class, "zoomToFitMenuMsg"));
    zoomFit.setImage(IMAGES_DIR + "zoomfit.gif");
    zoomFit.setActionListener(new JavaScriptActionListener("gantt.zoomToFit()"));
    root.addChild(zoomFit);

  }

  /**
   * Creates a submenu filled with information on the current activity selected
   * when the pop-up menu was triggered.
   * 
   * @param root
   *          The root menu.
   * @param selectedObject
   *          The activity selected, or null.
   */
  protected void createInfosSubMenu(IlvMenu root, Object selectedObject) {
    if (selectedObject instanceof IlvActivity) {
      root.addChild(new IlvMenuSeparator());
      IlvActivity node = (IlvActivity) selectedObject;
      IlvMenu info = new IlvMenu(IlvResourceUtil.getCurrentLocaleString(DemoFactory.class, "nodePropertiesMenuMsg"),
          "data/images/properties.gif");
      root.addChild(info);

      String propImg = "data/images/property.gif";

      info.addChild(new IlvMenuItem(node.getID(), propImg));
      info.addChild(new IlvMenuItem(node.getName(), propImg));
      info.addChild(new IlvMenuItem(node.getStartTime().toString(), propImg));
      info.addChild(new IlvMenuItem(node.getEndTime().toString(), propImg));

    }
  }

  public void createContextualActions(IlvHierarchyChart chart, IlvMenu root, Object selectedObject) {
    if (selectedObject instanceof IlvActivity) {
      root.addChild(new IlvMenuSeparator());

      IlvGanttModel model = chart.getGanttModel();
      IlvActivity activity = (IlvActivity) selectedObject;
      int count = model.getChildActivityCount(activity);
      if (count > 0) {
        String s = chart.isRowExpanded(activity)
            ? IlvResourceUtil.getCurrentLocaleString(DemoFactory.class, "collapseRowMenuMsg")
            : IlvResourceUtil.getCurrentLocaleString(DemoFactory.class, "expandRowMenuMsg");
        IlvMenuItem expandRow = new IlvMenuItem(s);
        expandRow.setImage(IMAGES_DIR + "arrowpm.gif");
        FacesMethodBindingActionListener l = new FacesMethodBindingActionListener(IMAGE_SERVLET_CONTEXT,
            "#{ganttBean.expandCollapse}");
        expandRow.setActionListener(l);
        root.addChild(expandRow);
      }
      IlvMenuItem selectRow = new IlvMenuItem(
          IlvResourceUtil.getCurrentLocaleString(DemoFactory.class, "selectRowMenuMsg"));
      selectRow.setImage(IMAGES_DIR + "arrow.gif");
      FacesMethodBindingActionListener l = new FacesMethodBindingActionListener(JSF_CONTEXT,
          "#{ganttBean.nodeValueChanged}");
      selectRow.setActionListener(l);
      root.addChild(selectRow);
    }
  }

  /**
   * Dynamically builds a menu depending on the selected activity and the
   * current set on the client view.
   * 
   * @param graphicComponent
   *          The gantt chart component.
   * @param selectedObject
   *          The selected activity when the pop-up menu was triggered.
   * @param menuModelId
   *          The menu model ID set on the current interactor of the client
   *          view.
   */
  Override
  public IlvMenu createMenu(Object graphicComponent, Object selectedObject, String menuModelId) {

    IlvHierarchyChart chart = (IlvHierarchyChart) graphicComponent;
    IlvMenu root = new IlvMenu("root");

    createInteractorItems(root, menuModelId);
    root.addChild(new IlvMenuSeparator());
    createZoomItems(root);
    createContextualActions(chart, root, selectedObject);
    createInfosSubMenu(root, selectedObject);

    return root;
  }

}