/*
 * 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.IltCardDirectImageBaseRendererFactory;
import ilog.views.IlvRect;
import ilog.views.IlvTransformer;

/**
 * This class defines a base renderer factory for the custom card types.
 * 
 * <p>
 * In this sample, all the customized base renderers are based on the same
 * principle: using images to represent 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 cards and other business objects.
 * 
 * @see ilog.tgo.graphic.renderer.IltCardDirectImageBaseRendererFactory
 */
public class CardBaseRendererFactory extends IltCardDirectImageBaseRendererFactory {

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

  /**
   * This class defines a base renderer for all card types.
   * 
   * <p>
   * In this sample, all the customized base renderers are based on the same
   * principle: using images to represent cards.
   * 
   * <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 cards and other business objects.
   * 
   */
  public class CardBaseRenderer extends IltCardDirectImageBaseRendererFactory.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) {
      drawMain(g, t, rect);
      drawExtraBorders(g, t, rect);
    }

    /**
     * Draws the extra borders around the image.
     * 
     * <p>
     * This method is used to draw the selection border around the object by
     * drawing a bow 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();
      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 height = rect.heightFloor();
        int width = rect.widthFloor();
        int borderWidth = getSelectionBorderWidth();

        Stroke oldStroke = g2d.getStroke();
        g2d.setStroke(new BasicStroke(borderWidth));
        g2d.setPaint(fg);
        g2d.drawRect(xPos, yPos, width - borderWidth, height - borderWidth);

        g2d.setStroke(oldStroke);
        g2d.setPaint(oldPaint);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAntiAliasing);
      }
    }
  }
}