External/Aga.Controls/Tree/SortedTreeModel.cs
author Stephane Lenclud
Sat, 30 Jan 2016 23:01:51 +0100
branchMiniDisplay
changeset 454 f84878f52cd9
permissions -rw-r--r--
Disabling Nuvoton NCT6791D because of fan full speed bug on Asus Z97 WS.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Collections;
     5 
     6 namespace Aga.Controls.Tree
     7 {
     8 	public class SortedTreeModel: TreeModelBase
     9 	{
    10 		private ITreeModel _innerModel;
    11 		public ITreeModel InnerModel
    12 		{
    13 			get { return _innerModel; }
    14 		}
    15 
    16 		private IComparer _comparer;
    17 		public IComparer Comparer
    18 		{
    19 			get { return _comparer; }
    20 			set 
    21 			{ 
    22 				_comparer = value;
    23 				OnStructureChanged(new TreePathEventArgs(TreePath.Empty));
    24 			}
    25 		}
    26 
    27 		public SortedTreeModel(ITreeModel innerModel)
    28 		{
    29 			_innerModel = innerModel;
    30 			_innerModel.NodesChanged += new EventHandler<TreeModelEventArgs>(_innerModel_NodesChanged);
    31 			_innerModel.NodesInserted += new EventHandler<TreeModelEventArgs>(_innerModel_NodesInserted);
    32 			_innerModel.NodesRemoved += new EventHandler<TreeModelEventArgs>(_innerModel_NodesRemoved);
    33 			_innerModel.StructureChanged += new EventHandler<TreePathEventArgs>(_innerModel_StructureChanged);
    34 		}
    35 
    36 		void _innerModel_StructureChanged(object sender, TreePathEventArgs e)
    37 		{
    38 			OnStructureChanged(e);
    39 		}
    40 
    41 		void _innerModel_NodesRemoved(object sender, TreeModelEventArgs e)
    42 		{
    43 			OnStructureChanged(new TreePathEventArgs(e.Path));
    44 		}
    45 
    46 		void _innerModel_NodesInserted(object sender, TreeModelEventArgs e)
    47 		{
    48 			OnStructureChanged(new TreePathEventArgs(e.Path));
    49 		}
    50 
    51 		void _innerModel_NodesChanged(object sender, TreeModelEventArgs e)
    52 		{
    53 			OnStructureChanged(new TreePathEventArgs(e.Path));
    54 		}
    55 
    56 		public override IEnumerable GetChildren(TreePath treePath)
    57 		{
    58 			if (Comparer != null)
    59 			{
    60 				ArrayList list = new ArrayList();
    61 				IEnumerable res = InnerModel.GetChildren(treePath);
    62 				if (res != null)
    63 				{
    64 					foreach (object obj in res)
    65 						list.Add(obj);
    66 					list.Sort(Comparer);
    67 					return list;
    68 				}
    69 				else
    70 					return null;
    71 			}
    72 			else
    73 				return InnerModel.GetChildren(treePath);
    74 		}
    75 
    76 		public override bool IsLeaf(TreePath treePath)
    77 		{
    78 			return InnerModel.IsLeaf(treePath);
    79 		}
    80 	}
    81 }