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

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Stroke;

import ilog.tgo.graphic.renderer.IltBaseRenderer;
import ilog.tgo.graphic.renderer.IltNEDirectImageBaseRendererFactory;
import ilog.views.IlvRect;
import ilog.views.IlvTransformer;

/**
 * This class defines a base renderer factory for the custom network element
 * types.
 * 
 * <p>
 * In this sample, all the customized base renderers are based on the same
 * principle: using images to represent network elements.
 * 
 * <p>
 * Please refer to the <i>Customizing Network and Equipment Nodes </i> section
 * in the <i>Styling</i> chapter of the <i>User's Manual</i> for detailed
 * information on how to customize network elements and other business objects.
 * 
 * @see ilog.tgo.graphic.renderer.IltNEDirectImageBaseRendererFactory
 * @see ilog.tgo.graphic.renderer.IltNEBaseRenderer
 */
public class NEBaseRendererFactory extends IltNEDirectImageBaseRendererFactory {

  Override
  public IltBaseRenderer createValue() {
    return new NEBaseRenderer();
  }

  /**
   * This class defines a base renderer for all network element types.
   * 
   * <p>
   * In this sample, all the customized base renderers are based on the same
   * principle: using images to represent network elements.
   * 
   * <p>
   * Please refer to the <i>Customizing Network and Equipment Nodes </i> section
   * in the <i>Styling</i> chapter of the <i>User's Manual</i> for detailed
   * information on how to customize network elements and other business
   * objects.
   * 
   * @see ilog.tgo.graphic.renderer.IltNEBaseRenderer
   */
  public class NEBaseRenderer extends IltNEDirectImageBaseRendererFactory.BaseRenderer {
    /**
     * Always draw the extra borders before the object itself. The selection
     * border is draw around the object and we want it to be always behind the
     * main object.
     */
    Override
    public void draw(Graphics g, IlvTransformer t, IlvRect rect) {
      drawExtraBorders(g, t, rect);
      drawMain(g, t, rect);
    }

    /**
     * Draws the extra borders around the image.
     * 
     * <p>
     * This method is used to draw the selection border around the object; we
     * change the way the object is selected by defining a circle below the
     * object and not a line around it.
     * 
     * @see ilog.tgo.graphic.renderer.IltBaseRenderer#drawExtraBorders(java.awt.Graphics,
     *      ilog.views.IlvTransformer, ilog.views.IlvRect)
     */
    Override
    public void drawExtraBorders(Graphics g, IlvTransformer t, IlvRect rect) {
      Color fg = this.getSelectionBorderForeground();
      Color bg = this.getSelectionBorderBackground();
      if (null != fg) {
        Graphics2D g2d = (Graphics2D) g;
        Paint oldPaint = g2d.getPaint();
        Object oldAntiAliasing = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int xPos = rect.xFloor();
        int yPos = rect.yFloor();
        int size = rect.heightFloor();

        g2d.setPaint(fg);
        g2d.fillOval(xPos, yPos, size, size);
        if (null != bg) {
          Stroke oldStroke = g2d.getStroke();
          g2d.setStroke(new BasicStroke(getSelectionBorderWidth()));
          g2d.setPaint(bg);
          g2d.drawOval(xPos + 3, yPos + 3, size - 7, size - 7);
          g2d.setStroke(oldStroke);
        }
        g2d.setPaint(oldPaint);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAntiAliasing);
      }
    }

  }

}