Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / Windows Forms

Controls Library: Extended ListView with Column Mapping

Rate me:
Please Sign up or sign in to vote.
4.86/5 (29 votes)
27 Mar 2014CPOL5 min read 82.4K   7.6K   110  
Docking windows container, extended listview, extended property editor.
using System;
using dwf.tool;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;

namespace dwf.tool
{
    public class NodeList<T> : ListExtend<T> where T : Node, new()
    {
        private int index;
        [NonSerialized()]
        private NodeGroupList groups;
        private bool sense = true;

        public NodeList(NodeGroupList groups)
            : base()
        {
            this.groups = groups;
            this.Indexes.Add("Name");
            //this.Indexes.Add("Expand");
            //this.Indexes.Add ("Visible");
        }

        public bool Sense
        {
            get { return sense; }
            set
            {
                if (sense == value)
                    return;
                sense = value;
                if (sense)
                    RaiseListChanged(ListChangedType.Reset, -1);
            }
        }

        public override void Insert(int index, T item)
        {
            int idexold = -1;
            if (index < _items.Count)
                idexold = _items[index].Order;
            item.Order = idexold;
            for (int i = index + 1; i < _items.Count; i++)
                _items[i].Order++;
            base.Insert(index, item);
        }

        public void Reorder(T node)
        {
            //Node old = newIndex < Count ? _items[newIndex] : null;
            //node.order = old == null ? ++index : old.order;
            List<T> nodes = Select("Group", node.Group, CompareType.Equal);
            nodes.Sort();
            int index = 0;
            foreach (Node n in nodes)
                n.order = index++;

        }

        public Node Add(string name, string header)
        {
            T node = new T() { Name = name, Header = header };
            Add(node);
            return node;
        }

        public override void Add(T item)
        {
            if (Contains(item))
                return;
            if (item.Order == -1)
            {
                index++;
                item.Order = index;
            }
            CheckGrop(item);
            base.Add(item);
            for (int i = 0; i < item.Childs.Count; i++)
            {
                Add((T)item.Childs[i]);
            }
        }
        
        public void CheckGrop(T item)
        {
            if (item.gname != null && item._group == null)
                item._group = SelectOne("Name", item.gname, CompareType.Equal);
            
            if(item.ngname!=null && item._nodeGroup == null)
                item._nodeGroup = groups.SelectOne("Name", item.ngname, CompareType.Equal);
        }

        public override bool Remove(T item)
        {
            bool flag = base.Remove(item);
            foreach (Node n in item.Childs)
                Remove(n);
            return flag;
        }

        public T this[string name]
        {
            get { return Find(name); }
        }

        public T Find(string Name)
        {
            List<T> buf = Select("Name", Name, CompareType.Equal);
            return buf.Count == 0 ? null : buf[0];
        }

        protected override void OnNotifyPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //
            if (e.PropertyName.Equals("Expand", StringComparison.OrdinalIgnoreCase))
            {
                RaiseListChanged(ListChangedType.Reset, -1);
                //for (int i = 0; i < node.Childs.Count; i++)
                //{
                //    var n = node.Childs[i];
                //    this.OnNotifyPropertyChanged(n, e);
                //    base.OnNotifyPropertyChanged(n, e);
                //}
            }
            else if (e.PropertyName.Equals("Visible", StringComparison.OrdinalIgnoreCase))
            {
                RaiseListChanged(ListChangedType.Reset, -1);
                //base.OnNotifyPropertyChanged(sender, e);
                //for (int i = 0; i < node.Childs.Count; i++)
                //{
                //    var n = node.Childs[i];
                //    this.OnNotifyPropertyChanged(n, e);
                //}
            }
            else if (e.PropertyName.Equals("Group", StringComparison.OrdinalIgnoreCase))
            {
                //Node node = (Node)sender;
                if (_indexes.ContainsKey(e.PropertyName))
                {
                    RemoveItemPropertyIndex((T)sender, e.PropertyName);
                    AddItemPropertyIndex((T)sender, e.PropertyName);
                }
                RaiseListChanged(ListChangedType.Reset, -1);
            }
            else
            {
                base.OnNotifyPropertyChanged(sender, e);
            }
        }

        public List<T> GetTopLevel()
        {
            List<T> nodes = Select("Group", null, CompareType.IsNull);
            return nodes;
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Kazakstan Kazakstan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions