ImageComparer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.Collections;
  32. using System.Drawing;
  33. using System.Windows.Forms;
  34. using XPTable.Models;
  35. namespace XPTable.Sorting
  36. {
  37. /// <summary>
  38. /// An IComparer for sorting Cells that contain Images
  39. /// </summary>
  40. public class ImageComparer : ComparerBase
  41. {
  42. #region Constructor
  43. /// <summary>
  44. /// Initializes a new instance of the ImageComparer class with the specified
  45. /// TableModel, Column index and SortOrder
  46. /// </summary>
  47. /// <param name="tableModel">The TableModel that contains the data to be sorted</param>
  48. /// <param name="column">The index of the Column to be sorted</param>
  49. /// <param name="sortOrder">Specifies how the Column is to be sorted</param>
  50. public ImageComparer(TableModel tableModel, int column, SortOrder sortOrder) : base(tableModel, column, sortOrder)
  51. {
  52. }
  53. #endregion
  54. #region Methods
  55. /// <summary>
  56. /// Compares two objects and returns a value indicating whether one is less
  57. /// than, equal to or greater than the other
  58. /// </summary>
  59. /// <param name="a">First object to compare</param>
  60. /// <param name="b">Second object to compare</param>
  61. /// <returns>-1 if a is less than b, 1 if a is greater than b, or 0 if a equals b</returns>
  62. public override int Compare(object a, object b)
  63. {
  64. Cell cell1 = (Cell) a;
  65. Cell cell2 = (Cell) b;
  66. // check for null cells
  67. if (cell1 == null && cell2 == null)
  68. {
  69. return 0;
  70. }
  71. else if (cell1 == null)
  72. {
  73. return -1;
  74. }
  75. else if (cell2 == null)
  76. {
  77. return 1;
  78. }
  79. int retVal = 0;
  80. // check for null data
  81. if (cell1.Data == null && cell2.Data == null)
  82. {
  83. retVal = 0;
  84. }
  85. else if (cell1.Data == null)
  86. {
  87. retVal = -1;
  88. }
  89. else if (cell2.Data == null)
  90. {
  91. retVal = 1;
  92. }
  93. // since images aren't comparable, if retVal = 0 and the ImageColumn
  94. // they belong to allows text drawing, compare the text properties
  95. // to determine order
  96. if (retVal == 0 && ((ImageColumn) this.TableModel.Table.ColumnModel.Columns[this.SortColumn]).DrawText)
  97. {
  98. // check for null data
  99. if (cell1.Text == null && cell2.Text == null)
  100. {
  101. return 0;
  102. }
  103. else if (cell1.Text == null)
  104. {
  105. return -1;
  106. }
  107. retVal = cell1.Text.CompareTo(cell2.Text);
  108. }
  109. return retVal;
  110. }
  111. #endregion
  112. }
  113. }