/Branches/0.9.5-DeltaShell/src/Common/DelftTools.Utils/PropertySorter.cs

# · C# · 106 lines · 69 code · 8 blank · 29 comment · 5 complexity · 3142e6ddaca906cc709f610839172d08 MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. namespace DelftTools.Utils
  5. {
  6. /// <summary>
  7. /// Helps to display properties in a specific order in the property grid.
  8. /// </summary>
  9. public class PropertySorter : ExpandableObjectConverter
  10. {
  11. public override bool GetPropertiesSupported(ITypeDescriptorContext context)
  12. {
  13. return true;
  14. }
  15. public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value,
  16. Attribute[] attributes)
  17. {
  18. //
  19. // This override returns a list of properties in order
  20. //
  21. PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
  22. var orderedProperties = new ArrayList();
  23. foreach (PropertyDescriptor pd in pdc)
  24. {
  25. Attribute attribute = pd.Attributes[typeof (PropertyOrderAttribute)];
  26. if (attribute != null)
  27. {
  28. //
  29. // If the attribute is found, then create an pair object to hold it
  30. //
  31. var orderAttribute = (PropertyOrderAttribute) attribute;
  32. orderedProperties.Add(new PropertyOrderPair(pd.Name, orderAttribute.Order));
  33. }
  34. else
  35. {
  36. //
  37. // If no order attribute is specifed then given it an order of 0
  38. //
  39. orderedProperties.Add(new PropertyOrderPair(pd.Name, 0));
  40. }
  41. }
  42. //
  43. // Perform the actual order using the value PropertyOrderPair classes
  44. // implementation of IComparable to sort
  45. //
  46. orderedProperties.Sort();
  47. //
  48. // Build a string list of the ordered names
  49. //
  50. var propertyNames = new ArrayList();
  51. foreach (PropertyOrderPair pop in orderedProperties)
  52. {
  53. propertyNames.Add(pop.Name);
  54. }
  55. //
  56. // Pass in the ordered list for the PropertyDescriptorCollection to sort by
  57. //
  58. return pdc.Sort((string[]) propertyNames.ToArray(typeof (string)));
  59. }
  60. public class PropertyOrderPair : IComparable
  61. {
  62. private readonly string name;
  63. private readonly int order;
  64. public PropertyOrderPair(string name, int order)
  65. {
  66. this.order = order;
  67. this.name = name;
  68. }
  69. public string Name
  70. {
  71. get { return name; }
  72. }
  73. #region IComparable Members
  74. public int CompareTo(object obj)
  75. {
  76. //
  77. // Sort the pair objects by ordering by order value
  78. // Equal values get the same rank
  79. //
  80. int otherOrder = ((PropertyOrderPair)obj).order;
  81. if (otherOrder == order)
  82. {
  83. //
  84. // If order not specified, sort by name
  85. //
  86. string otherName = ((PropertyOrderPair)obj).name;
  87. return string.Compare(name, otherName);
  88. }
  89. if (otherOrder > order)
  90. {
  91. return -1;
  92. }
  93. return 1;
  94. }
  95. #endregion
  96. }
  97. }
  98. }