1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/External/Aga.Controls/Tree/TreeModel.cs Sun May 27 15:16:19 2012 +0000
1.3 @@ -0,0 +1,127 @@
1.4 +using System;
1.5 +using System.Collections.Generic;
1.6 +using System.Text;
1.7 +using System.Collections.ObjectModel;
1.8 +
1.9 +namespace Aga.Controls.Tree
1.10 +{
1.11 + /// <summary>
1.12 + /// Provides a simple ready to use implementation of <see cref="ITreeModel"/>. Warning: this class is not optimized
1.13 + /// to work with big amount of data. In this case create you own implementation of <c>ITreeModel</c>, and pay attention
1.14 + /// on GetChildren and IsLeaf methods.
1.15 + /// </summary>
1.16 + public class TreeModel : ITreeModel
1.17 + {
1.18 + private Node _root;
1.19 + public Node Root
1.20 + {
1.21 + get { return _root; }
1.22 + }
1.23 +
1.24 + public Collection<Node> Nodes
1.25 + {
1.26 + get { return _root.Nodes; }
1.27 + }
1.28 +
1.29 + public TreeModel()
1.30 + {
1.31 + _root = new Node();
1.32 + _root.Model = this;
1.33 + }
1.34 +
1.35 + public TreePath GetPath(Node node)
1.36 + {
1.37 + if (node == _root)
1.38 + return TreePath.Empty;
1.39 + else
1.40 + {
1.41 + Stack<object> stack = new Stack<object>();
1.42 + while (node != _root)
1.43 + {
1.44 + stack.Push(node);
1.45 + node = node.Parent;
1.46 + }
1.47 + return new TreePath(stack.ToArray());
1.48 + }
1.49 + }
1.50 +
1.51 + public Node FindNode(TreePath path)
1.52 + {
1.53 + if (path.IsEmpty())
1.54 + return _root;
1.55 + else
1.56 + return FindNode(_root, path, 0);
1.57 + }
1.58 +
1.59 + private Node FindNode(Node root, TreePath path, int level)
1.60 + {
1.61 + foreach (Node node in root.Nodes)
1.62 + if (node == path.FullPath[level])
1.63 + {
1.64 + if (level == path.FullPath.Length - 1)
1.65 + return node;
1.66 + else
1.67 + return FindNode(node, path, level + 1);
1.68 + }
1.69 + return null;
1.70 + }
1.71 +
1.72 + #region ITreeModel Members
1.73 +
1.74 + public System.Collections.IEnumerable GetChildren(TreePath treePath)
1.75 + {
1.76 + Node node = FindNode(treePath);
1.77 + if (node != null)
1.78 + foreach (Node n in node.Nodes)
1.79 + yield return n;
1.80 + else
1.81 + yield break;
1.82 + }
1.83 +
1.84 + public bool IsLeaf(TreePath treePath)
1.85 + {
1.86 + Node node = FindNode(treePath);
1.87 + if (node != null)
1.88 + return node.IsLeaf;
1.89 + else
1.90 + throw new ArgumentException("treePath");
1.91 + }
1.92 +
1.93 + public event EventHandler<TreeModelEventArgs> NodesChanged;
1.94 + internal void OnNodesChanged(TreeModelEventArgs args)
1.95 + {
1.96 + if (NodesChanged != null)
1.97 + NodesChanged(this, args);
1.98 + }
1.99 +
1.100 + public event EventHandler<TreePathEventArgs> StructureChanged;
1.101 + public void OnStructureChanged(TreePathEventArgs args)
1.102 + {
1.103 + if (StructureChanged != null)
1.104 + StructureChanged(this, args);
1.105 + }
1.106 +
1.107 + public event EventHandler<TreeModelEventArgs> NodesInserted;
1.108 + internal void OnNodeInserted(Node parent, int index, Node node)
1.109 + {
1.110 + if (NodesInserted != null)
1.111 + {
1.112 + TreeModelEventArgs args = new TreeModelEventArgs(GetPath(parent), new int[] { index }, new object[] { node });
1.113 + NodesInserted(this, args);
1.114 + }
1.115 +
1.116 + }
1.117 +
1.118 + public event EventHandler<TreeModelEventArgs> NodesRemoved;
1.119 + internal void OnNodeRemoved(Node parent, int index, Node node)
1.120 + {
1.121 + if (NodesRemoved != null)
1.122 + {
1.123 + TreeModelEventArgs args = new TreeModelEventArgs(GetPath(parent), new int[] { index }, new object[] { node });
1.124 + NodesRemoved(this, args);
1.125 + }
1.126 + }
1.127 +
1.128 + #endregion
1.129 + }
1.130 +}