Server spatial query server object extension
SpatialQuerySOE.ArcCatalog_VBNet\PropertyForm.vb
' Copyright 2010 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.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms

Namespace SpatialQuerySOE.ArcCatalog_VBNet
  ''' <summary>
  ''' Defines the UI for the Spatial Query SOE
  ''' </summary>
  Partial Public Class PropertyForm
    Inherits Form
#Region "Member Variables"

    ' The current SOE layer
    Private m_layer As String

    ' Current SOE field
    Private m_field As String

    ' Tracks whether the form is initializing
    Private m_init As Boolean = False

    Private m_fieldsDictionary As New System.Collections.Generic.Dictionary(Of String, String())()

#End Region

#Region "Constructor"

    Public Sub New()
      InitializeComponent()

      ' Get ArcCatalog's representation of the map service being modified
      Dim type As System.Type = System.Type.GetTypeFromCLSID(GetType(ESRI.ArcGIS.Framework.AppRefClass).GUID)
      Dim gxApp As ESRI.ArcGIS.CatalogUI.IGxApplication = TryCast(Activator.CreateInstance(type), ESRI.ArcGIS.CatalogUI.IGxApplication)
      Dim gxAgsObj As ESRI.ArcGIS.Catalog.IGxAGSObject = TryCast(gxApp.SelectedObject, ESRI.ArcGIS.Catalog.IGxAGSObject)

      ' If the service is not stopped, disable the layers and fields drop-down lists so that the SOE cannot be configured
      If gxAgsObj.Status <> "Stopped" Then
        ComboLayers.Enabled = False
        ComboFields.Enabled = False
      End If
    End Sub

#End Region

#Region "Internal Properties - getHWnd, Layer, Field, PageSite"

    ''' <summary>
    ''' The form's handle
    ''' </summary>
    Friend Function getHWnd() As Integer
      Return Me.Handle.ToInt32()
    End Function

    ''' <summary>
    ''' The currently selected layer
    ''' </summary>
    Friend Property Layer() As String
      Get
        Return m_layer
      End Get
      Set(ByVal value As String)
        m_layer = value
      End Set
    End Property

    ''' <summary>
    ''' The currently selected field
    ''' </summary>
    Friend Property Field() As String
      Get
        Return m_field
      End Get
      Set(ByVal value As String)
        m_field = value
      End Set
    End Property

    ''' <summary>
    ''' The PageSite of the IComPropertyPage object using the form.  Allows the form to report when its contents have changed.
    ''' </summary>
    Private privatePageSite As ESRI.ArcGIS.Framework.IComPropertyPageSite
    Friend Property PageSite() As ESRI.ArcGIS.Framework.IComPropertyPageSite
      Private Get
        Return privatePageSite
      End Get
      Set(ByVal value As ESRI.ArcGIS.Framework.IComPropertyPageSite)
        privatePageSite = value
      End Set
    End Property

#End Region

#Region "Internal Methods - SetMap"

    ''' <summary>
    ''' Sets the map used to populate the layers and fields drop-down lists
    ''' </summary>
    ''' <param name="filePath">the path to the map document</param>
    Friend Sub SetMap(ByVal filePath As String)
      ' Open the map document
      Dim mapDocument As ESRI.ArcGIS.Carto.IMapDocument = New ESRI.ArcGIS.Carto.MapDocumentClass()
      mapDocument.Open(filePath, Nothing)

      ' The call to get_Map() will create a lock (ldb) on a personal gdb.  Make sure ArcCatalog user and 
      ' ArcGIS Server container account has read\write access to data location.  If not,
      ' feature classes in the personal gdb may not be accessible (or visible in the map).
      Dim map As ESRI.ArcGIS.Carto.IMap = mapDocument.Map(0)

      ' Get IGeoFeatureLayers from the map
      Dim id As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass()
      id.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"
      Dim enumLayer As ESRI.ArcGIS.Carto.IEnumLayer = map.Layers(id, True)

      Dim selectedIndex As Integer = 0
      Dim fieldNames() As String = Nothing
      m_fieldsDictionary.Clear()
      ' Add the names of simple polygon feature layers to the layers drop-down and store the names
      ' of each layer's fields in a dictionary
      Dim featureLayer As ESRI.ArcGIS.Carto.IFeatureLayer = Nothing
      featureLayer = TryCast(enumLayer.Next(), ESRI.ArcGIS.Carto.IFeatureLayer)
      Do While featureLayer IsNot Nothing
        If featureLayer.FeatureClass.ShapeType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon AndAlso featureLayer.FeatureClass.FeatureType = ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple Then
          ComboLayers.Items.Add(featureLayer.Name)
        End If

        If featureLayer.Name = m_layer Then
          selectedIndex = ComboLayers.Items.Count - 1
        End If

        fieldNames = New String(featureLayer.FeatureClass.Fields.FieldCount - 1) {}
        For i As Integer = 0 To fieldNames.Length - 1
          fieldNames(i) = featureLayer.FeatureClass.Fields.Field(i).Name
        Next i

        m_fieldsDictionary.Add(featureLayer.Name, fieldNames)
        featureLayer = TryCast(enumLayer.Next(), ESRI.ArcGIS.Carto.IFeatureLayer)
      Loop

      mapDocument.Close()
      mapDocument = Nothing
      map = Nothing

      ' Toggle the init flag and initialize the layers drop-down to show the current SOE layer.  Init is used
      ' to prevent the drop-down's SelectedIndexChanged logic from setting m_field during initialization.
      m_init = True
      ComboLayers.SelectedIndex = selectedIndex
    End Sub

#End Region

#Region "Control Event Handlers - ComboLayers_SelectedIndexChanged, ComboFields_SelectedIndexChanged, PropertyForm_FormClosed"

    ' Fires when a new item is selected from the layers drop-down
    Private Sub ComboLayers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboLayers.SelectedIndexChanged
      m_layer = ComboLayers.Text

      ComboFields.Items.Clear()

      ' Add the fields for the currently selected layer to the fields drop-down
      Dim selectedIndex As Integer = 0
      Dim fieldNames() As String = m_fieldsDictionary(m_layer)
      For i As Integer = 0 To fieldNames.Length - 1
        ComboFields.Items.Add(fieldNames(i))

        ' Get the index of the current field if the method is executing during initialization and
        ' the current field match's the SOE field
        If m_init AndAlso fieldNames(i) = m_field Then
          selectedIndex = ComboFields.Items.Count - 1
        End If
      Next i

      ' Update the field and layer properties if executing during initialization.  Otherwise, toggle the
      ' init boolean to indicate initialization is complete.
      If m_init Then
        m_init = False
      End If

      ' Set the fields drop-down to show the current SOE field.
      ComboFields.SelectedIndex = selectedIndex
    End Sub

    ' Fires when a new item is selected from the fields drop-down
    Private Sub ComboFields_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboFields.SelectedIndexChanged
      ' Update the current SOE field
      m_field = ComboFields.Text

      ' Notify ArcCatalog that the properties have changed
      Me.PageSite.PageChanged()
    End Sub

#End Region
  End Class
End Namespace