Common Custom EditorTask
Common_CustomEditorTask_VBNet\CustomEditorTask_VBNet\PolygonToLineLayerEditorPanel.vb
' Copyright 2011 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.
' 

Imports Microsoft.VisualBasic
Imports System
Namespace CustomEditorTask_VBNet
  Friend Class PolygonToLineLayerEditorPanel
    Inherits ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditorPanel
    #Region "Instance Variable Declarations"

    ' Reference to the drop down list in the custom panel
    Private m_layersDropDownList As System.Web.UI.WebControls.DropDownList

    #End Region

    #Region "Constructor"

    Public Sub New(ByVal title As String, ByVal editorTask As ESRI.ArcGIS.ADF.ArcGISServer.Editor.EditorTask, ByVal ID As String)
      MyBase.New(title, editorTask, ID)
    End Sub

    #End Region

    #Region "WebControl Life Cycle Event Overrides"

    ' Instantiates the custom panel's child controls
    Protected Overrides Sub CreateChildControls()
      MyBase.CreateChildControls()

      ' Label for the target layer drop-down list
      Dim targetLayerLabel As System.Web.UI.WebControls.Label = New System.Web.UI.WebControls.Label()
      targetLayerLabel.Text = "Target layer:  "
      Controls.Add(targetLayerLabel)

      ' Target layer drop-down list
      m_layersDropDownList = New System.Web.UI.WebControls.DropDownList()
      m_layersDropDownList.ID = "targetLayers"
      Controls.Add(m_layersDropDownList)

      ' Call to add editable polyline layers to the drop down list
      AddPolylineLayers()
    End Sub

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
      ' When item selected (changed) in the drop down list, trigger a callback to this control.  
      ' Process in GetCallbackResult()
      Dim jsOnChange As String = String.Format("javascript:var context;var argument='EventArg=targetChanged&layer=' " & "+ document.getElementById('{0}').value; eval(""{1}"");", m_layersDropDownList.ClientID, CallbackFunctionString)
      m_layersDropDownList.Attributes.Add("onchange", jsOnChange)

      MyBase.Render(writer)
    End Sub

    #End Region

    #Region "Callback Handler"

    Public Overrides Function GetCallbackResult() As String
      ' Parse callback arguments using the CallbackUtility included with Web ADF.
      ' CallbackEventArgument is a string member variable inherited from 
      ' ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl.  Upon a callback, it 
      ' contains the string argument passed to RaiseCallbackEvent().
      Dim nameValueCollection As System.Collections.Specialized.NameValueCollection = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(Me.CallbackEventArgument)

      Dim polylineLayerID As Integer
      If Integer.TryParse(nameValueCollection("layer"), polylineLayerID) Then
        ' Set the property on the custom Editor that indicates the target layer for Polygon to Polyline
        Dim customEditor As CustomEditor = CType(ParentEditor, CustomEditor)
        customEditor.PolylineLayerID = polylineLayerID
      End If
      Return Nothing
    End Function

    #End Region

    #Region "Instance Properties"

    ' The layer ID for the selected layer in the drop down list
    Public ReadOnly Property TargetLayerID() As Integer
      Get
        Return Integer.Parse(m_layersDropDownList.SelectedValue)
      End Get
    End Property

    #End Region

    #Region "Instance Methods"

    ' Add editable polyline layers to drop down list
    Private Sub AddPolylineLayers()
      If Page.Application Is Nothing Then
        Return
      End If

      ' Retrieve polyline layer names and ids from session
      Dim polygonLayersNameValueCollection As System.Collections.Specialized.NameValueCollection = TryCast(Page.Session("polygonLayers"), System.Collections.Specialized.NameValueCollection)
      If polygonLayersNameValueCollection Is Nothing Then
        polygonLayersNameValueCollection = New System.Collections.Specialized.NameValueCollection()
        ' Iterate through the EditorTask's string list of editable layer IDs
        For Each stringLayerID As String In Task.EditableLayers
          Dim layerID As Integer = Integer.Parse(stringLayerID)

          ' Iterate through the MapLayerInfos in the editor's underlying map resource
          For Each mapLayerInfo As ESRI.ArcGIS.ADF.ArcGISServer.MapLayerInfo In ParentEditor.MapFunctionality.MapResource.MapServerInfo.MapLayerInfos
            ' Check whether the current MapLayerInfo corresponds to that referred by the current layer ID
            If (mapLayerInfo.IsFeatureLayer) AndAlso (mapLayerInfo.LayerID = layerID) Then
              ' If the MapLayerInfo contains a polyline geometry definition add it to the collection of
              ' polyline layers
              For Each agsField As ESRI.ArcGIS.ADF.ArcGISServer.Field In mapLayerInfo.Fields.FieldArray
                If Not agsField.GeometryDef Is Nothing AndAlso agsField.GeometryDef.GeometryType = ESRI.ArcGIS.ADF.ArcGISServer.esriGeometryType.esriGeometryPolyline Then
                  ' If editable layer is of type polyline, add to the collection
                  polygonLayersNameValueCollection.Add(stringLayerID, mapLayerInfo.Name)
                  Exit For
                End If
              Next agsField
              Exit For
            End If
          Next mapLayerInfo
        Next stringLayerID
        Page.Session("polygonLayers") = polygonLayersNameValueCollection
      End If

      ' Add list items to drop down list
      For Each layerID As String In polygonLayersNameValueCollection.Keys
        m_layersDropDownList.Items.Add(New System.Web.UI.WebControls.ListItem(polygonLayersNameValueCollection(layerID), layerID))
      Next layerID

      ' Set custom Editor PolylineLayerID property
      Dim customEditor As CustomEditor = CType(ParentEditor, CustomEditor)
      If customEditor.PolylineLayerID = -1 AndAlso polygonLayersNameValueCollection.Count > 0 Then
        customEditor.PolylineLayerID = Integer.Parse(polygonLayersNameValueCollection.Keys(0))
      End If
    End Sub

    #End Region
  End Class
End Namespace