/*
* Copyright (c) 2009 Craig Sutherland
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastForward.WinCore.Configuration;
namespace FastForward.WinCore
{
///
/// Helper methods for working with list views.
///
public static class ListViewHelper
{
#region Public methods
#region SaveListView()
///
/// Save the current settings for a list view.
///
///
///
///
public static void SaveListView(ListView value, int sortColumn, List listViews)
{
var config = (from record in listViews
where record.Key == value.Name
select record).SingleOrDefault();
if (config == null)
{
config = new ListViewDisplayConfig();
config.Key = value.Name;
listViews.Add(config);
}
for (var loop = 0; loop < value.Columns.Count; loop++)
{
var column = value.Columns[loop] as ColumnHeader;
var colConfig = config.FindColumn(loop);
if (colConfig == null)
{
colConfig = new ColumnDisplayConfig();
colConfig.Position = loop;
config.Columns.Add(colConfig);
}
colConfig.Width = column.Width;
}
config.SortColumn = sortColumn;
config.SortOrder = value.Sorting;
}
#endregion
#region LoadListView()
///
/// Load the saved settings for a list view.
///
///
///
///
public static int LoadListView(ListView value, List listViews)
{
var sortColumn = 0;
var config = (from record in listViews
where record.Key == value.Name
select record).SingleOrDefault();
if (config != null)
{
foreach (var colConfig in config.Columns)
{
var column = value.Columns[colConfig.Position] as ColumnHeader;
column.Width = colConfig.Width;
}
sortColumn = config.SortColumn;
SortListView(value, sortColumn, (config.SortOrder == SortOrder.Descending ? sortColumn : -1));
}
return sortColumn;
}
#endregion
#region SortListView()
///
/// Sort a list view.
///
///
///
///
///
public static int SortListView(ListView view, int newColumn, int oldColumn)
{
if (oldColumn == newColumn)
{
if (view.Sorting == SortOrder.Ascending)
{
view.Sorting = SortOrder.Descending;
}
else
{
view.Sorting = SortOrder.Ascending;
}
}
else
{
view.Sorting = SortOrder.Ascending;
}
var comparer = new ListViewStringSorter(newColumn, view);
view.ListViewItemSorter = comparer;
return newColumn;
}
#endregion
#endregion
}
}