///
/// Copyright © 2003-2008 JetBrains s.r.o.
/// You may distribute under the terms of the GNU General Public License, as published by the Free Software Foundation, version 2 (see License.txt in the repository root folder).
///
using System;
using System.Collections;
using System.Text;
namespace JetBrains.Omea.OpenAPI
{
///
/// Describes the way a resource list is sorted.
///
/// 2.0
public class SortSettings
{
private int[] _sortProps;
private bool[] _sortDirections;
///
/// Creates the sort settings which describe an empty list sort.
///
public SortSettings()
{
_sortProps = new int[] {};
_sortDirections = new bool[] {};
}
///
/// Creates the sort settings with the specified list of sort property IDs and sort directions
/// for each property ID.
///
/// The IDs of properties by which the resource list is sorted.
/// The sort direction for each property.
public SortSettings( int[] sortProps, bool[] sortDirections )
{
if ( sortProps.Length != sortDirections.Length )
{
throw new ArgumentException( "The length of the property IDs array must be equal to the length of the sort directions array." );
}
_sortProps = sortProps;
_sortDirections = sortDirections;
}
///
/// Creates the sort settings with the specified list of sort property IDs and the same direction
/// for each sort property,
///
/// The IDs of properties by which the resource list is sorted.
/// The sort direction.
public SortSettings( int[] sortProps, bool sortAscending )
{
_sortProps = sortProps;
_sortDirections = new bool [sortProps.Length];
for( int i=0; i<_sortDirections.Length; i++ )
{
_sortDirections [i] = sortAscending;
}
}
///
/// Creates the sort settings for sorting by a single property.
///
/// The ID of the property by which the resource list is sorted.
/// The direction of sorting.
public SortSettings( int sortProp, bool sortAsc )
{
_sortProps = new int[] { sortProp };
_sortDirections = new bool[] { sortAsc };
}
///
/// A constructor that implements the function behavior.
///
/// The ResourceStore instance used to retrieve the property IDs.
/// The string to parse.
protected SortSettings(IResourceStore resourceStore, string sortProp)
{
ArrayList propNameArray = new ArrayList( sortProp.Split( ' ' ) );
// remove multiple spaces
for( int i=propNameArray.Count-1; i >= 0; i-- )
{
if ( ((string) propNameArray [i]).Trim() == "" )
propNameArray.RemoveAt( i );
}
_sortProps = new int [propNameArray.Count];
_sortDirections = new bool [propNameArray.Count];
for( int i=0; i
/// Returns the array of property IDs by which the list is sorted.
///
public int[] SortProps
{
get { return _sortProps; }
}
///
/// Returns the array of sort directions for each property by which the list is sorted.
/// True stands for ascending.
///
public bool[] SortDirections
{
get { return _sortDirections; }
}
///
/// Returns true if the first column used to sort the list has ascending sort order.
///
public bool SortAscending
{
get { return _sortProps.Length > 0 && _sortDirections [0]; }
}
///
/// Parses the specified string containing a space-separated list of property names.
///
/// The ResourceStore instance used to retrieve the property IDs.
/// The string to parse.
/// The sort settings.
public static SortSettings Parse( IResourceStore resourceStore, string sortProp )
{
return new SortSettings(resourceStore, sortProp);
}
///
/// Returns the space-separated list of property names by which the list is sorted.
///
/// The ResourceStore instance used to retrieve the property names.
/// The property name list.
public string ToString( IResourceStore resourceStore )
{
StringBuilder result = new StringBuilder();
foreach( int propId in _sortProps )
{
if ( result.Length > 0 )
{
result.Append( " " );
}
result.Append( resourceStore.PropTypes [propId].DisplayName );
}
return result.ToString();
}
///
/// Returns a copy of the sort settings with sort directions for all columns reversed.
///
/// The reversed sort settings.
public SortSettings Reverse()
{
int[] sortProps = (int[]) _sortProps.Clone();
bool[] sortDirections = new bool[_sortDirections.Length];
for( int i=0; i