Go to the documentation of this file.00001 using System.Collections.Generic;
00002
00003 namespace N2.Collections
00004 {
00008 public class TreeHierarchyBuilder : HierarchyBuilder
00009 {
00010 private readonly ContentItem rootItem;
00011 private readonly int maxDepth;
00012
00013 public TreeHierarchyBuilder(ContentItem rootItem)
00014 : this(rootItem, int.MaxValue)
00015 {
00016 }
00017
00018 public TreeHierarchyBuilder(ContentItem rootItem, int maxDepth)
00019 {
00020 this.rootItem = rootItem;
00021 this.maxDepth = maxDepth;
00022 }
00023
00024 public override HierarchyNode<ContentItem> Build()
00025 {
00026 return BuildHierarchyTree(rootItem, maxDepth);
00027 }
00028
00029 protected virtual HierarchyNode<ContentItem> BuildHierarchyTree(ContentItem currentItem, int remainingDepth)
00030 {
00031 HierarchyNode<ContentItem> node = new HierarchyNode<ContentItem>(currentItem);
00032 if (remainingDepth > 1)
00033 {
00034 foreach (ContentItem childItem in GetChildren(currentItem))
00035 {
00036 node.Children.Add(BuildHierarchyTree(childItem, remainingDepth - 1));
00037 }
00038 }
00039 return node;
00040 }
00041 }
00042 }