External/Aga.Controls/Tree/TreeNodeAdv.cs
changeset 345 0c551e8818e0
child 346 f652ab1e06e2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/Aga.Controls/Tree/TreeNodeAdv.cs	Sun May 27 15:16:19 2012 +0000
     1.3 @@ -0,0 +1,443 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Collections.ObjectModel;
     1.7 +using System.Runtime.Serialization;
     1.8 +using System.Security.Permissions;
     1.9 +
    1.10 +namespace Aga.Controls.Tree
    1.11 +{
    1.12 +	[Serializable]
    1.13 +	public sealed class TreeNodeAdv : ISerializable
    1.14 +	{
    1.15 +		#region NodeCollection
    1.16 +		private class NodeCollection : Collection<TreeNodeAdv>
    1.17 +		{
    1.18 +			private TreeNodeAdv _owner;
    1.19 +
    1.20 +			public NodeCollection(TreeNodeAdv owner)
    1.21 +			{
    1.22 +				_owner = owner;
    1.23 +			}
    1.24 +
    1.25 +			protected override void ClearItems()
    1.26 +			{
    1.27 +				while (this.Count != 0)
    1.28 +					this.RemoveAt(this.Count - 1);
    1.29 +			}
    1.30 +
    1.31 +			protected override void InsertItem(int index, TreeNodeAdv item)
    1.32 +			{
    1.33 +				if (item == null)
    1.34 +					throw new ArgumentNullException("item");
    1.35 +
    1.36 +				if (item.Parent != _owner)
    1.37 +				{
    1.38 +					if (item.Parent != null)
    1.39 +						item.Parent.Nodes.Remove(item);
    1.40 +					item._parent = _owner;
    1.41 +					item._index = index;
    1.42 +					for (int i = index; i < Count; i++)
    1.43 +						this[i]._index++;
    1.44 +					base.InsertItem(index, item);
    1.45 +				}
    1.46 +
    1.47 +				if (_owner.Tree != null && _owner.Tree.Model == null)
    1.48 +				{
    1.49 +					_owner.Tree.SmartFullUpdate();
    1.50 +				}
    1.51 +			}
    1.52 +
    1.53 +			protected override void RemoveItem(int index)
    1.54 +			{
    1.55 +				TreeNodeAdv 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 +				if (_owner.Tree != null && _owner.Tree.Model == null)
    1.63 +				{
    1.64 +					_owner.Tree.UpdateSelection();
    1.65 +					_owner.Tree.SmartFullUpdate();
    1.66 +				}
    1.67 +			}
    1.68 +
    1.69 +			protected override void SetItem(int index, TreeNodeAdv item)
    1.70 +			{
    1.71 +				if (item == null)
    1.72 +					throw new ArgumentNullException("item");
    1.73 +				RemoveAt(index);
    1.74 +				InsertItem(index, item);
    1.75 +			}
    1.76 +		}
    1.77 +		#endregion
    1.78 +
    1.79 +		#region Events
    1.80 +
    1.81 +		public event EventHandler<TreeViewAdvEventArgs> Collapsing;
    1.82 +		internal void OnCollapsing()
    1.83 +		{
    1.84 +			if (Collapsing != null)
    1.85 +				Collapsing(this, new TreeViewAdvEventArgs(this));
    1.86 +		}
    1.87 +
    1.88 +		public event EventHandler<TreeViewAdvEventArgs> Collapsed;
    1.89 +		internal void OnCollapsed()
    1.90 +		{
    1.91 +			if (Collapsed != null)
    1.92 +				Collapsed(this, new TreeViewAdvEventArgs(this));
    1.93 +		}
    1.94 +
    1.95 +		public event EventHandler<TreeViewAdvEventArgs> Expanding;
    1.96 +		internal void OnExpanding()
    1.97 +		{
    1.98 +			if (Expanding != null)
    1.99 +				Expanding(this, new TreeViewAdvEventArgs(this));
   1.100 +		}
   1.101 +
   1.102 +		public event EventHandler<TreeViewAdvEventArgs> Expanded;
   1.103 +		internal void OnExpanded()
   1.104 +		{
   1.105 +			if (Expanded != null)
   1.106 +				Expanded(this, new TreeViewAdvEventArgs(this));
   1.107 +		}
   1.108 +
   1.109 +		#endregion
   1.110 +
   1.111 +		#region Properties
   1.112 +
   1.113 +		private TreeViewAdv _tree;
   1.114 +		public TreeViewAdv Tree
   1.115 +		{
   1.116 +			get { return _tree; }
   1.117 +		}
   1.118 +
   1.119 +		private int _row;
   1.120 +		public int Row
   1.121 +		{
   1.122 +			get { return _row; }
   1.123 +			internal set { _row = value; }
   1.124 +		}
   1.125 +
   1.126 +		private int _index = -1;
   1.127 +		public int Index
   1.128 +		{
   1.129 +			get
   1.130 +			{
   1.131 +				return _index;
   1.132 +			}
   1.133 +		}
   1.134 +
   1.135 +		private bool _isSelected;
   1.136 +		public bool IsSelected
   1.137 +		{
   1.138 +			get { return _isSelected; }
   1.139 +			set
   1.140 +			{
   1.141 +				if (_isSelected != value)
   1.142 +				{
   1.143 +					if (Tree.IsMyNode(this))
   1.144 +					{
   1.145 +						//_tree.OnSelectionChanging
   1.146 +						if (value)
   1.147 +						{
   1.148 +							if (!_tree.Selection.Contains(this))
   1.149 +								_tree.Selection.Add(this);
   1.150 +
   1.151 +							if (_tree.Selection.Count == 1)
   1.152 +								_tree.CurrentNode = this;
   1.153 +						}
   1.154 +						else
   1.155 +							_tree.Selection.Remove(this);
   1.156 +						_tree.UpdateView();
   1.157 +						_tree.OnSelectionChanged();
   1.158 +					}
   1.159 +					_isSelected = value;
   1.160 +				}
   1.161 +			}
   1.162 +		}
   1.163 +
   1.164 +		/// <summary>
   1.165 +		/// Returns true if all parent nodes of this node are expanded.
   1.166 +		/// </summary>
   1.167 +		internal bool IsVisible
   1.168 +		{
   1.169 +			get
   1.170 +			{
   1.171 +				TreeNodeAdv node = _parent;
   1.172 +				while (node != null)
   1.173 +				{
   1.174 +					if (!node.IsExpanded)
   1.175 +						return false;
   1.176 +					node = node.Parent;
   1.177 +				}
   1.178 +				return true;
   1.179 +			}
   1.180 +		}
   1.181 +
   1.182 +		private bool _isLeaf;
   1.183 +		public bool IsLeaf
   1.184 +		{
   1.185 +			get { return _isLeaf; }
   1.186 +			internal set { _isLeaf = value; }
   1.187 +		}
   1.188 +
   1.189 +		private bool _isExpandedOnce;
   1.190 +		public bool IsExpandedOnce
   1.191 +		{
   1.192 +			get { return _isExpandedOnce; }
   1.193 +			internal set { _isExpandedOnce = value; }
   1.194 +		}
   1.195 +
   1.196 +		private bool _isExpanded;
   1.197 +		public bool IsExpanded
   1.198 +		{
   1.199 +			get { return _isExpanded; }
   1.200 +			set
   1.201 +			{
   1.202 +				if (value)
   1.203 +					Expand();
   1.204 +				else
   1.205 +					Collapse();
   1.206 +			}
   1.207 +		}
   1.208 +
   1.209 +		internal void AssignIsExpanded(bool value)
   1.210 +		{
   1.211 +			_isExpanded = value;
   1.212 +		}
   1.213 +
   1.214 +		private TreeNodeAdv _parent;
   1.215 +		public TreeNodeAdv Parent
   1.216 +		{
   1.217 +			get { return _parent; }
   1.218 +		}
   1.219 +
   1.220 +		public int Level
   1.221 +		{
   1.222 +			get
   1.223 +			{
   1.224 +				if (_parent == null)
   1.225 +					return 0;
   1.226 +				else
   1.227 +					return _parent.Level + 1;
   1.228 +			}
   1.229 +		}
   1.230 +
   1.231 +		public TreeNodeAdv PreviousNode
   1.232 +		{
   1.233 +			get
   1.234 +			{
   1.235 +				if (_parent != null)
   1.236 +				{
   1.237 +					int index = Index;
   1.238 +					if (index > 0)
   1.239 +						return _parent.Nodes[index - 1];
   1.240 +				}
   1.241 +				return null;
   1.242 +			}
   1.243 +		}
   1.244 +
   1.245 +		public TreeNodeAdv NextNode
   1.246 +		{
   1.247 +			get
   1.248 +			{
   1.249 +				if (_parent != null)
   1.250 +				{
   1.251 +					int index = Index;
   1.252 +					if (index < _parent.Nodes.Count - 1)
   1.253 +						return _parent.Nodes[index + 1];
   1.254 +				}
   1.255 +				return null;
   1.256 +			}
   1.257 +		}
   1.258 +
   1.259 +		internal TreeNodeAdv BottomNode
   1.260 +		{
   1.261 +			get
   1.262 +			{
   1.263 +				TreeNodeAdv parent = this.Parent;
   1.264 +				if (parent != null)
   1.265 +				{
   1.266 +					if (parent.NextNode != null)
   1.267 +						return parent.NextNode;
   1.268 +					else
   1.269 +						return parent.BottomNode;
   1.270 +				}
   1.271 +				return null;
   1.272 +			}
   1.273 +		}
   1.274 +
   1.275 +		internal TreeNodeAdv NextVisibleNode
   1.276 +		{
   1.277 +			get
   1.278 +			{
   1.279 +				if (IsExpanded && Nodes.Count > 0)
   1.280 +					return Nodes[0];
   1.281 +				else
   1.282 +				{
   1.283 +					TreeNodeAdv nn = NextNode;
   1.284 +					if (nn != null)
   1.285 +						return nn;
   1.286 +					else
   1.287 +						return BottomNode;
   1.288 +				}
   1.289 +			}
   1.290 +		}
   1.291 +
   1.292 +		public bool CanExpand
   1.293 +		{
   1.294 +			get
   1.295 +			{
   1.296 +				return (Nodes.Count > 0 || (!IsExpandedOnce && !IsLeaf));
   1.297 +			}
   1.298 +		}
   1.299 +
   1.300 +		private object _tag;
   1.301 +		public object Tag
   1.302 +		{
   1.303 +			get { return _tag; }
   1.304 +		}
   1.305 +
   1.306 +		private Collection<TreeNodeAdv> _nodes;
   1.307 +		internal Collection<TreeNodeAdv> Nodes
   1.308 +		{
   1.309 +			get { return _nodes; }
   1.310 +		}
   1.311 +
   1.312 +		private ReadOnlyCollection<TreeNodeAdv> _children;
   1.313 +		public ReadOnlyCollection<TreeNodeAdv> Children
   1.314 +		{
   1.315 +			get
   1.316 +			{
   1.317 +				return _children;
   1.318 +			}
   1.319 +		}
   1.320 +
   1.321 +		private int? _rightBounds;
   1.322 +		internal int? RightBounds
   1.323 +		{
   1.324 +			get { return _rightBounds; }
   1.325 +			set { _rightBounds = value; }
   1.326 +		}
   1.327 +
   1.328 +		private int? _height;
   1.329 +		internal int? Height
   1.330 +		{
   1.331 +			get { return _height; }
   1.332 +			set { _height = value; }
   1.333 +		}
   1.334 +
   1.335 +		private bool _isExpandingNow;
   1.336 +		internal bool IsExpandingNow
   1.337 +		{
   1.338 +			get { return _isExpandingNow; }
   1.339 +			set { _isExpandingNow = value; }
   1.340 +		}
   1.341 +
   1.342 +		private bool _autoExpandOnStructureChanged = false;
   1.343 +		public bool AutoExpandOnStructureChanged
   1.344 +		{
   1.345 +			get { return _autoExpandOnStructureChanged; }
   1.346 +			set { _autoExpandOnStructureChanged = value; }
   1.347 +		}
   1.348 +
   1.349 +		#endregion
   1.350 +
   1.351 +		public TreeNodeAdv(object tag)
   1.352 +			: this(null, tag)
   1.353 +		{
   1.354 +		}
   1.355 +
   1.356 +		internal TreeNodeAdv(TreeViewAdv tree, object tag)
   1.357 +		{
   1.358 +			_row = -1;
   1.359 +			_tree = tree;
   1.360 +			_nodes = new NodeCollection(this);
   1.361 +			_children = new ReadOnlyCollection<TreeNodeAdv>(_nodes);
   1.362 +			_tag = tag;
   1.363 +		}
   1.364 +
   1.365 +		public override string ToString()
   1.366 +		{
   1.367 +			if (Tag != null)
   1.368 +				return Tag.ToString();
   1.369 +			else
   1.370 +				return base.ToString();
   1.371 +		}
   1.372 +
   1.373 +		public void Collapse()
   1.374 +		{
   1.375 +			if (_isExpanded)
   1.376 +				Collapse(true);
   1.377 +		}
   1.378 +
   1.379 +		public void CollapseAll()
   1.380 +		{
   1.381 +			Collapse(false);
   1.382 +		}
   1.383 +
   1.384 +		public void Collapse(bool ignoreChildren)
   1.385 +		{
   1.386 +			SetIsExpanded(false, ignoreChildren);
   1.387 +		}
   1.388 +
   1.389 +		public void Expand()
   1.390 +		{
   1.391 +			if (!_isExpanded)
   1.392 +				Expand(true);
   1.393 +		}
   1.394 +
   1.395 +		public void ExpandAll()
   1.396 +		{
   1.397 +			Expand(false);
   1.398 +		}
   1.399 +
   1.400 +		public void Expand(bool ignoreChildren)
   1.401 +		{
   1.402 +			SetIsExpanded(true, ignoreChildren);
   1.403 +		}
   1.404 +
   1.405 +		private void SetIsExpanded(bool value, bool ignoreChildren)
   1.406 +		{
   1.407 +			if (Tree == null)
   1.408 +				_isExpanded = value;
   1.409 +			else
   1.410 +				Tree.SetIsExpanded(this, value, ignoreChildren);
   1.411 +		}
   1.412 +
   1.413 +		#region ISerializable Members
   1.414 +
   1.415 +		private TreeNodeAdv(SerializationInfo info, StreamingContext context)
   1.416 +			: this(null, null)
   1.417 +		{
   1.418 +			int nodesCount = 0;
   1.419 +			nodesCount = info.GetInt32("NodesCount");
   1.420 +			_isExpanded = info.GetBoolean("IsExpanded");
   1.421 +			_tag = info.GetValue("Tag", typeof(object));
   1.422 +
   1.423 +			for (int i = 0; i < nodesCount; i++)
   1.424 +			{
   1.425 +				TreeNodeAdv child = (TreeNodeAdv)info.GetValue("Child" + i, typeof(TreeNodeAdv));
   1.426 +				Nodes.Add(child);
   1.427 +			}
   1.428 +
   1.429 +		}
   1.430 +
   1.431 +		[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
   1.432 +		public void GetObjectData(SerializationInfo info, StreamingContext context)
   1.433 +		{
   1.434 +			info.AddValue("IsExpanded", IsExpanded);
   1.435 +			info.AddValue("NodesCount", Nodes.Count);
   1.436 +			if ((Tag != null) && Tag.GetType().IsSerializable)
   1.437 +				info.AddValue("Tag", Tag, Tag.GetType());
   1.438 +
   1.439 +			for (int i = 0; i < Nodes.Count; i++)
   1.440 +				info.AddValue("Child" + i, Nodes[i], typeof(TreeNodeAdv));
   1.441 +
   1.442 +		}
   1.443 +
   1.444 +		#endregion
   1.445 +	}
   1.446 +}