Common Custom EditorTask
Common_CustomEditorTask_CSharp\CustomEditorTask_CSharp\CustomEditorTask.cs
// Copyright 2007 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.
// 

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.ADF.ArcGISServer.Editor.Tools;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.ADF.ArcGISServer.Editor;
using System.Web.UI;
using System.ComponentModel;
using ESRI.ArcGIS.ADF.ArcGISServer;

namespace CustomEditorTask_CSharp
{
    public class CustomEditorTask : ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditorTask
    {
        private CustomEditorSettingsPanel m_customEditorSettingsPanel;

        // Override to create and return a custom Editor
        protected override ESRI.ArcGIS.ADF.ArcGISServer.Editor.Editor CreateEditor()
        {
            return new CustomEditor(this);
        }

        // Custom property to define snapping in map units
        [
            Browsable(true),
            Category("Snapping"),
            PersistenceMode(PersistenceMode.Attribute),
            DefaultValue(CustomSnappingPanel.DEFAULT_SNAP_TOL_MAPUNITS),
            Description("The maximum distance in map units for which snapping rules will be effective.")
        ]
        public double SnapToleranceMapUnits
        {
            get
            {
                object o = StateManager.GetProperty("snapTolMapUnits");
                if (o == null)
                {
                    return CustomSnappingPanel.DEFAULT_SNAP_TOL_MAPUNITS;
                }

                return (double)o;
            }

            set
            {
                StateManager.SetProperty("snapTolMapUnits", value);
            }
        }

        // Custom property to define if snapping should use pixels (default) or map units.
        [
            Browsable(true),
            Category("Snapping"),
            PersistenceMode(PersistenceMode.Attribute),
            DefaultValue(true),
            Description("Indicates if snapping use map units or pixels.")
        ]
        public bool UseMapUnitsForSnapping
        {
            get
            {
                object obj = StateManager.GetProperty("useSnappingMapUnits");
                if (obj != null) return (bool)obj;
                return true;
            }
            set
            {
                StateManager.SetProperty("useSnappingMapUnits", value);
            }
        }

        // Override to create and return a custom settings panel
        protected override ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditorSettingsPanel CreateSettingsPanel()
        {
            m_customEditorSettingsPanel = new CustomEditorSettingsPanel(this);
            return m_customEditorSettingsPanel;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // Set attribute filtering parameters for the EditorTask during the 
            // Page Load event of the initial request.
            if (!this.Page.IsPostBack)
            {
                FilterAttributes();
            }
        }

        // Override OnPostToolExecute event
        protected override void OnPostToolExecute(EditorToolEventArgs e, StringList returnMessages)
        {
            base.OnPostToolExecute(e, returnMessages);            
            CustomEditorTask_PostToolExecute(this, e);
        }

        // After a tool that modifies features has executed, add creation and modified time to edited feature.
        void CustomEditorTask_PostToolExecute(object sender, ESRI.ArcGIS.ADF.ArcGISServer.Editor.Tools.EditorToolEventArgs e)
        {
            // The Tentative Assessed Parcel" layer contains two Date fields: CreationTime and LastModified.
            // If a new feature was created, update the CreationTime field with the current Date.  
            // If an existing feature was modified (via the tools listed), update the LastModified field with the current Date.
            if (e.ServerAction.Editor.SelectedLayerName == "Tentative Assessed Parcels")
            {
                IFeatureClass featureClass = e.ServerAction.Editor.FeatureLayer.FeatureClass;

                if (e.ServerAction is CreateFeature)
                {
                    CreateFeature createFeature = (CreateFeature)e.ServerAction;
                    IFeature feature = featureClass.GetFeature(createFeature.Feature);
                    int index = feature.Fields.FindField("CreationTime");
                    if (index > -1)
                    {
                        feature.set_Value(index, DateTime.Now);
                        feature.Store();
                    }
                }
                // For simplicity only a few tools that modify features have been included
                else if (e.ServerAction is AddVertex)
                {
                    AddVertex addVertex = (AddVertex)e.ServerAction;
                    UpdateLastModifiedTime(addVertex.Features, featureClass);
                }
                else if (e.ServerAction is MoveVertex)
                {
                    MoveVertex moveVertex = (MoveVertex)e.ServerAction;
                    UpdateLastModifiedTime(moveVertex.Features, featureClass);
                }
                else if (e.ServerAction is MoveFeature)
                {
                    MoveFeature moveFeature = (MoveFeature)e.ServerAction;
                    UpdateLastModifiedTime(moveFeature.Features, featureClass);
                }
                
                // Refreshes the EditAttributesPanel (returned from call to Editor.AttributesEditor property)
                // and adds callbacks to the Map control associated with the Editor.
                EditorUtilities.RefreshAttributes(e.ServerAction.Editor, null);
            }
        }

        // Used during post tool execute
        private void UpdateLastModifiedTime(List<int> fids, ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass)
        {
            int index = featureClass.Fields.FindField("LastModified");

            foreach (int fid in fids)
            {
                IFeature feature = featureClass.GetFeature(fid);
                feature.set_Value(index, DateTime.Now);
                feature.Store();
            }
        }
        
        private void FilterAttributes()
        {
            // Get the ArcGIS Server Local MapFunctionality associated with the EditorTask
            ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapFunctionality agsLocalMapFunctionality =
                this.Editor.MapFunctionality;

            string[] layerIDs = null;
            string[] layerNames = null;
            agsLocalMapFunctionality.GetLayers(out layerIDs, out layerNames);

            // Iterate through feature layers in the MapFunction (map resource) and set
            // attribute display parameters when editing attributes in the EditAttributesPanel.
            for (int i = 0; i < layerIDs.Length; i++)
            {
                string layerName = layerNames[i];
                int layerID = Int32.Parse(layerIDs[i]);
                if (layerName == "Tentative Assessed Parcels")
                {
                    AttributeDisplayInfo attributeDisplayInfo =
                        new AttributeDisplayInfo(layerID, AttributeDisplayMode.ReadOnly);
                    attributeDisplayInfo.Overrides.Add(
                        new AttributeDisplayOverride("APN", AttributeDisplayMode.Editable));
                    this.AttributeDisplay.AttributeDisplayInfos.Add(attributeDisplayInfo);
                }
                else if (layerName == "Address Points")
                {
                    AttributeDisplayInfo attributeDisplayInfo =
                        new AttributeDisplayInfo(layerID, AttributeDisplayMode.Editable);
                    attributeDisplayInfo.Overrides.Add(
                        new AttributeDisplayOverride("APN", AttributeDisplayMode.ReadOnly));
                    this.AttributeDisplay.AttributeDisplayInfos.Add(attributeDisplayInfo);
                }
                else if (layerName == "Water Bodies")
                {
                    AttributeDisplayInfo attributeDisplayInfo =
                        new AttributeDisplayInfo(layerID, AttributeDisplayMode.Hidden);
                    attributeDisplayInfo.Overrides.Add(
                        new AttributeDisplayOverride("NAME", AttributeDisplayMode.Editable));
                    this.AttributeDisplay.AttributeDisplayInfos.Add(attributeDisplayInfo);
                }
            }
        }
    }
}