using System;
using System.Collections.Generic;
using System.Linq;
using Chernobyl.Collections.Generic.Event;
using Chernobyl.Event;
using Chernobyl.Interface.Tool;
using Chernobyl.Mathematics.Mechanics;
namespace Chernobyl.Interface.Collections
{
///
/// An interface that implements and renders s. This
/// class extends the class and adds
/// the ability to move items in the list around.
///
/// The type that is to be held in the
/// .
public class ListView : CollectionListView, IList
{
///
/// Initializes a new instance of the class.
///
/// The instance that gives out services for use
/// by this type and takes services from this type for use by other
/// systems.
/// The that is to
/// be rendered on screen.
public ListView(IEventCollection services, IList list)
: this(services, list, null)
{ }
///
/// Initializes a new instance of the class.
///
/// The instance that gives out services for use
/// by this type and takes services from this type for use by other
/// systems.
/// The that is to
/// be rendered on screen.
/// The method that determines when items in
/// the list can be selected or null if the default
/// should be used
/// (The default value is given by the field
/// ). Several
/// methods are available in the
/// struct
/// or you can use your own. For performance reasons, if you don't need
/// an item to be selected, you should use
/// .
public ListView(IEventCollection services, IList list, SelectionModeFunc selectionMode)
: this(services, list, selectionMode, null)
{ }
///
/// Initializes a new instance of the class.
///
/// The instance that gives out services for use
/// by this type and takes services from this type for use by other
/// systems.
/// The that is to
/// be rendered on screen.
/// The that will be used to
/// interact with the list or null if
/// should be used.
/// The method that determines when items in
/// the list can be selected or null if the default
/// should be used
/// (The default value is given by the field
/// ). Several
/// methods are available in the
/// struct
/// or you can use your own. For performance reasons, if you don't need
/// an item to be selected, you should use
/// .
public ListView(IEventCollection services, IList list, SelectionModeFunc selectionMode, IButton button)
: base(services, list, selectionMode, button)
{
_selectedIndices = Enumerable.Empty();
List = list;
}
///
/// The 0 based index of the items that were selected or an empty
/// if nothing is selected.
///
public IEnumerable SelectedIndices
{
get
{
if (_selectedIndices == null)
_selectedIndices = from item in SelectedItems
select (uint)List.IndexOf(item);
return _selectedIndices;
}
set
{
// we already know that the SelectedItems are going to change so
// don't warn us about it.
SelectedItemsChanged -= ListViewSelectedItemsChanged;
_selectedIndices = value;
SelectedItems = from index in value
select List[(int)index];
SelectedItemsChanged += ListViewSelectedItemsChanged;
}
}
///
/// Determines the index of a specific item in the .
///
/// The object to locate in the .
/// The index of if found in the list;
/// otherwise, -1.
///
public int IndexOf(T item)
{
return List.IndexOf(item);
}
///
/// Inserts an item to the at the specified index.
///
/// The zero-based index at which
/// should be inserted.
/// The object to insert into the
/// .
///
/// is not a valid index in the .
/// The is
/// read-only.
public void Insert(int index, T item)
{
List.Insert(index, item);
}
///
/// Removes the item at the specified index.
///
/// The zero-based index of the item to remove.
///
/// is not a valid index in the .
/// The is
/// read-only.
public void RemoveAt(int index)
{
List.RemoveAt(index);
}
///
/// Gets or sets the at the specified index.
///
public T this[int index]
{
get
{
return List[index];
}
set
{
List[index] = value;
}
}
///
///An event handler that is invoked when the items in the
/// property has
/// changed.
///
/// The sender of the event.
/// The
/// instance containing the event data.
void ListViewSelectedItemsChanged(object sender, ValueChangedEventArgs> e)
{
// clear out the selected indices so that they will get recomputed in
// the get of the SelectedIndices property
_selectedIndices = null;
}
///
/// The that is viewable in the interface list.
///
IList List { get; set; }
///
/// The backing field to .
///
IEnumerable _selectedIndices;
}
}