Common Custom controls
Common_CustomControls_VBNet\ADFWebPart\MapWebPart.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 ADFWebPart_VBNet
    ''' <summary>
    ''' WebPart containing an ArcGIS Server Web ADF Map, Toc, and Toolbar with Zoom In, Zoom Out,
    ''' Pan, and Identify
    ''' </summary>
    Public Class MapWebPart
        Inherits AJAXSharePointWebPart
#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_dataSourceType As String
        Private m_dataSource As String
        Private m_mapResourceDefinition As String

#End Region

#Region "Constructor"

        ''' <summary>
        ''' Creates a new, uninitialized instance of the MapWebPart class.
        ''' </summary>
        Public Sub New()
            ' Default property values
            m_dataSourceType = "ArcGIS Server Internet"
            m_dataSource = "http://serverapps.esri.com/arcgis/services"
            m_mapResourceDefinition = "Layers@SamplesNET/NorthAmerica"
        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()

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

                    ' Create the WebPart's Map Control
                    m_adfMap = New ESRI.ArcGIS.ADF.Web.UI.WebControls.Map()
                    m_adfMap.Visible = True
                    m_adfMap.Width = 400
                    m_adfMap.Height = 400
                    m_adfMap.ID = Me.ID & "_webPartMap"
                    m_adfMap.MapResourceManager = m_mapResourceManager.UniqueID

                    ' Create the WebPart'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 = 200
                    m_adfToc.Height = 300
                    m_adfToc.ID = Me.ID & "_webPartToc"
                    m_adfToc.BuddyControl = m_adfMap.UniqueID

                    ' Create the WebPart'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 WebPart's controls
                    Controls.Add(table)
                Else
                    Dim noResourceDefLabel As System.Web.UI.WebControls.Label = New System.Web.UI.WebControls.Label()
                    noResourceDefLabel.ID = "lblNoResourceDef"
                    noResourceDefLabel.Text = "Data source type and definition, resource definition, and data layer name must be defined."
                    Controls.Add(noResourceDefLabel)
                End If
            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 WebPart's MapResourceManager and initialize
            ' the Map extent
            If propertyCheck() Then
                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.Reset()
                    m_adfMap.Extent = commonMapResource.MapInformation.DefaultExtent

                End If
            End If
        End Sub

#End Region

#Region "Child Control Initialization Methods"

        ' Initializes the WebPart'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 & "_mapWebPartToolbar"
            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(), "zoom-in.png")
            zoomInTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "zoom-in.png")
            zoomInTool.HoverImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "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(), "zoom-out.png")
            zoomOutTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "zoom-out.png")
            zoomOutTool.HoverImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "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(), "pan.png")
            panTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "pan.png")
            panTool.HoverImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "pan.png")
            m_adfToolbar.ToolbarItems.Add(panTool)

            ' initialize and add a custom identify tool that references the 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 = "ADFWebPart_VBNet"
            identifyTool.ServerActionClass = "ADFWebPart_VBNet.MapIdentify"
            identifyTool.ClientAction = ESRI.ArcGIS.ADF.Web.UI.WebControls.Constants.HTML_POINT
            identifyTool.DefaultImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "identify.png")
            identifyTool.SelectedImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "identify.png")
            identifyTool.HoverImage = Page.ClientScript.GetWebResourceUrl(Me.GetType(), "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 WebPart's MapResourceManager
        Private Function AddResourceToMapResourceManager() As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem
            ' Optional: define default data source-resource properties
            'if (string.IsNullOrEmpty(m_dataSource))
            '{
            '    // Define default resource item properties if MapWebPart properties are empty
            '    m_mapResourceType = "ArcGIS Server Internet";
            '    m_dataSource = "http://localhost/arcgis/services";
            '    m_mapResourceDefinition = "(default)@MapService";
            '}
            Dim mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem = Nothing

            ' 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 Then
                ' Create a GISResourceItemDefinition with user-specified parameters
                Dim gisResourceItemDefinition As ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition = Utility.CreateGISResourceItemDefinition(m_dataSource, m_dataSourceType, String.Empty, m_mapResourceDefinition, True)

                If m_mapResourceManager.ResourceItems.Count > 0 Then
                    Dim existingResourceItemDefinition As String = m_mapResourceManager.ResourceItems(0).Definition.ToString()

                    If existingResourceItemDefinition = gisResourceItemDefinition.ToString() Then
                        Return mapResourceItem
                    End If
                End If

                m_mapResourceManager.ResourceItems.Clear()

                ' Create a mapResourceItem from the resource item definition
                mapResourceItem = Utility.CreateResourceItem("AGSMapResource<!--" & Me.UniqueID & "-->", gisResourceItemDefinition)

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

                Utility.AddMapResourceItemToResourceManager(m_mapResourceManager, False, mapResourceItem)
            End If

            'if (m_mapResourceManager != null && m_mapResourceManager.ResourceItems.Count == 0)
            '{
            '    // Create a GISResourceItemDefinition with user-specified parameters
            '    ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition gisResourceItemDefinition =
            '        Utility.CreateGISResourceItemDefinition(m_dataSource, m_dataSourceType, string.Empty,
            '        m_mapResourceDefinition, true);

            '    // Create a mapResourceItem from the resource item definition
            '    mapResourceItem =
            '        Utility.CreateResourceItem("AGSMapResource<!--" + this.UniqueID + "-->",
            '        gisResourceItemDefinition);

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

            '    Utility.AddMapResourceItemToResourceManager(m_mapResourceManager, false, mapResourceItem);
            '}

            Return mapResourceItem
        End Function

#End Region

#Region "Other Instance Methods"

        ' Removes all the web control'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

#End Region

        Private Function propertyCheck() As Boolean
            If (Not String.IsNullOrEmpty(m_dataSource)) AndAlso (Not String.IsNullOrEmpty(m_dataSourceType)) AndAlso (Not String.IsNullOrEmpty(m_mapResourceDefinition)) Then
                Return True
            End If

            Return False
        End Function

#Region "Instance Properties"

        ''' <summary>
        ''' ArcGIS Server resource types
        ''' <remarks>Possible values: "ArcGIS Server Local" and "ArcGIS Server Internet"</remarks>
        ''' </summary>
        <System.Web.UI.WebControls.WebParts.Personalizable(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared), System.Web.UI.WebControls.WebParts.WebBrowsable(True), System.ComponentModel.Category("Resources"), System.Web.UI.WebControls.WebParts.WebDescription("Valid values are 'ArcGIS Server Local' and 'ArcGIS Server Internet'"), System.Web.UI.WebControls.WebParts.WebDisplayName("ArcGIS Server Data Source Type")> _
        Public Property DataSourceType() As String
            Get
                Return m_dataSourceType
            End Get
            Set(ByVal value As String)
                m_dataSourceType = Value
            End Set
        End Property

        ''' <summary>
        ''' Machine name or URL of the ArcGIS Server services host
        ''' </summary>
        <System.Web.UI.WebControls.WebParts.Personalizable(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared), System.Web.UI.WebControls.WebParts.WebBrowsable(True), System.ComponentModel.Category("Resources"), System.Web.UI.WebControls.WebParts.WebDescription("ArcGIS Server machine name or Url"), System.Web.UI.WebControls.WebParts.WebDisplayName("ArcGIS Server Data Source")> _
        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.Web.UI.WebControls.WebParts.Personalizable(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared), System.Web.UI.WebControls.WebParts.WebBrowsable(True), System.ComponentModel.Category("Resources"), System.Web.UI.WebControls.WebParts.WebDescription("Formatted as DataFrameName@ServiceName"), System.Web.UI.WebControls.WebParts.WebDisplayName("ArcGIS Server Resource String")> _
        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
End Namespace