Manage Versions
VersionManagerCmd.cls

' Copyright 2008 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 use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
' 




'*******************************************************************************
' Module Name:  VersionManager.VersionManagerCmd
'
' Requirements:  This class can be registered with the component categories
'                "ESRI Gx Commands" and "ESRI Mx Commands"
'
' Purpose:  This commmand is enabled if a GxDatabase is selected in ArcCatalog or
'           a FeatureLayer, Workspace, FeatureDataset, FeatureClass, or
'           ObjectClass in ArcMap.  When clicked it brings up the Version
'           Manager Form
'*******************************************************************************
Option Explicit

Implements esriSystemUI.ICommand

'Reference to ArcMap or ArcCatalog
Private m_pApp As AppRef

'If in ArcMap, this is set in the Enabled property when the
'VersionedWorkspace can be determined from the selected
'FeatureLayer, Workspace, FeatureDataset, FeatureClass, or ObjectClass
Private m_pVersionedWorkspace As IVersionedWorkspace

'If in ArcCatalog, this is set in the Enabled property when a
'Remote GxDatabase object is selected
Private m_pGxDatabase As IGxDatabase

Private Property Get ICommand_Bitmap() As esriSystem.OLE_HANDLE

End Property

Private Property Get ICommand_Caption() As String
  ICommand_Caption = "Version Manager (sample)"
End Property

Private Property Get ICommand_Category() As String
  ICommand_Category = "Developer Samples"
End Property

Private Property Get ICommand_Checked() As Boolean

End Property

Private Property Get ICommand_Enabled() As Boolean
  On Error GoTo ErrorHandler
  
  'Expect the worse, make it disabled and clear all references
  ICommand_Enabled = False
  Set m_pVersionedWorkspace = Nothing
  Set m_pGxDatabase = Nothing
  
  'Exit if not in ArcMap or ArcCatalog
  If m_pApp Is Nothing Then Exit Sub
  
  
  If TypeOf m_pApp Is IGxApplication Then 'In ArcCatalog
    Dim pGxApp As IGxApplication
    Dim pGxObject As IGxObject
    Dim pGxDatabase As IGxDatabase
    
    'Set m_pGxDatabase if remote Database is selected
    Set pGxApp = m_pApp
    Set pGxObject = pGxApp.SelectedObject
    If TypeOf pGxObject Is IGxDatabase Then
      Set pGxDatabase = pGxObject
      If pGxDatabase.IsRemoteDatabase = True Then
        Set m_pGxDatabase = pGxObject
      End If
    End If
  ElseIf TypeOf m_pApp Is IMxApplication Then 'In ArcMap
    Dim pMxDoc As IMxDocument
    Dim pUnk As IUnknown
    Dim pStandaloneTable As IStandaloneTable
    Dim pFLayer As IFeatureLayer
    Dim pDataset As IDataset
    Dim pWorkspace As IWorkspace
    
    'Set m_pVersionedWorkspace if can somehow get to
    'remote versioned workspace from the selected object
    Set pMxDoc = m_pApp.Document
    Set pUnk = pMxDoc.SelectedItem
    If pUnk Is Nothing Then
      Set pUnk = pMxDoc.SelectedLayer
    End If
    
    If Not pUnk Is Nothing Then
      If TypeOf pUnk Is IWorkspace Then
        Set pWorkspace = pUnk
      ElseIf TypeOf pUnk Is IDataset Then
        Set pDataset = pUnk
      ElseIf TypeOf pUnk Is IStandaloneTable Then
        Set pStandaloneTable = pUnk
        Set pDataset = pStandaloneTable.Table
      ElseIf TypeOf pUnk Is IFeatureLayer Then
        Set pFLayer = pUnk
        Set pDataset = pFLayer.FeatureClass
      End If
      If Not pDataset Is Nothing Then
        Set pWorkspace = pDataset.Workspace
      End If
      If Not pWorkspace Is Nothing Then
        If pWorkspace.Type = esriRemoteDatabaseWorkspace Then
          If TypeOf pWorkspace Is IVersionedWorkspace Then
            Set m_pVersionedWorkspace = pWorkspace
          End If
        End If
      End If
    End If
  End If
        
  'Enabled if have either m_pversionedworkspace or m_pGxDatabase.  Will never have both set
  ICommand_Enabled = (Not m_pVersionedWorkspace Is Nothing) Or (Not m_pGxDatabase Is Nothing)
  Exit Sub
ErrorHandler:
  MsgBox Err.Description
End Property

Private Property Get ICommand_HelpContextID() As Long

End Property

Private Property Get ICommand_HelpFile() As String

End Property

Private Property Get ICommand_Message() As String
  ICommand_Message = "Version Manager"
End Property

Private Property Get ICommand_Name() As String
  ICommand_Name = "Version Manager (sample)"
End Property

Private Sub ICommand_OnClick()
  'If we have the m_pGxDatabase set, open it up and get the
  'VersionedWorkspace if possible
  If Not m_pGxDatabase Is Nothing Then
    Dim pWorkspace As IWorkspace
    Set pWorkspace = m_pGxDatabase.Workspace
    If TypeOf pWorkspace Is IVersionedWorkspace Then
      Set m_pVersionedWorkspace = pWorkspace
    End If
  End If
  
  'Open the VersionManager if have a valid VersionedWorkspace
  If Not m_pVersionedWorkspace Is Nothing Then
    frmVersionManager.DoModal m_pVersionedWorkspace
  End If
End Sub

Private Sub ICommand_OnCreate(ByVal hook As Object)
  If TypeOf hook Is AppRef Then
    Set m_pApp = hook 'Either ArcMap or ArcCatalog supported
  End If
End Sub

Private Property Get ICommand_Tooltip() As String

End Property