home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD14546252001.psc / AddNode.cls < prev    next >
Encoding:
Visual Basic class definition  |  2001-01-04  |  6.8 KB  |  155 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "AddNode"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = True
  14. 'Module Level Comment-----------------------------------------------------------
  15. '
  16. 'Project name   : XMLTree
  17. 'Module name    : AddNode
  18. 'Classification : Class Module
  19. 'Created        : 02 January 2001
  20. 'Author         : Paul Stevens
  21. 'Description    :
  22. '
  23. 'Dependencies   : MSXML3, MSComctlLib.Treeview
  24. 'Issues         :
  25. 'Revision(s)    : Paul Stevens
  26. '-------------------------------------------------------------------------------
  27. Private Instance As Long
  28. Public Function AddChildNode(NodeName As IXMLDOMNode, ParentNode As String, Treeview As Object, Optional IsProperty As Boolean = False, Optional Property As String, Optional AddInstance As Long)
  29. Dim ParentTree As MSComctlLib.Treeview
  30. Dim ParentName As String
  31. Dim AddChild As AddChild
  32.  
  33. Set ParentTree = Treeview
  34.  
  35. 'Because we can not determine if a node already exists in the tree
  36. 'we will try to add it and if it does not work handle the error
  37. On Error GoTo ErrHandler
  38. ParentName = ""
  39. 'First Make sure that this is not a root Node
  40. If Not ParentNode = "#document" Then
  41.         'Keep track of how many nodes we have
  42.         NodeCount = NodeCount + 1
  43.     ' if we are not adding a property
  44.     If Not IsProperty Then
  45.         'Make sure that what we are adding is really a Node Element
  46.         If NodeName.nodeType = NODE_ELEMENT Then
  47.                 AddToTree ParentTree, ParentNode, tvwChild, NodeName.NodeName, NodeName.NodeName
  48.                 'Set the current node to the one that was just added
  49.                 CurrentNode = NodeName.NodeName
  50.                 'Set the ParentNode for this instance of the class adding the node
  51.                 'To the Node that was just added (AddChild Or AddProperty Class)
  52.                 CurrentParentNode(AddInstance) = NodeName.NodeName
  53.         ElseIf NodeName.nodeType = NODE_TEXT Then
  54.             'If the Node is a Value not an elament add the value
  55.             If Not IsNumeric(NodeName.nodeValue) Then
  56.                 AddToTree ParentTree, ParentNode, tvwChild, NodeName.nodeValue, NodeName.nodeValue
  57.                 CurrentNode = CurrentParentNode(AddInstance)
  58.                 CurrentParentNode(AddInstance) = NodeName.ParentNode.ParentNode.NodeName
  59.             Else
  60.                 AddToTree ParentTree, ParentNode, tvwChild, "K" & NodeName.nodeValue, NodeName.nodeValue
  61.                 CurrentNode = CurrentParentNode(AddInstance)
  62.                 CurrentParentNode(AddInstance) = NodeName.ParentNode.ParentNode.NodeName
  63.             End If
  64.         Else
  65.             'if its neither an Element or a Vlaue then we add the Raw XML
  66.             AddToTree ParentTree, ParentNode, tvwChild, NodeName.XML, NodeName.XML
  67.                 CurrentNode = NodeName.XML
  68.                 CurrentParentNode(AddInstance) = NodeName.XML
  69.         End If
  70.     Else
  71.     'This means we are adding a property
  72.         If Not IsNumeric(Property) Then
  73.             AddToTree ParentTree, ParentNode, tvwChild, Property, Property
  74.             'We wont set the parent Node here as the Property Can not be  a Parent
  75.             If isRootChild Then
  76.                 CurrentNode = Property
  77.             End If
  78.         Else
  79.            AddToTree ParentTree, ParentNode, tvwChild, "P" & Property, Property
  80.            If isRootChild Then
  81.                 CurrentNode = "P" & Property
  82.             End If
  83.         End If
  84.     End If
  85. End If
  86.     If Not isRootChild Then
  87.         If NodeName.hasChildNodes Then
  88.             'Check if the Node has any Children, if it has instantiate a new
  89.             'Instance of AddChild and add the Child Node
  90.             Set AddChild = New AddChild
  91.             AddChild.HasChild NodeName, Treeview, CurrentParentNode(AddInstance)
  92.         End If
  93.     End If
  94. Exit Function
  95. ErrHandler:
  96.     'if an error occured the we check the error Number
  97.     'If its = 35602 that means the key already exists
  98.     'in the Tree and we need to use another key
  99.     'the Code that follows is not very pretty but it
  100.     'creates unique keys for each node it adds
  101.     'other than that it is a complete replica of the code above
  102.     If Err.Number = 35602 Then
  103.         If Not IsProperty Then
  104.              If NodeName.nodeType = NODE_ELEMENT Then
  105.                  AddToTree ParentTree, ParentNode, tvwChild, ParentNode & NodeCount & NodeName.NodeName, NodeName.NodeName
  106.             If isRootChild Then
  107.                  CurrentNode = ParentNode & NodeCount & NodeName.NodeName
  108.             End If
  109.                  CurrentParentNode(AddInstance) = ParentNode & NodeCount & NodeName.NodeName
  110.              Else
  111.                  If Not IsNumeric(NodeName.nodeValue) Then
  112.                     AddToTree ParentTree, ParentNode, tvwChild, ParentNode & NodeCount & NodeName.nodeValue, NodeName.nodeValue
  113.                     CurrentParentNode(AddInstance) = ParentNode & NodeCount & NodeName.nodeValue
  114.                     CurrentNode = CurrentParentNode(AddInstance)
  115.                     CurrentParentNode(AddInstance) = NodeName.ParentNode.ParentNode.NodeName
  116.                  Else
  117.                     AddToTree ParentTree, ParentNode, tvwChild, "K" & NodeCount & NodeName.nodeValue, NodeName.nodeValue
  118.                     CurrentParentNode(AddInstance) = "K" & NodeCount & NodeName.nodeValue
  119.                     CurrentNode = CurrentParentNode(AddInstance)
  120.                     CurrentParentNode(AddInstance) = NodeName.ParentNode.ParentNode.NodeName
  121.                  End If
  122.              End If
  123.         Else
  124.             AddToTree ParentTree, ParentNode, tvwChild, ParentNode & NodeCount & Property, Property
  125.             CurrentParentNode(AddInstance) = ParentNode & NodeCount & Property
  126.             If isRootChild Then
  127.                 CurrentNode = ParentNode & NodeCount & Property
  128.             End If
  129.         End If
  130.     If Not isRootChild Then
  131.         If NodeName.hasChildNodes Then
  132.             Set AddChild = New AddChild
  133.             AddChild.HasChild NodeName, Treeview, CurrentParentNode(AddInstance)
  134.         End If
  135.     End If
  136.     ElseIf Err.Number = 424 Then
  137.         Resume Next
  138.     End If
  139. End Function
  140. Private Sub Class_Initialize()
  141.     Instance = GlobalInstance + 1
  142.     GlobalInstance = Instance
  143.     Debug.Print Instance
  144. End Sub
  145. Private Sub Class_Terminate()
  146.     GlobalInstance = GlobalInstance - 1
  147. End Sub
  148. Private Function AddToTree(Tree As Object, Optional Relative, Optional Relationship, Optional Key, Optional Text, Optional Image, Optional SelectedImage)
  149. Dim NewNode As MSComctlLib.Treeview
  150. Set NewNode = Tree
  151. 'This adds the node to the tree
  152. NewNode.Nodes.Add Relative, Relationship, Key, Text, Image, SelectedImage
  153.  
  154. End Function
  155.