using System;
using System.Collections.Generic;
using System.Text;
using Ubik.Engine.Client;
namespace StoresAndStockPricing.Controls{
public class BoundComboBoxSource<T> : IList<BoundComboBoxItem<T>>,
System.Collections.IList where T : Individual
{
public delegate string DisplayDelegate(Session session, T t);
private Session _session;
private DisplayDelegate _displayDelegate;
private string _filter;
private List<BoundComboBoxItem<T>> _items = new List<BoundComboBoxItem<T>>();
public BoundComboBoxSource(Session session, DisplayDelegate displayDelegate)
{
if (session == null)
throw new ArgumentNullException("session");
if (displayDelegate == null)
throw new ArgumentNullException("displayDelegate");
_session = session;
_displayDelegate = displayDelegate;
}
public string Filter
{
get
{
return _filter;
}
set
{
string oldValue = _filter;
try
{
_filter = value;
Load();
}
catch (Exception)
{
_filter = oldValue;
throw;
}
}
}
private string UPathFilter
{
get
{
if (string.IsNullOrEmpty(_filter))
return typeof(T).Name;
else
return typeof(T).Name + "[" + _filter + "]";
}
}
private void Load()
{
List<BoundComboBoxItem<T>> oldItems = _items;
try
{
_items = new List<BoundComboBoxItem<T>>();
IList<Individual> newItems = _session.Select(UPathFilter);
foreach (T item in newItems)
{
_items.Add(new BoundComboBoxItem<T>(_session, _displayDelegate, item));
}
}
catch (Exception)
{
_items = oldItems;
throw;
}
}
#region IEnumerable<BindingProxy<T>> Members
public IEnumerator<BoundComboBoxItem<T>> GetEnumerator()
{
return _items.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region IList<BoundComboBoxItem<T>> Members
public int IndexOf(BoundComboBoxItem<T> item)
{
return _items.IndexOf(item);
}
public void Insert(int index, BoundComboBoxItem<T> item)
{
throw new Exception("The method or operation is not implemented.");
}
public void RemoveAt(int index)
{
throw new Exception("The method or operation is not implemented.");
}
public BoundComboBoxItem<T> this[int index]
{
get
{
return _items[index];
}
set
{
throw new Exception("The method or operation is not implemented.");
}
}
#endregion
#region ICollection<BoundComboBoxItem<T>> Members
public void Add(BoundComboBoxItem<T> item)
{
throw new Exception("The method or operation is not implemented.");
}
public void Clear()
{
throw new Exception("The method or operation is not implemented.");
}
public bool Contains(BoundComboBoxItem<T> item)
{
return _items.Contains(item);
}
public void CopyTo(BoundComboBoxItem<T>[] array, int arrayIndex)
{
_items.CopyTo(array, arrayIndex);
}
public int Count
{
get
{
return _items.Count;
}
}
public bool IsReadOnly
{
get
{
return true;
}
}
public bool Remove(BoundComboBoxItem<T> item)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
#region IList Members
int System.Collections.IList.Add(object value)
{
throw new Exception("The method or operation is not implemented.");
}
void System.Collections.IList.Clear()
{
throw new Exception("The method or operation is not implemented.");
}
bool System.Collections.IList.Contains(object value)
{
return Contains((BoundComboBoxItem<T>)value);
}
int System.Collections.IList.IndexOf(object value)
{
return IndexOf((BoundComboBoxItem<T>)value);
}
void System.Collections.IList.Insert(int index, object value)
{
throw new Exception("The method or operation is not implemented.");
}
bool System.Collections.IList.IsFixedSize
{
get
{
return true;
}
}
bool System.Collections.IList.IsReadOnly
{
get
{
return true;
}
}
void System.Collections.IList.Remove(object value)
{
throw new Exception("The method or operation is not implemented.");
}
void System.Collections.IList.RemoveAt(int index)
{
throw new Exception("The method or operation is not implemented.");
}
object System.Collections.IList.this[int index]
{
get
{
return this[index];
}
set
{
throw new Exception("The method or operation is not implemented.");
}
}
#endregion
#region ICollection Members
void System.Collections.ICollection.CopyTo(Array array, int index)
{
for (int i = index; i < _items.Count; i++)
array.SetValue(_items[i], i);
}
int System.Collections.ICollection.Count
{
get
{
return Count;
}
}
bool System.Collections.ICollection.IsSynchronized
{
get
{
return false;
}
}
object System.Collections.ICollection.SyncRoot
{
get
{
return this;
}
}
#endregion
}
}
|