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

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports ESRI.ArcGIS.ADF.ArcGISServer.Editor
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections.Specialized
Imports ESRI.ArcGIS.ADF.ArcGISServer
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls

Namespace CustomEditorTask_VBNet
  Friend Class PolygonToLineLayerEditorPanel
    Inherits EditorPanel
    ' Reference to the drop down list in the custom panel
    Private dropDownListLayers As DropDownList

    Public Sub New(ByVal title As String, ByVal task As EditorTask, ByVal ID As String)
      MyBase.New(title, task, ID)
    End Sub

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

    Protected Overrides Sub CreateChildControls()
      MyBase.CreateChildControls()

      Dim label As Label = New Label()
      label.Text = "Target layer:  "
      Controls.Add(label)

      dropDownListLayers = New DropDownList()
      dropDownListLayers.ID = "targetLayers"
      Controls.Add(dropDownListLayers)
      ' Add editable polyline layers to the drop down list
      AddPolylineLayers()
    End Sub

    Protected Overrides Sub Render(ByVal writer As 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}"");", dropDownListLayers.ClientID, CallbackFunctionString)
      dropDownListLayers.Attributes.Add("onchange", jsOnChange)

      MyBase.Render(writer)
    End Sub


    Public Overrides Function GetCallbackResult() As String
      ' Parse callback arguments using the CallbackUtility included with Web ADF.
      ' The _callbackArg is a protected 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 NameValueCollection = CallbackUtility.ParseStringIntoNameValueCollection(_callbackArg)
      Dim polylineLayerID As Integer
      If Integer.TryParse(nameValueCollection("layer"), polylineLayerID) Then
        ' Set property on custom Editor
        Dim editor As CustomEditor = CType(ParentEditor, CustomEditor)
        editor.PolylineLayerID = polylineLayerID
      End If
      Return Nothing
    End Function

    ' Add editable polyline layers to drop down list
    Private Sub AddPolylineLayers()
      ' Retrieve polyline layer names and ids from session
      Dim nameValueCollection As NameValueCollection = TryCast(Page.Session("polygonLayers"), NameValueCollection)
      If nameValueCollection Is Nothing Then
        nameValueCollection = New NameValueCollection()
        ' EditorTask maintains a string list of editable layer ids
        For Each stringLayerID As String In Task.EditableLayers
          Dim layerID As Integer = Integer.Parse(stringLayerID)
          For Each layerInfo As MapLayerInfo In ParentEditor.MapFunctionality.MapResource.MapServerInfo.MapLayerInfos
            If layerInfo.LayerID = layerID Then
              For Each field As Field In layerInfo.Fields.FieldArray
                If Not field.GeometryDef Is Nothing AndAlso field.GeometryDef.GeometryType = esriGeometryType.esriGeometryPolyline Then
                  ' If editable layer is of type polyline, add to the collection
                  nameValueCollection.Add(stringLayerID, layerInfo.Name)
                  Exit For
                End If
              Next field
              Exit For
            End If
          Next layerInfo
        Next stringLayerID
        Page.Session("polygonLayers") = nameValueCollection
      End If

      ' Add list items to drop down list
      For Each layerID As String In nameValueCollection.Keys
        dropDownListLayers.Items.Add(New ListItem(nameValueCollection(layerID), layerID))
      Next layerID

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