External/Aga.Controls/Tree/Node.cs
author moel.mich
Sun, 27 May 2012 15:16:19 +0000
changeset 345 0c551e8818e0
permissions -rw-r--r--
Added the source code of Aga.Controls (TreeViewAdv for .Net) version 1.7.0.0.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Collections.ObjectModel;
     5 using System.Windows.Forms;
     6 using System.Drawing;
     7 
     8 namespace Aga.Controls.Tree
     9 {
    10 	public class Node
    11 	{
    12 		#region NodeCollection
    13 
    14 		private class NodeCollection : Collection<Node>
    15 		{
    16 			private Node _owner;
    17 
    18 			public NodeCollection(Node owner)
    19 			{
    20 				_owner = owner;
    21 			}
    22 
    23 			protected override void ClearItems()
    24 			{
    25 				while (this.Count != 0)
    26 					this.RemoveAt(this.Count - 1);
    27 			}
    28 
    29 			protected override void InsertItem(int index, Node item)
    30 			{
    31 				if (item == null)
    32 					throw new ArgumentNullException("item");
    33 
    34 				if (item.Parent != _owner)
    35 				{
    36 					if (item.Parent != null)
    37 						item.Parent.Nodes.Remove(item);
    38 					item._parent = _owner;
    39 					item._index = index;
    40 					for (int i = index; i < Count; i++)
    41 						this[i]._index++;
    42 					base.InsertItem(index, item);
    43 
    44 					TreeModel model = _owner.FindModel();
    45 					if (model != null)
    46 						model.OnNodeInserted(_owner, index, item);
    47 				}
    48 			}
    49 
    50 			protected override void RemoveItem(int index)
    51 			{
    52 				Node item = this[index];
    53 				item._parent = null;
    54 				item._index = -1;
    55 				for (int i = index + 1; i < Count; i++)
    56 					this[i]._index--;
    57 				base.RemoveItem(index);
    58 
    59 				TreeModel model = _owner.FindModel();
    60 				if (model != null)
    61 					model.OnNodeRemoved(_owner, index, item);
    62 			}
    63 
    64 			protected override void SetItem(int index, Node item)
    65 			{
    66 				if (item == null)
    67 					throw new ArgumentNullException("item");
    68 
    69 				RemoveAt(index);
    70 				InsertItem(index, item);
    71 			}
    72 		}
    73 
    74 		#endregion
    75 
    76 		#region Properties
    77 
    78 		private TreeModel _model;
    79 		internal TreeModel Model
    80 		{
    81 			get { return _model; }
    82 			set { _model = value; }
    83 		}
    84 
    85 		private NodeCollection _nodes;
    86 		public Collection<Node> Nodes
    87 		{
    88 			get { return _nodes; }
    89 		}
    90 
    91 		private Node _parent;
    92 		public Node Parent
    93 		{
    94 			get { return _parent; }
    95 			set 
    96 			{
    97 				if (value != _parent)
    98 				{
    99 					if (_parent != null)
   100 						_parent.Nodes.Remove(this);
   101 
   102 					if (value != null)
   103 						value.Nodes.Add(this);
   104 				}
   105 			}
   106 		}
   107 
   108 		private int _index = -1;
   109 		public int Index
   110 		{
   111 			get
   112 			{
   113 				return _index;
   114 			}
   115 		}
   116 
   117 		public Node PreviousNode
   118 		{
   119 			get
   120 			{
   121 				int index = Index;
   122 				if (index > 0)
   123 					return _parent.Nodes[index - 1];
   124 				else
   125 					return null;
   126 			}
   127 		}
   128 
   129 		public Node NextNode
   130 		{
   131 			get
   132 			{
   133 				int index = Index;
   134 				if (index >= 0 && index < _parent.Nodes.Count - 1)
   135 					return _parent.Nodes[index + 1];
   136 				else
   137 					return null;
   138 			}
   139 		}
   140 
   141 		private string _text;
   142 		public virtual string Text
   143 		{
   144 			get { return _text; }
   145 			set 
   146 			{
   147 				if (_text != value)
   148 				{
   149 					_text = value;
   150 					NotifyModel();
   151 				}
   152 			}
   153 		}
   154 
   155 		private CheckState _checkState;
   156 		public virtual CheckState CheckState
   157 		{
   158 			get { return _checkState; }
   159 			set 
   160 			{
   161 				if (_checkState != value)
   162 				{
   163 					_checkState = value;
   164 					NotifyModel();
   165 				}
   166 			}
   167 		}
   168 
   169 		private Image _image;
   170 		public Image Image
   171 		{
   172 			get { return _image; }
   173 			set 
   174 			{
   175 				if (_image != value)
   176 				{
   177 					_image = value;
   178 					NotifyModel();
   179 				}
   180 			}
   181 		}
   182 
   183 		private object _tag;
   184 		public object Tag
   185 		{
   186 			get { return _tag; }
   187 			set { _tag = value; }
   188 		}
   189 
   190 		public bool IsChecked
   191 		{
   192 			get 
   193 			{ 
   194 				return CheckState != CheckState.Unchecked;
   195 			}
   196 			set 
   197 			{
   198 				if (value)
   199 					CheckState = CheckState.Checked;
   200 				else
   201 					CheckState = CheckState.Unchecked;
   202 			}
   203 		}
   204 
   205 		public virtual bool IsLeaf
   206 		{
   207 			get
   208 			{
   209 				return false;
   210 			}
   211 		}
   212 
   213 		#endregion
   214 
   215 		public Node()
   216 			: this(string.Empty)
   217 		{
   218 		}
   219 
   220 		public Node(string text)
   221 		{
   222 			_text = text;
   223 			_nodes = new NodeCollection(this);
   224 		}
   225 
   226 		public override string ToString()
   227 		{
   228 			return Text;
   229 		}
   230 
   231 		private TreeModel FindModel()
   232 		{
   233 			Node node = this;
   234 			while (node != null)
   235 			{
   236 				if (node.Model != null)
   237 					return node.Model;
   238 				node = node.Parent;
   239 			}
   240 			return null;
   241 		}
   242 
   243 		protected void NotifyModel()
   244 		{
   245 			TreeModel model = FindModel();
   246 			if (model != null && Parent != null)
   247 			{
   248 				TreePath path = model.GetPath(Parent);
   249 				if (path != null)
   250 				{
   251 					TreeModelEventArgs args = new TreeModelEventArgs(path, new int[] { Index }, new object[] { this });
   252 					model.OnNodesChanged(args);
   253 				}
   254 			}
   255 		}
   256 	}
   257 }