VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_PredeclaredId = False
Attribute VB_Creatable = True
Attribute VB_Exposed = True
Attribute VB_GlobalNameSpace = True
Attribute VB_Name = "mpxResources"
' MPX-Resources Collection
' ========================

' Copyright (c) Mindjet LLC 2002  All Rights Reserved

Option Explicit
'#uses "MPX-Resource-X5.cls" ' Resource object Class module

Dim i_ResourceCount As Integer ' resources count
Dim o_Resources() As mpxResource ' Resource array

Private Sub Class_Initialize()
	i_ResourceCount = 0
	Erase o_Resources
End Sub

Private Sub Class_Terminate()
' Destroy resources collection
Dim i_1 As Integer
	For i_1 = 1 To i_ResourceCount
		Set o_Resources(i_1) = Nothing ' destroy resource object
	Next i_1
	Erase o_Resources
	i_ResourceCount = 0
End Sub

Function Add(ByVal s_Name As String, Optional ByVal i_ID As Long) As Integer
' Add a resource to the Resources table, unless it is already there
' Return the index number of this resource
' Returns 0 if no resource defined
Dim i_1 As Integer
Dim b_Found As Boolean

	s_Name = Trim(s_Name)
	If Len(s_Name) = 0 Then ' no resource defined to add
		Add = 0
		Exit Function
	End If
	' Check if this resource is already present
	b_Found = False
	For i_1 = 1 To i_ResourceCount
		'b_Found = (UCase(s_Name) = UCase(o_Resources(i_1).Name)) ' strict comparison
		b_Found = f_LooseCompare(s_Name, o_Resources(i_1).Name) ' looser comparison
		If b_Found Then Exit For ' leave loop
	Next i_1
	If b_Found Then
		Add = i_1 ' return pointer
	Else ' not found, add new one
		i_ResourceCount = i_ResourceCount + 1 ' count new resource
		ReDim Preserve o_Resources(1 To i_ResourceCount)
		Set o_Resources(i_ResourceCount) = New mpxResource ' assign new resource
		If IsMissing(i_ID) Then
			o_Resources(i_ResourceCount).ID = i_ResourceCount ' use internal index as ID
		ElseIf (i_ID = 0) Then
			o_Resources(i_ResourceCount).ID = i_ResourceCount ' use internal index as ID
		Else
			o_Resources(i_ResourceCount).ID = i_ID ' use externally provided ID
		End If
		o_Resources(i_ResourceCount).Name = s_Name ' write name
		o_Resources(i_ResourceCount).Initials = "" ' zero initials
		Add = i_ResourceCount ' return index
	End If

End Function

Function f_LooseCompare(ByVal s_1 As String, ByVal s_2 As String) As Boolean
' Loose comparison of resource names
' Tolerates variable spacing and punctuation

	s_1 = UCase(Replace(Trim(s_1), ".", "")) ' ignore punctuation
	s_2 = UCase(Replace(Trim(s_2), ".", ""))
	f_LooseCompare = (s_1 Like Replace(s_2, " ", "*")) Or (s_2 Like Replace(s_1, " ", "*"))

End Function

Public Function FindbyID(ByVal i_ID As Long) As Integer
' Find the index of a Resource by its ID
Dim i_1 As Integer

	FindbyID = 0 ' assume not found
	For i_1 = 1 To i_ResourceCount
		If i_ID = o_Resources(i_1).ID Then
			FindbyID = i_1 ' found it
			Exit For
		End If
	Next i_1

End Function

Property Get Item(ByVal i_index As Integer) As mpxResource
' Get an item from the Resources collection
	If (i_index < 1) Or (i_index > i_ResourceCount) Then
		Err.Raise(1000,,"Invalid Resources collection pointer",,)
	Else
		Set Item = o_Resources(i_index)
	End If
End Property

Property Get Count() As Integer
' Return number of resources in the resources collection
	Count = i_ResourceCount
End Property
