Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2289 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
13 changed files with 572 additions and 1 deletions
@ -0,0 +1,63 @@
@@ -0,0 +1,63 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Christian Hornung" email="c-hornung@gmx.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Windows.Forms; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Widgets.ListViewSorting |
||||
{ |
||||
/// <summary>
|
||||
/// Provides the ability to compare two ListViewItems by the
|
||||
/// content of ListViewSubItems of a specific column.
|
||||
/// The text content is converted to the type T by parsing
|
||||
/// and the parsed value is compared using its IComparable implementation.
|
||||
/// Values that are not parseable are considered less than other values
|
||||
/// and are compared by their text value.
|
||||
/// </summary>
|
||||
public abstract class AbstractListViewParseableColumnComparer<T> |
||||
: AbstractListViewSubItemComparer |
||||
where T : IComparable<T> |
||||
{ |
||||
protected override int Compare(ListViewItem.ListViewSubItem lhs, ListViewItem.ListViewSubItem rhs) |
||||
{ |
||||
T lhsValue, rhsValue; |
||||
|
||||
bool lhsValid = this.TryParse(lhs.Text, out lhsValue); |
||||
bool rhsValid = this.TryParse(rhs.Text, out rhsValue); |
||||
|
||||
if (lhsValid) { |
||||
|
||||
if (rhsValid) { |
||||
// both are valid -> compare
|
||||
return lhsValue.CompareTo(rhsValue); |
||||
} else { |
||||
// only left is valid -> left is greater than right
|
||||
return 1; |
||||
} |
||||
|
||||
} else { |
||||
|
||||
if (rhsValid) { |
||||
// only right is valid -> left is less than right
|
||||
return -1; |
||||
} else { |
||||
// both are invalid -> compare the text values
|
||||
return String.Compare(lhs.Text, rhs.Text, StringComparison.InvariantCulture); |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Tries to convert a text value to a value of type T.
|
||||
/// </summary>
|
||||
/// <param name="textValue">The text value to convert.</param>
|
||||
/// <param name="parsedValue">Receives the converted value if successful. When <c>true</c> is returned, this parameter must not be set to <c>null</c> by the method.</param>
|
||||
/// <returns><c>true</c> if the text value has been converted successfully, otherwise <c>false</c>.</returns>
|
||||
protected abstract bool TryParse(string textValue, out T parsedValue); |
||||
} |
||||
} |
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Christian Hornung" email="c-hornung@gmx.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Windows.Forms; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Widgets.ListViewSorting |
||||
{ |
||||
/// <summary>
|
||||
/// Provides the ability to compare two ListViewItems by the
|
||||
/// ListViewSubItems of a specific column.
|
||||
/// </summary>
|
||||
public abstract class AbstractListViewSubItemComparer : IListViewItemComparer |
||||
{ |
||||
public int Compare(ListViewItem lhs, ListViewItem rhs, int column) |
||||
{ |
||||
return this.Compare(lhs.SubItems[column], rhs.SubItems[column]); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Compares two ListViewSubItems.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Less than zero if <paramref name="lhs"/> is less than <paramref name="rhs"/>.
|
||||
/// Zero if <paramref name="lhs"/> is equal to <paramref name="rhs"/>.
|
||||
/// Greater than zero if <paramref name="lhs"/> is greater than <paramref name="rhs"/>.
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// The implementation must always compare the specified subitems in ascending order.
|
||||
/// </remarks>
|
||||
protected abstract int Compare(ListViewItem.ListViewSubItem lhs, ListViewItem.ListViewSubItem rhs); |
||||
} |
||||
} |
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Christian Hornung" email="c-hornung@gmx.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Windows.Forms; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Widgets.ListViewSorting |
||||
{ |
||||
/// <summary>
|
||||
/// Provides the ability to compare two ListViewItems with regard to
|
||||
/// a specific column.
|
||||
/// </summary>
|
||||
public interface IListViewItemComparer |
||||
{ |
||||
/// <summary>
|
||||
/// Compares two ListViewItems with regard to the specified column.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Less than zero if <paramref name="lhs"/> is less than <paramref name="rhs"/>.
|
||||
/// Zero if <paramref name="lhs"/> is equal to <paramref name="rhs"/>.
|
||||
/// Greater than zero if <paramref name="lhs"/> is greater than <paramref name="rhs"/>.
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// The implementation must always compare the specified items in ascending order.
|
||||
/// </remarks>
|
||||
int Compare(ListViewItem lhs, ListViewItem rhs, int column); |
||||
} |
||||
} |
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Christian Hornung" email="c-hornung@gmx.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Globalization; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Widgets.ListViewSorting |
||||
{ |
||||
/// <summary>
|
||||
/// Compares ListViewItems by the signed integer content of a specific column.
|
||||
/// </summary>
|
||||
public sealed class ListViewIntegerParseColumnComparer |
||||
: AbstractListViewParseableColumnComparer<Int64> |
||||
{ |
||||
protected override bool TryParse(string textValue, out Int64 parsedValue) |
||||
{ |
||||
return Int64.TryParse(textValue, NumberStyles.Any, CultureInfo.InvariantCulture, out parsedValue); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,199 @@
@@ -0,0 +1,199 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Christian Hornung" email="c-hornung@gmx.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections; |
||||
using System.Windows.Forms; |
||||
|
||||
using ICSharpCode.SharpDevelop.Widgets.Resources; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Widgets.ListViewSorting |
||||
{ |
||||
/// <summary>
|
||||
/// Sorts items in a ListView.
|
||||
/// The user can select the column to sort by clicking on the column header.
|
||||
/// </summary>
|
||||
public sealed class ListViewItemSorter : IComparer, IDisposable |
||||
{ |
||||
readonly ListView listView; |
||||
readonly IListViewItemComparer[] itemComparers; |
||||
|
||||
int sortColumnIndex; |
||||
SortOrder sortOrder = SortOrder.None; |
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ListView this ListViewItemSorter is associated with.
|
||||
/// </summary>
|
||||
public ListView ListView { |
||||
get { return listView; } |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the index of the list view column that is currently sorted.
|
||||
/// Assigning a value to this property causes the ListView to be re-sorted.
|
||||
/// </summary>
|
||||
public int SortColumnIndex { |
||||
get { return sortColumnIndex; } |
||||
set { |
||||
sortColumnIndex = value; |
||||
this.SetColumnHeaderIcons(); |
||||
this.ListView.Sort(); |
||||
} |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the current sort order.
|
||||
/// Assigning a value to this property causes the ListView to be re-sorted.
|
||||
/// </summary>
|
||||
public SortOrder SortOrder { |
||||
get { return sortOrder; } |
||||
set { |
||||
sortOrder = value; |
||||
this.SetColumnHeaderIcons(); |
||||
this.ListView.Sort(); |
||||
} |
||||
} |
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ListViewItemSorter"/> class
|
||||
/// that can sort all columns by their text content
|
||||
/// and attaches the new instance to the specified ListView.
|
||||
/// </summary>
|
||||
/// <param name="listView">The ListView that should be sorted and monitored for column click events.</param>
|
||||
/// <remarks>
|
||||
/// Note that the ListViewItemSorter constructor adds two bitmaps to the
|
||||
/// SmallImageList of the ListView (an empty ImageList will be created
|
||||
/// first if none is assigned).
|
||||
/// </remarks>
|
||||
public ListViewItemSorter(ListView listView) |
||||
: this(listView, null) |
||||
{ |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ListViewItemSorter"/> class
|
||||
/// that uses the specified comparers to sort columns
|
||||
/// and attaches the new instance to the specified ListView.
|
||||
/// </summary>
|
||||
/// <param name="listView">The ListView that should be sorted and monitored for column click events.</param>
|
||||
/// <param name="itemComparers">An array of objects that implement IListViewItemComparer.
|
||||
/// The index of the comparer in the array corresponds to the index of the list view column that it sorts.
|
||||
/// If an element is <c>null</c>, the corresponding column cannot be sorted.
|
||||
/// If this parameter is <c>null</c>, all columns can be sorted by their text content.</param>
|
||||
/// <remarks>
|
||||
/// Note that the ListViewItemSorter constructor adds two bitmaps to the
|
||||
/// SmallImageList of the ListView (an empty ImageList will be created
|
||||
/// first if none is assigned).
|
||||
/// </remarks>
|
||||
public ListViewItemSorter(ListView listView, IListViewItemComparer[] itemComparers) |
||||
{ |
||||
if (listView == null) { |
||||
throw new ArgumentNullException("listView"); |
||||
} |
||||
this.listView = listView; |
||||
|
||||
if (itemComparers == null) { |
||||
|
||||
IListViewItemComparer defaultComparer = new ListViewTextColumnComparer(); |
||||
this.itemComparers = new IListViewItemComparer[this.ListView.Columns.Count]; |
||||
for (int i = 0; i < this.itemComparers.Length; i++) { |
||||
this.itemComparers[i] = defaultComparer; |
||||
} |
||||
|
||||
} else { |
||||
|
||||
this.itemComparers = itemComparers; |
||||
|
||||
} |
||||
|
||||
if (this.ListView.SmallImageList == null) { |
||||
this.ListView.SmallImageList = new ImageList(); |
||||
} |
||||
this.ListView.SmallImageList.Images.Add("SortAscending", BitmapResources.GetBitmap("Icons.16x16.SortAscending.png")); |
||||
this.ListView.SmallImageList.Images.Add("SortDescending", BitmapResources.GetBitmap("Icons.16x16.SortDescending.png")); |
||||
|
||||
this.ListView.ColumnClick += this.ListViewColumnClick; |
||||
this.ListView.ListViewItemSorter = this; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Cleans up all used resources.
|
||||
/// </summary>
|
||||
public void Dispose() |
||||
{ |
||||
this.Dispose(true); |
||||
} |
||||
|
||||
void Dispose(bool disposing) |
||||
{ |
||||
if (disposing) { |
||||
this.ListView.ListViewItemSorter = null; |
||||
this.ListView.ColumnClick -= this.ListViewColumnClick; |
||||
} |
||||
} |
||||
|
||||
void SetColumnHeaderIcons() |
||||
{ |
||||
for (int i = 0; i < this.ListView.Columns.Count; i++) { |
||||
if (i == this.SortColumnIndex && this.SortOrder != SortOrder.None) { |
||||
if (this.SortOrder == SortOrder.Ascending) { |
||||
this.ListView.Columns[i].ImageKey = "SortAscending"; |
||||
} else { |
||||
this.ListView.Columns[i].ImageKey = "SortDescending"; |
||||
} |
||||
} else { |
||||
this.ListView.Columns[i].ImageKey = null; |
||||
} |
||||
} |
||||
} |
||||
|
||||
void ListViewColumnClick(object sender, ColumnClickEventArgs e) |
||||
{ |
||||
if (e.Column >= this.itemComparers.Length || this.itemComparers[e.Column] == null) { |
||||
return; |
||||
} |
||||
|
||||
if (this.sortColumnIndex == e.Column) { |
||||
|
||||
// Reverse the current sort direction for this column.
|
||||
if (this.SortOrder == SortOrder.Ascending) { |
||||
this.SortOrder = SortOrder.Descending; |
||||
} else { |
||||
this.SortOrder = SortOrder.Ascending; |
||||
} |
||||
|
||||
} else { |
||||
|
||||
// assign to the field to prevent re-sorting twice
|
||||
this.sortOrder = SortOrder.Ascending; |
||||
this.SortColumnIndex = e.Column; |
||||
|
||||
} |
||||
} |
||||
|
||||
public int Compare(object x, object y) |
||||
{ |
||||
if (this.SortOrder == SortOrder.None || |
||||
this.SortColumnIndex < 0 || this.SortColumnIndex >= this.itemComparers.Length || |
||||
this.itemComparers[this.SortColumnIndex] == null) { |
||||
return 0; |
||||
} |
||||
|
||||
int compareResult = this.itemComparers[this.SortColumnIndex].Compare((ListViewItem)x, (ListViewItem)y, this.SortColumnIndex); |
||||
|
||||
if (this.SortOrder == SortOrder.Ascending) { |
||||
return compareResult; |
||||
} else { |
||||
return -compareResult; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,74 @@
@@ -0,0 +1,74 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Christian Hornung" email="c-hornung@gmx.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Windows.Forms; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Widgets.ListViewSorting |
||||
{ |
||||
/// <summary>
|
||||
/// Compares ListViewItems by comparing multiple columns,
|
||||
/// using an object that implements <see cref="IListViewItemComparer"/>
|
||||
/// for every column to compare.
|
||||
/// </summary>
|
||||
public class ListViewMultipleColumnsComparer : IListViewItemComparer |
||||
{ |
||||
readonly List<IListViewItemComparer> comparers = new List<IListViewItemComparer>(2); |
||||
readonly List<int> columns = new List<int>(2); |
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ListViewMultipleColumnsComparer"/> class.
|
||||
/// </summary>
|
||||
/// <param name="firstComparer">The <see cref="IListViewItemComparer"/> to use to compare the first column.</param>
|
||||
/// <param name="firstColumn">The 0-based index of the first column to compare.</param>
|
||||
/// <param name="secondComparer">The <see cref="IListViewItemComparer"/> to use to compare the second column.</param>
|
||||
/// <param name="secondColumn">The 0-based index of the second column to compare.</param>
|
||||
/// <remarks>
|
||||
/// You can add more columns to compare by using the <see cref="AddComparer"/> method.
|
||||
/// </remarks>
|
||||
public ListViewMultipleColumnsComparer(IListViewItemComparer firstComparer, int firstColumn, IListViewItemComparer secondComparer, int secondColumn) |
||||
{ |
||||
if (firstComparer == null) { |
||||
throw new ArgumentNullException("firstComparer"); |
||||
} |
||||
if (secondComparer == null) { |
||||
throw new ArgumentNullException("secondComparer"); |
||||
} |
||||
|
||||
this.AddComparer(firstComparer, firstColumn); |
||||
this.AddComparer(secondComparer, secondColumn); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Adds another column to compare.
|
||||
/// </summary>
|
||||
/// <param name="comparer">The <see cref="IListViewItemComparer"/> to use to compare this column.</param>
|
||||
/// <param name="column">The 0-based index of the column to compare.</param>
|
||||
public void AddComparer(IListViewItemComparer comparer, int column) |
||||
{ |
||||
if (comparer == null) { |
||||
throw new ArgumentNullException("comparer"); |
||||
} |
||||
this.comparers.Add(comparer); |
||||
this.columns.Add(column); |
||||
} |
||||
|
||||
public int Compare(ListViewItem lhs, ListViewItem rhs, int column) |
||||
{ |
||||
int compareResult; |
||||
|
||||
for (int i = 0; i < this.comparers.Count; i++) { |
||||
if ((compareResult = this.comparers[i].Compare(lhs, rhs, this.columns[i])) != 0) { |
||||
return compareResult; |
||||
} |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Christian Hornung" email="c-hornung@gmx.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Windows.Forms; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Widgets.ListViewSorting |
||||
{ |
||||
/// <summary>
|
||||
/// Compares ListViewItems by the text of a specific column.
|
||||
/// </summary>
|
||||
public class ListViewTextColumnComparer : AbstractListViewSubItemComparer |
||||
{ |
||||
readonly IComparer<string> stringComparer; |
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="ListViewTextColumnComparer"/> class
|
||||
/// with a culture-independent and case-sensitive string comparer.
|
||||
/// </summary>
|
||||
public ListViewTextColumnComparer() |
||||
: this(StringComparer.InvariantCulture) |
||||
{ |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="ListViewTextColumnComparer"/> class
|
||||
/// with the specified string comparer.
|
||||
/// </summary>
|
||||
/// <param name="stringComparer">The string comparer used to compare the item texts.</param>
|
||||
public ListViewTextColumnComparer(IComparer<string> stringComparer) |
||||
{ |
||||
if (stringComparer == null) { |
||||
throw new ArgumentNullException("stringComparer"); |
||||
} |
||||
this.stringComparer = stringComparer; |
||||
} |
||||
|
||||
protected override int Compare(ListViewItem.ListViewSubItem lhs, ListViewItem.ListViewSubItem rhs) |
||||
{ |
||||
return this.stringComparer.Compare(lhs.Text, rhs.Text); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,43 @@
@@ -0,0 +1,43 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Christian Hornung" email="c-hornung@gmx.de"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Drawing; |
||||
|
||||
namespace ICSharpCode.SharpDevelop.Widgets.Resources |
||||
{ |
||||
/// <summary>
|
||||
/// Manages the bitmap resources used by the widgets library.
|
||||
/// These resources are kept outside of the SharpDevelop core
|
||||
/// so that this library can be used independently.
|
||||
/// </summary>
|
||||
public static class BitmapResources |
||||
{ |
||||
static readonly Dictionary<string, Bitmap> bitmapCache = new Dictionary<string, Bitmap>(); |
||||
|
||||
/// <summary>
|
||||
/// Gets a bitmap from the embedded bitmap resources.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the bitmap to get.</param>
|
||||
/// <returns>The Bitmap.</returns>
|
||||
public static Bitmap GetBitmap(string name) |
||||
{ |
||||
lock(bitmapCache) { |
||||
Bitmap bmp; |
||||
|
||||
if (bitmapCache.TryGetValue(name, out bmp)) { |
||||
return bmp; |
||||
} |
||||
|
||||
bmp = new Bitmap(typeof(BitmapResources).Assembly.GetManifestResourceStream(typeof(BitmapResources), name)); |
||||
bitmapCache[name] = bmp; |
||||
return bmp; |
||||
} |
||||
} |
||||
} |
||||
} |
After Width: | Height: | Size: 217 B |
After Width: | Height: | Size: 219 B |
Loading…
Reference in new issue