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