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

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import ilog.views.IlvManagerView;
import ilog.views.IlvPoint;
import ilog.views.diagrammer.IlvDiagrammer;
import ilog.views.diagrammer.servlet.IlvDiagrammerServlet;
import ilog.views.sdm.IlvSDMView;
import ilog.views.servlet.ServerActionEvent;
import ilog.views.servlet.ServerActionListener;

/**
 * A subclass of <code>IlvDiagrammerServlet</code> that loads a Diagrammer
 * project file.
 * <p>
 * This subclass simply sets the parameters of the servlet in its
 * <code>init</code> methods. The basic behavior of the servlet is fully
 * implemented by the base class <code>IlvDiagrammerServlet</code>.
 * <p>
 * This demo also shows how to implement interactions in thin-client mode. The
 * user can click on the objects on the client-side to select objects in the
 * diagram. This is implemented by adding a server action listener.
 */
public class DiagrammerServlet extends IlvDiagrammerServlet implements ServerActionListener {
  public DiagrammerServlet() {
  }

  Override
  public void init(ServletConfig config) throws ServletException {
    super.init(config);

    addServerActionListener(this);

    config.getServletContext().log("multiSession=" + isMultiSession());
    config.getServletContext().log("project=" + getDefaultProject());
  }

  public float getMaxZoomLevel(HttpServletRequest request, IlvManagerView view) {
    return (8);
  }

  /**
   * Implementation of the <code>ServerActionListener</code> that handles mouse
   * clicks in the client view.
   * <p>
   * In this demo, you simply select the object where the user clicked.
   */
  Override
  public void actionPerformed(ServerActionEvent e) {
    IlvDiagrammer diagrammer = getDiagrammer(e.getRequest());
    IlvSDMView view = diagrammer.getView();

    diagrammer.deselectAll();

    Object obj;
    try {
      IlvPoint p = e.getPointParameter(0);
      // The point is in manager coordinates.
      view.getTransformer().apply(p);
      obj = diagrammer.getEngine().getObject(p, view, true);
    } catch (ServletException ex1) {
      try {
        String id = e.getStringParameter(0);
        obj = diagrammer.getObject(id);
      } catch (ServletException ex2) {
        getServletContext().log("No parameter!", ex2);
        obj = null;
      }
    }
    if (obj != null) {
      diagrammer.setSelected(obj, true);
    }
  }
}