arcgissamples\editing\OutOfBoxVertexTools.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Custom vertex editing tools
arcgissamples\editing\OutOfBoxVertexTools.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.io.IOException;

import com.esri.arcgis.carto.IActiveView;
import com.esri.arcgis.carto.IFeatureLayer;
import com.esri.arcgis.carto.IFeatureSelection;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.controls.BaseTool;
import com.esri.arcgis.controls.ControlsEditingVertexDeleteCommand;
import com.esri.arcgis.controls.ControlsEditingVertexInsertCommand;
import com.esri.arcgis.controls.EngineEditor;
import com.esri.arcgis.controls.HookHelper;
import com.esri.arcgis.controls.IEngineEditTask;
import com.esri.arcgis.controls.esriEngineEditState;
import com.esri.arcgis.geodatabase.ISelectionSet;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.systemUI.ICommand;
import com.esri.arcgis.systemUI.ICommandSubType;

class OutOfBoxVertexTools extends BaseTool implements ICommandSubType {

  private static final long serialVersionUID = 1L;

  private EngineEditor editor;

  private HookHelper hookHelper;

  private IEngineEditTask currentEditTask;

  private int type;

  public void onCreate(Object hook) {
  super.onCreate(hook);
  try {

    this.hookHelper = new HookHelper();
    this.hookHelper.setHookByRef(hook);
    this.editor = new EngineEditor();
  } catch (Exception e) {
    e.printStackTrace();
  }
  }

  public void onClick() {
  try {

    // find the Modify Feature task and set it as the current task
    // so the vertices will be automatically highlighted when tool is
    // clicked
    for (int i = 0; i < editor.getTaskCount() ; i++) {
    IEngineEditTask editTask = editor.getTask(i);

    if (editTask.getName().equalsIgnoreCase("Modify Feature")) {
      this.currentEditTask = editTask;
      editor.setCurrentTaskByRef(editTask);
      break;
    }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  }


  public void onMouseUp(int button, int shift, int x, int y) {
  try {
    // get layer being edited
    IFeatureLayer featureLayer = (IFeatureLayer) editor.getTargetLayer();

    // set the x,y location which will be used by the out-of-the-box
    // commands
    editor.setEditLocation(x, y);
    switch (type) {

    case 1:
    // Insert Vertex using out-of-the-box command
    ICommand insertVertexCommand = new ControlsEditingVertexInsertCommand();
    insertVertexCommand.onCreate(this.hookHelper.getHook());
    insertVertexCommand.onClick();
    break;

    case 2:
    // Delete Vertex using out-of-the-box command
    ICommand deleteVertexCommand = new ControlsEditingVertexDeleteCommand();
    deleteVertexCommand.onCreate(this.hookHelper.getHook());
    deleteVertexCommand.onClick();
    break;
    }

    // Due to the behaviour of the Modify Task the following code is
    // required to
    // ensure that any changes are displayed immediately.
    if (currentEditTask != null) {
    currentEditTask.onFinishSketch();
    editor.setCurrentTaskByRef(currentEditTask);
    }

    // refresh the display
    IActiveView activeView = (IActiveView) editor.getMap();
    activeView.partialRefresh(esriViewDrawPhase.esriViewGeography,
      featureLayer, activeView.getExtent());

  } catch (Exception ex) {
    ex.printStackTrace();
  }

  }

  public boolean isEnabled() {
  try {
    if (editor.getEditState() == esriEngineEditState.esriEngineStateNotEditing) {
    return false;
    }

    // check for appropriate geometry types
    int geomType = ((IFeatureLayer) editor.getTargetLayer())
      .getFeatureClass().getShapeType();
    if ((geomType != esriGeometryType.esriGeometryPolygon)
      && (geomType != esriGeometryType.esriGeometryPolyline)) {
    return false;
    }

    // check that only one feature is currently selected
    IFeatureSelection featureSelection = (IFeatureSelection) editor
      .getTargetLayer();
    ISelectionSet selectionSet = featureSelection.getSelectionSet();
    if (selectionSet.getCount() != 1) {
    return false;
    }

    return true;
  } catch (Exception e) {
    e.printStackTrace();
    return false;
  }
  }

  public int getCount() throws IOException, AutomationException {
  // the number of subtypes in this tool
  return 2;
  }

  public void setSubType(int index) throws IOException, AutomationException {
  type = index;

  this.category = "Vertex Cmds (Java)";

  switch (type) {
  case 1: // Insert Vertex using the out-of-the-box
    // ControlsEditingSketchInsertPointCommand command

    this.caption = "Insert vertex (out-of-box)";
    this.message = "Insert vertex at clicked location using out-of-box command";
    this.toolTip = "Insert vertex at clicked location using out-of-box command";
    this.name = "VertexCommands_UsingOutOfBoxInsertVertex";
    this.cursorPath = this.getClass().getClassLoader().getResource(
      "InsertVertexCursor.cur").getPath();
    this.bitmapPath = this.getClass().getClassLoader().getResource(
      "OOBInsertVertex.bmp").getPath();
    if(System.getProperty("os.name").toLowerCase().indexOf("win")>-1){
    this.cursorPath =this.cursorPath.substring(1).replaceAll("%20"," ");
    this.bitmapPath =this.bitmapPath.substring(1).replaceAll("%20"," ");
    }
    break;

  case 2: // Delete vertex at clicked location using the out-of-the-box
    // ControlsEditingSketchDeletePointCommand

    this.caption = "Delete vertex (out-of-box)";
    this.message = "Delete vertex at clicked location using out-of-box command";
    this.toolTip = "Delete vertex at clicked location using out-of-box command";
    this.name = "VertexCommands_UsingOutOfBoxDeleteVertex";
    this.cursorPath = this.getClass().getClassLoader().getResource(
      "DeleteVertexCursor.cur").getPath();
    ;
    this.bitmapPath = this.getClass().getClassLoader().getResource(
      "OOBDeleteVertex.bmp").getPath();
    if(System.getProperty("os.name").toLowerCase().indexOf("win")>-1){
    this.cursorPath =this.cursorPath.substring(1).replaceAll("%20"," ");
    this.bitmapPath =this.bitmapPath.substring(1).replaceAll("%20"," ");
    }
    break;
  }

  }
}