/// <summary>
 /// Setup constructor for a sub category
 /// </summary>
 /// <param name="parentCategory">Parent category</param>
 /// <param name="name">Category name</param>
 /// <param name="description">Category description</param>
 /// <param name="icon">Category icon</param>
 /// <exception cref="ArgumentNullException">Thrown if name, description or icon is null</exception>
 /// <exception cref="ArgumentException">Thrown if name is an empty string</exception>
 public Category( Category parentCategory, string name, string description, Icon icon )
     : base(name, description, icon)
 {
     if ( parentCategory != null )
     {
         parentCategory.AddSubCategory( this );
     }
 }
 /// <summary>
 /// Adds a category to the control
 /// </summary>
 /// <param name="category">Category to add</param>
 /// <returns>Returns category</returns>
 public Category AddCategory( Category category )
 {
     Arguments.CheckNotNull( category, "category" );
     if ( category.ParentCategory != null )
     {
         throw new ArgumentException( "Cannot add non-root category" );
     }
     categoryView.Nodes.Add( CreateNodeForCategory( category ) );
     m_RootCategories.Add( category );
     return category;
 }
 /// <summary>
 /// Changes the current parent category
 /// </summary>
 internal override bool ChangeParentCategory( Category parentCategory )
 {
     if ( ParentCategory == parentCategory )
     {
         return false;
     }
     if ( ParentCategory != null )
     {
         ParentCategory.m_SubCategories.Remove( this );
         if ( ParentCategory.SubCategoryRemoved != null )
         {
             ParentCategory.SubCategoryRemoved( this );
         }
     }
     base.ChangeParentCategory( parentCategory );
     if ( ParentCategory != null )
     {
         ParentCategory.m_SubCategories.Add( this );
         if ( ParentCategory.SubCategoryAdded != null )
         {
             ParentCategory.SubCategoryAdded( this );
         }
     }
     return true;
 }
 /// <summary>
 /// Removes a sub-category from this category
 /// </summary>
 /// <param name="category">Category to remove</param>
 public void RemoveSubCategory( Category category )
 {
     Arguments.CheckNotNull( category, "category" );
     if ( category.ParentCategory != this )
     {
         throw new ArgumentException( string.Format( "Category \"{0}\" was not a sub-category of \"{1}\", and cannot be removed", category.Name, Name ), "category" );
     }
     category.ChangeParentCategory( null );
 }
 /// <summary>
 /// Adds a sub-category to this category
 /// </summary>
 /// <param name="category">Category to add</param>
 public void AddSubCategory( Category category )
 {
     Arguments.CheckNotNull( category, "category" );
     category.ChangeParentCategory( this );
 }
 /// <summary>
 /// Changes the current parent category
 /// </summary>
 internal virtual bool ChangeParentCategory( Category parentCategory )
 {
     m_ParentCategory = parentCategory;
     return true;
 }
        /// <summary>
        /// Filters a category
        /// </summary>
        private TreeNode FilterCategory( Category category, Regex filter )
        {
            TreeNode curNode = null;

            //	Run through all the subcategories of the current category, and recursively filter them
            foreach ( Category subCategory in category.SubCategories )
            {
                TreeNode subNode = FilterCategory( subCategory, filter );
                if ( subNode != null )
                {
                    //	subNode is not null, which means that the sub-category contained unfiltered items
                    //	Get or create the current category's node, and add the sub-category node to it
                    if ( curNode == null )
                    {
                        curNode = m_NodeMap[ category ] ?? CreateNodeForCategoryItem( category );
                    }
                    if ( subNode.Parent != curNode )
                    {
                        curNode.Nodes.Add( subNode );
                    }
                }
            }

            //	Run through all the items in the current category, and filter them
            foreach ( CategoryItem item in category.Items )
            {
                TreeNode itemNode = FilterItem( item, filter );
                if ( itemNode != null )
                {
                    if ( curNode == null )
                    {
                        curNode = m_NodeMap[ category ] ?? CreateNodeForCategoryItem( category );
                    }
                    if ( itemNode.Parent != curNode )
                    {
                        curNode.Nodes.Add( itemNode );
                    }
                }
            }

            if ( curNode == null )
            {
                //	Current node was never created, meaning that all items and sub-categories it
                //	contained were filtered. Make sure it is removed
                curNode = m_NodeMap[ category ];
                if ( curNode != null )
                {
                    curNode.Remove( );
                    m_NodeMap[ category ] = null;
                }
                return null;
            }
            curNode.Expand( );
            return curNode;
        }
        /// <summary>
        /// Creates a tree node for a category
        /// </summary>
        private TreeNode CreateNodeForCategory( Category category )
        {
            TreeNode node = CreateNodeForCategoryItem( category );
            foreach ( Category subCategory in category.SubCategories )
            {
                node.Nodes.Add( CreateNodeForCategory( subCategory ) );
            }
            foreach ( CategoryItem item in category.Items )
            {
                node.Nodes.Add( CreateNodeForCategoryItem( item ) );
            }
            node.ExpandAll( );

            return node;
        }