/*
 * 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 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 MenuFactory implements IlvMenuFactory, IlvDHTMLConstants {

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

  /**
   * 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(MenuFactory.class, "zoomIn"));
    zoomIn.setImage(IMAGES_DIR + "zoom.gif");
    zoomIn.setActionListener(new JavaScriptActionListener("gantt.zoomIn()"));
    root.addChild(zoomIn);

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

    IlvMenuItem zoomFit = new IlvMenuItem(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "zoomToFit"));
    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 (null != selectedObject && selectedObject instanceof IlvActivity) {
      root.addChild(new IlvMenuSeparator());
      IlvActivity node = (IlvActivity) selectedObject;
      IlvMenu info = new IlvMenu(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "nodeInfos"),
          "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 createContextualAction(IlvHierarchyChart chart, IlvMenu root, Object selectedObject) {
    if (null != selectedObject && 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 = IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "expandAll");
        IlvMenuItem expandRow = new IlvMenuItem(s);
        expandRow.setImage(IMAGES_DIR + "arrowpm.gif");
        FacesMethodBindingActionListener l = new FacesMethodBindingActionListener(IMAGE_SERVLET_CONTEXT,
            "#{ganttBean.expandAllRows}");
        expandRow.setActionListener(l);
        root.addChild(expandRow);
      }

    }
  }

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

    createZoomItems(root);

    createContextualAction(chart, root, selectedObject);

    createInfosSubMenu(root, selectedObject);

    return root;
  }

}