|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.ComponentModel; |
| 6 | +using System.Windows.Forms; |
| 7 | + |
| 8 | +namespace XRay.SdkControls |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Represents a SORTED collection of IntegerUpDownAcceleration objects in the NumericUpDown Control. |
| 12 | + /// The elements in the collection are sorted by the IntegerUpDownAcceleration.Seconds property. |
| 13 | + /// </summary> |
| 14 | + [ListBindable(false)] |
| 15 | + public class IntegerUpDownAccelerationCollection : |
| 16 | + MarshalByRefObject, |
| 17 | + ICollection<IntegerUpDownAcceleration>, |
| 18 | + IEnumerable<IntegerUpDownAcceleration> |
| 19 | + { |
| 20 | + private List<IntegerUpDownAcceleration> items; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Class constructor. |
| 24 | + /// </summary> |
| 25 | + public IntegerUpDownAccelerationCollection() |
| 26 | + { |
| 27 | + items = new List<IntegerUpDownAcceleration>(); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets (ReadOnly) the element at the specified index. In C#, this property is the indexer for |
| 32 | + /// the IList class. |
| 33 | + /// </summary> |
| 34 | + /// <param name="index"></param> |
| 35 | + /// <returns></returns> |
| 36 | + public IntegerUpDownAcceleration this[int index] |
| 37 | + { |
| 38 | + get |
| 39 | + { |
| 40 | + return items[index]; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // ICollection<IntegerUpDownAcceleration> implementation. |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Adds an item (IntegerUpDownAcceleration object) to the ICollection. |
| 48 | + /// The item is added preserving the collection sorted. |
| 49 | + /// </summary> |
| 50 | + /// <param name="acceleration"></param> |
| 51 | + public void Add(IntegerUpDownAcceleration acceleration) |
| 52 | + { |
| 53 | + if (acceleration == null) |
| 54 | + { |
| 55 | + throw new ArgumentNullException("acceleration"); |
| 56 | + } |
| 57 | + |
| 58 | + // Keep the array sorted, insert in the right spot. |
| 59 | + var index = 0; |
| 60 | + |
| 61 | + while (index < items.Count) |
| 62 | + { |
| 63 | + if (acceleration.Seconds < items[index].Seconds) |
| 64 | + { |
| 65 | + break; |
| 66 | + } |
| 67 | + ++index; |
| 68 | + } |
| 69 | + items.Insert(index, acceleration); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Removes all items from the ICollection. |
| 74 | + /// </summary> |
| 75 | + public void Clear() |
| 76 | + { |
| 77 | + items.Clear(); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Determines whether the IList contains a specific value. |
| 82 | + /// </summary> |
| 83 | + /// <param name="acceleration"></param> |
| 84 | + /// <returns></returns> |
| 85 | + public bool Contains(IntegerUpDownAcceleration acceleration) |
| 86 | + { |
| 87 | + return items.Contains(acceleration); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Copies the elements of the ICollection to an Array, starting at a particular Array index. |
| 92 | + /// </summary> |
| 93 | + /// <param name="array"></param> |
| 94 | + /// <param name="index"></param> |
| 95 | + public void CopyTo(IntegerUpDownAcceleration[] array, int index) |
| 96 | + { |
| 97 | + items.CopyTo(array, index); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Gets the number of elements contained in the ICollection. |
| 102 | + /// </summary> |
| 103 | + public int Count |
| 104 | + { |
| 105 | + get |
| 106 | + { |
| 107 | + return items.Count; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Gets a value indicating whether the ICollection is read-only. |
| 113 | + /// This collection property returns false always. |
| 114 | + /// </summary> |
| 115 | + public bool IsReadOnly |
| 116 | + { |
| 117 | + get |
| 118 | + { |
| 119 | + return false; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Removes the specified item from the ICollection. |
| 125 | + /// </summary> |
| 126 | + /// <param name="acceleration"></param> |
| 127 | + /// <returns></returns> |
| 128 | + public bool Remove(IntegerUpDownAcceleration acceleration) |
| 129 | + { |
| 130 | + return items.Remove(acceleration); |
| 131 | + } |
| 132 | + |
| 133 | + // IEnumerable<IntegerUpDownAcceleration> implementation. |
| 134 | + |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Returns an enumerator that can iterate through the collection. |
| 138 | + /// </summary> |
| 139 | + /// <returns></returns> |
| 140 | + IEnumerator<IntegerUpDownAcceleration> IEnumerable<IntegerUpDownAcceleration>.GetEnumerator() |
| 141 | + { |
| 142 | + return items.GetEnumerator(); |
| 143 | + } |
| 144 | + |
| 145 | + IEnumerator IEnumerable.GetEnumerator() |
| 146 | + { |
| 147 | + return ((IEnumerable)items).GetEnumerator(); |
| 148 | + } |
| 149 | + |
| 150 | + // IntegerUpDownAccelerationCollection methods. |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Adds the elements of specified array to the collection, keeping the collection sorted. |
| 154 | + /// </summary> |
| 155 | + /// <param name="accelerations"></param> |
| 156 | + public void AddRange(params IntegerUpDownAcceleration[] accelerations) |
| 157 | + { |
| 158 | + if (accelerations == null) |
| 159 | + { |
| 160 | + throw new ArgumentNullException("accelerations"); |
| 161 | + } |
| 162 | + |
| 163 | + // Accept the range only if ALL elements in the array are not null. |
| 164 | + foreach (var acceleration in accelerations) |
| 165 | + { |
| 166 | + if (acceleration == null) |
| 167 | + { |
| 168 | + throw new ArgumentNullException("At least oe entry is null"); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // The expected array size is typically small (5 items?), so we don't need to try to be smarter about the |
| 173 | + // way we add the elements to the collection, just call Add. |
| 174 | + foreach (var acceleration in accelerations) |
| 175 | + { |
| 176 | + Add(acceleration); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments