/*
 * Licensed Materials - Property of Rogue Wave Software, Inc. 
 * © Copyright Rogue Wave Software, Inc. 2014, 2015 
 * © 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 ilog.tgo.graphic.renderer.IltBaseRenderer;
import ilog.tgo.graphic.renderer.IltPortDirectImageBaseRendererFactory;

import ilog.views.IlvRect;
import ilog.views.IlvTransformer;

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;

/**
 * This class defines a base renderer factory for the custom port 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 ports and other
 * business objects.
 * 
 * @see ilog.tgo.graphic.renderer.IltPortDirectImageBaseRendererFactory
 */
public class PortBaseRendererFactory extends IltPortDirectImageBaseRendererFactory {

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

  /**
   * This class defines a base renderer for all port types.
   * 
   * <p>In this sample, all the customized base renderers are based on
   * the same principle: using images to represent ports.
   * 
   * <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 ports and other
   * business objects.
   * 
   */
  public class PortBaseRenderer extends IltPortDirectImageBaseRendererFactory.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 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-borderWidth, yPos-borderWidth, width + 2*borderWidth, height + 2*borderWidth);
        
        g2d.setStroke(oldStroke);
        g2d.setPaint(oldPaint);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAntiAliasing);
      }
    }    
  }
}