/*
 * 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.faces.dhtml.event.FacesViewActionListener;
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 dynamic menu factory for chart-user15.jsp page.
 */
public class MenuFactory implements IlvMenuFactory {

  private static final String PAN_MENU_MODEL_ID = "pan";

  private static final String ZOOM_MENU_MODEL_ID = "zoom";

  private static final String SELECT_MENU_MODEL_ID = "select";

  /**
   * Create a menu item with the specified image and the specified javascript
   * action.
   * 
   * @param label
   *          The menu item's label.
   * @param jsAction
   *          The Javascript action attached to this label.
   * @param image
   *          The image of this menu item.
   * @return The menu item created.
   */
  private IlvMenuItem createJSMenuItem(String label, String jsAction, String image) {
    return createJSMenuItem(label, jsAction, image, true);
  }

  /**
   * Create a menu item with the specified image and the specified javascript
   * action.
   * 
   * @param label
   *          The menu item's label.
   * @param jsAction
   *          The Javascript action attached to this label.
   * @param image
   *          The image of this menu item.
   * @param enabled
   *          The state of the menu item.
   * @return The menu item created.
   */
  private IlvMenuItem createJSMenuItem(String label, String jsAction, String image, boolean enabled) {

    JavaScriptActionListener eltAction = new JavaScriptActionListener(jsAction);
    IlvMenuItem elt = new IlvMenuItem(label, eltAction, image, enabled);
    return elt;
  }

  /**
   * Creates the interactors and zoom menu items.
   * 
   * @param root
   *          The root menu
   * @param menuModelId
   *          The menu model ID
   */
  public void createNavigationMenuItems(IlvMenu root, String menuModelId) {

    boolean enabled = !ZOOM_MENU_MODEL_ID.equals(menuModelId);
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "zoom"),
        "viewID.setInteractor(zoomInteractor)", "data/images/zoomrect.gif", enabled));

    enabled = !PAN_MENU_MODEL_ID.equals(menuModelId);
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "pan"),
        "viewID.setInteractor(panInteractor)", "data/images/pan.gif", enabled));

    enabled = !SELECT_MENU_MODEL_ID.equals(menuModelId);
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "select"),
        "viewID.setInteractor(selectInteractor)", "data/images/arrow.gif", enabled));

    root.addChild(new IlvMenuSeparator());

    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "zoomIn"),
        "viewID.zoomInX();", "data/images/zoom.gif"));

    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "zoomOut"),
        "viewID.zoomOutX();", "data/images/unzoom.gif"));

    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "zoomToFit"),
        "viewID.zoomToFit();", "data/images/zoomfit.gif"));

  }

  Override
  public IlvMenu createMenu(Object graphicComponent, Object selectedObject, String menuModelId) {

    IlvMenu root = new IlvMenu("Root");

    createNavigationMenuItems(root, menuModelId);

    if (selectedObject != null) {

      IlvMenuItem item = new IlvMenuItem(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "HighLight"), null,
          "data/images/arrowclick.gif");
      FacesViewActionListener l = new FacesMethodBindingActionListener("#{chartBean.selectedDataPointChanged}");
      l.setInvocationContext(IlvDHTMLConstants.IMAGE_SERVLET_CONTEXT);
      item.setActionListener(l);
      root.addChild(item);

    }

    return root;
  }

}