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

import java.awt.Color;
import java.util.Collection;

import ilog.views.IlvGraphic;
import ilog.views.IlvPoint;
import ilog.views.graphic.IlvEllipse;
import ilog.views.sdm.IlvSDMEngine;
import ilog.views.sdm.graphic.IlvSimpleLink;
import ilog.views.sdm.renderer.IlvSDMRenderer;
import ilog.views.util.IlvColorUtil;

/**
 * An implementation of SDM renderer that manages directly the objects (no css
 * involved).
 * 
 * @author kaplan
 * @since JViews 8.5
 */
public class SimpleDiagrammerRenderer extends IlvSDMRenderer {

  ///////////////////////
  // Actually, only the 2 following methods are mandatory.

  Override
  public IlvGraphic createLinkGraphic(IlvSDMEngine engine, Object link, IlvGraphic from, IlvGraphic to) {
    IlvSimpleLink result = new IlvSimpleLink(from, to, true, null);
    return result;
  }

  Override
  public IlvGraphic createNodeGraphic(IlvSDMEngine engine, Object node) {
    // let's create a filled circle for all objects
    Number X = (Number) engine.getModel().getObjectProperty(node, "x");
    Number Y = (Number) engine.getModel().getObjectProperty(node, "y");
    IlvEllipse result = new IlvEllipse(new IlvPoint(X.doubleValue(), Y.doubleValue()), 20.);
    setBackground(engine, node, result);
    result.setFillOn(true);
    return result;
  }

  //////////////////////////
  // All the other methods are optional, but offer more services.

  // called when model has changed
  Override
  public void propertiesChanged(IlvSDMEngine engine, Object object, Collection<String> propertyNames,
      IlvGraphic graphic) {
    if (graphic instanceof IlvEllipse) {
      IlvEllipse gnode = (IlvEllipse) graphic;
      setBackground(engine, object, gnode);
    }
  }

  // called when a pseudo class has change (i.e.: selected)
  Override
  public void customize(IlvSDMEngine engine, Object object, IlvGraphic g, String[] pseudoClasses) {

    if (g == null) {
      return;
    }
    boolean selectRequest = false;
    // is requesting selection ?
    if (pseudoClasses != null) {
      for (String s : pseudoClasses) {
        if ("selected".equals(s)) {
          selectRequest = true;
        }
      }
    }

    // manage node selection feedback
    if (g instanceof IlvEllipse) {
      IlvEllipse gnode = (IlvEllipse) g;
      // compute current selected state
      boolean selectedState = gnode.getForeground() == Color.red;
      // match ?
      if (selectedState != selectRequest) {
        // toggle color
        gnode.setForeground(selectedState ? Color.black : Color.red);
      }
    }

    // same for links
    if (g instanceof IlvSimpleLink) {
      IlvSimpleLink glink = (IlvSimpleLink) g;
      // compute current selected state
      boolean selectedState = glink.getForeground() == Color.red;
      // match ?
      // toggle color
      if (selectedState != selectRequest) {
        glink.setForeground(selectedState ? Color.black : Color.red);
      }
    }
  }

  /**
   * Sets node background, according to model state
   * 
   * @param engine
   *          the engine
   * @param node
   *          the model object
   * @param graphic
   *          the graphic representation of node
   */
  private void setBackground(IlvSDMEngine engine, Object node, IlvEllipse graphic) {
    String colorString = (String) engine.getModel().getObjectProperty(node, "color");
    Color color = colorString == null ? Color.orange : IlvColorUtil.getColor(colorString);
    graphic.setBackground(color);
  }

}