File size: 5,880 Bytes
b1b3bae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
using System.Collections;
using System.ComponentModel;
namespace FarsiLibrary.Win.Helpers
{
/// <summary>
/// Represents the method that will handle the event that has no data.
/// </summary>
public delegate void CollectionClear();
/// <summary>
/// Represents the method that will handle the event that has item data.
/// </summary>
public delegate void CollectionChange(int index, object value);
/// <summary>
/// Extend collection base class by generating change events.
/// </summary>
public abstract class CollectionWithEvents : CollectionBase
{
// Instance fields
private int _suspendCount;
/// <summary>
/// Occurs just before the collection contents are cleared.
/// </summary>
[Browsable(false)]
public event CollectionClear Clearing;
/// <summary>
/// Occurs just after the collection contents are cleared.
/// </summary>
[Browsable(false)]
public event CollectionClear Cleared;
/// <summary>
/// Occurs just before an item is added to the collection.
/// </summary>
[Browsable(false)]
public event CollectionChange Inserting;
/// <summary>
/// Occurs just after an item has been added to the collection.
/// </summary>
[Browsable(false)]
public event CollectionChange Inserted;
/// <summary>
/// Occurs just before an item is removed from the collection.
/// </summary>
[Browsable(false)]
public event CollectionChange Removing;
/// <summary>
/// Occurs just after an item has been removed from the collection.
/// </summary>
[Browsable(false)]
public event CollectionChange Removed;
/// <summary>
/// Initializes DrawTab new instance of the CollectionWithEvents class.
/// </summary>
public CollectionWithEvents()
{
// Default to not suspended
_suspendCount = 0;
}
/// <summary>
/// Do not generate change events until resumed.
/// </summary>
public void SuspendEvents()
{
_suspendCount++;
}
/// <summary>
/// Safe to resume change events.
/// </summary>
public void ResumeEvents()
{
--_suspendCount;
}
/// <summary>
/// Gets DrawTab value indicating if events are currently suspended.
/// </summary>
[Browsable(false)]
public bool IsSuspended
{
get { return (_suspendCount > 0); }
}
/// <summary>
/// Raises the Clearing event when not suspended.
/// </summary>
protected override void OnClear()
{
if (!IsSuspended)
{
// Any attached event handlers?
if (Clearing != null)
Clearing();
}
}
/// <summary>
/// Raises the Cleared event when not suspended.
/// </summary>
protected override void OnClearComplete()
{
if (!IsSuspended)
{
// Any attached event handlers?
if (Cleared != null)
Cleared();
}
}
/// <summary>
/// Raises the Inserting event when not suspended.
/// </summary>
/// <param name="index">Index of object being inserted.</param>
/// <param name="value">The object that is being inserted.</param>
protected override void OnInsert(int index, object value)
{
if (!IsSuspended)
{
// Any attached event handlers?
if (Inserting != null)
Inserting(index, value);
}
}
/// <summary>
/// Raises the Inserted event when not suspended.
/// </summary>
/// <param name="index">Index of inserted object.</param>
/// <param name="value">The object that has been inserted.</param>
protected override void OnInsertComplete(int index, object value)
{
if (!IsSuspended)
{
// Any attached event handlers?
if (Inserted != null)
Inserted(index, value);
}
}
/// <summary>
/// Raises the Removing event when not suspended.
/// </summary>
/// <param name="index">Index of object being removed.</param>
/// <param name="value">The object that is being removed.</param>
protected override void OnRemove(int index, object value)
{
if (!IsSuspended)
{
// Any attached event handlers?
if (Removing != null)
Removing(index, value);
}
}
/// <summary>
/// Raises the Removed event when not suspended.
/// </summary>
/// <param name="index">Index of removed object.</param>
/// <param name="value">The object that has been removed.</param>
protected override void OnRemoveComplete(int index, object value)
{
if (!IsSuspended)
{
// Any attached event handlers?
if (Removed != null)
Removed(index, value);
}
}
/// <summary>
/// Returns the index of the first occurrence of DrawTab value.
/// </summary>
/// <param name="value">The object to locate.</param>
/// <returns>Index of object; otherwise -1</returns>
protected int IndexOf(object value)
{
// Find the 0 based index of the requested entry
return List.IndexOf(value);
}
}
}
|