How to create an edit session


Summary
This topic discusses recommended approaches for creating edit sessions, including the use of edit operations in an ArcGIS Engine application, with emphasis on the use of the EngineEditor singleton object.

In this topic


Start edit session

An edit session is started by calling the IEngineEditor.StartEditing method with the following arguments:
Every editable layer from the same workspace that is in the map can be edited in the edit session. The IEngineEditLayers.IsEditable property indicates whether a particular layer is editable. When working with SDE workspaces, set the IEngineEditor.EditSessionMode to specify whether versioned or non-versioned layers will be editable, as it is not possible to edit both types in a single edit session.
The following code example starts an edit session on the workspace of the first feature layer in the map and sets this to be the layer being edited:
[Java]
// Declare the engine editor at the class level.
private IEngineEditor editor;
private void startEditing(IMapControl2 mapControl)throws Exception{
    if (editor == null)
        editor = new EngineEditor();
    IMap map = mapControl.getMap();
    // If an edit session has already been started, exit.
    if (editor.getEditState() != esriEngineEditState.esriEngineStateNotEditing)
        return ;
    // Start editing the workspace of the first feature layer found.
    for (int layerCounter = 0; layerCounter <= map.getLayerCount() - 1; layerCounter
        ++){
        ILayer currentLayer = map.getLayer(layerCounter);
        if (currentLayer instanceof IFeatureLayer){
            IFeatureLayer featureLayer = (IFeatureLayer)currentLayer;
            IDataset dataset = new IDatasetProxy(featureLayer.getFeatureClass());
            IWorkspace workspace = dataset.getWorkspace();
            editor.startEditing(workspace, map);
            ((IEngineEditLayers)editor).setTargetLayer(featureLayer, 0);
            break;
        }
    }
}
The Start Editing command (ControlsEditingStartCommand) works in a similar manner to this.
Attempting to start an edit session on a workspace already being edited by the same application results in an error. Use the IEngineEditor.EditState property to check this.
Once editing, the workspace and map being edited can be obtained using IEngineEditor.EditWorkspace and IEngineEditor.Map properties respectively.

Edit operations

All edits made within an edit session that create, modify, or delete features should be performed within an edit operation. They provide the capability to undo or redo edits by adding each edit operation onto the operation stack.
Use the IEngineEditor.StartOperation method to create an edit operation. Error handling and program logic should be used to ensure that any edits made within the operation are valid. If required, IEngineEditor.AbortOperation can be used to cancel the operation. To finish the edit operation and add it to the operation stack, call IEngineEditor.StopOperation. The string argument allows the operation to be identified on the operation stack and is used as the ToolTip for the Undo and Redo commands.
The following code example illustrates creating an edit operation:
[Java]
private void cmdEditOperation()throws Exception{
    editor.startOperation();

    try{
        //Perform feature edits here.
        //.....   
        if (someEditValidationChecksMethod() == true){
            editor.stopOperation("Test edit operation");
        }
        else{
            m_engineEditor.abortOperation();
        }
    }
    catch (Exception ex){
        editor.abortOperation();
        //Add code to handle exception.
    }
}
Since ArcGIS 9.2, edit operations cannot be nested. Calling IEngineEditor.StartOperation within another edit operation raises an error.
The IEngineEditEvents.OnBeforeStopOperation event provides the opportunity to analyze any edits made before the edit operation is committed to the geodatabase. The operation will already be committed when the IEngineEditEvents.OnStopOperation event fires.

Save edit session

Edits made during the edit session can be saved by executing ControlsEditingSaveCommand. Alternatively, call IEngineEditor.StopEditing with the saveChanges argument set to true followed by IEngineEditor.StartEditing to continue with the edit session.

Stop edit session

Edits made within the edit session are held in memory. Stopping the edit session provides the option to save any changes to disk or to rollback the entire transaction to the state before editing began. In both cases, the operation stack will be cleared of all edit and sketch operations. The following code example stops an edit session and prompts the user to save edits if any changes have been made:
[Java]
private void stopEditing()throws Exception{
    if (editor.hasEdits() == false)
        editor.stopEditing(false);
    else{
        if (JOptionPane.showConfirmDialog(null, "Do you want to save your edits?", 
            "Save Edits?", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION)
            editor.stopEditing(true);
        else
            editor.stopEditing(false);
    }
}


See Also:

How to create a sketch operation
How to work with the operation stack
How to listen to an edit event
Sample: EditingCustomApplication
Sample: Reshape PolyLine Task




Development licensingDeployment licensing
Engine Developer KitEngine
ArcGIS for Desktop Basic
ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced