Common Custom data source
Common_CustomDataSource_VBNet\REXMLDataSource_VBNet\MapTocFunctionality.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
Namespace REXMLDataSource_VBNet
    ' Along with IGISDataSource and IGISResource, IGISFunctionality is one of the three required
    ' interfaces for any Web ADF Data Source implementation.  This interface is responsible for
    ' providing members to functionally interact with the underlying data.  Essentially, an 
    ' IGISFunctionality implementation can be thought of as describing what can be done with the 
    ' data.
    '
    ' This particular implementation inherits from IMapTocFunctionality, which implements 
    ' IGISFunctionality.  IMapTocFunctionality provides methods and properties that allow
    ' interaction with the data source via a Web ADF Toc Control .
    Public Class MapTocFunctionality
        Implements ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality
#Region "Instance Variable Declarations"

        Private m_name As String = String.Empty
        Private m_gisResource As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource = Nothing
        Private m_initialized As Boolean = False
        Private m_mapResource As REXMLDataSource_VBNet.MapResource
        Private m_webControl As System.Web.UI.WebControls.WebControl

#End Region

#Region "Constructor"

        ' Constructor requiring specification of the functionality name and the MapResource with
        ' which to associate the functionality
        Public Sub New(ByVal name As String, ByVal resource As REXMLDataSource_VBNet.MapResource)
            ' Initialize instance variables with the passed-in parameters
            m_name = name
            m_gisResource = resource
            m_mapResource = resource
        End Sub

#End Region

#Region "REXML MapTocFunctionality Members"

        ' Allows for convenient retrieval of a REXML MapFunctionality object by name
        Private Function GetMapFunctionality(ByVal name As String) As REXMLDataSource_VBNet.MapFunctionality
            Dim mapFunctionality As REXMLDataSource_VBNet.MapFunctionality = TryCast(m_gisResource.Functionalities.Find(name), REXMLDataSource_VBNet.MapFunctionality)
            Return mapFunctionality
        End Function

        ' Sets visibility of the layer with the passed-in ID contained in the map functionality with
        ' the passed-in name
        Public Sub SetLayerVisibility(ByVal mapFunctionalityName As String, ByVal layerID As Object, ByVal visible As Boolean)
            ' Get a reference to the REXML map functionality with the passed-in functionality name
            Dim rexmlMapFunctionality As REXMLDataSource_VBNet.MapFunctionality = GetMapFunctionality(mapFunctionalityName)

            ' Make sure the functionality was found
            If rexmlMapFunctionality Is Nothing Then
                Throw New System.ArgumentException("A map functionality with the specified name was not found.")
            End If

            ' Set the visibility of the layer with the passed-in ID according to the passed in boolean
            rexmlMapFunctionality.SetLayerVisibility(layerID.ToString(), visible)
        End Sub

#End Region

#Region "IMapTocFunctionality Members"

        Public Function GetMapContents(ByVal mapFunctionalityName As String, ByVal format As ESRI.ArcGIS.ADF.Web.WebImageFormat, ByVal useMimeData As Boolean, ByVal showAllDataFrames As Boolean) As ESRI.ArcGIS.ADF.Web.TocDataFrame() Implements ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality.GetMapContents
            Dim tocDataFrames As ESRI.ArcGIS.ADF.Web.TocDataFrame() = New ESRI.ArcGIS.ADF.Web.TocDataFrame(0) {}
            tocDataFrames(0) = New ESRI.ArcGIS.ADF.Web.TocDataFrame(m_gisResource.Name)
            Dim session As System.Web.SessionState.HttpSessionState = Nothing
            Try
                Dim httpContext As System.Web.HttpContext = System.Web.HttpContext.Current
                If Not httpContext Is Nothing Then
                    session = httpContext.Session
                End If
            Catch
            End Try
            Dim swatchInfo As ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchInfo = New ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchInfo(10, 10, session)
            For Each table As System.Data.DataTable In m_mapResource.Graphics.Tables
                tocDataFrames(0).Add(ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer.GetTocLayer(TryCast(table, ESRI.ArcGIS.ADF.Web.Display.Graphics.GraphicsLayer), swatchInfo))
            Next table
            Return tocDataFrames
        End Function

#End Region

#Region "IGISFunctionality Members"

        Public Property WebControl() As System.Web.UI.WebControls.WebControl Implements ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality.WebControl
            Get
                Return m_webControl
            End Get
            Set(ByVal value As System.Web.UI.WebControls.WebControl)
                m_webControl = Value
            End Set
        End Property

        Public Property Name() As String Implements ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality.Name
            Get
                Return m_name
            End Get
            Set(ByVal value As String)
                m_name = Value
            End Set
        End Property

        Public Property Resource() As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource Implements ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality.Resource
            Get
                Return m_gisResource
            End Get
            Set(ByVal value As ESRI.ArcGIS.ADF.Web.DataSources.IGISResource)
                m_gisResource = Value
            End Set
        End Property

        Public ReadOnly Property Initialized() As Boolean Implements ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality.Initialized
            Get
                Return m_initialized
            End Get
        End Property

        Public Sub LoadState() Implements ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality.LoadState
        End Sub

        Public Sub Initialize() Implements ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality.Initialize
            m_initialized = True
        End Sub

        Public Sub SaveState() Implements ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality.SaveState
        End Sub

        ' Set the flag indicating whether the functionality is intitialized to false.  Any necessary
        ' disposal logic (e.g. releasing object references) should go here.  Note that, if there is
        ' additional logic here, users of this class will have to EXPLCITLY call dispose.  It is not
        ' invoked by other Web ADF components or the Page life-cycle.        
        Public Sub Dispose() Implements ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality.Dispose
            m_initialized = False
        End Sub

        Public Function Supports(ByVal operation As String) As Boolean Implements ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality.Supports
            Return True
        End Function
#End Region
    End Class
End Namespace