arcgissamples\cartography\OptimizedRenderer.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Optimized renderer
arcgissamples\cartography\OptimizedRenderer.java
/* Copyright 2015 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
package arcgissamples.cartography;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

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.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.IQueryFilter;
import com.esri.arcgis.geodatabase.esriDrawStyle;
import com.esri.arcgis.geometry.IGeometry;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.interop.extn.ArcGISExtension;
import com.esri.arcgis.system.IDocumentVersionSupportGEN;
import com.esri.arcgis.system.ITrackCancel;
import com.esri.arcgis.system.esriDrawPhase;
import com.esri.arcgis.interop.AutomationException;

/**
 * The renderer draws point line or polygon features by using three symbols.
 * There are two methods to draw features. They have a different perfomance.
 * @see #draw
 */

@ArcGISExtension
public class OptimizedRenderer implements IFeatureRenderer, Externalizable, IDocumentVersionSupportGEN, IOptimizedRenderer {
  SimpleMarkerSymbol simpleMarkerSymbol = null;
  SimpleLineSymbol simpleLineSymbol = null;
  SimpleFillSymbol simpleFillSymbol = null;
  ISymbol symbol = null;
  boolean fast = false;

  /**
   * Default constructor. creates three symbols for drawing.
   */
  public OptimizedRenderer() {
    try {
      this.simpleMarkerSymbol = new SimpleMarkerSymbol();
      this.simpleLineSymbol = new SimpleLineSymbol();
      this.simpleFillSymbol = new SimpleFillSymbol();
    } catch (Exception e) {
      // never happened
    }
  }

  // IFeatureRenderer interface

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

  /**
   * @see IFeatureRenderer#prepareFilter
   */
  public void prepareFilter(IFeatureClass featureClass, IQueryFilter queryFilter) throws IOException, AutomationException {
    switch (featureClass.getShapeType()) {
    case esriGeometryType.esriGeometryPoint:
    case esriGeometryType.esriGeometryMultipoint:
      this.symbol = this.simpleMarkerSymbol;
      break;
    case esriGeometryType.esriGeometryPolyline:
      this.symbol = this.simpleLineSymbol;
      break;
    case esriGeometryType.esriGeometryPolygon:
      this.symbol = this.simpleFillSymbol;
      break;
    }

  }

  /**
   * Loop through and draw each feature.
   * There are two drawing methods depending 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 featureCursor, int drawPhase, IDisplay display, ITrackCancel trackCancel) throws IOException, AutomationException {
    // do not draw features if no display or cursor
    if (display == null || featureCursor == null)
      return;
    if (drawPhase != esriDrawPhase.esriDPGeography){
      //return;
      // We throw this automation exception so that the selection will work when the custom renderer is applied
      AutomationException orefAE = new AutomationException(1, "OptimizedRenderer.java", "For Selection");
      throw orefAE;
    }
    display.setSymbol(this.symbol);
    IFeature feature = featureCursor.nextFeature();
    // while there are still more features and drawing has not been canceled
    while (feature != null) {
      IFeatureDraw featureDraw = (IFeatureDraw) feature;
      if (this.fast) {
        // draw feature fast
        featureDraw.draw(drawPhase, display, this.symbol, true, null, esriDrawStyle.esriDSNormal);
      } else {
        // draw feature slowly
        IGeometry geometry = feature.getShape();
        switch (geometry.getGeometryType()) {
        case esriGeometryType.esriGeometryPoint:
          display.drawPoint(geometry);
          break;
        case esriGeometryType.esriGeometryMultipoint:
          display.drawMultipoint(geometry);
          break;
        case esriGeometryType.esriGeometryPolyline:
          display.drawPolyline(geometry);
          break;
        case esriGeometryType.esriGeometryPolygon:
          display.drawPolygon(geometry);
          break;
        }
      }
      feature = featureCursor.nextFeature();
      if (trackCancel != null)
        if (!trackCancel.esri_continue())
          break;
    }
  }

  /**
   * @see IFeatureRenderer#getSymbolByFeature
   */
  public ISymbol getSymbolByFeature(IFeature feature) {
    // no implement
    return null;
  }

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

  /**
   * @see IFeatureRenderer#setExclusionSetByRef
   * @param featureIDSet IFeatureIDSet
   */
  public void setExclusionSetByRef(IFeatureIDSet featureIDSet) {
    // no implement
  }

  // own public methods

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

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

  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    symbol = (ISymbol) in.readObject();
  }

  public void writeExternal(ObjectOutput out) throws IOException {
    out.writeObject(symbol);
  }

  public Object convertToSupportedObject(int arg0) throws IOException, AutomationException {
    return null;
  }

  public boolean isSupportedAtVersion(int arg0) throws IOException, AutomationException {
    return true;
  }
}