BranchHierarchyBuilder.cs :  » Content-Management-Systems-CMS » N2 » N2 » Collections » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » N2 
N2 » N2 » Collections » BranchHierarchyBuilder.cs
namespace N2.Collections{
  /// <summary>
  /// Builds a hierarchy of nodes between a certain item and one of it's 
  /// ancestors (or the root item). This is useful in certain situations when
  /// creating a navigation menu.
  /// </summary>
  public class BranchHierarchyBuilder : HierarchyBuilder
  {
    private readonly ContentItem initialItem;
    private readonly ContentItem lastAncestor;
    bool appendAdditionalLevel = false;

    /// <summary>
    /// Initializes a new instance of the <see cref="BranchHierarchyBuilder"/> class.
    /// </summary>
    /// <param name="initialItem">The initial item.</param>
    /// <param name="lastAncestor">The last ancestor.</param>
    /// <param name="appendAdditionalLevel">if set to <c>true</c> [append additional level].</param>
    public BranchHierarchyBuilder(ContentItem initialItem, ContentItem lastAncestor, bool appendAdditionalLevel)
    {
      this.initialItem = initialItem;
      this.lastAncestor = lastAncestor;
      this.appendAdditionalLevel = appendAdditionalLevel;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="BranchHierarchyBuilder"/> class.
    /// </summary>
    /// <param name="initialItem">The initial item.</param>
    /// <param name="lastAncestor">The last ancestor.</param>
    public BranchHierarchyBuilder(ContentItem initialItem, ContentItem lastAncestor)
    {
      this.initialItem = initialItem;
      this.lastAncestor = lastAncestor;
    }

    /// <summary>
    /// Builds this instance.
    /// </summary>
    /// <returns></returns>
    public override HierarchyNode<ContentItem> Build()
    {
      if (initialItem == lastAncestor && !appendAdditionalLevel)
      {
        return new HierarchyNode<ContentItem>(initialItem);
      }

      HierarchyNode<ContentItem> previousNode = null;
      foreach (ContentItem currentItem in Find.EnumerateParents(initialItem, lastAncestor, appendAdditionalLevel))
      {
        HierarchyNode<ContentItem> currentNode = new HierarchyNode<ContentItem>(currentItem);
        if (previousNode != null)
        {
          previousNode.Parent = currentNode;
        }

        foreach (ContentItem childItem in GetChildren(currentItem))
        {
          if (previousNode != null && childItem.Equals(previousNode.Current))
          {
            currentNode.Children.Add(previousNode);
          }
          else
          {
            HierarchyNode<ContentItem> childNode = new HierarchyNode<ContentItem>(childItem);
            currentNode.Children.Add(childNode);
            childNode.Parent = currentNode;
          }
        }
        previousNode = currentNode;
      }
      return previousNode;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.