arcgissamples\editing\SnappingTableModel.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Manage snap agents
arcgissamples\editing\SnappingTableModel.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.editing;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import com.esri.arcgis.carto.IFeatureLayer;
import com.esri.arcgis.controls.EngineEditor;
import com.esri.arcgis.controls.EngineFeatureSnap;
import com.esri.arcgis.controls.IEngineFeatureSnapAgent;
import com.esri.arcgis.controls.IEngineSnapAgent;
import com.esri.arcgis.geometry.esriGeometryHitPartType;

public class SnappingTableModel extends AbstractTableModel {
  private static final long serialVersionUID = 1L;

  private EngineEditor editor;

  String[] columns = { "Snap Agent", "Feature Class", "Hit Type" };

  ArrayList<IEngineSnapAgent> agents = new ArrayList<IEngineSnapAgent>();

  public SnappingTableModel() {
    try {
      // EngineEditor is a singleton object
      this.editor = new EngineEditor();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public int getColumnCount() {
    return columns.length;
  }

  public String getColumnName(int column) {
    return columns[column];
  }

  public int getRowCount() {
    try {
      //The number of snap agents in the snapping environment
      return editor.getSnapAgentCount();
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }

  public Object getValueAt(int rowIndex, int columnIndex) {
    try {
      IEngineSnapAgent agent = editor.getSnapAgent(rowIndex);
      switch (columnIndex) {
      case 0:
        //Snap agent name
        return agent.getName();
      case 1:
        //Snap agent Feature Class (only for Feature Snap agents)
        if (agent instanceof EngineFeatureSnap)
          return ((EngineFeatureSnap) agent).getFeatureClass()
              .getAliasName();
        else
          return null;
      case 2:
        //Snap agent HitType (only for Feature Snap agents)
        if (agent instanceof EngineFeatureSnap) {
          switch (((EngineFeatureSnap) agent).getHitType()) {
          case esriGeometryHitPartType.esriGeometryPartNone:
            return "None";
          case esriGeometryHitPartType.esriGeometryPartVertex:
            return "Vertex";
          case esriGeometryHitPartType.esriGeometryPartBoundary:
            return "Edge";
          case esriGeometryHitPartType.esriGeometryPartEndpoint:
            return "End";
          case esriGeometryHitPartType.esriGeometryPartVertex
              + esriGeometryHitPartType.esriGeometryPartBoundary:
            return "Vertex, Edge";
          case esriGeometryHitPartType.esriGeometryPartVertex
              + esriGeometryHitPartType.esriGeometryPartEndpoint:
            return "Vertex, End";
          case esriGeometryHitPartType.esriGeometryPartEndpoint
              + esriGeometryHitPartType.esriGeometryPartBoundary:
            return "Edge, End";
          case esriGeometryHitPartType.esriGeometryPartEndpoint
              + esriGeometryHitPartType.esriGeometryPartBoundary
              + esriGeometryHitPartType.esriGeometryPartVertex:
            return "Vertex, Edge, End";
          }
        } else
          return null;
      default:
        return null;
      }
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  public boolean isCellEditable(int rowIndex, int columnIndex) {
    return false;
  }

  public void removeAll() {
    try {
      //remove all snap agents
      editor.clearSnapAgents();
      //udpate the table
      this.fireTableDataChanged();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void addFeatureSnapAgents() {
    try {
      //Add a Feature Snap agent for the target layer
      EngineFeatureSnap featureSnapAgent = new EngineFeatureSnap();
      featureSnapAgent.setFeatureClassByRef(((IFeatureLayer) editor
          .getTargetLayer()).getFeatureClass());
      //Initialize it to snap to vertex, edge, and end points.
      featureSnapAgent
          .setHitType(esriGeometryHitPartType.esriGeometryPartVertex
              | esriGeometryHitPartType.esriGeometryPartBoundary
              | esriGeometryHitPartType.esriGeometryPartEndpoint);
      editor.addSnapAgent(featureSnapAgent);
      //Update the table
      this.fireTableDataChanged();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void addSketchSnapAgents() {
      //There is no way currently in the Java API to add sketch snap agents.
  }

  public void reverseSnapAgens() {
    try {
      //Get all the snap agents in reverse order and then deactivate them
      ArrayList<IEngineSnapAgent> snapAgentList = new ArrayList<IEngineSnapAgent>();
      for (int i = editor.getSnapAgentCount() - 1; i >= 0; i--) {
        IEngineSnapAgent tmpAgent = editor.getSnapAgent(i);
        snapAgentList.add(tmpAgent);
        editor.removeSnapAgent(i);
      }
      //Add the agents back to the environment
      for (IEngineSnapAgent agent : snapAgentList) {
        editor.addSnapAgent(agent);
      }
      //Update the table
      this.fireTableDataChanged();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void turnOffAgents() {
    try {
      for (int i = editor.getSnapAgentCount() - 1; i >= 0; i--) {
        IEngineSnapAgent snapAgent = editor.getSnapAgent(i);
        if (snapAgent instanceof IEngineFeatureSnapAgent) {
          //Turn off the feature snap agent
          IEngineFeatureSnapAgent ftrSnapAgent = (IEngineFeatureSnapAgent) snapAgent;
          ftrSnapAgent
              .setHitType(esriGeometryHitPartType.esriGeometryPartNone);
        } else {
          //There is no way to turn snap agents off, they must be
          //removed
          editor.removeSnapAgent(i);
        }
      }
      //Update the table
      this.fireTableDataChanged();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}