arcgissamples\cartography\ValenceRenderer.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Valence renderer
arcgissamples\cartography\ValenceRenderer.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.carto.ILegendClass;
import com.esri.arcgis.carto.ILegendGroup;
import com.esri.arcgis.carto.ILegendInfo;
import com.esri.arcgis.carto.ILegendItem;
import com.esri.arcgis.carto.LegendClass;
import com.esri.arcgis.carto.LegendGroup;
import com.esri.arcgis.carto.SimpleRenderer;
import com.esri.arcgis.display.IDisplay;
import com.esri.arcgis.display.IMarkerSymbol;
import com.esri.arcgis.display.IRgbColor;
import com.esri.arcgis.display.ISimpleTextSymbol;
import com.esri.arcgis.display.ISymbol;
import com.esri.arcgis.display.RgbColor;
import com.esri.arcgis.display.SimpleMarkerSymbol;
import com.esri.arcgis.display.TextSymbol;
import com.esri.arcgis.display.esriSimpleMarkerStyle;
import com.esri.arcgis.display.esriTextHorizontalAlignment;
import com.esri.arcgis.display.esriTextVerticalAlignment;
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.ISimpleJunctionFeature;
import com.esri.arcgis.geodatabase.esriDrawStyle;
import com.esri.arcgis.geodatabase.esriFeatureType;
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.esriArcGISVersion;
import com.esri.arcgis.system.esriDrawPhase;

@ArcGISExtension
public class ValenceRenderer implements IFeatureRenderer, ILegendInfo, Externalizable, IDocumentVersionSupportGEN {

  private ILegendGroup legendGroup;
  ISimpleTextSymbol textSym = null;
  private String[] labels;

  public ValenceRenderer() {
    try {
      initialize();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void initialize() throws Exception {
    try {
      textSym = new TextSymbol();
      legendGroup = new LegendGroup();
      legendGroup.setHeading("Number of Connections");
      ILegendClass legendClass;
      RgbColor[] colors = new RgbColor[4];
      labels = new String[4];
      // create and set default colors
      colors[0] = new RgbColor();
      colors[0].setRGB(-65281);// Pink
      colors[1] = new RgbColor();
      colors[1].setRGB(-65485);// Red
      colors[2] = new RgbColor();
      colors[2].setRGB(-16711681);// Cyan
      colors[3] = new RgbColor();
      colors[3].setRGB(-167511-4);// Dark Green
      // create a legend class, create and assign symbol, then add to legendGroup. repeat.
      for (int i = 0; i < 4; i++) {
        legendClass = new LegendClass();
        SimpleMarkerSymbol simpleMSym = new SimpleMarkerSymbol();
        // initial symbol will be a simple marker symbol
        simpleMSym.setColor(colors[i]);
        simpleMSym.setStyle(esriSimpleMarkerStyle.esriSMSCircle);
        simpleMSym.setSize(6.0);
        legendClass.setSymbolByRef(simpleMSym);
        legendClass.setLabel("color" + i);
        legendGroup.addClass(legendClass);
      }
      legendGroup.setVisible(true);
    } catch (AutomationException e) {
      System.out.println("ValenceRend Initialize: AutomationException "+ e); // never happened
    } catch (IOException e) {
      System.out.println("ValenceRend Initialize: IOException " + e); // never happened
    }
  }

  public void terminate() {
    legendGroup = null;
  }

  public boolean canRender(IFeatureClass fc, IDisplay display)
      throws IOException, AutomationException {
    if (fc.getFeatureType() == esriFeatureType.esriFTSimpleJunction) {
      return true;
    } else {
      return false;
    }
  }

  public void draw(IFeatureCursor featureCursor, int drawPhase,
      IDisplay display, ITrackCancel trackCancel) throws IOException,
      AutomationException {
    if (display == null || 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, "ValenceRenderer.java", "For Selection");
      throw orefAE;
    }
    IFeature feat = featureCursor.nextFeature();
    boolean bContinue = true;
    // while there are still more features and drawing has not been canceled
    while (feat != null && bContinue) {
      ISimpleJunctionFeature junctionFeat = (ISimpleJunctionFeature) (feat);
      int conCount = junctionFeat.getEdgeFeatureCount();
      ISymbol symbol;
      switch (conCount) {
      case 0:
        symbol = legendGroup.esri_getClass(0).getSymbol();
        break;
      case 1:
        symbol = legendGroup.esri_getClass(1).getSymbol();
        break;
      case 2:
        symbol = legendGroup.esri_getClass(2).getSymbol();
        break;
      default:
        symbol = legendGroup.esri_getClass(3).getSymbol();
        break;
      }
      display.setSymbol(symbol);
      IFeatureDraw featDraw = (IFeatureDraw) (feat);
      // draw marker
      featDraw.draw(drawPhase, display, symbol, true, null,
          esriDrawStyle.esriDSNormal);
      textSym = new TextSymbol();
      // draw text, where the position depends on connections.
      switch (conCount) {
      case 0:
        textSym.setHorizontalAlignment(esriTextHorizontalAlignment.esriTHARight);
        textSym.setVerticalAlignment(esriTextVerticalAlignment.esriTVABottom);
        break;
      case 1:
        textSym.setHorizontalAlignment(esriTextHorizontalAlignment.esriTHALeft);
        textSym.setVerticalAlignment(esriTextVerticalAlignment.esriTVABottom);
        break;
      case 2:
        textSym.setHorizontalAlignment(esriTextHorizontalAlignment.esriTHARight);
        textSym.setVerticalAlignment(esriTextVerticalAlignment.esriTVATop);
        break;
      default:
        textSym.setHorizontalAlignment(esriTextHorizontalAlignment.esriTHALeft);
        textSym.setVerticalAlignment(esriTextVerticalAlignment.esriTVATop);
        break;
      }
      display.setSymbol((ISymbol) textSym);
      display.drawText(feat.getShape(), labels[0]);
      feat = featureCursor.nextFeature();
      if (trackCancel == null)
        bContinue = trackCancel.esri_continue();
    }
  }

  public ISymbol getSymbolByFeature(IFeature arg0) throws IOException,AutomationException {
    return null;
  }

  public boolean isRenderPhase(int drawPhase) throws IOException,AutomationException {
    if (drawPhase == esriDrawPhase.esriDPGeography)
      return true;
    else
      return false;
  }

  public void prepareFilter(IFeatureClass fc, IQueryFilter qFilter)throws IOException, AutomationException {
    qFilter.addField(fc.getOIDFieldName());
  }

  public void setExclusionSetByRef(IFeatureIDSet featureIDSet)throws IOException, AutomationException {
  }

  public ILegendGroup getLegendGroup(int arg0) throws IOException,AutomationException {
    return legendGroup;
  }

  public int getLegendGroupCount() throws IOException, AutomationException {
    if (legendGroup == null)
      return 0;
    else
      return 1;
  }

  public ILegendItem getLegendItem() throws IOException, AutomationException {
    return null;
  }

  public boolean isSymbolsAreGraduated() throws IOException,AutomationException {
    return false;
  }

  public void setSymbolsAreGraduated(boolean arg0) throws IOException,AutomationException {
  }

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

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

  public void setColor0(IRgbColor inColor) throws Exception {
    IMarkerSymbol pIMSym;
    pIMSym = (IMarkerSymbol) legendGroup.esri_getClass(0).getSymbol();
    pIMSym.setColor(inColor);
  }

  public void setLabel0(String label0) throws Exception {
    legendGroup.esri_getClass(0).setLabel(label0);
  }

  public void setColor1(IRgbColor inColor) throws Exception {
    IMarkerSymbol pIMSym;
    pIMSym = (IMarkerSymbol) legendGroup.esri_getClass(1).getSymbol();
    pIMSym.setColor(inColor);
  }

  public void setLabel1(String label1) throws Exception {
    legendGroup.esri_getClass(1).setLabel(label1);
  }

  public void setColor2(IRgbColor inColor) throws Exception {
    IMarkerSymbol pIMSym;
    pIMSym = (IMarkerSymbol) legendGroup.esri_getClass(2).getSymbol();
    pIMSym.setColor(inColor);
  }

  public void setLabel2(String label2) throws Exception {
    legendGroup.esri_getClass(2).setLabel(label2);
  }

  public void setColor3(IRgbColor inColor) throws Exception {
    IMarkerSymbol pIMSym;
    pIMSym = (IMarkerSymbol) legendGroup.esri_getClass(3).getSymbol();
    pIMSym.setColor(inColor);
  }

  public void setLabel3(String label3) throws Exception {
    legendGroup.esri_getClass(3).setLabel(label3);
  }
  
  /**
   * convertToSupportedObject. For every esriArcGISVersion for which you return false from 
   * isSupportedAtVersion, you should implement a suitable alternative in 
   * convertToSupportedObject. 
   * If you do not do this, you prevent users from saving a copy of a document to that version,
   * and the user may not receive any information about why their attempt to save 
   * a copy failed. Do not return a null reference either, as ArcGIS may attempt to 
   * apply this null reference to a property, which may cause the saved document to 
   * become corrupted.

   * @param     docVersion An reference to a com.esri.arcgis.system.esriArcGISVersion(in)
   * @return    An Object which is compatable to the version specified
   * @exception java.io.IOException If there are interop problems.
   * @exception com.esri.arcgis.interop.AutomationException If the ArcObject component throws an exception.
   */
  
  public Object convertToSupportedObject(int docVersion) throws IOException,
      AutomationException {
    SimpleRenderer simpleRend = new SimpleRenderer();
    SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol();
    simpleMarkerSymbol.setSize(8);
    simpleMarkerSymbol.setStyle(esriSimpleMarkerStyle.esriSMSDiamond);
    simpleRend.setSymbolByRef(simpleMarkerSymbol);
    return simpleRend;
  }
  
  /**
   * isSupportedAtVersion. This method is where you determine to which ArcGIS document versions 
   * your component can be persisted. Return true or false from this method depending on the 
   * document version indicated by the parameter passed in to this method. The parameter is an
   * esriArcGISVersion enumeration value.

   * @param     docVersion An reference to a com.esri.arcgis.system.esriArcGISVersion(in)
   * @return    The result
   * @exception java.io.IOException If there are interop problems.
   * @exception com.esri.arcgis.interop.AutomationException If the ArcObject component throws an exception.
   */
  public boolean isSupportedAtVersion(int docVersion) throws IOException,
      AutomationException {
    // Support all versions above or equal 9.3.1.
    if (docVersion >= esriArcGISVersion.esriArcGISVersion93)
      return true;
    else
      return false;
  }
}