Bookmarks Gallery
BookmarksGallery.vb
' Copyright 2011 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.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text

Imports ESRI.ArcGISExplorer
Imports ESRI.ArcGISExplorer.Mapping
Imports ESRI.ArcGISExplorer.Geometry
Imports ESRI.ArcGISExplorer.Data
Imports ESRI.ArcGISExplorer.Threading
Imports ESRI.ArcGISExplorer.Application

Namespace BookmarksGalleryVB
    ''' <summary>
    ''' Implements a custom bookmarks gallery
    ''' </summary>
    ''' <remarks>
    ''' Listens to the DocumentOpened event on the Application and includes
    ''' a gallery item for each View in the map. The Views are attached as tags
    ''' to the gallery items so the map can be navigated to them when they are clicked.
    ''' </remarks>
    Public Class BookmarksGallery
        Inherits ESRI.ArcGISExplorer.Application.Gallery
        ''' <summary>
        ''' Creates a new BookmarksGallery instance
        ''' </summary>
        Public Sub New()
            ' listen for the document opened event
            AddHandler ESRI.ArcGISExplorer.Application.Application.DocumentOpened, AddressOf Application_DocumentOpened

            ' listen for the map item changed
            AddHandler ESRI.ArcGISExplorer.Application.Application.MapItemChanged, AddressOf Application_MapItemChanged

            ' call the event handler in case the gallery is loaded
            ' after the document has been opened
            Application_DocumentOpened(Nothing, EventArgs.Empty)
        End Sub

        ''' <summary>
        ''' Raised when a new map document (.nmf) is opened
        ''' </summary>
        Private Sub Application_DocumentOpened(ByVal sender As Object, ByVal e As EventArgs)
            ' add the views from the map to the gallery
            PopulateGallery()
        End Sub

        ''' <summary>
        ''' Raised when an item in the map changes
        ''' </summary>
        Private Sub Application_MapItemChanged(ByVal sender As Object, ByVal e As MapItemEventArgs)
            ' if the changed item is a view, then re-populate the gallery
            If TypeOf e.MapItem Is View Then
                PopulateGallery()
            End If
        End Sub

        ''' <summary>
        ''' Raised when a gallery item is clicked
        ''' </summary>
        ''' <param name="item"></param>
        Public Overrides Sub OnClick(ByVal item As GalleryItem)
            ' get the view from the item's tag property and zoom
            ' the map to its viewpoint
            Dim view As View = TryCast(item.Tag, View)
            ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.ZoomTo(view.Viewpoint)
        End Sub

        ''' <summary>
        ''' Helper method to clear and populate the gallery items
        ''' with the views in the map
        ''' </summary>
        Private Sub PopulateGallery()
            Items.Clear()

            ' find all the Views in the map and add a gallery item for each one
            Dim map As Map = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Map
            For Each view As View In map.GetMapItems(Of View)()
                Dim itm As New GalleryItem(view.Name, Resources.Flag64, view.Name)
                itm.Tag = view
                Items.Add(itm)

            Next view
        End Sub
    End Class
End Namespace