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

import java.awt.Graphics;
import java.util.ArrayList;

import ilog.views.IlvGraphic;
import ilog.views.IlvGraphicBag;
import ilog.views.IlvManager;
import ilog.views.IlvTransformer;
import ilog.views.graphic.IlvRectangle;
import ilog.views.graphic.composite.IlvCompositeGraphic;
import ilog.views.sdm.graphic.IlvSDMCompositeNode;
import ilog.views.servlet.IlvHitmapDefinition;
import ilog.views.util.hitmap.IlvHitmapConstants;

/**
 * A customized hitmap info definition example.
 */
public class HitmapDefinition extends IlvHitmapDefinition {

  /**
   * Creates a customized IlvHitmapDefinition.
   */
  public HitmapDefinition() {
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * ilog.views.servlet.IlvHitmapDefinition#shouldRender(ilog.views.IlvGraphic)
   */
  Override
  public boolean shouldRender(IlvGraphic graphic) {
    return (graphic instanceof IlvCompositeGraphic);
  }

  /**
   * A utility class used to draw a rectangle instead of the label.
   */
  class LabelRectangle extends IlvRectangle {

    /**
     * The parent object of the label.
     */
    private IlvCompositeGraphic compo;

    /**
     * Creates a new <code>LabelRectangle</code>.
     */
    LabelRectangle() {
    }

    /**
     * Creates a new <code>LabelRectangle</code>.
     */
    LabelRectangle(IlvCompositeGraphic compo) {
      this.setFillOn(true);
      this.compo = compo;
    }

    String getLabel() {
      return compo.getLabel();
    }

    /*
     * (non-Javadoc)
     * 
     * @see ilog.views.graphic.IlvRectangle#draw(java.awt.Graphics,
     * ilog.views.IlvTransformer)
     */
    Override
    public void draw(Graphics g, IlvTransformer t) {
      IlvGraphicBag bag = compo.getGraphicBag();
      if (bag instanceof IlvManager) {
        IlvManager manager = (IlvManager) bag;
        IlvTransformer mt = manager.getTopLevelTransformer();
        mt.compose(t);
        t = mt;
      }
      setDefinitionRect(compo.getLabelBBox(t));
      super.draw(g, null);
    }
  }

  private void addLabelAsSeparatedItem(IlvSDMCompositeNode composite) {
    String label = composite.getLabel();
    if (label != null && label.length() > 0) {

      ArrayList<Integer> indexs = new ArrayList<Integer>();
      indexs.add(getGraphics().size());
      indexs.add(getGraphics().size() + 1);
      composite.setProperty(IlvHitmapConstants.JVIEWS_GROUP_PROPERTY, indexs.toString());

      LabelRectangle rectangle = new LabelRectangle(composite);
      super.addGraphic(rectangle);
      // rectangle.setProperty(IlvHitmapConstants.JVIEWS_GROUP_PROPERTY,
      // getGraphics().size());

    }
  }

  /**
   * Adds a graphic object to the managed list.
   * 
   * @param graphic
   *          The graphic object to be added.
   */
  Override
  public void addGraphic(IlvGraphic graphic) {
    super.addGraphic(graphic);
    if (graphic instanceof IlvSDMCompositeNode) {
      addLabelAsSeparatedItem((IlvSDMCompositeNode) graphic);
    }
  }

  /**
   * Returns the tooltip text of the graphic object;
   * 
   * @see ilog.views.servlet.IlvHitmapDefinition#getTooltip(ilog.views.IlvGraphic)
   */
  Override
  SuppressWarnings("deprecation")
  protected String getTooltip(IlvGraphic graphic) {
    String tooltip = null;
    String label = "Label";
    if (graphic instanceof LabelRectangle) {
      return formatLabelTooltip("Label:", ((LabelRectangle) graphic).getLabel());
    } else if (graphic instanceof IlvCompositeGraphic) {
      return formatCompositeTooltip("This is a composite", "Name:", ((IlvCompositeGraphic) graphic).getName());
    }
    if (tooltip == null || tooltip.length() == 0) {
      tooltip = graphic.getName();
      label = "Name";
    }
    if (tooltip == null || tooltip.length() == 0) {
      tooltip = graphic.getToolTipText();
      label = "Information";
    }
    if (tooltip != null && tooltip.length() > 0) {
      StringBuffer buffer = new StringBuffer();
      buffer.append("<table class='jviews_tooltip'>");
      buffer.append("<tr class='jviews_tooltip_title'>");
      buffer.append("<td>" + label + "</td>" + "<td>" + tooltip + "</td>");
      buffer.append("</table>");
      return buffer.toString();
    }
    return super.getTooltip(graphic);
  }

  private static String formatCompositeTooltip(String title, String label, String text) {
    StringBuffer buffer = new StringBuffer();
    buffer.append("<table>");
    buffer.append("<tr>");
    buffer.append("<th colspan='2'>" + title + "</th>");
    buffer.append("<tr>");
    buffer.append("<td>" + label + "</td>" + "<td>" + text + "</td>");
    buffer.append("</table>");
    return buffer.toString();
  }

  private static String formatLabelTooltip(String label, String text) {
    StringBuffer buffer = new StringBuffer();
    buffer.append("<table>");
    buffer.append("<tr>");
    buffer.append("<td>" + label + "</td>" + "<td>" + text + "</td>");
    buffer.append("</tr>");
    buffer.append("</table>");
    return buffer.toString();
  }

}