'Draws the tree from scratch, we'll use recursion to draw this tree - it'll be a lot easier than stacking...
Private Sub DrawTree()
'Quick check to see everything's in place...
If _OccurenceTypes Is Nothing Or groupEvent Is Nothing Or groupAction Is Nothing Or groupCausality Is Nothing Then
Throw New InvalidOperationException("Occurence Codes not set.")
End If
'Dimensions...
Dim currOcc As Occurence
'Clear the controls from the controls collection...
Controls.Clear()
SuspendLayout()
nodeLine = 0
stackTop = 0
'Loop through the StartOfDay within the base of the tree and call a build routine to build the tree...
DrawNode(_StartOfDay)
'stackLevels(0) = currOcc
'DrawNode(stackLevels(0))
'Next currOcc
ResumeLayout()
'Debugging...
If _Debug Then MessageBox.Show("Controls.Count: " & Controls.Count.ToString)
End Sub
'Draws the start of the node...
Private Sub DrawNode(ByVal pOccurence As Occurence)
'Check to see if the node has been deleted first (no need to check anyfurther up the tree)...
If Not pOccurence.Deleted Then
'Build the strings...
Dim startString As String = "Start: " & pOccurence.Started.ToString("HH:mm") & " - " & pOccurence.Title
Dim descString As String = pOccurence.Description
Dim endString As String = "Ended: " & pOccurence.Ended.ToString("HH:mm")
Dim occClass As Integer = CType(pOccurence.Class.OptionValue, ehLabelType)
'Set-up the values for icons that will be drawn on the control...
Dim occHiLite As Integer = Math.Abs(CType(_Value Is pOccurence, Integer)) * hiliteStart
Dim occIsOpen As Integer = CType(pOccurence.Ended = Util.defaultDate, Integer)
Dim iconTop As Integer = occClass * iconGap + bracket.Top + occHiLite - 1
Dim iconBot As Integer = occClass * iconGap + bracket.Bottom + occHiLite + occIsOpen - 1
Dim iconLeft As Integer = stackTop * tileX
Dim textLeft As Integer = (stackTop + 2) * tileX
Dim tempGraphic As PictureBox
Dim tempLabel As Label
'Determine how much stack space we have, and whether we need to increase it...
stackLevels(stackTop) = pOccurence
If stackTop > stackLevels.GetUpperBound(0) Then
ReDim Preserve stackLevels(stackLevels.GetUpperBound(0) * 2)
End If
'Draw the start of the node...
DrawLevels(stackTop - 1)
AddHandler NewGraphic(iconTop, iconLeft, nodeLine * tileY, pOccurence).Click, AddressOf SelectNode
AddHandler NewLabel(startString, textLeft, nodeLine * tileY, pOccurence).Click, AddressOf SelectNode
AddHandler NewGraphic(iconStart, iconLeft + tileX, nodeLine * tileY, pOccurence).Click, AddressOf SelectNode
nodeLine += 1
'Draw the next detail into the tree...
If CType(pOccurence.Class.OptionValue, Integer) <> ehClassType.typeCausality Then
'Draw the bracket levels and description text...
DrawLevels(stackTop)
AddHandler NewLabel(pOccurence.Description, textLeft, nodeLine * tileY, pOccurence).Click, AddressOf SelectNode
nodeLine += 1
'Now draw any internal StartOfDay...
If pOccurence.Count > 0 Then
Dim currOcc As Occurence
stackTop += 1
For Each currOcc In pOccurence
DrawNode(currOcc)
Next
stackTop -= 1
End If
'Close the node...
DrawLevels(stackTop - 1)
tempGraphic = NewGraphic(iconEnd, (stackTop + 1) * tileX, nodeLine * tileY, pOccurence)
If pOccurence.Ended = Util.defaultDate Then
endString = "[ Open - Click stop to end ]"
AddHandler NewLabel(endString, textLeft, nodeLine * tileY, pOccurence).Click, AddressOf CloseClass
Else
AddHandler NewLabel(endString, textLeft, nodeLine * tileY, pOccurence).Click, AddressOf SelectNode
End If
AddHandler NewGraphic(iconBot, stackTop * tileX, nodeLine * tileY, pOccurence).Click, AddressOf SelectNode
AddHandler tempGraphic.Click, AddressOf SelectNode
nodeLine += 1
If _Debug Then Debug.WriteLine("Controls.Count: " & Controls.Count)
End If
End If
End Sub
'Draws the stack levels...
Private Sub DrawLevels(ByVal pBottom As Integer)
'Dimensions...
Dim loopCounter As Integer
Dim occHiLite As Integer
Dim iconMid As Integer
'Loop through the stack and add each level bar to the screen...
For loopCounter = 0 To pBottom
occHiLite = Math.Abs(CType(_Value Is stackLevels(loopCounter), Integer)) * hiliteStart
iconMid = CType(stackLevels(loopCounter).Class.OptionValue, Integer) * iconGap + bracket.Middle + occHiLite - 1
AddHandler NewGraphic(iconMid, loopCounter * tileX, nodeLine * tileY, stackLevels(loopCounter)).Click, AddressOf SelectNode
Next
End Sub
'Adds a new graphic to the form at the specified location...
Private Function NewGraphic(ByVal pType As Integer, ByVal pLocX As Integer, ByVal pLocY As Integer, ByVal pTarget As Occurence) As PictureBox
'Define and build the object...
Dim addObject As New PictureBox
Dim iType As Integer = pType
If pType >= hiliteStart Then iType -= hiliteStart
addObject.Name = "ehPic_" & iType & "_" & Me.Controls.Count()
addObject.Image = iconList.Images(pType)
addObject.Top = pLocY
addObject.Left = pLocX
addObject.Width = tileX
addObject.Height = tileY
addObject.Tag = pTarget
addObject.Cursor = Cursors.Hand
'Add it to the controls collection and increment the object counter...
If _Debug Then
Debug.WriteLine(addObject.Name & ": " & addObject.Left & ", " & addObject.Top)
End If
Me.Controls.Add(addObject)
Return addObject
End Function
'Adds a new graphic to the form at the specified location...
Private Function NewLabel(ByVal pText As String, ByVal pLocX As Integer, ByVal pLocY As Integer, ByVal pTarget As Occurence) As Label
'Define and build the object...
Dim addObject As New Label
addObject.Name = "ehLab" & Me.Controls.Count()
addObject.Left = pLocX
addObject.Top = pLocY
addObject.AutoSize = True
addObject.Tag = pTarget
addObject.Text = pText
addObject.Cursor = Cursors.Hand
'Check to see if the current node is selected...
If pTarget Is _Value Then
addObject.BackColor = hiliteColor
Else
addObject.BackColor = Color.Transparent
End If
'Check to see if the current node has been changed...
If pTarget.IsDirty Then
addObject.Font = New Font(addObject.Font, FontStyle.Bold)
End If
'Set the font colour according to the class properties...
Select Case pTarget.Class.Name
Case groupEvent.Name
addObject.ForeColor = eventColor
Case groupAction.Name
addObject.ForeColor = actionColor
Case groupCausality.Name
addObject.ForeColor = causeColor
End Select
'Add it to the controls collection and increment the object counter...
If _Debug Then Debug.WriteLine(addObject.Name & ": " & addObject.Left & ", " & addObject.Top)
Me.Controls.Add(addObject)
Return addObject
End Function