arcgissamples\editing\ReshapePolylineEditTask.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Reshape Polyline edit task
arcgissamples\editing\ReshapePolylineEditTask.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.EngineEditor;
import com.esri.arcgis.controls.IEngineEditEvents;
import com.esri.arcgis.controls.IEngineEditEventsOnAbortEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnAfterDrawSketchEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnBeforeStopEditingEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnBeforeStopOperationEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnChangeFeatureEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnConflictsDetectedEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnCreateFeatureEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnCurrentTaskChangedEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnCurrentZChangedEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnDeleteFeatureEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnSaveEditsEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnSelectionChangedEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnSketchFinishedEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnSketchModifiedEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnStartEditingEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnStartOperationEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnStopEditingEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnStopOperationEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnTargetLayerChangedEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnVertexAddedEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnVertexDeletedEvent;
import com.esri.arcgis.controls.IEngineEditEventsOnVertexMovedEvent;
import com.esri.arcgis.controls.IEngineEditTask;
import com.esri.arcgis.controls.IEngineEditor;
import com.esri.arcgis.geodatabase.ICursor;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IFeatureCursorProxy;
import com.esri.arcgis.geodatabase.ISelectionSet;
import com.esri.arcgis.geometry.IGeometry;
import com.esri.arcgis.geometry.IPath;
import com.esri.arcgis.geometry.IPointCollection;
import com.esri.arcgis.geometry.IPolyline;
import com.esri.arcgis.geometry.Path;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.interop.AutomationException;

class ReshapePolylineEditTask implements IEngineEditTask, IEngineEditEvents {

  private static final long serialVersionUID = 1L;
  private EngineEditor editor;

  public String getGroupName() throws IOException, AutomationException {
    return "Modify Tasks";
  }

  public String getName() throws IOException, AutomationException {
    return "Reshape Polyline Java";
  }

  public String getUniqueName() throws IOException, AutomationException {
    return "Reshape_Polyline_Java";
  }

  public void activate(IEngineEditor editor, IEngineEditTask task)
  throws IOException, AutomationException {
    //This method gets called when the task is activated by the Editing framework
    if (editor == null)
      return;
    this.editor = (EngineEditor) editor;
    //Register this task as an Edit events listener
    this.editor.addIEngineEditEventsListener(this);
    //Set the geometry that this task will work with
    this.editor.setGeometryType(esriGeometryType.esriGeometryPolyline);
  }

  public void deactivate() throws IOException, AutomationException {
    this.editor.refreshSketch();
    //Remove this task from the edit events listeners
    this.editor.removeIEngineEditEventsListener(this);
  }



  public void onDeleteSketch() throws IOException, AutomationException {
    //Don't do anything
  }


  public void onFinishSketch() throws IOException, AutomationException {
    // get reference to featurelayer being edited
    IFeatureLayer featureLayer = (IFeatureLayer) this.editor
    .IEngineEditLayers_getTargetLayer();
    // get reference to the sketch geometry
    IGeometry reshapeGeom = this.editor.getGeometry();

    if (!reshapeGeom.isEmpty()) {
      // get the currently selected feature
      IFeatureSelection featureSelection = (IFeatureSelection) featureLayer;
      ISelectionSet selectionSet = featureSelection.getSelectionSet();
      ICursor[] cursor = new ICursor[1];
      selectionSet.search(null, true, cursor);
      IFeatureCursor featureCursor = new IFeatureCursorProxy(cursor[0]);
      // the PerformSketchToolEnabledChecks property has already checked
      // that only 1 feature is selected
      IFeature feature = featureCursor.nextFeature();

      // Take a copy of geometry for the selected feature
      IGeometry editShape = feature.getShapeCopy();

      // create a path from the editsketch geometry
      IPointCollection reshapePath = new Path();
      reshapePath.addPointCollection((IPointCollection) reshapeGeom);

      // reshape the selected feature
      IPolyline polyline = (IPolyline) editShape;
      polyline.reshape((IPath) reshapePath);

      // Perform an edit operation to store the new geometry
      // for the selected feature

      try {
        this.editor.startOperation();
        feature.setShapeByRef(editShape);
        feature.store();
        this.editor.stopOperation("Reshape Feature");
      } catch (Exception ex) {
        this.editor.abortOperation();
        ex.printStackTrace();
      }

    }

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

  }

  private void performChecks() {

    try {
      if (this.editor == null)
        return;

      // Only enable the sketch tool if there is a polyline target
      // layer.
      if (this.editor.IEngineEditLayers_getTargetLayer().getFeatureClass()
          .getShapeType() != esriGeometryType.esriGeometryPolyline) {
        //This will disable the sketch tool
        this.editor.setGeometryType(esriGeometryType.esriGeometryNull);
        return;
      }

      // check that only one feature in the target layer is currently
      // selected
      IFeatureSelection featureSelection = (IFeatureSelection) this.editor
      .IEngineEditLayers_getTargetLayer();
      ISelectionSet selectionSet = featureSelection.getSelectionSet();
      if (selectionSet.getCount() != 1) {
        //This will disable the sketch tool
        this.editor.setGeometryType(esriGeometryType.esriGeometryNull);
        return;
      }

      //this will enable the sketch tool
      this.editor.setGeometryType(esriGeometryType.esriGeometryPolyline);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void onCurrentTaskChanged(
      IEngineEditEventsOnCurrentTaskChangedEvent arg0) throws IOException,
      AutomationException {
    if (this.editor.getCurrentTask().getName().equals(this.getName())) {
      performChecks();
    }
  }

  public void onTargetLayerChanged(
      IEngineEditEventsOnTargetLayerChangedEvent arg0) throws IOException,
      AutomationException {
    performChecks();

  }

  public void onSelectionChanged(IEngineEditEventsOnSelectionChangedEvent arg0)
  throws IOException, AutomationException {
    performChecks();
  }

  // We dont care about the remaining events for this task.

  public void onAbort(IEngineEditEventsOnAbortEvent arg0) throws IOException,
  AutomationException {
  }

  public void onAfterDrawSketch(IEngineEditEventsOnAfterDrawSketchEvent arg0)
  throws IOException, AutomationException {
  }

  public void onBeforeStopEditing(IEngineEditEventsOnBeforeStopEditingEvent arg0)
  throws IOException, AutomationException {
  }

  public void onBeforeStopOperation(
      IEngineEditEventsOnBeforeStopOperationEvent arg0) throws IOException,
      AutomationException {
  }

  public void onChangeFeature(IEngineEditEventsOnChangeFeatureEvent arg0)
  throws IOException, AutomationException {
  }

  public void onConflictsDetected(IEngineEditEventsOnConflictsDetectedEvent arg0)
  throws IOException, AutomationException {
  }

  public void onCreateFeature(IEngineEditEventsOnCreateFeatureEvent arg0)
  throws IOException, AutomationException {
  }

  public void onCurrentZChanged(IEngineEditEventsOnCurrentZChangedEvent arg0)
  throws IOException, AutomationException {
  }

  public void onDeleteFeature(IEngineEditEventsOnDeleteFeatureEvent arg0)
  throws IOException, AutomationException {
  }

  public void onSaveEdits(IEngineEditEventsOnSaveEditsEvent arg0)
  throws IOException, AutomationException {
  }

  public void onSketchFinished(IEngineEditEventsOnSketchFinishedEvent arg0)
  throws IOException, AutomationException {
  }

  public void onSketchModified(IEngineEditEventsOnSketchModifiedEvent arg0)
  throws IOException, AutomationException {
  }

  public void onStartEditing(IEngineEditEventsOnStartEditingEvent arg0)
  throws IOException, AutomationException {
  }

  public void onStartOperation(IEngineEditEventsOnStartOperationEvent arg0)
  throws IOException, AutomationException {
  }

  public void onStopEditing(IEngineEditEventsOnStopEditingEvent arg0)
  throws IOException, AutomationException {
  }

  public void onStopOperation(IEngineEditEventsOnStopOperationEvent arg0)
  throws IOException, AutomationException {
  }

  public void onVertexAdded(IEngineEditEventsOnVertexAddedEvent arg0)
  throws IOException, AutomationException {
  }

  public void onVertexDeleted(IEngineEditEventsOnVertexDeletedEvent arg0)
  throws IOException, AutomationException {
  }

  public void onVertexMoved(IEngineEditEventsOnVertexMovedEvent arg0)
  throws IOException, AutomationException {
  }

}