2012-08-16 07:30:59 +00:00
|
|
|
|
#region Using Statements
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
|
|
|
|
|
|
|
|
|
namespace ANX.Framework.Content.Pipeline
|
|
|
|
|
{
|
|
|
|
|
public abstract class ChildCollection<TParent, TChild> : Collection<TChild> where TParent : class where TChild : class
|
|
|
|
|
{
|
2012-08-25 21:47:03 +00:00
|
|
|
|
TParent parent;
|
|
|
|
|
|
2012-08-16 07:30:59 +00:00
|
|
|
|
protected ChildCollection(TParent parent)
|
|
|
|
|
{
|
2012-08-25 21:47:03 +00:00
|
|
|
|
if (parent == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("parent");
|
|
|
|
|
}
|
|
|
|
|
this.parent = parent;
|
2012-08-16 07:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 14:50:03 +02:00
|
|
|
|
public void AddRange(IEnumerable<TChild> enumerable)
|
|
|
|
|
{
|
|
|
|
|
if (enumerable == null)
|
|
|
|
|
throw new ArgumentNullException("enumerable");
|
|
|
|
|
|
|
|
|
|
foreach (var child in enumerable)
|
|
|
|
|
{
|
|
|
|
|
this.Add(child);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-16 07:30:59 +00:00
|
|
|
|
protected override void ClearItems()
|
|
|
|
|
{
|
2012-08-25 21:47:03 +00:00
|
|
|
|
foreach (var child in this)
|
|
|
|
|
{
|
|
|
|
|
this.SetParent(child, default(TParent));
|
|
|
|
|
}
|
2012-08-16 07:30:59 +00:00
|
|
|
|
base.ClearItems();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract TParent GetParent(TChild child);
|
|
|
|
|
|
|
|
|
|
protected override void InsertItem(int index, TChild item)
|
|
|
|
|
{
|
2012-08-25 21:47:03 +00:00
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
if (this.GetParent(item) != null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("item already has a parent item");
|
|
|
|
|
}
|
2012-08-16 07:30:59 +00:00
|
|
|
|
base.InsertItem(index, item);
|
2012-08-25 21:47:03 +00:00
|
|
|
|
this.SetParent(item, this.parent);
|
2012-08-16 07:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void RemoveItem(int index)
|
|
|
|
|
{
|
2012-08-25 21:47:03 +00:00
|
|
|
|
var child = base[index];
|
|
|
|
|
this.SetParent(child, default(TParent));
|
2012-08-16 07:30:59 +00:00
|
|
|
|
base.RemoveItem(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void SetItem(int index, TChild item)
|
|
|
|
|
{
|
2012-08-25 21:47:03 +00:00
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var child = base[index];
|
|
|
|
|
if (child == item)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-08-16 07:30:59 +00:00
|
|
|
|
|
2012-08-25 21:47:03 +00:00
|
|
|
|
if (this.GetParent(item) != null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("item already has a parent item");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.SetItem(index, item);
|
|
|
|
|
this.SetParent(item, parent);
|
|
|
|
|
this.SetParent(child, default(TParent));
|
2012-08-16 07:30:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void SetParent(TChild child, TParent parent);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|