SonicAudioTools/Source/SonicAudioLib/CriMw/CriFieldCollection.cs

122 lines
2.6 KiB
C#
Raw Normal View History

2016-11-12 17:02:48 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace SonicAudioLib.CriMw
{
public class CriFieldCollection : IEnumerable<CriField>
{
private CriTable parent;
private List<CriField> fields = new List<CriField>();
public CriField this[int index]
{
get
{
return fields[index];
}
}
public CriField this[string name]
{
get
{
2016-12-31 13:56:10 +01:00
return fields.FirstOrDefault(field => field.FieldName == name);
2016-11-12 17:02:48 +01:00
}
}
public int Count
{
get
{
return fields.Count;
}
}
public CriTable Parent
{
get
{
return parent;
}
internal set
{
parent = value;
}
}
public void Add(CriField criField)
{
criField.Parent = parent;
fields.Add(criField);
}
public CriField Add(string name, Type type)
{
CriField criField = new CriField(name, type);
Add(criField);
return criField;
}
public CriField Add(string name, Type type, object defaultValue)
{
CriField criField = new CriField(name, type, defaultValue);
Add(criField);
return criField;
}
public void Insert(int index, CriField criField)
{
if (index >= fields.Count || index < 0)
{
fields.Add(criField);
}
else
{
fields.Insert(index, criField);
}
}
public void Remove(CriField criField)
{
fields.Remove(criField);
2016-12-31 13:56:10 +01:00
// Update the rows
2016-11-12 17:02:48 +01:00
foreach (CriRow criRow in parent.Rows)
{
2016-12-31 13:56:10 +01:00
criRow.Records.RemoveAll(record => record.Field == criField);
2016-11-12 17:02:48 +01:00
}
}
public void RemoveAt(int index)
{
Remove(fields[index]);
}
internal void Clear()
{
fields.Clear();
}
public IEnumerator<CriField> GetEnumerator()
{
return ((IEnumerable<CriField>)fields).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<CriField>)fields).GetEnumerator();
}
public CriFieldCollection(CriTable parent)
{
this.parent = parent;
}
}
}