arcgissamples\editing\EnableSnappingCommand.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Buffer snap agent
arcgissamples\editing\EnableSnappingCommand.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 com.esri.arcgis.controls.BaseCommand;
import com.esri.arcgis.controls.EngineEditor;
import com.esri.arcgis.controls.IEngineSnapAgent;
import com.esri.arcgis.controls.esriEngineEditState;

public class EnableSnappingCommand extends BaseCommand {

  private static final long serialVersionUID = 1L;

  private EngineEditor editor;

  private IEngineSnapAgent bufferSnapAgent;

  private boolean checked;

  @Override
  public void onClick() {
    try {
      if (!this.checked) {
        editor.addSnapAgent(bufferSnapAgent);
        this.caption = "Disable Buffer Snap Agent";
      } else {
        this.caption = "Enable Buffer Snap Agent";
        for (int i = 0; i < editor.getSnapAgentCount(); i++) {
          IEngineSnapAgent snapAgent = editor.getSnapAgent(i);
          if (snapAgent == bufferSnapAgent) {
            editor.removeSnapAgent(i);
            break;
          }
        }
      }
      this.checked = !this.checked;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void onCreate(Object arg0) {
    this.caption = "Enable Buffer Snap Agent";
    this.checked = false;
    try {
      editor = new EngineEditor();
      bufferSnapAgent = new BufferSnapAgent();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public boolean isChecked() {
    return this.checked;
  }

  public boolean isEnabled(){
    try{
    return editor.getEditState() == esriEngineEditState.esriEngineStateEditing;
    }catch(Exception e){
    e.printStackTrace();
    return false;
    }
  }
}