using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
namespace System.Windows.Forms.Classes.Collections
{
///
/// Base class for Ribbon items collections. Including Tab and Panel.
/// Implements the IList interface because this is used by the visual studio designer to add and remove items.
///
/// typeof of ribbon item
public abstract class RibbonCollectionBase : List, IList where T : IRibbonElement
{
private Ribbon _owner;
///
/// Creates a new RibbonCollectionBase
///
/// the owner ribbon
protected RibbonCollectionBase(Ribbon owner)
{
_owner = owner;
}
///
/// Gets the Ribbon that owns this collection
///
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual Ribbon Owner => _owner;
///
/// Sets the value of the Owner Property
///
internal virtual void SetOwner(Ribbon owner)
{
_owner = owner;
}
internal void SetOwner(IEnumerable items)
{
if (items != null)
{
foreach (T item in items)
{
SetOwner(item);
}
}
}
internal abstract void SetOwner(T item);
internal void ClearOwner(IEnumerable items)
{
if (items != null)
{
foreach (T item in items)
{
ClearOwner(item);
}
}
}
internal abstract void ClearOwner(T item);
internal abstract void UpdateRegions();
#region virtual new List overrides
public new virtual T this[int index]
{
get => base[index];
set
{
SetOwner(value);
base[index] = value;
UpdateRegions();
}
}
///
/// Adds the specified item to the collection
///
/// Item to add to the collection
public new virtual void Add(T item)
{
SetOwner(item);
base.Add(item);
UpdateRegions();
}
///
/// Adds the specified items to the collection
///
/// Items to add to the collection
public new virtual void AddRange(IEnumerable items)
{
SetOwner(items);
base.AddRange(items);
UpdateRegions();
}
///
/// Inserts the specified item into the specified index
///
/// Desired index of the item into the collection
/// item to be inserted
public new virtual void Insert(int index, T item)
{
SetOwner(item);
base.Insert(index, item);
UpdateRegions();
}
///
/// Removes the item from the collection.
///
/// item to remove
public new virtual bool Remove(T item)
{
if (base.Remove(item))
{
ClearOwner(item);
UpdateRegions();
return true;
}
return false;
}
///
/// Removes all items matching the given predicate.
///
/// defines the items to remove
/// number of elements removed
public new virtual int RemoveAll(Predicate predicate)
{
List toRemove = FindAll(predicate);
int ret = base.RemoveAll(predicate);
if (toRemove.Count > 0)
{
ClearOwner(toRemove);
UpdateRegions();
}
return ret;
}
///
/// Removes an item at the given index.
///
/// index of item to remove
public new virtual void RemoveAt(int index)
{
T item = this[index];
base.RemoveAt(index);
ClearOwner(item);
UpdateRegions();
}
///
/// Removes a given range from the collection
///
/// start index
/// number of items to remove
public new virtual void RemoveRange(int index, int count)
{
List toRemove = GetRange(index, count);
base.RemoveRange(index, count);
if (toRemove.Count > 0)
{
ClearOwner(toRemove);
UpdateRegions();
}
}
public new virtual void Clear()
{
List allItems = new List(this);
base.Clear();
if (allItems.Count > 0)
{
ClearOwner(allItems);
UpdateRegions();
}
}
#endregion virtual new List overrides
#region IList
object IList.this[int index]
{
get => this[index];
set => this[index] = (T)value;
}
int IList.Add(object item)
{
Add((T)item);
return Count - 1;
}
void IList.Insert(int index, object item)
{
Insert(index, (T)item);
}
void IList.Remove(object value)
{
Remove((T)value);
}
void IList.RemoveAt(int index)
{
RemoveAt(index);
}
void IList.Clear()
{
Clear();
}
#endregion
}
}