/*
* VirtualListDataSource - Encapsulate how data is provided to a virtual list
*
* Author: Phillip Piper
* Date: 28/08/2009 11:10am
*
* Change log:
* v2.4
* 2010-04-01 JPP - Added IFilterableDataSource
* v2.3
* 2009-08-28 JPP - Initial version (Separated from VirtualObjectListView.cs)
*
* To do:
*
* Copyright (C) 2009-2014 Phillip Piper
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
///
/// A VirtualListDataSource is a complete manner to provide functionality to a virtual list.
/// An object that implements this interface provides a VirtualObjectListView with all the
/// information it needs to be fully functional.
///
/// Implementors must provide functioning implementations of at least GetObjectCount()
/// and GetNthObject(), otherwise nothing will appear in the list.
public interface IVirtualListDataSource
{
///
/// Return the object that should be displayed at the n'th row.
///
/// The index of the row whose object is to be returned.
/// The model object at the n'th row, or null if the fetching was unsuccessful.
Object GetNthObject(int n);
///
/// Return the number of rows that should be visible in the virtual list
///
/// The number of rows the list view should have.
int GetObjectCount();
///
/// Get the index of the row that is showing the given model object
///
/// The model object sought
/// The index of the row showing the model, or -1 if the object could not be found.
int GetObjectIndex(Object model);
///
/// The ListView is about to request the given range of items. Do
/// whatever caching seems appropriate.
///
///
///
void PrepareCache(int first, int last);
///
/// Find the first row that "matches" the given text in the given range.
///
/// The text typed by the user
/// Start searching from this index. This may be greater than the 'to' parameter,
/// in which case the search should descend
/// Do not search beyond this index. This may be less than the 'from' parameter.
/// The column that should be considered when looking for a match.
/// Return the index of row that was matched, or -1 if no match was found
int SearchText(string value, int first, int last, OLVColumn column);
///
/// Sort the model objects in the data source.
///
///
///
void Sort(OLVColumn column, SortOrder order);
//-----------------------------------------------------------------------------------
// Modification commands
// THINK: Should we split these four into a separate interface?
///
/// Add the given collection of model objects to this control.
///
/// A collection of model objects
void AddObjects(ICollection modelObjects);
///
/// Remove all of the given objects from the control
///
/// Collection of objects to be removed
void RemoveObjects(ICollection modelObjects);
///
/// Set the collection of objects that this control will show.
///
///
void SetObjects(IEnumerable collection);
///
/// Update/replace the nth object with the given object
///
///
///
void UpdateObject(int index, object modelObject);
}
///
/// This extension allow virtual lists to filter their contents
///
public interface IFilterableDataSource
{
///
/// All subsequent retrievals on this data source should be filtered
/// through the given filters. null means no filtering of that kind.
///
///
///
void ApplyFilters(IModelFilter modelFilter, IListFilter listFilter);
}
///
/// A do-nothing implementation of the VirtualListDataSource interface.
///
public class AbstractVirtualListDataSource : IVirtualListDataSource, IFilterableDataSource
{
///
/// Creates an AbstractVirtualListDataSource
///
///
public AbstractVirtualListDataSource(VirtualObjectListView listView) {
this.listView = listView;
}
///
/// The list view that this data source is giving information to.
///
protected VirtualObjectListView listView;
///
///
///
///
///
public virtual object GetNthObject(int n) {
return null;
}
///
///
///
///
public virtual int GetObjectCount() {
return -1;
}
///
///
///
///
///
public virtual int GetObjectIndex(object model) {
return -1;
}
///
///
///
///
///
public virtual void PrepareCache(int from, int to) {
}
///
///
///
///
///
///
///
///
public virtual int SearchText(string value, int first, int last, OLVColumn column) {
return -1;
}
///
///
///
///
///
public virtual void Sort(OLVColumn column, SortOrder order) {
}
///
///
///
///
public virtual void AddObjects(ICollection modelObjects) {
}
///
///
///
///
public virtual void RemoveObjects(ICollection modelObjects) {
}
///
///
///
///
public virtual void SetObjects(IEnumerable collection) {
}
///
/// Update/replace the nth object with the given object
///
///
///
public virtual void UpdateObject(int index, object modelObject) {
}
///
/// This is a useful default implementation of SearchText method, intended to be called
/// by implementors of IVirtualListDataSource.
///
///
///
///
///
///
///
static public int DefaultSearchText(string value, int first, int last, OLVColumn column, IVirtualListDataSource source) {
if (first <= last) {
for (int i = first; i <= last; i++) {
string data = column.GetStringValue(source.GetNthObject(i));
if (data.StartsWith(value, StringComparison.CurrentCultureIgnoreCase))
return i;
}
} else {
for (int i = first; i >= last; i--) {
string data = column.GetStringValue(source.GetNthObject(i));
if (data.StartsWith(value, StringComparison.CurrentCultureIgnoreCase))
return i;
}
}
return -1;
}
#region IFilterableDataSource Members
///
///
///
///
///
virtual public void ApplyFilters(IModelFilter modelFilter, IListFilter listFilter) {
}
#endregion
}
///
/// This class mimics the behavior of VirtualObjectListView v1.x.
///
public class VirtualListVersion1DataSource : AbstractVirtualListDataSource
{
///
/// Creates a VirtualListVersion1DataSource
///
///
public VirtualListVersion1DataSource(VirtualObjectListView listView)
: base(listView) {
}
#region Public properties
///
/// How will the n'th object of the data source be fetched?
///
public RowGetterDelegate RowGetter {
get { return rowGetter; }
set { rowGetter = value; }
}
private RowGetterDelegate rowGetter;
#endregion
#region IVirtualListDataSource implementation
///
///
///
///
///
public override object GetNthObject(int n) {
if (this.RowGetter == null)
return null;
else
return this.RowGetter(n);
}
///
///
///
///
///
///
///
///
public override int SearchText(string value, int first, int last, OLVColumn column) {
return DefaultSearchText(value, first, last, column, this);
}
#endregion
}
}