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

import ilog.cpl.model.IlpAttributeValueHolder;
import ilog.cpl.model.IlpRepresentationObject;
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;

/**
 * <p>
 * This bean defines a pop-up menu factory for a network or equipment Faces
 * component by implementing the <code>IlvMenuFactory</code>, allowing the
 * creation of dynamic pop-up menus.
 */
public class MyPopupMenuFactory implements IlvMenuFactory {
  // The menu model ids
  private static final String PAN_MENU_MODEL_ID = "panItr";

  private static final String ZOOM_MENU_MODEL_ID = "zoomItr";

  private static final String SELECT_MENU_MODEL_ID = "selectItr";

  /**
   * <p>
   * Access method to the <i>pan</i> menu model identifier.
   * 
   * @return The appropriate value for <code>menuModelId</code>
   */
  public String getPanMenuModelId() {
    return MyPopupMenuFactory.PAN_MENU_MODEL_ID;
  }

  /**
   * <p>
   * Access method to the <i>zoom</i> menu model identifier.
   * 
   * @return The appropriate value for <code>menuModelId</code>
   */
  public String getzoomMenuModelId() {
    return MyPopupMenuFactory.ZOOM_MENU_MODEL_ID;
  }

  /**
   * <p>
   * Access method to the <i>select</i> menu model identifier.
   * 
   * @return The appropriate value for <code>menuModelId</code>
   */
  public String getSelectMenuModelId() {
    return MyPopupMenuFactory.SELECT_MENU_MODEL_ID;
  }

  /**
   * <p>
   * Generates the pop-up menu.
   *
   * @param graphic
   *          The <code>IlpNetworkView</code> or <code>IlpEquipmentView</code>
   *          component
   * @param activeObject
   *          The representation object (<code>IlpRepresentationObject</code>)
   *          currently active (right below the mouse pointer)
   * @param menuModelId
   *          The menu model ID
   * @return The root menu.
   */
  Override
  public IlvMenu createMenu(Object graphicComponent, Object activeObject, String menuModelId) {
    // Create the root menu
    IlvMenu root = new IlvMenu("Root");

    // Create the menu items from interactors
    createInteractorMenuItems(root, menuModelId);

    // Create a submenu for the selected object attributes
    if (null != activeObject) {
      createAttributesMenuItems(root, (IlpRepresentationObject) activeObject);
    }

    return root;
  }

  /**
   * <p>
   * Create menu items from the interactors.
   *
   * @param root
   *          The root pop-up menu
   * @param menuModelId
   *          The interactor currently selected
   */
  private void createInteractorMenuItems(IlvMenu root, String menuModelId) {
    // Create a menu item that activates (if not already active) the
    // "selectBttn" button in the JSP page
    boolean enabled = !SELECT_MENU_MODEL_ID.equals(menuModelId);
    createJSMenuItem(root, IlvResourceUtil.getCurrentLocaleString(MyPopupMenuFactory.class, "select"),
        "/resources/images/arrow.png", "selectBttn.doClick()", enabled);

    // Create a menu item that activates (if not already active) the
    // "zoomBttn" button in the JSP page
    enabled = !ZOOM_MENU_MODEL_ID.equals(menuModelId);
    createJSMenuItem(root, IlvResourceUtil.getCurrentLocaleString(MyPopupMenuFactory.class, "zoom"),
        "/resources/images/zoomrect.png", "zoomBttn.doClick()", enabled);

    // Create a menu item that activates (if not already active) the
    // "panBttn" button in the JSP page
    enabled = !PAN_MENU_MODEL_ID.equals(menuModelId);
    createJSMenuItem(root, IlvResourceUtil.getCurrentLocaleString(MyPopupMenuFactory.class, "pan"),
        "/resources/images/pan.png", "panBttn.doClick()", enabled);

    // Add a menu separator
    root.addChild(new IlvMenuSeparator());

    // Create a menu item that activates the "ftcBttn" button in the
    // JSP page
    createJSMenuItem(root, IlvResourceUtil.getCurrentLocaleString(MyPopupMenuFactory.class, "showall"),
        "/resources/images/zoomfit.png", "example16.showAll()", true);

    // Create a menu item that activates the "zoomInBttn" button in the
    // JSP page
    createJSMenuItem(root, IlvResourceUtil.getCurrentLocaleString(MyPopupMenuFactory.class, "Zoomin"),
        "/resources/images/zoom.png", "example16.zoomIn()", true);

    // Create a menu item that activates the "zoomOutBttn" button in the
    // JSP page
    createJSMenuItem(root, IlvResourceUtil.getCurrentLocaleString(MyPopupMenuFactory.class, "Zoomout"),
        "/resources/images/unzoom.png", "example16.zoomOut()", true);
  }

  /**
   * <p>
   * Create a menu item that triggers a JavaScript action, adding it into a
   * given menu.
   *
   * @param menu
   *          The menu where the item should be added to.
   * @param label
   *          The label of the menu item
   * @param image
   *          The icon of the menu item
   * @param jsAction
   *          The JavaScript action to be triggered
   * @param enabled
   *          Whether the menu item is enabled or not
   */
  private void createJSMenuItem(IlvMenu menu, String label, String image, String jsAction, boolean enabled) {
    JavaScriptActionListener action = new JavaScriptActionListener();
    action.setJsAction(jsAction);
    menu.addChild(new IlvMenuItem(label, action, image, enabled));
  }

  /**
   * <p>
   * Create a submenu containing some of the attributes from the representation
   * object (<code>IlpRepresentationObject</code>) currently active.
   * 
   * @param root
   *          The root pop-up menu
   * @param object
   *          The representation object currently selected
   */
  private void createAttributesMenuItems(IlvMenu root, IlpRepresentationObject object) {
    // add a menu separator
    root.addChild(new IlvMenuSeparator());

    // create a submenu
    IlvMenu subMenu = new IlvMenu(IlvResourceUtil.getCurrentLocaleString(MyPopupMenuFactory.class, "Attributes"),
        "/resources/images/attributes.png");
    root.addChild(subMenu);

    // create the 'name' attribute
    createAttribMenuItem(subMenu, "name", object);

    // the 'type' attribute
    createAttribMenuItem(subMenu, "type", object);

    // the 'family' attribute
    createAttribMenuItem(subMenu, "family", object);

    // the 'alarmCount' attribute
    createAttribMenuItem(subMenu, "alarmCount", object);

    // the 'newAlarmCount' attribute
    createAttribMenuItem(subMenu, "newAlarmCount", object);
  }

  /**
   * <p>
   * Create a menu item that displays the attribute name and value of a given
   * representation object, adding it into the corresponding menu.
   *
   * @param menu
   *          The menu where the item should be added to
   * @param attrib
   *          The attribute name
   * @param object
   *          The representation object carrying the attribute values
   */
  private void createAttribMenuItem(IlvMenu menu, String attrib, IlpRepresentationObject object) {
    Object value = object.getAttributeValue(attrib);

    attrib = IlvResourceUtil.getCurrentLocaleString(MyPopupMenuFactory.class, attrib);

    if (null != value && !IlpAttributeValueHolder.VALUE_NOT_SET.equals(value)) {
      menu.addChild(new IlvMenuItem(attrib + ": " + value.toString(), null, "/resources/images/attribute.png", true));
    }
  }
}