RadItemOwnerCollection.cs
Upload User: jqzhiye
Upload Date: 2013-01-31
Package Size: 22692k
Code Size: 5k
Category:

CSharp

Development Platform:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing.Design;
  4. using System.Text;
  5. using System.Collections;
  6. namespace Telerik.WinControls
  7. {
  8.     /// <summary>
  9. ///     <para>
  10.     ///       A collection that stores <see cref='Telerik.WinControls.RadItem'/> objects.
  11. ///    </para>
  12. /// </summary>
  13. /// <seealso cref='RadItemCollection'/>
  14.     public class RadItemOwnerCollection : RadItemCollection
  15. {
  16.         private RadElement owner;
  17. /// <summary>
  18. ///     <para>
  19. ///       Initializes a new instance of <see cref='RadItemCollection'/>.
  20. ///    </para>
  21. /// </summary>
  22. public RadItemOwnerCollection(RadElement owner)
  23. {
  24. this.owner = owner;
  25. }
  26.         /// <summary>Initializes a new instance of the RadItemCollection class.</summary>
  27.         public RadItemOwnerCollection()
  28.         {
  29.         }
  30. /// <summary>
  31. ///     <para>
  32. ///       Initializes a new instance of <see cref='Telerik.WinControls.RadItemCollection'/> based on another <see cref='Telerik.WinControls.RadItemCollection'/>.
  33. ///    </para>
  34. /// </summary>
  35. /// <param name='value'>
  36. ///       A <see cref='Telerik.WinControls.RadItemCollection'/> from which the contents are copied
  37. /// </param>
  38. public RadItemOwnerCollection(RadItemOwnerCollection value) : base(value)
  39. {
  40. }
  41. /// <summary>
  42. ///     <para>
  43.         ///       Initializes a new instance of <see cref='Telerik.WinControls.RadItemCollection'/> containing any array of <see cref='Telerik.WinControls.RadItem'/> objects.
  44. ///    </para>
  45. /// </summary>
  46. /// <param name='value'>
  47.         ///       A array of <see cref='Telerik.WinControls.RadItem'/> objects with which to intialize the collection
  48. /// </param>
  49. public RadItemOwnerCollection(RadItem[] value) : base(value)
  50. {
  51. }
  52. /// <summary>
  53.         /// Gets or sets the owner of the collection.
  54.         /// </summary>
  55.         public RadElement Owner
  56.         {
  57.             get
  58.             { 
  59.                 return owner;
  60.             }
  61.             set
  62.             {
  63. if (this.owner == value)
  64. {
  65. return;
  66. }
  67. if (this.owner != null)
  68.                 {
  69. this.RemoveAllFromOwner();
  70.                 }
  71.                 this.owner = value;
  72.                 if (this.owner != null)
  73.                 {
  74.                     this.SynchronizeAllWithOwner();
  75.                 }
  76.             }
  77.         }
  78.   
  79.         private void SynchronizeAllWithOwner()
  80.         {
  81.             if (!this.Owner.UseNewLayoutSystem)
  82.     this.Owner.SuspendLayout();
  83.             foreach (RadItem element in this)
  84.             {
  85.                 if (!this.Owner.Children.Contains(element))
  86.                 {
  87.                     this.Owner.Children.Add(element);
  88.                 }
  89.             }
  90.             if (!this.Owner.UseNewLayoutSystem)
  91.     this.Owner.ResumeLayout(true);
  92.         }
  93.         private void RemoveAllFromOwner()
  94.         {
  95.             if (!this.Owner.UseNewLayoutSystem)
  96.     this.Owner.SuspendLayout();
  97.             foreach (RadItem element in this)
  98.             {
  99. int index = this.Owner.Children.IndexOf(element);
  100. if (index >= 0 )
  101. this.Owner.Children.RemoveAt(index);
  102.             }
  103.             if (!this.Owner.UseNewLayoutSystem)
  104.     this.Owner.ResumeLayout(true);
  105.         }
  106.         protected override void OnSetComplete(int index, object oldValue, object newValue)
  107.         {
  108.             if (this.Owner != null)
  109.             {
  110.                 int childIndex = this.owner.Children.IndexOf((RadItem)oldValue);
  111.                 if (childIndex >= 0)
  112.                 {
  113.                     this.owner.Children[childIndex] = (RadItem)newValue;
  114.                 }
  115.                 else
  116.                 {
  117.                     this.owner.Children.Add((RadItem)newValue);
  118.                 }
  119.             }
  120.             base.OnSetComplete(index, oldValue, newValue);
  121.         }
  122.         protected override void OnClear()
  123.         {
  124.             base.OnClear();
  125.             //Clear all the corresponding items from the children collection
  126.             if (this.Owner != null)
  127.             {
  128.                 this.RemoveAllFromOwner();
  129.             }            
  130.         }
  131. protected override void OnInsertComplete(int index, object value)
  132. {
  133.             if (this.Owner != null)
  134.             {
  135. int newIndex= index;
  136. if (newIndex > this.Owner.Children.Count)
  137. newIndex = this.Owner.Children.Count;
  138.                 this.Owner.Children.Insert(newIndex, (RadItem)value);
  139.             }
  140.             base.OnInsertComplete(index, value);
  141. }
  142. protected override void OnRemoveComplete(int index, object value)
  143. {
  144.             if (this.Owner != null)
  145.             {
  146.                 this.Owner.Children.Remove((RadItem)value);
  147.             }
  148.             base.OnRemoveComplete(index, value);
  149. }
  150.         protected override void OnSortComplete()
  151.         {
  152.             if (this.Owner != null)
  153.                 this.owner.ChangeCollection(null, ItemsChangeOperation.Sorted);
  154.             base.OnSortComplete();
  155.         }
  156. }
  157. }