External/Aga.Controls/Tree/TreeNodeAdv.cs
author StephaneLenclud
Tue, 03 Feb 2015 10:14:18 +0100
branchMiniDisplay
changeset 450 f2d8620e2434
parent 345 0c551e8818e0
permissions -rw-r--r--
Rebracer update.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Collections.ObjectModel;
     4 using System.Runtime.Serialization;
     5 using System.Security.Permissions;
     6 
     7 namespace Aga.Controls.Tree
     8 {
     9 	[Serializable]
    10 	public sealed class TreeNodeAdv : ISerializable
    11 	{
    12 		#region NodeCollection
    13 		private class NodeCollection : Collection<TreeNodeAdv>
    14 		{
    15 			private TreeNodeAdv _owner;
    16 
    17 			public NodeCollection(TreeNodeAdv owner)
    18 			{
    19 				_owner = owner;
    20 			}
    21 
    22 			protected override void ClearItems()
    23 			{
    24 				while (this.Count != 0)
    25 					this.RemoveAt(this.Count - 1);
    26 			}
    27 
    28 			protected override void InsertItem(int index, TreeNodeAdv item)
    29 			{
    30 				if (item == null)
    31 					throw new ArgumentNullException("item");
    32 
    33 				if (item.Parent != _owner)
    34 				{
    35 					if (item.Parent != null)
    36 						item.Parent.Nodes.Remove(item);
    37 					item._parent = _owner;
    38 					item._index = index;
    39 					for (int i = index; i < Count; i++)
    40 						this[i]._index++;
    41 					base.InsertItem(index, item);
    42 				}
    43 
    44 				if (_owner.Tree != null && _owner.Tree.Model == null)
    45 				{
    46 					_owner.Tree.SmartFullUpdate();
    47 				}
    48 			}
    49 
    50 			protected override void RemoveItem(int index)
    51 			{
    52 				TreeNodeAdv 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 				if (_owner.Tree != null && _owner.Tree.Model == null)
    60 				{
    61 					_owner.Tree.UpdateSelection();
    62 					_owner.Tree.SmartFullUpdate();
    63 				}
    64 			}
    65 
    66 			protected override void SetItem(int index, TreeNodeAdv item)
    67 			{
    68 				if (item == null)
    69 					throw new ArgumentNullException("item");
    70 				RemoveAt(index);
    71 				InsertItem(index, item);
    72 			}
    73 		}
    74 		#endregion
    75 
    76 		#region Events
    77 
    78 		public event EventHandler<TreeViewAdvEventArgs> Collapsing;
    79 		internal void OnCollapsing()
    80 		{
    81 			if (Collapsing != null)
    82 				Collapsing(this, new TreeViewAdvEventArgs(this));
    83 		}
    84 
    85 		public event EventHandler<TreeViewAdvEventArgs> Collapsed;
    86 		internal void OnCollapsed()
    87 		{
    88 			if (Collapsed != null)
    89 				Collapsed(this, new TreeViewAdvEventArgs(this));
    90 		}
    91 
    92 		public event EventHandler<TreeViewAdvEventArgs> Expanding;
    93 		internal void OnExpanding()
    94 		{
    95 			if (Expanding != null)
    96 				Expanding(this, new TreeViewAdvEventArgs(this));
    97 		}
    98 
    99 		public event EventHandler<TreeViewAdvEventArgs> Expanded;
   100 		internal void OnExpanded()
   101 		{
   102 			if (Expanded != null)
   103 				Expanded(this, new TreeViewAdvEventArgs(this));
   104 		}
   105 
   106 		#endregion
   107 
   108 		#region Properties
   109 
   110 		private TreeViewAdv _tree;
   111 		public TreeViewAdv Tree
   112 		{
   113 			get { return _tree; }
   114 		}
   115 
   116 		private int _row;
   117 		public int Row
   118 		{
   119 			get { return _row; }
   120 			internal set { _row = value; }
   121 		}
   122 
   123 		private int _index = -1;
   124 		public int Index
   125 		{
   126 			get
   127 			{
   128 				return _index;
   129 			}
   130 		}
   131 
   132 		private bool _isSelected;
   133 		public bool IsSelected
   134 		{
   135 			get { return _isSelected; }
   136 			set
   137 			{
   138 				if (_isSelected != value)
   139 				{
   140 					if (Tree.IsMyNode(this))
   141 					{
   142 						//_tree.OnSelectionChanging
   143 						if (value)
   144 						{
   145 							if (!_tree.Selection.Contains(this))
   146 								_tree.Selection.Add(this);
   147 
   148 							if (_tree.Selection.Count == 1)
   149 								_tree.CurrentNode = this;
   150 						}
   151 						else
   152 							_tree.Selection.Remove(this);
   153 						_tree.UpdateView();
   154 						_tree.OnSelectionChanged();
   155 					}
   156 					_isSelected = value;
   157 				}
   158 			}
   159 		}
   160 
   161 		/// <summary>
   162 		/// Returns true if all parent nodes of this node are expanded.
   163 		/// </summary>
   164 		internal bool IsVisible
   165 		{
   166 			get
   167 			{
   168 				TreeNodeAdv node = _parent;
   169 				while (node != null)
   170 				{
   171 					if (!node.IsExpanded)
   172 						return false;
   173 					node = node.Parent;
   174 				}
   175 				return true;
   176 			}
   177 		}
   178 
   179 		private bool _isLeaf;
   180 		public bool IsLeaf
   181 		{
   182 			get { return _isLeaf; }
   183 			internal set { _isLeaf = value; }
   184 		}
   185 
   186 		private bool _isExpandedOnce;
   187 		public bool IsExpandedOnce
   188 		{
   189 			get { return _isExpandedOnce; }
   190 			internal set { _isExpandedOnce = value; }
   191 		}
   192 
   193 		private bool _isExpanded;
   194 		public bool IsExpanded
   195 		{
   196 			get { return _isExpanded; }
   197 			set
   198 			{
   199 				if (value)
   200 					Expand();
   201 				else
   202 					Collapse();
   203 			}
   204 		}
   205 
   206 		internal void AssignIsExpanded(bool value)
   207 		{
   208 			_isExpanded = value;
   209 		}
   210 
   211 		private TreeNodeAdv _parent;
   212 		public TreeNodeAdv Parent
   213 		{
   214 			get { return _parent; }
   215 		}
   216 
   217 		public int Level
   218 		{
   219 			get
   220 			{
   221 				if (_parent == null)
   222 					return 0;
   223 				else
   224 					return _parent.Level + 1;
   225 			}
   226 		}
   227 
   228 		public TreeNodeAdv PreviousNode
   229 		{
   230 			get
   231 			{
   232 				if (_parent != null)
   233 				{
   234 					int index = Index;
   235 					if (index > 0)
   236 						return _parent.Nodes[index - 1];
   237 				}
   238 				return null;
   239 			}
   240 		}
   241 
   242 		public TreeNodeAdv NextNode
   243 		{
   244 			get
   245 			{
   246 				if (_parent != null)
   247 				{
   248 					int index = Index;
   249 					if (index < _parent.Nodes.Count - 1)
   250 						return _parent.Nodes[index + 1];
   251 				}
   252 				return null;
   253 			}
   254 		}
   255 
   256 		internal TreeNodeAdv BottomNode
   257 		{
   258 			get
   259 			{
   260 				TreeNodeAdv parent = this.Parent;
   261 				if (parent != null)
   262 				{
   263 					if (parent.NextNode != null)
   264 						return parent.NextNode;
   265 					else
   266 						return parent.BottomNode;
   267 				}
   268 				return null;
   269 			}
   270 		}
   271 
   272 		internal TreeNodeAdv NextVisibleNode
   273 		{
   274 			get
   275 			{
   276 				if (IsExpanded && Nodes.Count > 0)
   277 					return Nodes[0];
   278 				else
   279 				{
   280 					TreeNodeAdv nn = NextNode;
   281 					if (nn != null)
   282 						return nn;
   283 					else
   284 						return BottomNode;
   285 				}
   286 			}
   287 		}
   288 
   289 		public bool CanExpand
   290 		{
   291 			get
   292 			{
   293 				return (Nodes.Count > 0 || (!IsExpandedOnce && !IsLeaf));
   294 			}
   295 		}
   296 
   297 		private object _tag;
   298 		public object Tag
   299 		{
   300 			get { return _tag; }
   301 		}
   302 
   303 		private Collection<TreeNodeAdv> _nodes;
   304 		internal Collection<TreeNodeAdv> Nodes
   305 		{
   306 			get { return _nodes; }
   307 		}
   308 
   309 		private ReadOnlyCollection<TreeNodeAdv> _children;
   310 		public ReadOnlyCollection<TreeNodeAdv> Children
   311 		{
   312 			get
   313 			{
   314 				return _children;
   315 			}
   316 		}
   317 
   318 		private int? _rightBounds;
   319 		internal int? RightBounds
   320 		{
   321 			get { return _rightBounds; }
   322 			set { _rightBounds = value; }
   323 		}
   324 
   325 		private int? _height;
   326 		internal int? Height
   327 		{
   328 			get { return _height; }
   329 			set { _height = value; }
   330 		}
   331 
   332 		private bool _isExpandingNow;
   333 		internal bool IsExpandingNow
   334 		{
   335 			get { return _isExpandingNow; }
   336 			set { _isExpandingNow = value; }
   337 		}
   338 
   339 		private bool _autoExpandOnStructureChanged = true;
   340 		public bool AutoExpandOnStructureChanged
   341 		{
   342 			get { return _autoExpandOnStructureChanged; }
   343 			set { _autoExpandOnStructureChanged = value; }
   344 		}
   345 
   346 		#endregion
   347 
   348 		public TreeNodeAdv(object tag)
   349 			: this(null, tag)
   350 		{
   351 		}
   352 
   353 		internal TreeNodeAdv(TreeViewAdv tree, object tag)
   354 		{
   355 			_row = -1;
   356 			_tree = tree;
   357 			_nodes = new NodeCollection(this);
   358 			_children = new ReadOnlyCollection<TreeNodeAdv>(_nodes);
   359 			_tag = tag;
   360 		}
   361 
   362 		public override string ToString()
   363 		{
   364 			if (Tag != null)
   365 				return Tag.ToString();
   366 			else
   367 				return base.ToString();
   368 		}
   369 
   370 		public void Collapse()
   371 		{
   372 			if (_isExpanded)
   373 				Collapse(true);
   374 		}
   375 
   376 		public void CollapseAll()
   377 		{
   378 			Collapse(false);
   379 		}
   380 
   381 		public void Collapse(bool ignoreChildren)
   382 		{
   383 			SetIsExpanded(false, ignoreChildren);
   384 		}
   385 
   386 		public void Expand()
   387 		{
   388 			if (!_isExpanded)
   389 				Expand(true);
   390 		}
   391 
   392 		public void ExpandAll()
   393 		{
   394 			Expand(false);
   395 		}
   396 
   397 		public void Expand(bool ignoreChildren)
   398 		{
   399 			SetIsExpanded(true, ignoreChildren);
   400 		}
   401 
   402 		private void SetIsExpanded(bool value, bool ignoreChildren)
   403 		{
   404 			if (Tree == null)
   405 				_isExpanded = value;
   406 			else
   407 				Tree.SetIsExpanded(this, value, ignoreChildren);
   408 		}
   409 
   410 		#region ISerializable Members
   411 
   412 		private TreeNodeAdv(SerializationInfo info, StreamingContext context)
   413 			: this(null, null)
   414 		{
   415 			int nodesCount = 0;
   416 			nodesCount = info.GetInt32("NodesCount");
   417 			_isExpanded = info.GetBoolean("IsExpanded");
   418 			_tag = info.GetValue("Tag", typeof(object));
   419 
   420 			for (int i = 0; i < nodesCount; i++)
   421 			{
   422 				TreeNodeAdv child = (TreeNodeAdv)info.GetValue("Child" + i, typeof(TreeNodeAdv));
   423 				Nodes.Add(child);
   424 			}
   425 
   426 		}
   427 
   428 		[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
   429 		public void GetObjectData(SerializationInfo info, StreamingContext context)
   430 		{
   431 			info.AddValue("IsExpanded", IsExpanded);
   432 			info.AddValue("NodesCount", Nodes.Count);
   433 			if ((Tag != null) && Tag.GetType().IsSerializable)
   434 				info.AddValue("Tag", Tag, Tag.GetType());
   435 
   436 			for (int i = 0; i < Nodes.Count; i++)
   437 				info.AddValue("Child" + i, Nodes[i], typeof(TreeNodeAdv));
   438 
   439 		}
   440 
   441 		#endregion
   442 	}
   443 }