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

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.
 */
public class ChartMenuFactory implements IlvMenuFactory {
  // private transient ServletContext context;
  private static final String PAN_MENU_MODEL_ID = "pan"; //$NON-NLS-1$
  private static final String ZOOM_MENU_MODEL_ID = "zoom";//$NON-NLS-1$
  private static final String SELECT_MENU_MODEL_ID = "select";//$NON-NLS-1$

  /**
   * 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();
    eltAction.setJsAction(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(ChartMenuFactory.class, "zoom"), //$NON-NLS-1$ but
                                                                                                           // should
                                                                                                           // be
                                                                                                           // translated
        "zoomButton.doClick()", //$NON-NLS-1$
        "/images/zoomrect.gif", enabled));//$NON-NLS-1$
    enabled = !PAN_MENU_MODEL_ID.equals(menuModelId);
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(ChartMenuFactory.class, "pan"), //$NON-NLS-1$ but
                                                                                                          // should
                                                                                                          // be
                                                                                                          // translated
        "panButton.doClick()", //$NON-NLS-1$
        "/images/pan.gif", enabled));//$NON-NLS-1$
    enabled = !SELECT_MENU_MODEL_ID.equals(menuModelId);
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(ChartMenuFactory.class, "select"), //$NON-NLS-1$ but
                                                                                                             // should
                                                                                                             // be
                                                                                                             // translated
        "selectButton.doClick()", //$NON-NLS-1$
        "/images/arrow.gif", enabled));//$NON-NLS-1$
    root.addChild(new IlvMenuSeparator());
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(ChartMenuFactory.class, "zoomIn"), //$NON-NLS-1$ but
                                                                                                             // should
                                                                                                             // be
                                                                                                             // translated
        "chartView.zoomInX();", //$NON-NLS-1$
        "/images/zoom.gif"));//$NON-NLS-1$
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(ChartMenuFactory.class, "zoomOut"), //$NON-NLS-1$ but
                                                                                                              // should
                                                                                                              // be
                                                                                                              // translated
        "chartView.zoomOutX();", //$NON-NLS-1$
        "/images/unzoom.gif"));//$NON-NLS-1$
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(ChartMenuFactory.class, "zoomToFit"), //$NON-NLS-1$ but
                                                                                                                // should
                                                                                                                // be
                                                                                                                // translated
        "chartView.zoomToFit();", //$NON-NLS-1$
        "/images/zoomfit.gif"));//$NON-NLS-1$
  }

  Override
  public IlvMenu createMenu(Object graphicComponent, Object selectedObject, String menuModelId) {
    IlvMenu root = new IlvMenu("Root");//$NON-NLS-1$
    createNavigationMenuItems(root, menuModelId);
    if (selectedObject != null) {
      IlvMenuItem item = new IlvMenuItem(IlvResourceUtil.getCurrentLocaleString(ChartMenuFactory.class, "show"), //$NON-NLS-1$ but
                                                                                                                 // should
                                                                                                                 // be
                                                                                                                 // translated
          null, "/images/arrowclick.gif");//$NON-NLS-1$
      FacesViewActionListener l = new FacesMethodBindingActionListener("#{chartBean.pointSelected}");//$NON-NLS-1$
      l.setInvocationContext(IlvDHTMLConstants.JSF_CONTEXT);
      item.setActionListener(l);
      root.addChild(item);
    }
    return root;
  }
}