AddRSSWeatherLayer.vb
RSS Weather layer
AddRSSWeatherLayer.vb
' Copyright 2006 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.Drawing
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Display

  ''' <summary>
  ''' Add a new weather item given a zipCode.
  ''' </summary>
  ''' <remarks>Should the weather item exist, it will be updated</remarks>
  <ClassInterface(ClassInterfaceType.None), Guid("C9260965-D3AA-4c28-B55A-023C41F1CA39"), ProgId("RSSWeatherLayer.AddRSSWeatherLayer"), ComVisible(True)> _
  Public NotInheritable Class AddRSSWeatherLayer : Inherits BaseCommand
    #Region "COM Registration Function(s)"
    <ComRegisterFunction(), ComVisible(False)> _
    Private Shared Sub RegisterFunction(ByVal registerType As Type)
      ' Required for ArcGIS Component Category Registrar support
      ArcGISCategoryRegistration(registerType)

      '
      ' TODO: Add any COM registration code here
      '
    End Sub

    <ComUnregisterFunction(), ComVisible(False)> _
    Private Shared Sub UnregisterFunction(ByVal registerType As Type)
      ' Required for ArcGIS Component Category Registrar support
      ArcGISCategoryUnregistration(registerType)

      '
      ' TODO: Add any COM unregistration code here
      '
    End Sub

    #Region "ArcGIS Component Category Registrar generated code"
    ''' <summary>
    ''' Required method for ArcGIS Component Category registration -
    ''' Do not modify the contents of this method with the code editor.
    ''' </summary>
    Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
      Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
      ControlsCommands.Register(regKey)
    MxCommands.Register(regKey)

    End Sub
    ''' <summary>
    ''' Required method for ArcGIS Component Category unregistration -
    ''' Do not modify the contents of this method with the code editor.
    ''' </summary>
    Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
      Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
      ControlsCommands.Unregister(regKey)
    MxCommands.Unregister(regKey)

    End Sub

    #End Region
    #End Region

  'class members
    Private m_pHookHelper As IHookHelper = Nothing
    Private m_weatherLayer As RSSWeatherLayerClass = Nothing
    Private m_bOnce As Boolean = True
    Private m_bConnected As Boolean = False
    Private m_invlidArea As IInvalidArea3 = Nothing
    Private m_point As IPoint = Nothing

    Public Sub New()
      MyBase.m_category = "Weather"
      MyBase.m_caption = "Add RSS Weather Layer"
      MyBase.m_message = "Add RSS Weather Layer"
      MyBase.m_toolTip = "Add RSS Weather Layer"
      MyBase.m_name = MyBase.m_category & "_" & MyBase.m_caption

      Try
        Dim bitmapResourceName As String = Me.GetType().Name & ".bmp"
        MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
      Catch ex As Exception
        System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
      End Try
    End Sub

    #Region "Overriden Class Methods"

    ''' <summary>
    ''' Occurs when this command is created
    ''' </summary>
    ''' <param name="hook">Instance of the application</param>
    Public Overrides Sub OnCreate(ByVal hook As Object)
    'Instantiate the hook helper
    If m_pHookHelper Is Nothing Then
        m_pHookHelper = New HookHelperClass ()
    End If

    'set the hook
      m_pHookHelper.Hook = hook

      m_invlidArea = New InvalidAreaClass()

      m_point = New PointClass()

    'set verbose events in order to listen to ItemDeleted event
    CType(m_pHookHelper.FocusMap, IViewManager).VerboseEvents = True

    'hook the ItemDeleted event in order to track removal of the layer from the TOC
    AddHandler (CType(m_pHookHelper.FocusMap, IActiveViewEvents_Event)).ItemDeleted, AddressOf OnItemDeleted
    End Sub

    ''' <summary>
    ''' Occurs when this command is clicked
    ''' </summary>
    Public Overrides Sub OnClick()
      Try
        If (Not m_bConnected) Then
      'set the display for the InvalidArea member
      m_invlidArea.Display = m_pHookHelper.ActiveView.ScreenDisplay

      'check first that the layer was added to the globe
          If m_bOnce = True Then
          'instantiate the layer
            m_weatherLayer = New RSSWeatherLayerClass()
            m_bOnce = False
          End If
      'test whether the layer has been added to the map
          Dim bLayerHasBeenAdded As Boolean = False
          Dim layer As ILayer = Nothing

          If m_pHookHelper.FocusMap.LayerCount <> 0 Then
          Dim layers As IEnumLayer = m_pHookHelper.FocusMap.Layers(Nothing, False)
            layers.Reset()
            layer = layers.Next()
            Do While Not layer Is Nothing
              If TypeOf layer Is RSSWeatherLayerClass Then
                bLayerHasBeenAdded = True
                Exit Do
              End If
              layer = layers.Next()
            Loop
          End If

      'add the layer to the map
          If (Not bLayerHasBeenAdded) Then
            layer = CType(m_weatherLayer, ILayer)
            layer.Name = "RSS Weather Layer"
            Try
              m_pHookHelper.FocusMap.AddLayer(layer)
              'wires layer's events
              AddHandler m_weatherLayer.OnWeatherItemAdded, AddressOf OnWeatherItemAdded
              AddHandler m_weatherLayer.OnWeatherItemsUpdated, AddressOf OnWeatherItemsUpdated
            Catch ex As Exception
              System.Diagnostics.Trace.WriteLine("Failed" & ex.Message)
            End Try
          End If

          'connect to the service
      m_weatherLayer.Connect()
        Else
          'disconnect from the service
          m_weatherLayer.Disconnect()

          'delete the layer
          m_pHookHelper.FocusMap.DeleteLayer(m_weatherLayer)

          'dispose the layer
          m_weatherLayer = Nothing
          m_bOnce = True
        End If


        m_bConnected = Not m_bConnected
      Catch ex As Exception
        System.Diagnostics.Trace.WriteLine(ex.Message)
      End Try
    End Sub

    ''' <summary>
  ''' Indicates whether or not this command is checked. 
    ''' </summary>
  Public Overrides ReadOnly Property Checked() As Boolean
      Get
        Return m_bConnected
      End Get
  End Property

    #End Region

  ''' <summary>
  ''' weather layer ItemAdded event handler
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="args"></param>
  ''' <remarks>gets fired when an item is added to the table</remarks>
    Private Sub OnWeatherItemAdded(ByVal sender As Object, ByVal args As WeatherItemEventArgs)
      'get the coordinate of the middle of the area needed to be refreshed from the event argument
    m_point.X = args.mapX
      m_point.Y = args.mapY
      m_invlidArea.Add(m_point)

      'refresh the symbol area only
    Dim cacheId As Short = m_pHookHelper.ActiveView.ScreenCacheID(esriViewDrawPhase.esriViewGeography, m_weatherLayer)
      m_invlidArea.InvalidateEx(cacheId, Math.Max(args.IconWidth, args.IconHeight))
      m_pHookHelper.ActiveView.ScreenDisplay.UpdateWindow()
    End Sub

  ''' <summary>
  ''' Weather layer ItemsUpdated event handler
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="args"></param>
  ''' <remarks>gets fired when the update thread finish updating all the items in the table</remarks>
    Private Sub OnWeatherItemsUpdated(ByVal sender As Object, ByVal args As EventArgs)
      'refresh the display
      m_pHookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, m_weatherLayer, m_pHookHelper.ActiveView.Extent)
    End Sub

  ''' <summary>
  ''' Listen to ActiveViewEvents.ItemDeleted in order to track whether the layer has been 
  ''' removed from the TOC
  ''' </summary>
  ''' <param name="Item"></param>
  Private Sub OnItemDeleted(ByVal Item As Object)
    'test that the deleted layer is RSSWeatherLayerClass
    If TypeOf Item Is RSSWeatherLayerClass Then
    If m_bConnected AndAlso Not Nothing Is m_weatherLayer Then
      m_bConnected = False

      'disconnect from the service
      m_weatherLayer.Disconnect()

      'dispose the layer
      m_weatherLayer = Nothing
      m_bOnce = True
    End If
    End If
  End Sub
  End Class