Optimized_Renderer
OptimizedRenderer.java
/*
 * ArcGIS Engine Developer Sample
 * Application Name: OptimizedRenderer.java
 */

package com.esri.arcgis.samples.carto.renderers.optimizedrenderer;

import java.io.IOException;

import com.esri.arcgis.carto.IFeatureIDSet;
import com.esri.arcgis.carto.IFeatureRenderer;
import com.esri.arcgis.display.IDisplay;
import com.esri.arcgis.display.ISymbol;
import com.esri.arcgis.display.ISymbolProxy;
import com.esri.arcgis.display.SimpleFillSymbol;
import com.esri.arcgis.display.SimpleLineSymbol;
import com.esri.arcgis.display.SimpleMarkerSymbol;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IFeatureDraw;
import com.esri.arcgis.geodatabase.IFeatureDrawProxy;
import com.esri.arcgis.geodatabase.IQueryFilter;
import com.esri.arcgis.geodatabase.esriDrawStyle;
import com.esri.arcgis.geometry.IGeometry;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.system.ITrackCancel;
import com.esri.arcgis.system.esriDrawPhase;
import com.linar.jintegra.AutomationException;

/**
 * The renderer draws point line or polygon features by useing three symbols.
 * There are two methods to draw futures. They have a different perfomance.
 * @see #draw
 */
public class OptimizedRenderer implements IFeatureRenderer {

  private ISymbol pSimpleMarkerSymbol = null;
  private ISymbol pSimpleLineSymbol = null;
  private ISymbol pISimpleFillSymbol = null;
  private ISymbol pSymbol = null;
  private boolean fast = false;

  /**
   * Default constructor. creates three symbols for drawing.
   */
  OptimizedRenderer() {
    try {
      pSimpleMarkerSymbol = new ISymbolProxy(new SimpleMarkerSymbol());
      pSimpleLineSymbol = new ISymbolProxy(new SimpleLineSymbol());
      pISimpleFillSymbol = new ISymbolProxy(new SimpleFillSymbol());
    } catch (Exception e) {
    }
  }

  // IFeatureRenderer interface

  /**
   * @see IFeatureRenderer#canRender
   */
  public boolean canRender(IFeatureClass iFeatureClass, IDisplay iDisplay) throws IOException, AutomationException {
    if (iFeatureClass.getShapeType() != esriGeometryType.esriGeometryNull)
      return true;
    return false;
  }

  /**
   * @see IFeatureRenderer#prepareFilter
   */
  public void prepareFilter(IFeatureClass pFeatureClass, IQueryFilter iQueryFilter) throws IOException, AutomationException {
    switch (pFeatureClass.getShapeType()) {
      case esriGeometryType.esriGeometryPoint:
      case esriGeometryType.esriGeometryMultipoint:
        pSymbol = pSimpleMarkerSymbol;
        break;
      case esriGeometryType.esriGeometryPolyline:
        pSymbol = pSimpleLineSymbol;
        break;
      case esriGeometryType.esriGeometryPolygon:
        pSymbol = pISimpleFillSymbol;
        break;
    }
  }

  /**
   * Loop through and draw each feature.
   * There are two drawing methods depend on fast value.
   * The method IFeatureDraw.draw(...) has better perfomance.
   * The methods IDisplay.draw*(...) gives more control over geometry.
   * @see IFeatureRenderer#draw
   */
  public void draw(IFeatureCursor pFeatureCursor, int drawPhase, IDisplay pDisplay, ITrackCancel iTrackCancel) throws IOException, AutomationException {
    // do not draw features if no display or cursor
    if (pDisplay == null || pFeatureCursor == null)
      return;
    if (drawPhase != esriDrawPhase.esriDPGeography)
      return;
    pDisplay.setSymbol(pSymbol);
    IFeature pFeature = pFeatureCursor.nextFeature();
    // while there are still more features and drawing has not been canceled
    while (pFeature != null) {
      if (fast) {
        // draw feature fast
        IFeatureDraw pFeatureDraw = new IFeatureDrawProxy(pFeature);
        pFeatureDraw.draw(drawPhase, pDisplay, pSymbol, true, null, esriDrawStyle.esriDSNormal);
      } else {
        // draw feature slowly
        IGeometry pGeometry = pFeature.getShape();
        switch (pGeometry.getGeometryType()) {
          case esriGeometryType.esriGeometryPoint:
            pDisplay.drawPoint(pGeometry);
            break;
          case esriGeometryType.esriGeometryMultipoint:
            pDisplay.drawMultipoint(pGeometry);
            break;
          case esriGeometryType.esriGeometryPolyline:
            pDisplay.drawPolyline(pGeometry);
            break;
          case esriGeometryType.esriGeometryPolygon:
            pDisplay.drawPolygon(pGeometry);
            break;
        }
      }
      pFeature = pFeatureCursor.nextFeature();
      if (iTrackCancel != null)
        if (!iTrackCancel.esri_continue())
          break;
    } // while
  }

  /**
   * @see IFeatureRenderer#getSymbolByFeature
   */
  public ISymbol getSymbolByFeature(IFeature iFeature) throws IOException, AutomationException {
    // no implement
    return null;
  }

  /**
   * @see IFeatureRenderer#isRenderPhase
   */
  public boolean isRenderPhase(int drawPhase) throws IOException, AutomationException {
    if (drawPhase == esriDrawPhase.esriDPGeography)
      return true;
    else
      return false;
  }

  /**
   * @see IFeatureRenderer#setExclusionSetByRef
   */
  public void setExclusionSetByRef(IFeatureIDSet iFeatureIDSet) throws IOException, AutomationException {
    // no implement
  }

  // own public methods

  /**
   * Set drawing method
   * @param value
   */
  public void setFastDraw(boolean value) {
    fast = value;
  }

  /**
   * Return drawing method.
   * @return
   */
  public boolean setFastDraw() {
    return fast;
  }
}