[12503] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Collections.ObjectModel;
|
---|
| 6 | using System.Collections.Specialized;
|
---|
| 7 | using System.ComponentModel;
|
---|
| 8 |
|
---|
| 9 | namespace Microsoft.Research.DynamicDataDisplay.Common
|
---|
| 10 | {
|
---|
| 11 | /// <summary>
|
---|
| 12 | /// This is a base class for some of collections in DynamicDataDisplay assembly.
|
---|
| 13 | /// It provides means to be notified when item adding and added events, which enables successors to, for example,
|
---|
| 14 | /// check if adding item is not equal to null.
|
---|
| 15 | /// </summary>
|
---|
| 16 | /// <typeparam name="T"></typeparam>
|
---|
| 17 | public abstract class D3Collection<T> : ObservableCollection<T>
|
---|
| 18 | {
|
---|
| 19 | #region Overrides
|
---|
| 20 |
|
---|
| 21 | protected override void InsertItem(int index, T item)
|
---|
| 22 | {
|
---|
| 23 | OnItemAdding(item);
|
---|
| 24 | base.InsertItem(index, item);
|
---|
| 25 | OnItemAdded(item);
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | protected override void ClearItems()
|
---|
| 29 | {
|
---|
| 30 | foreach (var item in Items)
|
---|
| 31 | {
|
---|
| 32 | OnItemRemoving(item);
|
---|
| 33 | }
|
---|
| 34 | base.ClearItems();
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | protected override void RemoveItem(int index)
|
---|
| 38 | {
|
---|
| 39 | T item = Items[index];
|
---|
| 40 | OnItemRemoving(item);
|
---|
| 41 |
|
---|
| 42 | base.RemoveItem(index);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | protected override void SetItem(int index, T item)
|
---|
| 46 | {
|
---|
| 47 | T oldItem = Items[index];
|
---|
| 48 | OnItemRemoving(oldItem);
|
---|
| 49 |
|
---|
| 50 | OnItemAdding(item);
|
---|
| 51 | base.SetItem(index, item);
|
---|
| 52 | OnItemAdded(item);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
|
---|
| 56 | {
|
---|
| 57 | attemptsToRaiseEvent++;
|
---|
| 58 | if (raiseCollectionChangedEvent)
|
---|
| 59 | {
|
---|
| 60 | base.OnCollectionChanged(e);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | #endregion // end of Overrides
|
---|
| 65 |
|
---|
| 66 | /// <summary>
|
---|
| 67 | /// Called before item added to collection. Enables to perform validation.
|
---|
| 68 | /// </summary>
|
---|
| 69 | /// <param name="item">The adding item.</param>
|
---|
| 70 | protected virtual void OnItemAdding(T item) { }
|
---|
| 71 |
|
---|
| 72 | /// <summary>
|
---|
| 73 | /// Called when item is added.
|
---|
| 74 | /// </summary>
|
---|
| 75 | /// <param name="item">The added item.</param>
|
---|
| 76 | protected virtual void OnItemAdded(T item) { }
|
---|
| 77 | /// <summary>
|
---|
| 78 | /// Called when item is being removed, but before it is actually removed.
|
---|
| 79 | /// </summary>
|
---|
| 80 | /// <param name="item">The removing item.</param>
|
---|
| 81 | protected virtual void OnItemRemoving(T item) { }
|
---|
| 82 |
|
---|
| 83 | #region Public
|
---|
| 84 |
|
---|
| 85 | int attemptsToRaiseEvent = 0;
|
---|
| 86 | bool raiseCollectionChangedEvent = true;
|
---|
| 87 |
|
---|
| 88 | public void BeginUpdate()
|
---|
| 89 | {
|
---|
| 90 | attemptsToRaiseEvent = 0;
|
---|
| 91 | raiseCollectionChangedEvent = false;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public void EndUpdate(bool raiseReset)
|
---|
| 95 | {
|
---|
| 96 | raiseCollectionChangedEvent = true;
|
---|
| 97 | if (attemptsToRaiseEvent > 0 && raiseReset)
|
---|
| 98 | {
|
---|
| 99 | OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public IDisposable BlockEvents(bool raiseReset)
|
---|
| 104 | {
|
---|
| 105 | return new EventBlocker<T>(this, raiseReset);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | private sealed class EventBlocker<TT> : IDisposable
|
---|
| 109 | {
|
---|
| 110 | private readonly D3Collection<TT> collection;
|
---|
| 111 | private readonly bool raiseReset = true;
|
---|
| 112 |
|
---|
| 113 | public EventBlocker(D3Collection<TT> collection, bool raiseReset)
|
---|
| 114 | {
|
---|
| 115 | this.collection = collection;
|
---|
| 116 | this.raiseReset = raiseReset;
|
---|
| 117 | collection.BeginUpdate();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | #region IDisposable Members
|
---|
| 121 |
|
---|
| 122 | public void Dispose()
|
---|
| 123 | {
|
---|
| 124 | collection.EndUpdate(raiseReset);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | #endregion
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | #endregion // end of Public
|
---|
| 131 | }
|
---|
| 132 | }
|
---|