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

import ilog.views.IlvManager;
import ilog.views.diagrammer.IlvDiagrammer;
import ilog.views.diagrammer.faces.IlvFacesDiagrammerUtil;
import ilog.views.faces.dhtml.IlvDHTMLConstants;
import ilog.views.faces.dhtml.event.FacesMethodBindingActionListener;
import ilog.views.sdm.IlvSDMView;
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;

public class GalleryMenuFactory 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();
    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(GalleryMenuFactory.class, "setZoomIt"),
        "zoomButton.doClick()", "images/zoomrect.gif", enabled));

    enabled = !PAN_MENU_MODEL_ID.equals(menuModelId);
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(GalleryMenuFactory.class, "setPanIt"),
        "panButton.doClick()", "images/pan.gif", enabled));

    enabled = !SELECT_MENU_MODEL_ID.equals(menuModelId);
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(GalleryMenuFactory.class, "setSelectIt"),
        "selectButton.doClick()", "images/arrow.gif", enabled));

    root.addChild(new IlvMenuSeparator());

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

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

    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(GalleryMenuFactory.class, "showallTooltip"),
        "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)
      for (int i = 0; i < names.length; i++) {
        if (names[i].indexOf("sdm:") != -1 || "x".equalsIgnoreCase(names[i]) || "y".equalsIgnoreCase(names[i])
            || "width".equalsIgnoreCase(names[i]) || "height".equalsIgnoreCase(names[i])
            || "CSSclass".equalsIgnoreCase(names[i]) || "".equals(node.getProperty(names[i]))) {
          continue;
        }
        String s = names[i] + " : " + node.getProperty(names[i]);
        root.addChild(new IlvMenuItem(s, null, "images/property.gif", true));
      }

  }

//  /**
//   * Create a menu item to show or hide the current layer.
//   * 
//   * param layer
//   *          The manager layer.
//   * return The menu item to show/hide layer.
//   */
//  private IlvMenuItem buildMenuItem(IlvManagerLayer layer) {
//
//    IlvMenuItem item = new IlvMenuItem();
//    String name = layer.getName();
//    item.setLabel(name);
//
//    return item;
//  }

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

    IlvMenu root = new IlvMenu("Root");

    createNavigationMenuItems(root, menuModelId);

    if (selectedObject != null) {

      IlvDiagrammer diagrammer = IlvFacesDiagrammerUtil.getDiagrammer((IlvSDMView) graphicComponent);

      if (diagrammer.getEngine().getGraphic(selectedObject, false) instanceof IlvManager) {

        IlvMenuItem item = new IlvMenuItem(
            IlvResourceUtil.getCurrentLocaleString(GalleryMenuFactory.class, "expandCollapse"), null,
            "images/arrowclick.gif");
        FacesMethodBindingActionListener l = new FacesMethodBindingActionListener(
            "#{galleryBean.expandCollapseSubGraph}");
        l.setInvocationContext(IlvDHTMLConstants.IMAGE_SERVLET_CONTEXT);
        item.setActionListener(l);
        root.addChild(item);

      }

      root.addChild(new IlvMenuSeparator());

      IlvMenu subMenu = new IlvMenu(IlvResourceUtil.getCurrentLocaleString(GalleryMenuFactory.class, "Properties"),
          "images/properties.gif");
      IlvSDMNode node = (IlvSDMNode) selectedObject;
      createPropertyMenuItems(subMenu, node);
      root.addChild(subMenu);

    }

    return root;
  }

}