Common Custom controls
Common_CustomControls_VBNet\ADFCompositeControl\MapToolTocControl.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

Public Class MapToolTocControl
    Inherits System.Web.UI.WebControls.CompositeControl
#Region "Instance Variable Declarations"

    Private Const TOOLBARHEIGHT As Integer = 32
    Private m_adfMap As ESRI.ArcGIS.ADF.Web.UI.WebControls.Map = Nothing
    Private m_mapResourceManager As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager = Nothing
    Private m_adfToolbar As ESRI.ArcGIS.ADF.Web.UI.WebControls.Toolbar = Nothing
    Private m_adfToc As ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc = Nothing
    Private m_mapResourceType As String = String.Empty
    Private m_dataSource As String = String.Empty
    Private m_mapResourceDefinition As String = String.Empty

#End Region

#Region "Constructor"

    ''' <summary>
    ''' Creates a new, uninitialized instance of the MapToolTocControl class.
    ''' </summary>
    Public Sub New()
    End Sub

#End Region

#Region "WebControl Life Cycle Event Handlers"

    ' Renders the contents of the control
    Protected Overrides Sub RenderContents(ByVal writer As System.Web.UI.HtmlTextWriter)
        ' Render the control's ID if the control is being drawn at design-time, or render normally
        ' if the control is being rendered at run-time
        If DesignMode Then
            writer.WriteLine(Me.ID)
        Else
            MyBase.RenderContents(writer)
        End If
    End Sub

    ' Create the controls making up the WebPart and add them to the page
    Protected Overrides Sub CreateChildControls()
        Try
            MyBase.CreateChildControls()

            ' Create and initialize a MapResourceManager
            m_mapResourceManager = New ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager()
            m_mapResourceManager.ID = Me.ID & "_MapResourceManager"
            Controls.Add(m_mapResourceManager)

            ' Create the CompositeControl's Map Control
            m_adfMap = New ESRI.ArcGIS.ADF.Web.UI.WebControls.Map()
            m_adfMap.Visible = True
            m_adfMap.Width = CInt(Fix(Width.Value * 0.7))
            m_adfMap.Height = CInt(Fix(Height.Value * 0.8))
            m_adfMap.ID = Me.ID & "_Map"
            m_adfMap.MapResourceManager = m_mapResourceManager.UniqueID

            ' Create the CompositeControl's Toc and buddy it to the map
            m_adfToc = New ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc()
            m_adfToc.Visible = True
            m_adfToc.Width = CInt(Fix(Width.Value * 0.3))
            m_adfToc.Height = CInt(Fix(Height.Value * 0.8))
            m_adfToc.ID = Me.ID & "_Toc"
            m_adfToc.BuddyControl = m_adfMap.UniqueID

            ' Create the CompositeControl's toolbar and buddy it to the map
            m_adfToolbar = CreateToolbar()
            m_adfToolbar.BuddyControls.Add(New ESRI.ArcGIS.ADF.Web.UI.WebControls.BuddyControl(m_adfMap.UniqueID))

            Controls.Add(m_adfToolbar)

            ' Create a table to hold the Map and Toc so they appear side-by-side
            Dim table As System.Web.UI.WebControls.Table = New System.Web.UI.WebControls.Table()
            Dim tableRow As System.Web.UI.WebControls.TableRow = New System.Web.UI.WebControls.TableRow()
            table.Controls.Add(tableRow)
            Dim tableCell As System.Web.UI.WebControls.TableCell = New System.Web.UI.WebControls.TableCell()

            ' Add the Map to the table
            tableCell.Controls.Add(m_adfMap)
            tableCell.Style.Add(System.Web.UI.HtmlTextWriterStyle.VerticalAlign, "top")
            tableRow.Controls.Add(tableCell)

            ' Add the Toc to the table
            tableCell = New System.Web.UI.WebControls.TableCell()
            tableCell.Controls.Add(m_adfToc)
            tableCell.Style.Add(System.Web.UI.HtmlTextWriterStyle.VerticalAlign, "top")
            tableRow.Controls.Add(tableCell)

            ' Add the table to the CompositeControl's controls
            Controls.Add(table)
        Catch ex As System.Exception
            ' Call the method to output the stack trace if an error occurs during initialization
            ShowErrorMessage(ex)
        End Try
    End Sub

    ' Executes before the control is rendered
    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        MyBase.OnPreRender(e)

        ' Add the user-specified resource to the CompositeControl's MapResourceManager and initialize
        ' the Map extent
        Dim mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem = AddResourceToMapResourceManager()
        If Not m_mapResourceManager Is Nothing AndAlso Not mapResourceItem Is Nothing Then
            Dim commonMapResource As ESRI.ArcGIS.ADF.Web.DataSources.IMapResource = TryCast(mapResourceItem.Resource, ESRI.ArcGIS.ADF.Web.DataSources.IMapResource)
            m_adfMap.Extent = commonMapResource.MapInformation.DefaultExtent
        End If
    End Sub

#End Region

#Region "Child Control Initialization Methods"

    ' Initializes the CompositeControl's toolbar
    Private Function CreateToolbar() As ESRI.ArcGIS.ADF.Web.UI.WebControls.Toolbar
        ' Initialize toolbar properties
        m_adfToolbar = New ESRI.ArcGIS.ADF.Web.UI.WebControls.Toolbar()
        m_adfToolbar.ID = Me.ID & "_Toolbar"
        m_adfToolbar.BuddyControlType = ESRI.ArcGIS.ADF.Web.UI.WebControls.BuddyControlType.Map
        m_adfToolbar.ToolbarStyle = ESRI.ArcGIS.ADF.Web.UI.WebControls.ToolbarStyle.ImageOnly
        m_adfToolbar.Height = New System.Web.UI.WebControls.Unit(TOOLBARHEIGHT, System.Web.UI.WebControls.UnitType.Pixel)
        m_adfToolbar.Width = New System.Web.UI.WebControls.Unit(120, System.Web.UI.WebControls.UnitType.Pixel)

        ' Initialize and add a zoom in tool
        Dim zoomInTool As ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool = New ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool()
        zoomInTool.Name = "MapZoomIn"
        zoomInTool.ServerActionAssembly = "ESRI.ArcGIS.ADF.Web.UI.WebControls"
        zoomInTool.ServerActionClass = "ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.MapZoomIn"
        zoomInTool.ToolTip = "Zoom In"
        zoomInTool.ClientAction = ESRI.ArcGIS.ADF.Web.UI.WebControls.Constants.HTML_DRAG_RECTANGLE
        zoomInTool.DefaultImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.zoom-in.png")
        zoomInTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.zoom-in.png")
        zoomInTool.HoverImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.zoom-in.png")
        zoomInTool.EnablePostBack = False
        m_adfToolbar.ToolbarItems.Add(zoomInTool)

        ' Initialize and add a zoom out tool
        Dim zoomOutTool As ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool = New ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool()
        zoomOutTool.Name = "MapZoomOut"
        zoomOutTool.ServerActionAssembly = "ESRI.ArcGIS.ADF.Web.UI.WebControls"
        zoomOutTool.ServerActionClass = "ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.MapZoomOut"
        zoomOutTool.ToolTip = "Zoom Out"
        zoomOutTool.ClientAction = ESRI.ArcGIS.ADF.Web.UI.WebControls.Constants.HTML_DRAG_RECTANGLE
        zoomOutTool.DefaultImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.zoom-out.png")
        zoomOutTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.zoom-out.png")
        zoomOutTool.HoverImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.zoom-out.png")
        m_adfToolbar.ToolbarItems.Add(zoomOutTool)

        ' initialize and add a pan tool
        Dim panTool As ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool = New ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool()
        panTool.Name = "MapPan"
        panTool.ServerActionAssembly = "ESRI.ArcGIS.ADF.Web.UI.WebControls"
        panTool.ServerActionClass = "ESRI.ArcGIS.ADF.Web.UI.WebControls.Tools.MapPan"
        panTool.ToolTip = "Pan"
        panTool.ClientAction = ESRI.ArcGIS.ADF.Web.UI.WebControls.Constants.HTML_DRAG_IMAGE
        panTool.DefaultImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.pan.png")
        panTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.pan.png")
        panTool.HoverImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.pan.png")
        m_adfToolbar.ToolbarItems.Add(panTool)

        ' initialize and add a custom identify tool that references the ADFCompositeControl.MapIdentify
        ' class
        Dim identifyTool As ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool = New ESRI.ArcGIS.ADF.Web.UI.WebControls.Tool()
        identifyTool.Name = "MapIdentify"
        identifyTool.ToolTip = "Identify"
        identifyTool.ServerActionAssembly = "ADFCompositeControl"
        identifyTool.ServerActionClass = "ADFCompositeControl_VBNet.MapIdentify"
        identifyTool.ClientAction = ESRI.ArcGIS.ADF.Web.UI.WebControls.Constants.HTML_POINT
        identifyTool.DefaultImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.identify.png")
        identifyTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.identify.png")
        identifyTool.HoverImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "ADFCompositeControl_VBNet.identify.png")
        ' Use a full page postback for the Identify tool
        identifyTool.EnablePostBack = True
        m_adfToolbar.ToolbarItems.Add(identifyTool)

        Return m_adfToolbar
    End Function

    ' Adds the user-specified resource to the CompositeControl's MapResourceManager
    Private Function AddResourceToMapResourceManager() As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem
        If String.IsNullOrEmpty(m_dataSource) Then
            ' Define default resource item properties if ToolTocControl properties are empty
            m_mapResourceType = "ArcGIS Server Internet"
            m_dataSource = "http://localhost/arcgis/services"
            m_mapResourceDefinition = "(default)@MapService"
        End If

        ' Create a GISResourceItemDefinition with user-specified parameters
        Dim gisResourceItemDefinition As ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition = CreateGISResourceItemDefinition(m_dataSource, m_mapResourceType, String.Empty, m_mapResourceDefinition, True)

        ' Create a mapResourceItem from the resource item definition
        Dim mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem = CreateResourceItem("AGSMapResource<!--" & Me.UniqueID & "-->", gisResourceItemDefinition)

        ' Assign the parent of the map resource item and initialize the underlying resource
        mapResourceItem.Parent = m_mapResourceManager
        mapResourceItem.InitializeResource()

        ' Make sure the map resource manager exists and that it does not contain any resource
        ' items before adding the resource item to it
        If Not m_mapResourceManager Is Nothing AndAlso m_mapResourceManager.ResourceItems.Count = 0 Then
            AddMapResourceItemToResourceManager(m_mapResourceManager, False, mapResourceItem)
        End If

        Return mapResourceItem
    End Function

    ' Adds the passed-in resource to the passed-in map resource manager
    Private Sub AddMapResourceItemToResourceManager(ByVal mapResourceManager As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceManager, ByVal insertIntoBeginning As Boolean, ByVal mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem)
        If insertIntoBeginning Then
            mapResourceManager.ResourceItems.Insert(0, mapResourceItem)
        Else
            mapResourceManager.ResourceItems.Add(mapResourceItem)
        End If
    End Sub


#End Region

#Region "Other Instance Methods"

    ' Removes all the CompositeControl's child controls and replaces them with a Literal control 
    ' containing the stack trace of the passed-in exception.
    Protected Sub ShowErrorMessage(ByVal ex As System.Exception)
        Dim errorMessageLiteral As System.Web.UI.WebControls.Literal = New System.Web.UI.WebControls.Literal()
        errorMessageLiteral.Text = ex.StackTrace
        Me.Controls.Clear()
        Me.Controls.Add(errorMessageLiteral)
    End Sub

    ' Creates a resource item with the passed-in name, the passed-in resource definition string, 
    ' and default settings
    Private Function CreateResourceItem(ByVal resourceName As String, ByVal gisResourceItemDefinition As ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition) As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem
        Dim mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem = New ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem()
        mapResourceItem.Definition = gisResourceItemDefinition
        mapResourceItem.Name = resourceName
        mapResourceItem.DisplaySettings = New ESRI.ArcGIS.ADF.Web.DisplaySettings()
        mapResourceItem.DisplaySettings.Visible = True
        Dim imageDescriptor As ESRI.ArcGIS.ADF.Web.ImageDescriptor = New ESRI.ArcGIS.ADF.Web.ImageDescriptor()
        imageDescriptor.ImageFormat = ESRI.ArcGIS.ADF.Web.ImageFormat.PNG8
        imageDescriptor.TransparentBackground = True
        imageDescriptor.TransparentColor = System.Drawing.Color.White
        imageDescriptor.ReturnMimeData = True
        mapResourceItem.DisplaySettings.ImageDescriptor = imageDescriptor
        mapResourceItem.DisplaySettings.Transparency = 0
        Return mapResourceItem
    End Function

    ' Creates a GISResourceItemDefinition with the passed-in parameters
    Private Function CreateGISResourceItemDefinition(ByVal dataSourceDefinition As String, ByVal dataSourceType As String, ByVal identity As String, ByVal resourceDefinition As String, ByVal dataSourceShared As Boolean) As ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition
        Dim gisResourceItemDefinition As ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition = New ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition()
        gisResourceItemDefinition.DataSourceDefinition = dataSourceDefinition
        If (identity Is Nothing) Then
            gisResourceItemDefinition.Identity = String.Empty
        Else
            gisResourceItemDefinition.Identity = identity
        End If
        gisResourceItemDefinition.ResourceDefinition = resourceDefinition
        gisResourceItemDefinition.DataSourceShared = dataSourceShared
        gisResourceItemDefinition.DataSourceType = dataSourceType
        Return gisResourceItemDefinition
    End Function

#End Region

#Region "Instance Properties"

    ''' <summary>
    ''' ArcGIS Server resource types
    ''' <remarks>Possible values: "ArcGIS Server Local" and "ArcGIS Server Internet"</remarks>
    ''' </summary>
    <System.ComponentModel.Category("Resources")> _
    Public Property MapResourceType() As String
        Get

            Return m_mapResourceType
        End Get
        Set(ByVal value As String)
            m_mapResourceType = Value
        End Set
    End Property

    ''' <summary>
    ''' Machine name or URL of the ArcGIS Server services host
    ''' </summary>
    <System.ComponentModel.Category("Resources")> _
    Public Property DataSource() As String
        Get
            Return m_dataSource
        End Get
        Set(ByVal value As String)
            m_dataSource = Value
        End Set
    End Property

    ''' <summary>
    ''' ArcGIS Server Resource.  Must be formatted as &lt;DataFrameName&gt;@&lt;ServiceName&gt;
    ''' </summary>
    <System.ComponentModel.Category("Resources")> _
    Public Property ResourceDefinition() As String
        Get
            Return m_mapResourceDefinition
        End Get
        Set(ByVal value As String)
            m_mapResourceDefinition = Value
        End Set
    End Property

#End Region
End Class