/*
 * 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.sdm.model.IlvSDMNode;
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 {

  private static final String PAN_MENU_MODEL_ID = "pan";

  private static final String ZOOM_MENU_MODEL_ID = "zoom";

  /**
   * 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(MenuFactory.class, "zoom"),
        "diagrammer.setInteractor(zoom)", "images/zoomrect.gif", enabled));

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

    root.addChild(new IlvMenuSeparator());

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

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

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

  }

  /**
   * Creates a submenu filled with properties of the specified node.
   * 
   * @param root
   *          The root menu
   * @param node
   *          The current SDM node.
   */
  private void createPropertyMenuItems(IlvMenu root, IlvSDMNode node) {

    String[] names = node.getPropertyNames();

    if (names.length > 0) {
      root.addChild(new IlvMenuSeparator());
      IlvMenu subMenu = new IlvMenu(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "properties"),
          "images/properties.gif");
      root.addChild(subMenu);

      for (int i = 0; i < names.length; i++) {
        String s = names[i] + " : " + node.getProperty(names[i]);
        subMenu.addChild(new IlvMenuItem(s, null, "images/property.gif", true));
      }
    }

  }

  /**
   * Creates the contextual pop-up menu.
   * 
   * @param graphicComponent
   *          The SDM view.
   * @param selectedObject
   *          The SDM node or link.
   * @param menuModelId
   *          The menu model ID.
   */
  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, "select"), null,
          "images/arrow.gif");
      FacesMethodBindingActionListener l = new FacesMethodBindingActionListener("#{diagrammerBean.onSelectNodeOrLink}");
      l.setInvocationContext(IlvDHTMLConstants.IMAGE_SERVLET_CONTEXT);
      item.setActionListener(l);
      root.addChild(item);

      IlvSDMNode node = (IlvSDMNode) selectedObject;
      createPropertyMenuItems(root, node);

    }

    return root;
  }

}