using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using CategoryTheory;
namespace DiagramUI
{
/// <summary>
/// The belongs to collection link
/// </summary>
[Serializable()]
public class BelongsToCollection : CategoryArrow, ISerializable, IRemovableObject
{
#region Fields
/// <summary>
/// Source
/// </summary>
private ObjectsCollection source;
/// <summary>
/// Target
/// </summary>
private ICategoryObject target;
#endregion
#region Ctor
/// <summary>
/// Default constructor
/// </summary>
public BelongsToCollection()
{
}
/// <summary>
/// Deserialization constructor
/// </summary>
/// <param name="info">Serialization info</param>
/// <param name="context">Streaming context</param>
protected BelongsToCollection(SerializationInfo info, StreamingContext context)
{
}
#endregion
#region ISerializable Members
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
}
#endregion
#region Overriden Members
/// <summary>
/// Source
/// </summary>
public override ICategoryObject Source
{
get
{
return source;
}
set
{
source = CategoryOperations.GetObject<ObjectsCollection>(value);
}
}
/// <summary>
/// Target
/// </summary>
public override ICategoryObject Target
{
get
{
return target as ICategoryObject;
}
set
{
if (Check(value))
{
return;
}
CategoryException.ThrowIllegalTargetException();
}
}
#endregion
#region IRemovableObject Members
void IRemovableObject.RemoveObject()
{
if ((source != null) & (target != null))
{
source.Remove(target);
source = null;
target = null;
}
}
#endregion
#region Members
private void Accept(ICategoryObject value)
{
target = value;
source.Add(target);
}
private bool Check(ICategoryObject value)
{
Type t = value.GetType();
Type to = source.Type;
if (t.Equals(to) | t.IsSubclassOf(to))
{
Accept(value);
return true;
}
Type[] types = t.GetInterfaces();
foreach (Type tp in types)
{
if (tp.Equals(to))
{
Accept(value);
return true;
}
}
if (value is IChildrenObject)
{
IChildrenObject ch = value as IChildrenObject;
IAssociatedObject[] ass = ch.Children;
if (ass != null)
{
foreach (object o in ass)
{
if (o is ICategoryObject)
{
ICategoryObject co = o as ICategoryObject;
if (Check(co))
{
return true;
}
}
}
}
}
return false;
}
#endregion
}
}