External/Aga.Controls/Tree/Node.cs
changeset 345 0c551e8818e0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/Aga.Controls/Tree/Node.cs	Sun May 27 15:16:19 2012 +0000
     1.3 @@ -0,0 +1,257 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Text;
     1.7 +using System.Collections.ObjectModel;
     1.8 +using System.Windows.Forms;
     1.9 +using System.Drawing;
    1.10 +
    1.11 +namespace Aga.Controls.Tree
    1.12 +{
    1.13 +	public class Node
    1.14 +	{
    1.15 +		#region NodeCollection
    1.16 +
    1.17 +		private class NodeCollection : Collection<Node>
    1.18 +		{
    1.19 +			private Node _owner;
    1.20 +
    1.21 +			public NodeCollection(Node owner)
    1.22 +			{
    1.23 +				_owner = owner;
    1.24 +			}
    1.25 +
    1.26 +			protected override void ClearItems()
    1.27 +			{
    1.28 +				while (this.Count != 0)
    1.29 +					this.RemoveAt(this.Count - 1);
    1.30 +			}
    1.31 +
    1.32 +			protected override void InsertItem(int index, Node item)
    1.33 +			{
    1.34 +				if (item == null)
    1.35 +					throw new ArgumentNullException("item");
    1.36 +
    1.37 +				if (item.Parent != _owner)
    1.38 +				{
    1.39 +					if (item.Parent != null)
    1.40 +						item.Parent.Nodes.Remove(item);
    1.41 +					item._parent = _owner;
    1.42 +					item._index = index;
    1.43 +					for (int i = index; i < Count; i++)
    1.44 +						this[i]._index++;
    1.45 +					base.InsertItem(index, item);
    1.46 +
    1.47 +					TreeModel model = _owner.FindModel();
    1.48 +					if (model != null)
    1.49 +						model.OnNodeInserted(_owner, index, item);
    1.50 +				}
    1.51 +			}
    1.52 +
    1.53 +			protected override void RemoveItem(int index)
    1.54 +			{
    1.55 +				Node item = this[index];
    1.56 +				item._parent = null;
    1.57 +				item._index = -1;
    1.58 +				for (int i = index + 1; i < Count; i++)
    1.59 +					this[i]._index--;
    1.60 +				base.RemoveItem(index);
    1.61 +
    1.62 +				TreeModel model = _owner.FindModel();
    1.63 +				if (model != null)
    1.64 +					model.OnNodeRemoved(_owner, index, item);
    1.65 +			}
    1.66 +
    1.67 +			protected override void SetItem(int index, Node item)
    1.68 +			{
    1.69 +				if (item == null)
    1.70 +					throw new ArgumentNullException("item");
    1.71 +
    1.72 +				RemoveAt(index);
    1.73 +				InsertItem(index, item);
    1.74 +			}
    1.75 +		}
    1.76 +
    1.77 +		#endregion
    1.78 +
    1.79 +		#region Properties
    1.80 +
    1.81 +		private TreeModel _model;
    1.82 +		internal TreeModel Model
    1.83 +		{
    1.84 +			get { return _model; }
    1.85 +			set { _model = value; }
    1.86 +		}
    1.87 +
    1.88 +		private NodeCollection _nodes;
    1.89 +		public Collection<Node> Nodes
    1.90 +		{
    1.91 +			get { return _nodes; }
    1.92 +		}
    1.93 +
    1.94 +		private Node _parent;
    1.95 +		public Node Parent
    1.96 +		{
    1.97 +			get { return _parent; }
    1.98 +			set 
    1.99 +			{
   1.100 +				if (value != _parent)
   1.101 +				{
   1.102 +					if (_parent != null)
   1.103 +						_parent.Nodes.Remove(this);
   1.104 +
   1.105 +					if (value != null)
   1.106 +						value.Nodes.Add(this);
   1.107 +				}
   1.108 +			}
   1.109 +		}
   1.110 +
   1.111 +		private int _index = -1;
   1.112 +		public int Index
   1.113 +		{
   1.114 +			get
   1.115 +			{
   1.116 +				return _index;
   1.117 +			}
   1.118 +		}
   1.119 +
   1.120 +		public Node PreviousNode
   1.121 +		{
   1.122 +			get
   1.123 +			{
   1.124 +				int index = Index;
   1.125 +				if (index > 0)
   1.126 +					return _parent.Nodes[index - 1];
   1.127 +				else
   1.128 +					return null;
   1.129 +			}
   1.130 +		}
   1.131 +
   1.132 +		public Node NextNode
   1.133 +		{
   1.134 +			get
   1.135 +			{
   1.136 +				int index = Index;
   1.137 +				if (index >= 0 && index < _parent.Nodes.Count - 1)
   1.138 +					return _parent.Nodes[index + 1];
   1.139 +				else
   1.140 +					return null;
   1.141 +			}
   1.142 +		}
   1.143 +
   1.144 +		private string _text;
   1.145 +		public virtual string Text
   1.146 +		{
   1.147 +			get { return _text; }
   1.148 +			set 
   1.149 +			{
   1.150 +				if (_text != value)
   1.151 +				{
   1.152 +					_text = value;
   1.153 +					NotifyModel();
   1.154 +				}
   1.155 +			}
   1.156 +		}
   1.157 +
   1.158 +		private CheckState _checkState;
   1.159 +		public virtual CheckState CheckState
   1.160 +		{
   1.161 +			get { return _checkState; }
   1.162 +			set 
   1.163 +			{
   1.164 +				if (_checkState != value)
   1.165 +				{
   1.166 +					_checkState = value;
   1.167 +					NotifyModel();
   1.168 +				}
   1.169 +			}
   1.170 +		}
   1.171 +
   1.172 +		private Image _image;
   1.173 +		public Image Image
   1.174 +		{
   1.175 +			get { return _image; }
   1.176 +			set 
   1.177 +			{
   1.178 +				if (_image != value)
   1.179 +				{
   1.180 +					_image = value;
   1.181 +					NotifyModel();
   1.182 +				}
   1.183 +			}
   1.184 +		}
   1.185 +
   1.186 +		private object _tag;
   1.187 +		public object Tag
   1.188 +		{
   1.189 +			get { return _tag; }
   1.190 +			set { _tag = value; }
   1.191 +		}
   1.192 +
   1.193 +		public bool IsChecked
   1.194 +		{
   1.195 +			get 
   1.196 +			{ 
   1.197 +				return CheckState != CheckState.Unchecked;
   1.198 +			}
   1.199 +			set 
   1.200 +			{
   1.201 +				if (value)
   1.202 +					CheckState = CheckState.Checked;
   1.203 +				else
   1.204 +					CheckState = CheckState.Unchecked;
   1.205 +			}
   1.206 +		}
   1.207 +
   1.208 +		public virtual bool IsLeaf
   1.209 +		{
   1.210 +			get
   1.211 +			{
   1.212 +				return false;
   1.213 +			}
   1.214 +		}
   1.215 +
   1.216 +		#endregion
   1.217 +
   1.218 +		public Node()
   1.219 +			: this(string.Empty)
   1.220 +		{
   1.221 +		}
   1.222 +
   1.223 +		public Node(string text)
   1.224 +		{
   1.225 +			_text = text;
   1.226 +			_nodes = new NodeCollection(this);
   1.227 +		}
   1.228 +
   1.229 +		public override string ToString()
   1.230 +		{
   1.231 +			return Text;
   1.232 +		}
   1.233 +
   1.234 +		private TreeModel FindModel()
   1.235 +		{
   1.236 +			Node node = this;
   1.237 +			while (node != null)
   1.238 +			{
   1.239 +				if (node.Model != null)
   1.240 +					return node.Model;
   1.241 +				node = node.Parent;
   1.242 +			}
   1.243 +			return null;
   1.244 +		}
   1.245 +
   1.246 +		protected void NotifyModel()
   1.247 +		{
   1.248 +			TreeModel model = FindModel();
   1.249 +			if (model != null && Parent != null)
   1.250 +			{
   1.251 +				TreePath path = model.GetPath(Parent);
   1.252 +				if (path != null)
   1.253 +				{
   1.254 +					TreeModelEventArgs args = new TreeModelEventArgs(path, new int[] { Index }, new object[] { this });
   1.255 +					model.OnNodesChanged(args);
   1.256 +				}
   1.257 +			}
   1.258 +		}
   1.259 +	}
   1.260 +}