Go to the documentation of this file.00001 namespace N2.Collections
00002 {
00008 public class BranchHierarchyBuilder : HierarchyBuilder
00009 {
00010 private readonly ContentItem initialItem;
00011 private readonly ContentItem lastAncestor;
00012 bool appendAdditionalLevel = false;
00013
00020 public BranchHierarchyBuilder(ContentItem initialItem, ContentItem lastAncestor, bool appendAdditionalLevel)
00021 {
00022 this.initialItem = initialItem;
00023 this.lastAncestor = lastAncestor;
00024 this.appendAdditionalLevel = appendAdditionalLevel;
00025 }
00026
00032 public BranchHierarchyBuilder(ContentItem initialItem, ContentItem lastAncestor)
00033 {
00034 this.initialItem = initialItem;
00035 this.lastAncestor = lastAncestor;
00036 }
00037
00042 public override HierarchyNode<ContentItem> Build()
00043 {
00044 if (initialItem == lastAncestor && !appendAdditionalLevel)
00045 {
00046 return new HierarchyNode<ContentItem>(initialItem);
00047 }
00048
00049 HierarchyNode<ContentItem> previousNode = null;
00050 foreach (ContentItem currentItem in Find.EnumerateParents(initialItem, lastAncestor, appendAdditionalLevel))
00051 {
00052 HierarchyNode<ContentItem> currentNode = new HierarchyNode<ContentItem>(currentItem);
00053 if (previousNode != null)
00054 {
00055 previousNode.Parent = currentNode;
00056 }
00057
00058 foreach (ContentItem childItem in GetChildren(currentItem))
00059 {
00060 if (previousNode != null && childItem.Equals(previousNode.Current))
00061 {
00062 currentNode.Children.Add(previousNode);
00063 }
00064 else
00065 {
00066 HierarchyNode<ContentItem> childNode = new HierarchyNode<ContentItem>(childItem);
00067 currentNode.Children.Add(childNode);
00068 childNode.Parent = currentNode;
00069 }
00070 }
00071 previousNode = currentNode;
00072 }
00073 return previousNode;
00074 }
00075 }
00076 }