Common Add dynamic data
Common_AddDynamicData_VBNet\DynamicDataWithContextMenu.aspx.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 Partial Class DynamicDataWithContextMenu
  Inherits System.Web.UI.Page
  Implements System.Web.UI.ICallbackEventHandler
  #Region "Instance Variable Declarations"

  Private m_NameValueCollection As System.Collections.Specialized.NameValueCollection = Nothing
  Private m_CallbackArg As String = Nothing
  Private Const AGSLocalName As String = "AGSLocalMapResource"
  Private Const AGSInternetName As String = "AGSInternetMapResource"
  Private Const IMSName As String = "IMSMapResource"

  #End Region

  #Region "ASP.NET Page Event Handlers"
  Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
    ' Assign functions to handle events on the custom context menus.  Handle the ItemClicked
    ' event on the MapContextMenu and the ItemClicked and Dismissed events on the TocContextMenu.
    AddHandler MapContextMenu.ItemClicked, AddressOf MapContextMenu_ItemClicked
    AddHandler TocContextMenu.ItemClicked, AddressOf TocContextMenu_ItemClicked
    AddHandler TocContextMenu.Dismissed, AddressOf TocContextMenu_Dismissed

  End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    ' Create a string to store the JavaScript callback function for ASP.NET's callback framework
    Dim adfCallbackFunctionString As String = Page.ClientScript.GetCallbackEventReference(Me, "argument", "processCallbackResult", "context", True)

    ' Construct the JavaScript needed to get the ArcIMS service parameters from the ArcIMS
    ' data source specification floating panel (ArcImsDataFloatingPanel), call the callback
    ' function with those parameters, and hide the floating panel
    Dim jsAddArcIMSdata As String = String.Format("var argument='EventArg=AddIMS&host=' + " & "document.getElementById('imsHost').value + '&port=' + " & "document.getElementById('imsPort').value + '&service=' + " & "document.getElementById('imsService').value; var context=null; eval(""{0}""); " & "hideFloatingPanel('{1}',false);", adfCallbackFunctionString, ArcImsDataFloatingPanel.ClientID)
    ' Wire the JavaScript to execute when the OK button on ArcImsDataFloatingPanel is clicked
    ImsOKButton.Attributes.Add("onclick", jsAddArcIMSdata)

    ' Construct the JavaScript needed to get the display parameters from the display settings
    ' floating panel (DisplaySettingsFloatingPanel), call the callback function with those
    ' parameters, and hide the floating panel
    Dim jsChangeDisplaySettings As String = String.Format("var argument=" & "'EventArg=DisplaySettings&transparency=' + " & "document.getElementById('txtTransparency').value + '&transparentBackground=' + " & "document.getElementById('chkTransparentBackground').checked; var context=null; " & "eval(""{0}""); hideFloatingPanel('{1}',false);", adfCallbackFunctionString, DisplaySettingsFloatingPanel.ClientID)
    ' Wire the JavaScript to execute when the OK button on DisplaySettingsFloatingPanel is clicked
    DisplaySettingsOKButton.Attributes.Add("onclick", jsChangeDisplaySettings)

    If (Not Page.IsPostBack) Then
      ' Set up the custom context menu on the map control during initial page load
      SetupMapContextMenu()
    End If
  End Sub

  Protected Overrides Sub OnPreRenderComplete(ByVal e As System.EventArgs)
    MyBase.OnPreRenderComplete(e)

    ' Set up the custom context menu on the Toc.  This is done in PreRenderComplete
    ' because the nodes in a Toc control are populated during the PreRender event. 
    SetupTocContextMenu()
  End Sub

  #End Region

  #Region "Web ADF Control Event Handlers"

  ' Item clicked event for the custom context menu on the map control
  Private Sub MapContextMenu_ItemClicked(ByVal sender As Object, ByVal contextMenuItemEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs)
    Try
      ' Declare variables to store the name of the resource to be added and whether or not
      ' a resource needs to be created and added
      Dim resourceName As String = ""
      Dim createAndAddResource As Boolean = False

      ' Check the text of the context menu item clicked and take action accordingly
      Select Case contextMenuItemEventArgs.Item.Text
        Case "Add ArcGIS Server Local Data"
          ' Set the resource name to the ArcGIS Server Local resource name
          ' and set the flag for creating and adding a resource to true
          resourceName = AGSLocalName
          createAndAddResource = True
        Case "Add ArcGIS Server Internet Data"
          ' Set the resource name to the ArcGIS Server Internet resource name
          ' and set the flag for creating and adding a resource to true
          resourceName = AGSInternetName
          createAndAddResource = True
        Case "Add ArcIMS Data"
          ' Set the resource name to the ArcIMS resource name and
          ' set the flag for creating and adding a resource to true
          resourceName = IMSName
          createAndAddResource = True
        Case "Add Custom ArcIMS Data"
          ' Construct the JavaScript necessary to show the ArcIMS data source specification
          ' floating panel (ArcImsDataFloatingPanel) and add it to the MapContextMenu's
          ' collection of callback results.  showFloatingPanel is the Web ADF JavaScript
          ' function to display floating panels.
          Dim jsShowArcImsDataFloatingPanel As String = String.Format("showFloatingPanel('{0}',false);", ArcImsDataFloatingPanel.ClientID)
          MapContextMenu.CallbackResults.Add(Me, "javascript", jsShowArcImsDataFloatingPanel)
        Case "Zoom In"
          ' Zoom the map in by a factor of two
          Map1.Zoom(2)
        Case "Zoom To Full Extent"
          ' Zoom the map to its full extent
          Map1.ZoomToFullExtent()
        Case Else
      End Select

      ' Check whether the flag indicating a resource needs to be created and added is true
      If createAndAddResource Then
        ' Call CreateAndAddMapResource, which creates a map resource 
        ' with the passed-in resource name and adds it to the map
        If (Not CreateAndAddMapResource(resourceName)) Then
          Return
        End If

        ' Refresh the Toc so the newly added map resource is shown
        Toc1.Refresh()

        ' Set up the Toc's context menu again.  This needs to be done here
        ' because the call to Refresh removes the association between the
        ' context menu and the Toc.
        SetupTocContextMenu()
      End If

      ' Return the map's and Toc's callback results via the custom context menu.  
      ' The callback results are returned this way because the context menu 
      ' initiated the callback, and thus will handle the response.
      MapContextMenu.CallbackResults.CopyFrom(Map1.CallbackResults)

      ' Refresh the html content of the Toc and add the result to the context
      ' menu's callback results.  This will re-render the Toc with the context 
      ' menu associations created in SetupTocContextMenu.
      MapContextMenu.CallbackResults.CopyFrom(Toc1.CallbackResults)
      'MapContextMenu.CallbackResults.Add(RefreshControlHtml(Toc1));
    Catch exception As System.Exception
      Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ErrorHandling.GetErrorCallback(exception)
      MapContextMenu.CallbackResults.Add(errorCallbackResult)
    End Try
  End Sub

  ' Item clicked event for parent nodes in a Toc control
  Private Sub TocContextMenu_ItemClicked(ByVal sender As Object, ByVal contextMenuItemEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItemEventArgs)
    Try
      ' Get the Toc node on which the context menu was displayed
      Dim treeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = Toc1.Nodes.FindByNodeID(contextMenuItemEventArgs.Context)
      ' Make sure a node was found
      If Not treeViewPlusNode Is Nothing Then
        ' Declare an object variable to store a reference to a MapResourceItem
        Dim mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem = Nothing
        Dim index As Integer = -1

        ' Check the text of the context menu item clicked and take action accordingly
        Select Case contextMenuItemEventArgs.Item.Text
          Case "Zoom To Resource"
            ' Get a reference to the map resource indicated by the text of the Toc node
            Dim mapResource As ESRI.ArcGIS.ADF.Web.DataSources.IMapResource = Map1.MapResourceManagerInstance.GetResource(treeViewPlusNode.Text)
            ' Set the map's coordinate system to that of the map resource indicated by
            ' the Toc node.  This is done so that zooming to the full extent will work
            ' properly, even in cases where the units of the coordinate systems don't match.
            Map1.SpatialReference.CoordinateSystem = mapResource.MapInformation.DefaultSpatialReference.CoordinateSystem
            ' Set the map extent to the full extent of the map resource
            Map1.Extent = mapResource.MapInformation.FullExtent
            ' Add the map's callback results to the context menu's callback results collection.
            ' This is done because the context menu initiated the callback, and will therefore
            ' handle the callback response.
            TocContextMenu.CallbackResults.CopyFrom(Map1.CallbackResults)
          Case "Remove Resource"
            ' Get a reference to the map resource item indicated by the text of the Toc node
            mapResourceItem = MapResourceManager1.ResourceItems.Find(treeViewPlusNode.Text)
            ' Make sure the resource item was found
            If Not mapResourceItem Is Nothing Then
              ' Remove the map resource item from the MapResourceManger
              MapResourceManager1.ResourceItems.Remove(mapResourceItem)
              ' Add the map's callback results to the context menu's callback 
              ' results collection.
              TocContextMenu.CallbackResults.CopyFrom(Map1.CallbackResults)
              ' Refresh the Toc so the removed map resource is also removed from the Toc
              Toc1.Refresh()
              ' Set up the Toc's context menu again.  This needs to be done here
              ' because the call to Refresh removes the association between the
              ' context menu and the Toc.
              SetupTocContextMenu()
              ' Refresh the html content of the Toc and add the result to the context
              ' menu's callback results.  This will re-render the Toc with the context 
              ' menu associations created in SetupTocContextMenu.
              TocContextMenu.CallbackResults.Add(RefreshControlHtml(Toc1))
              Exit Select
            End If
          Case "Move Down"
            ' Get a reference to the map resource item indicated by the text of the Toc node
            mapResourceItem = MapResourceManager1.ResourceItems.Find(treeViewPlusNode.Text)
            ' Get the index of the map resource item. This specifies where the map resource
            ' is ordered in the map and Toc.
            index = MapResourceManager1.ResourceItems.IndexOf(mapResourceItem)

            ' Make sure that the map resource item was found and that it is not already
            ' at the bottom of all the map resource items
            If Not mapResourceItem Is Nothing AndAlso index <> MapResourceManager1.ResourceItems.Count - 1 Then
              ' Move the map resource item down one position
              MapResourceManager1.ResourceItems.Move(index, index + 1)

              ' Add the map's callback results to the context menu's callback 
              ' results collection.
              TocContextMenu.CallbackResults.CopyFrom(Map1.CallbackResults)
              ' Refresh the Toc so the removed map resource is also removed from the Toc
              Toc1.Refresh()
              ' Set up the Toc's context menu again.  This needs to be done here
              ' because the call to Refresh removes the association between the
              ' context menu and the Toc.
              SetupTocContextMenu()
              ' Refresh the html content of the Toc and add the result to the context
              ' menu's callback results.  This will re-render the Toc with the context 
              ' menu associations created in SetupTocContextMenu.
              TocContextMenu.CallbackResults.Add(RefreshControlHtml(Toc1))
              Exit Select
            End If
          Case "Move Up"
            ' Get a reference to the map resource item indicated by the text of the Toc node
            mapResourceItem = MapResourceManager1.ResourceItems.Find(treeViewPlusNode.Text)
            ' Get the index of the map resource item. This specifies where the map resource
            ' is ordered in the map and Toc.
            index = MapResourceManager1.ResourceItems.IndexOf(mapResourceItem)

            ' Make sure that the map resource item was found and that it is not already
            ' at the top of all the map resource items
            If Not mapResourceItem Is Nothing AndAlso index <> 0 Then
              ' Move the map resource item up one position
              MapResourceManager1.ResourceItems.Move(index, index - 1)

              ' Add the map's callback results to the context menu's callback 
              ' results collection.
              TocContextMenu.CallbackResults.CopyFrom(Map1.CallbackResults)
              ' Refresh the Toc so the removed map resource is also removed from the Toc
              Toc1.Refresh()
              ' Set up the Toc's context menu again.  This needs to be done here
              ' because the call to Refresh removes the association between the
              ' context menu and the Toc.
              SetupTocContextMenu()
              ' Refresh the html content of the Toc and add the result to the context
              ' menu's callback results.  This will re-render the Toc with the context 
              ' menu associations created in SetupTocContextMenu.
              TocContextMenu.CallbackResults.Add(RefreshControlHtml(Toc1))
              Exit Select
            End If
          Case "Resource Display Settings"
            ' Construct the JavaScript necessary to show the display settings floating panel 
            ' (DisplaySettingsFloatingPanel) and add it to the TocContextMenu's collection 
            ' of callback results.  showFloatingPanel is the Web ADF JavaScript function to 
            ' display floating panels.
            Dim jsShowDisplaySettingsPanel As String = String.Format("showFloatingPanel('{0}',false);", DisplaySettingsFloatingPanel.ClientID)
            TocContextMenu.CallbackResults.Add(Me, "javascript", jsShowDisplaySettingsPanel)
            ' Store the text of the Toc node in a session variable.  This variable will be
            ' used to specify the resource the display settings will be applied to.
            Session("displaySettingsResourceKey") = treeViewPlusNode.Text
          Case Else
        End Select
      End If
    Catch exception As System.Exception
      Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ErrorHandling.GetErrorCallback(exception)
      TocContextMenu.CallbackResults.Add(errorCallbackResult)
    End Try
  End Sub

  ' Dismissed event for context menu off a parent node in a Toc control
  Private Sub TocContextMenu_Dismissed(ByVal sender As Object, ByVal contextMenuDismissedEventArgs As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuDismissedEventArgs)
    Try
      ' Get a reference to the TocContextMenu via the sender argument
      Dim contextMenu As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenu = TryCast(sender, ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenu)

      ' Make sure event occured within this Page
      If Not contextMenuDismissedEventArgs.Control Is Me Then
      Return
      End If

      ' Get a reference to the Toc node that was right-clicked
      Dim treeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = Toc1.Nodes.FindByNodeID(contextMenuDismissedEventArgs.Context)

      ' Make sure the node was found
      If Not treeViewPlusNode Is Nothing Then
        ' Construct the JavaScript necessary to set background color of the Toc node 
        ' to that of the Toc (so it no longer appears selected)
        Dim jsSetTocNodeBackgroundColor As String = String.Format("var node=document.getElementById('{0}');" & "if(node!=null){{node.style.backgroundColor='{1}';}}", treeViewPlusNode.NodeID & "_textCell", System.Drawing.ColorTranslator.ToHtml(Toc1.BackColor))
        ' Create a new CallbackResult object with the JavaScript and add it 
        ' to the context menu's callback results collection.
        Dim setBackgroundCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsSetTocNodeBackgroundColor)
        contextMenu.CallbackResults.Add(setBackgroundCallbackResult)
      End If
    Catch exception As System.Exception
      Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ErrorHandling.GetErrorCallback(exception)
      TocContextMenu.CallbackResults.Add(errorCallbackResult)
    End Try
  End Sub

  #End Region

  #Region "ICallbackEventHandler Members"

  ' The OK buttons on the "Add ArcIMS Data" and "Resource Display Settings" floating panels
  ' were wired to invoke the JavaScript callback function in the Page_Load event.  So when either
  ' of those buttons is clicked, a callback is initiated, causing RaiseCallbackEvent and
  ' GetCallbackResult to be called.  We therefore add code to these methods to parse the arguments
  ' passed in the callback and take action accordingly.
  Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
    ' RaiseCallbackEvent fires first and ingests the callback string passed
    ' from the client to the server.  Set a class-level variable to reference
    ' the string so it can be accessed and handled in GetCallbackResult.
    m_CallbackArg = eventArgument
  End Sub

  Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
    ' Create a new CallbackResultCollection object
    Dim callbackResultCollection As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection = New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection()

    Try
      ' Use the Web ADF's callback parsing utility to parse the callback string
      ' into its arguments and store it in a NameValueCollection object
      Dim nameValueCollection As System.Collections.Specialized.NameValueCollection = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackUtility.ParseStringIntoNameValueCollection(m_CallbackArg)

      ' Get the event argument from the collection of arguments
      Dim eventArg As String = nameValueCollection("EventArg")

      ' Declare a string variable to reference the name of the resource to be manipulated
      Dim resourceName As String = Nothing

      ' Check the event argument and take action accordingly
      Select Case eventArg
        Case "AddIMS"
          ' Set a class-level NameValueCollection object to reference the collection
          ' of arguments.  This is done so the ArcIMS resource definition parameters
          ' can be accessed from the method CreateAndAddMapResource
          m_NameValueCollection = nameValueCollection

          ' Get the host and service from the collection of arguments
          Dim host As String = nameValueCollection("host")
          Dim service As String = nameValueCollection("service")

          ' Generate a random number between 0 and 1000
          Dim random As System.Random = New System.Random()
          Dim randomInt As Integer = random.Next(0, 1000)

          ' Create a unique name for the ArcIMS resource by concatenating the host,
          ' service, and random number
          resourceName = String.Format("{0}-{1}-{2}", host, service, randomInt.ToString())

          ' Create the map resource specified and add it to the map
          If (Not CreateAndAddMapResource(resourceName)) Then
            Return MapContextMenu.CallbackResults.ToString()
          End If

          ' Add the map's callback results to the custom callback results collection
          callbackResultCollection.CopyFrom(Map1.CallbackResults)

          ' Refresh the Toc so the newly added map resource is also added to the Toc
          Toc1.Refresh()

          ' Set up the Toc's context menu again.  This needs to be done here
          ' because the call to Refresh removes the association between the
          ' context menu and the Toc.
          SetupTocContextMenu()

          ' Refresh the html content of the Toc and add the result to the context
          ' menu's callback results.  This will re-render the Toc with the context 
          ' menu associations created in SetupTocContextMenu.
          callbackResultCollection.Add(RefreshControlHtml(Toc1))
        Case "DisplaySettings"
          ' Get the transparency input by the user from the collection of arguments
          Dim transparency As Integer = System.Convert.ToInt32(nameValueCollection("transparency"))

          ' Get whether or not the background transparency checkbox was checked from the
          ' collection of arguments
          Dim transparentBackground As Boolean = System.Convert.ToBoolean(nameValueCollection("transparentBackground"))

          ' Get the resource name from the session variable that was set in the
          ' TocContextMenu's ItemClicked event and initialize a MapResourceItem
          ' based on the resource name.
          resourceName = TryCast(Session("displaySettingsResourceKey"), String)
          Dim mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem = MapResourceManager1.ResourceItems.Find(resourceName)

          ' Make sure a MapResourceItem was found
          If Not mapResourceItem Is Nothing Then
            ' Instantiate a DisplaySettings object and initialize its properties
            ' based on the user input from the display settings floating panel
            Dim displaySettings As ESRI.ArcGIS.ADF.Web.DisplaySettings = New ESRI.ArcGIS.ADF.Web.DisplaySettings()
            displaySettings.Transparency = transparency
            displaySettings.ImageDescriptor.TransparentColor = ColorPicker1.ChosenColor
            displaySettings.ImageDescriptor.TransparentBackground = transparentBackground

            ' Pass the DisplaySettings object to the MapResourceItem to push the
            ' new settings to the map resource
            mapResourceItem.DisplaySettings = displaySettings

            Map1.RefreshResource(mapResourceItem.Name)
          End If

          ' Add the map's callback results to the custom callback results collection
          callbackResultCollection.CopyFrom(Map1.CallbackResults)
      End Select

      ' Return the custom callback results collection for processing on the client
      Return callbackResultCollection.ToString()
    Catch exception As System.Exception
      Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ErrorHandling.GetErrorCallback(exception)
      callbackResultCollection.Add(errorCallbackResult)
      Return callbackResultCollection.ToString()
    End Try
  End Function

  #End Region

  #Region "Instance Methods"

  #Region "Context Menu Setup Methods"

  Private Sub SetupMapContextMenu()
    Try
      ' Instantiate context menu items for zooming in, zooming to the full extent, adding
      ' ArcIMS data, adding ArcGIS Server Local data, adding ArcGIS Server Internet ata, and 
      ' adding user-defined ArcIMS Data.  For each, initialize the item's image and text.  
      ' Once each item is instantiated, add it to the MapContextMenu
      Dim item As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem = Nothing
      item = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/contextMenuZoomTo.png", "Zoom In", Nothing)
      MapContextMenu.Items.Add(item)
      item = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/ArcGIS-Globe-Service16.gif", "Zoom To Full Extent", Nothing)
      MapContextMenu.Items.Add(item)
      item = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/add-new-service_16x16.gif", "Add ArcIMS Data", Nothing)
      MapContextMenu.Items.Add(item)
      item = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/add-new-service_16x16.gif", "Add ArcGIS Server Local Data", Nothing)
      MapContextMenu.Items.Add(item)
      item = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/add-new-service_16x16.gif", "Add ArcGIS Server Internet Data", Nothing)
      MapContextMenu.Items.Add(item)
      item = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/add-folder_16x16.png", "Add Custom ArcIMS Data", Nothing)
      MapContextMenu.Items.Add(item)

      ' Construct a JavaScript call to a Web ADF function that shows a context menu.
      ' The statement "return false;" must be included in this JavaScript to prevent 
      ' the native browser context menu from displaying.
      Dim jsShowDataContextMenu As String = String.Format("esriShowContextMenu(event,'{0}','{1}','{2}');" & "return false;", MapContextMenu.ClientID, Map1.UniqueID, "")

      ' MapDiv is the element id associated with a div in the page that contains the map.
      ' Wire the context menu initialized above by adding the showDataContextMenu JavaScript
      ' call to the div's oncontextmenu event
      MapDiv.Attributes.Add("oncontextmenu", jsShowDataContextMenu)
    Catch exception As System.Exception
      Dim jsErrorMessageAlert As String = String.Format("<script>{0}</script>", ErrorHandling.GetJavaScriptErrorString(exception))
      Response.Write(jsErrorMessageAlert)
    End Try
  End Sub

  Private Sub SetupTocContextMenu()
    Try
      ' Instantiate context menu items for zooming to a resource, removing a resource, moving
      ' a resource down in display order, moving a resource up in display order, and modifying
      ' a resource's display settings. For each, initialize the item's image and text.  Once
      ' each item is instantiated, add it to the TocContextMenu
      Dim item As ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem = Nothing
      item = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/contextMenuZoomTo.png", "Zoom To Resource", Nothing)
      item = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/delete.gif", "Remove Resource", Nothing)
      TocContextMenu.Items.Add(item)
      item = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/expand.png", "Move Down", Nothing)
      TocContextMenu.Items.Add(item)
      item = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/collapse.png", "Move Up", Nothing)
      TocContextMenu.Items.Add(item)
      item = New ESRI.ArcGIS.ADF.Web.UI.WebControls.ContextMenuItem("images/properties_16x16.png", "Resource Display Settings", Nothing)
      TocContextMenu.Items.Add(item)

      ' Iterate through each parent node in the Toc and set the properties of each 
      ' to show the custom context menu when right-clicked
      Dim i As Integer = 0
      Do While i < Toc1.Nodes.Count
        ' Get a reference to the current node and assign it to a TreeViewPlusNode object
        Dim treeViewPlusNode As ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode = Toc1.Nodes(i)

        ' Set the ClickBehavior property of the node so nothing happens when the node is left-clicked
        treeViewPlusNode.ClickBehavior = ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNodeClickBehavior.None

        ' Construct a JavaScript call to a Web ADF JavaScript function that will show the Toc
        ' context menu.  The statement "return false;" must be included in this JavaScript to
        ' prevent the native browser context menu from displaying.
        Dim jsShowTocContextMenu As String = String.Format("esriShowContextMenu(event,'{0}','{1}','{2}');" & "this.style.backgroundColor='{3}';return false;", TocContextMenu.ClientID, UniqueID, treeViewPlusNode.NodeID, System.Drawing.ColorTranslator.ToHtml(Toc1.SelectedColor))
        ' Construct JavaScript to set the mouse cursor to look like an arrow
        Dim jsOnMouseOver As String = "this.style.cursor='arrow';"
        ' Wire the JavaScript contained in jsOnMouseOver to the current node's onmouseover
        ' event so that the cursor changes to an arrow when over the node.
        If treeViewPlusNode.TextCellAttributes.ContainsKey("onmouseover") Then
          treeViewPlusNode.TextCellAttributes("onmouseover") = jsOnMouseOver
        Else
          treeViewPlusNode.TextCellAttributes.Add("onmouseover", jsOnMouseOver)
        End If

        ' Wire the JavaScript to display the Toc context menu to the current node's
        ' oncontextmenu event.
        If treeViewPlusNode.TextCellAttributes.ContainsKey("oncontextmenu") Then
          treeViewPlusNode.TextCellAttributes("oncontextmenu") = jsShowTocContextMenu
        Else
          treeViewPlusNode.TextCellAttributes.Add("oncontextmenu", jsShowTocContextMenu)
        End If
        ' Add a tooltip to the current node informing the user how to display the context menu
        treeViewPlusNode.ToolTip = "Right-click for context menu"
        i += 1
      Loop
    Catch exception As System.Exception
      Dim jsErrorMessageAlert As String = String.Format("<script>{0}</script>", ErrorHandling.GetJavaScriptErrorString(exception))
      Response.Write(jsErrorMessageAlert)
    End Try
  End Sub

  #End Region

  Private Function CreateAndAddMapResource(ByVal resourceName As String) As Boolean
    Dim addResourceSuccessful As Boolean = False
    Try
      ' Map resource items consist of a definition and display settings.  The definition 
      ' will define the data source and resource parameters.  Display settings will define
      ' map image properties and retrieval types. 
      Dim gisResourceItemDefinition As ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition = New ESRI.ArcGIS.ADF.Web.UI.WebControls.GISResourceItemDefinition()

      ' Check the name of the resource corresponding to the checkbox checked and initialize
      ' the resource definition accordingly
      Select Case resourceName
        Case (AGSLocalName)
          gisResourceItemDefinition.DataSourceDefinition = "localhost"
          gisResourceItemDefinition.DataSourceType = "ArcGIS Server Local"
                    gisResourceItemDefinition.ResourceDefinition = "Layers@USA"

        Case (AGSInternetName)
                    gisResourceItemDefinition.DataSourceDefinition = "http://serverapps.esri.com/arcgis/services/"
          gisResourceItemDefinition.DataSourceType = "ArcGIS Server Internet"
                    gisResourceItemDefinition.ResourceDefinition = "Layers@SamplesNet/NorthAmerica"

        Case (IMSName)
          gisResourceItemDefinition.ResourceDefinition = "states"
          gisResourceItemDefinition.DataSourceDefinition = "localhost@5300"
          gisResourceItemDefinition.DataSourceType = "ArcIMS"

        Case Else
          ' Since the passed-in resourceName didn't match any of the pre-defined constants,
          ' assume that the resource is a user-defined ArcIMS map resource.  Get the resource
          ' definition properties from the class level variable m_NameValueCollection, which
          ' was populated in the GetCallbackResult callback handling method
          gisResourceItemDefinition.ResourceDefinition = m_NameValueCollection("service")
          gisResourceItemDefinition.DataSourceDefinition = String.Format("{0}@{1}", m_NameValueCollection("host"), m_NameValueCollection("port"))
          gisResourceItemDefinition.DataSourceType = "ArcIMS"

      End Select

      ' Instantiate a map resource item
      Dim mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem = New ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem()

      ' Associate the resource item definition with the 
      ' newly instantiated map resource item
      mapResourceItem.Definition = gisResourceItemDefinition
      ' Set the resource item's name to the passed-in resource name
      mapResourceItem.Name = resourceName

      ' Initialize display settings
      Dim displaySettings As ESRI.ArcGIS.ADF.Web.DisplaySettings = New ESRI.ArcGIS.ADF.Web.DisplaySettings()
      displaySettings.Visible = True
      displaySettings.Transparency = 50.0F
      displaySettings.ImageDescriptor.TransparentBackground = True
      displaySettings.ImageDescriptor.TransparentColor = System.Drawing.Color.White
      displaySettings.ImageDescriptor.ImageFormat = ESRI.ArcGIS.ADF.Web.ImageFormat.PNG8
      displaySettings.ImageDescriptor.ReturnMimeData = False

      ' Associate the map resource with the display settings
      mapResourceItem.DisplaySettings = displaySettings

      ' Add the resource to the map's MapResourceManager and initialize it
      Map1.MapResourceManagerInstance.ResourceItems.Insert(0, mapResourceItem)
      mapResourceItem.InitializeResource()

      addResourceSuccessful = True
    Catch exception As System.Exception
      Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ErrorHandling.GetErrorCallback(exception)
      MapContextMenu.CallbackResults.Add(errorCallbackResult)
    End Try

    Return addResourceSuccessful
  End Function

  ' Utility function to refresh a web control's html and return it as a callback result
  Private Function RefreshControlHtml(ByVal webControl As System.Web.UI.Control) As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult
    ' Instantaite an HtmlTextWriter object
    Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter()
    Dim htmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stringWriter)

    ' Render the passed-in control's html to the HtmlTextWriter
    webControl.RenderControl(htmlTextWriter)

    ' Get the control's html as a string
    Dim htmlContent As String = stringWriter.ToString()

    ' Close the StringWriter since we are done with it
    stringWriter.Close()

    ' Instantiate and return a callback result with the control and the html.  Instantiating
    ' a callback result with 3 arguments where "content" is the 2nd argument will create a
    ' result instructing the out-of-the-box callback handler (ESRI.ADF.System.processCallbackResult)
    ' to re-render the control specified by the 1st argument with the html contained in the 3rd
    ' argument.
    Return ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateSetContent(webControl, htmlContent)
  End Function

  #End Region
End Class