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

import java.awt.Rectangle;

import ilog.views.IlvRect;
import ilog.views.sdm.IlvSDMView;
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;

/**
 * A menu model factory.
 */
public class MenuFactory implements IlvMenuFactory {

  /**
   * 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 view
   *          the sdm view
   * @param root
   *          The root menu
   * @param menuModelId
   *          The menu model ID
   */
  public void createNavigationMenuItems(IlvSDMView view, IlvMenu root, String menuModelId) {
    double zoomLevel = 5;
    if (view != null) {
      Rectangle r = view.getBounds();
      IlvRect bbox = view.getManager().boundingBox(view.getTransformer());
      double wr = bbox.width / r.width;
      double hr = bbox.height / r.height;
      zoomLevel = wr > hr ? wr : hr;
    }
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "zoomIn"), //$NON-NLS-1$ but
                                                                                                        // should
                                                                                                        // be
                                                                                                        // translated
        "diagrammer.zoomIn();", //$NON-NLS-1$
        "/images/zoom.gif", (zoomLevel < 10))); //$NON-NLS-1$
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "zoomOut"), //$NON-NLS-1$ but
                                                                                                         // should
                                                                                                         // be
                                                                                                         // translated
        "diagrammer.zoomOut();", //$NON-NLS-1$
        "/images/unzoom.gif", (zoomLevel > 1.1))); //$NON-NLS-1$
    root.addChild(createJSMenuItem(IlvResourceUtil.getCurrentLocaleString(MenuFactory.class, "zoomToFit"), //$NON-NLS-1$ but
                                                                                                           // should
                                                                                                           // be
                                                                                                           // translated
        "diagrammer.showAll();", //$NON-NLS-1$
        "/images/zoomfit.gif")); //$NON-NLS-1$
  }

  /**
   * 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"); //$NON-NLS-1$
    createNavigationMenuItems((IlvSDMView) graphicComponent, root, menuModelId);
    return root;
  }
}