/*
 * 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.
 */

import ilog.views.IlvGraphic;
import ilog.views.graphic.IlvGraphicSet;
import ilog.views.graphic.IlvLabel;
import ilog.views.graphic.IlvMarker;
import ilog.views.io.IlvInputStream;
import ilog.views.io.IlvOutputStream;
import ilog.views.io.IlvReadFileException;
import ilog.views.maps.IlvFeatureAttribute;
import ilog.views.maps.IlvFeatureAttributeProperty;
import ilog.views.maps.IlvMapFeature;
import ilog.views.maps.IlvMapRenderException;
import ilog.views.maps.rendering.IlvDefaultPointRenderer;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformation;
import ilog.views.maps.srs.coordtrans.IlvCoordinateTransformationException;

/**
 * This renderer renders IlvMapPoint geometries into IlvMarker with
 * eventually an associated text extracted from a specified attribute.
 */
public class MarkerTextRenderer 
  extends IlvDefaultPointRenderer
{
  /**
   * Name of attribute to display
   */
  private String myAttributeName;

  /**
   * Constructs a new MarkerTextRenderer to render points with an attached
   * label.
   * @param attributeName the name of the attribute for text of label
   */
  public MarkerTextRenderer(String attributeName)
  {
    super();
    myAttributeName = attributeName;
  }

  /**
   * Translates the feature into a graphic object
   */
  public IlvGraphic makeGraphic(IlvMapFeature feature,
                                IlvCoordinateTransformation tr)
    throws IlvMapRenderException, IlvCoordinateTransformationException 
  {
    // Let the super class create the marker
    IlvMarker marker = (IlvMarker)super.makeGraphic(feature, tr);

    // Create label if needed
    IlvLabel label = null;
    IlvFeatureAttributeProperty attributeList = feature.getAttributes();
    if (attributeList != null) {
      try {
        IlvFeatureAttribute attribute = null;
        attribute = attributeList.getAttribute(myAttributeName);
        if (attribute != null)
          label = new IlvLabel(marker.getPoint(), attribute.toString());
      } catch (IllegalArgumentException e) {
        label = null;
      }
    }

    // Case no label : return marker
    if (label == null) {
      return marker;
    }
    // else generate a graphic set containing the marker and the label
    else {
      // make this label of the same color than the marker
      label.setForeground(getPointRenderingStyle().getMarkerColor());

      IlvGraphicSet set = new IlvGraphicSet();
      set.addObject(marker, false);
      set.addObject(label, false);
      return set;
    }
  }

  //
  // Constructors and methods to implement persistence
  //

  /**
   * Returns true if the object must be saved.
   */
  public boolean isPersistent()
  {
    return true;
  }

  /**
   * Write this to specified stream
   */
  public void write(IlvOutputStream stream)
    throws java.io.IOException
  {
    super.write(stream);
    stream.write("attributeName",myAttributeName);
  }
  
  /**
   * Constructs a new ColorLineRenderer from stream
   */
  public MarkerTextRenderer(IlvInputStream stream)
    throws IlvReadFileException
  {
    super(stream);
    myAttributeName = stream.readString("attributeName");
  }
}