/*
 * 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.IltAbstractDirectImageBaseRendererFactory;
import ilog.tgo.graphic.renderer.IltBaseRenderer;
import ilog.tgo.graphic.renderer.IltLedBaseRenderer;
import ilog.tgo.model.IltLed;

import ilog.views.IlvRect;
import ilog.views.IlvTransformer;
import ilog.views.graphic.IlvIcon;

import java.awt.Graphics;
import java.awt.Image;

/**
 * This class defines a base renderer factory for the custom LED 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 LEDs and other
 * business objects.
 * 
 * @see ilog.tgo.graphic.renderer.IltLedImageBaseRendererFactory
 */
public class LedBaseRendererFactory extends IltAbstractDirectImageBaseRendererFactory {

  Override
  protected IltBaseRenderer createBaseRenderer() {
    return new BaseRenderer();
  }
  
  /**
   * This implementation of a simple image renderer that can draw a given
   * image.
   */
  protected static class BaseRenderer extends IltLedBaseRenderer {
    // Use an IlvIcon to draw the image
    private IlvIcon _base;
    
    Override
    public void initResources() {
      // Initialize original resources
      super.initResources();
      // Get image
      Image icon = getSourceImage();
      // Get image dimension
      IlvRect rect = new IlvRect(0,0,icon.getWidth(null),icon.getHeight(null));
      // Create IlvIcon responsible for drawing the image
      _base = new IlvIcon(icon, rect);
      // Update representation object with appropriate width/height attribute
      getRepresentationObject().setAttributeValue(IltLed.WidthAttribute, rect.width);
      getRepresentationObject().setAttributeValue(IltLed.HeightAttribute, rect.height);
    }

    Override
    protected void drawMain(Graphics g, IlvTransformer t, IlvRect rect) {
      int x1 = (int)rect.x;
      int y1 = (int)rect.y;
      int x2 = x1 + (int)rect.width - 1;
      int y2 = y1 + (int)rect.height - 1;
      // preventing drawing if image is too small
      if (x1 <= x2 && y1 <= y2) {
        // Compute the transformer to convert the current bounding box
        // into the given rect
        IlvTransformer transf = new IlvTransformer();
        IlvTransformer.computeTransformer(_base.boundingBox(), rect, transf);
        _base.draw(g, transf);
      }
    }
  }
}