Common Custom data source
Common_CustomDataSource_VBNet\TiledMapDataSource_VBNet\TileFunctionality.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.Collections.Generic
Imports System.Text
Imports ESRI.ArcGIS.ADF.Web.DataSources
Imports ESRI.ArcGIS.ADF.Web

Namespace TiledMapDataSource_VBNet
    Public Class TileFunctionality
        Implements ITileFunctionality, IRequiresJavaScriptRegistration

        Private serviceUrl_Renamed As String = Nothing
        Private urlGeneratorJSFunction_Renamed As String = Nothing
        Private vdir As String

        Private firstInitialization As Boolean = True

        Private tileCacheInfo As TileCacheInfo
        Private tileAccessInfo As TileAccessInfo

        Private tileAccessInfoKey As String

        Private name_Renamed As String = String.Empty
        Private resource_Renamed As IGISResource = Nothing
        Private _webControl As System.Web.UI.WebControls.WebControl
        Private _initialized As Boolean = False

        Private scriptTag_Renamed As ScriptTag


        Public Sub New(ByVal name_Renamed As String, ByVal resource_Renamed As MapResource, ByVal tci As TileCacheInfo)
            Me.name_Renamed = name_Renamed
            Me.resource_Renamed = resource_Renamed
            Me.tileCacheInfo = tci

        End Sub

        Public ReadOnly Property MapResource() As MapResource
            Get
                Return TryCast(resource_Renamed, MapResource)
            End Get
        End Property

#Region "ITileFunctionality Members"
        Public ReadOnly Property UrlGeneratorJSFunction() As String Implements ITileFunctionality.UrlGeneratorJSFunction
            Get
                Return urlGeneratorJSFunction_Renamed
            End Get
        End Property

        Public ReadOnly Property ServiceUrl() As String Implements ITileFunctionality.ServiceUrl
            Get
                Return serviceUrl_Renamed
            End Get
        End Property

        Public ReadOnly Property ImageFormat() As WebImageFormat Implements ITileFunctionality.ImageFormat
            Get
                If Not tileAccessInfo Is Nothing Then
                    Return tileAccessInfo.WebImageFormat
                Else
                    Throw New Exception(ResourceUtility.GetResourceString("TileFunctionalityExceptionFunctionalityNotInitialized"))
                End If
            End Get
        End Property

        Public Function Draw(ByVal mapFunctionalityName As String, ByVal column As Long, ByVal row As Long, ByVal level As Integer) As ESRI.ArcGIS.ADF.Web.MapImage Implements ITileFunctionality.Draw
            Return Draw(column, row, level, tileAccessInfo)
        End Function

        Friend Shared Function Draw(ByVal column As Long, ByVal row As Long, ByVal level As Integer, ByRef tileInfo As TileAccessInfo) As ESRI.ArcGIS.ADF.Web.MapImage
            Dim mapImage As ESRI.ArcGIS.ADF.Web.MapImage = Nothing
            Return mapImage
        End Function

#End Region

#Region "Private properties and fields"

        Private ReadOnly Property key() As String
            Get
                Return String.Format("{0}:{1}", MapResource.key, name_Renamed)
            End Get
        End Property


#End Region

#Region "IGISFunctionality Members"

        Public Property WebControl() As System.Web.UI.WebControls.WebControl Implements ITileFunctionality.WebControl
            Get
                Return _webControl
            End Get
            Set(ByVal value As System.Web.UI.WebControls.WebControl)
                _webControl = Value
            End Set
        End Property

        Public Property Name() As String Implements ITileFunctionality.Name
            Get
                Return Me.name_Renamed
            End Get
            Set(ByVal value As String)
                Me.name_Renamed = Value
            End Set
        End Property

        Public Property Resource() As IGISResource Implements ITileFunctionality.Resource
            Get
                Return Me.resource_Renamed
            End Get
            Set(ByVal value As IGISResource)
                Me.resource_Renamed = Value
            End Set
        End Property

        Public ReadOnly Property Initialized() As Boolean Implements ITileFunctionality.Initialized
            Get
                Return _initialized
            End Get
        End Property

        Public Sub LoadState() Implements ITileFunctionality.LoadState
            If resource_Renamed Is Nothing OrElse resource_Renamed.DataSource Is Nothing OrElse resource_Renamed.DataSource.State Is Nothing Then
                Throw New Exception(ResourceUtility.GetResourceString("MapFunctionalityExceptionResourceAssociatedWithFunctionIsNotValid"))
            End If

            ' Load previous state if it was saved before.
            Dim o As Object = resource_Renamed.DataSource.State(key)
            If Not o Is Nothing Then
                Dim tf As TileFunctionality = TryCast(o, TileFunctionality)
                serviceUrl_Renamed = tf.serviceUrl
                urlGeneratorJSFunction_Renamed = tf.urlGeneratorJSFunction
                vdir = tf.vdir

                firstInitialization = False

                tileAccessInfo = tf.tileAccessInfo
                tileAccessInfoKey = tf.tileAccessInfoKey

                scriptTag_Renamed = tf.scriptTag
            End If
        End Sub

        ''' <summary>Initializes the functionality to prepare it for use.</summary>
        Public Sub Initialize() Implements ITileFunctionality.Initialize
            Dim javaScriptPossible As Boolean = Not WebControl Is Nothing AndAlso Not WebControl.Page Is Nothing
            If firstInitialization Then
                '        #Region "Get Image Format Information"
                Dim systemImageFormat As System.Drawing.Imaging.ImageFormat = Nothing
                Dim webImageFormat As WebImageFormat = Nothing
                Dim imageFormatExtension As String = Nothing
                setImageFormatVars(webImageFormat, systemImageFormat, imageFormatExtension)

                'Populate in tile access information
                tileAccessInfo = New TileAccessInfo()
                tileAccessInfo.ImageFormat = systemImageFormat
                tileAccessInfo.ImageFormatExtension = imageFormatExtension
                If tileCacheInfo Is Nothing Then
                    tileAccessInfo.CacheTileFormat = String.Empty
                Else
                    tileAccessInfo.CacheTileFormat = tileCacheInfo.CacheTileFormat
                End If
                tileAccessInfo.WebImageFormat = webImageFormat
                '        #End Region

                '        #Region "Figure out how to access tiles"
                '        #Region "Get Service Url/VDir"
                If vdir Is Nothing Then
                    Try
                        vdir = tileCacheInfo.Url
                        If vdir Is Nothing Then
                            vdir = String.Empty
                        End If
                    Catch
                        vdir = String.Empty
                    End Try
                End If
                '        #End Region

                Dim tileAccess As tileAccessMethod = tileAccessMethod.JavascriptAccessVirtualDirectory

                '        #End Region

                '        #Region "Tile access information                "
                tileAccessInfo.TileAccess = tileAccess
                tileAccessInfo.VirtualDirectory = vdir
                '        #End Region

                '        #Region "Store tile access information in session"
                tileAccessInfoKey = Guid.NewGuid().ToString("N")
                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
                If Not session Is Nothing Then
                    session(tileAccessInfoKey) = tileAccessInfo
                End If
                '        #End Region

            End If

            '      #Region "Set up javascript tile access if necessary"
            If javaScriptPossible Then
                If tileAccessInfo.TileAccess = tileAccessMethod.JavascriptAccessVirtualDirectory Then

                    'Function used by an ADF JavaScript tile layer to determine tile urls
                    ' Tile layer created for you in Map_Rendering.cs-ResourceToJS.  
                    serviceUrl_Renamed = vdir
                    urlGeneratorJSFunction_Renamed = String.Format("tiledMapVirtualDirectoryTileUrlGenerator_{0}", tileAccessInfo.ImageFormatExtension)
                    Dim builder As String = String.Format("" & ControlChars.CrLf & "                            function pad(toPad, padding, totalLength, padLeft)" & ControlChars.CrLf & "                            {{" & ControlChars.CrLf & "                                if (toPad.length < totalLength){{" & ControlChars.CrLf & "                                    if (padLeft) toPad = padding + toPad;" & ControlChars.CrLf & "                                else toPad = toPad + padding;" & ControlChars.CrLf & "                                }}" & ControlChars.CrLf & "                                if (toPad.length >= totalLength) return toPad;" & ControlChars.CrLf & "                                return pad(toPad, padding, totalLength, padLeft);" & ControlChars.CrLf & "                            }}" & ControlChars.CrLf & ControlChars.CrLf & "                            function {0}(level, column, row, vdir)" & ControlChars.CrLf & "                            {{" & ControlChars.CrLf & "                                var sLevel = 'L' + pad(level.toString(), '0', 2, true);" & ControlChars.CrLf & "                                var sRow = 'R' + pad(row.toString(16), '0', 8, true);" & ControlChars.CrLf & "                                var sColumn = 'C' + pad(column.toString(16), '0', 8, true) + '.{1}';" & ControlChars.CrLf & "                                return vdir + '/' + sLevel + '/' + sRow + '/' + sColumn;" & ControlChars.CrLf & "                            }}", urlGeneratorJSFunction_Renamed, tileAccessInfo.ImageFormatExtension)

                    scriptTag_Renamed = New ScriptTag(urlGeneratorJSFunction_Renamed, "javascript", Nothing, builder, Nothing, Nothing, Nothing)

                    '          #End Region
                End If

            End If
            '      #End Region

            _initialized = True
        End Sub

        ''' <summary>Saves the state of the functionality, typically for use between requests.</summary>
        Public Sub SaveState() Implements ITileFunctionality.SaveState
            resource_Renamed.DataSource.State(key) = Me
        End Sub

        ''' <summary>Disposes of the functionality.</summary>
        Public Sub Dispose() Implements ITileFunctionality.Dispose
            _initialized = False
        End Sub

        ''' <summary>
        ''' Determines if the functionality supports an operation.
        ''' </summary>
        ''' <param name="operation">Operations are members (methods, properties) on the functionality class.</param>
        Public Function Supports(ByVal operation As String) As Boolean Implements ITileFunctionality.Supports
            Return True
        End Function

#End Region


#Region "Private Methods"
        Private Function GetMapFunctionality(ByVal name_Renamed As String) As MapFunctionality
            Dim mapFunctionality As MapFunctionality = TryCast(resource_Renamed.Functionalities.Find(name_Renamed), MapFunctionality)
            Return mapFunctionality
        End Function

        Private Sub setImageFormatVars(ByRef webImageFormat As WebImageFormat, ByRef systemImageFormat As System.Drawing.Imaging.ImageFormat, ByRef imageFormatExtension As String)
            '      #Region "figure out web image format"
            webImageFormat = ESRI.ArcGIS.ADF.Web.WebImageFormat.PNG8
            Try
                Select Case tileCacheInfo.CacheTileFormat
                    Case "PNG8"
                        webImageFormat = ESRI.ArcGIS.ADF.Web.WebImageFormat.PNG8
                    Case "PNG24"
                        webImageFormat = ESRI.ArcGIS.ADF.Web.WebImageFormat.PNG24
                    Case "PNG32"
                        webImageFormat = ESRI.ArcGIS.ADF.Web.WebImageFormat.PNG32
                    Case "JPEG"
                        webImageFormat = ESRI.ArcGIS.ADF.Web.WebImageFormat.JPG
                End Select
            Catch ex As Exception
                Dim test As String = ex.Message
            End Try

            If String.IsNullOrEmpty(tileCacheInfo.CacheTileFormat) Then
                webImageFormat = Converter.ToWebImageFormat(MapResource.DisplaySettings.ImageDescriptor.ImageFormat)
            End If
            '      #End Region

            systemImageFormat = Converter.ToMicrosoftImageFormat(webImageFormat)
            imageFormatExtension = Converter.ExtensionFromWebImageFormat(webImageFormat)
        End Sub

#End Region


#Region "IRequiresJavaScriptRegistration Members"

        Public ReadOnly Property ScriptTag() As ScriptTag Implements IRequiresJavaScriptRegistration.ScriptTag
            Get
                Return scriptTag_Renamed
            End Get
        End Property
#End Region

    End Class
End Namespace