Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace PT_lab10
  11. {
  12. public class CarBindingList : BindingList<Car>
  13. {
  14. List<Car> originalList;
  15. public List<Car> foundCars;
  16. private bool isEngine = false;
  17.  
  18. private void UpdateCarBindingList(List<Car> items)
  19. {
  20. base.ClearItems();
  21.  
  22. for (int i = 0; i < items.Count; i++)
  23. {
  24. base.InsertItem(i, items[i]);
  25. }
  26. }
  27. public CarBindingList(List<Car> list)
  28. {
  29. originalList = list;
  30. UpdateCarBindingList(list);
  31. foundCars = new List<Car>();
  32. }
  33.  
  34. protected override bool SupportsSortingCore
  35. {
  36. get { return true; }
  37. }
  38. protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
  39. {
  40. if (prop.PropertyType.GetInterface("IComparable") != null)
  41. {
  42. originalList = (originalList.OrderBy(elem => prop.GetValue(elem))).ToList();
  43. if (direction == ListSortDirection.Descending)
  44. {
  45. originalList.Reverse();
  46. }
  47. UpdateCarBindingList(originalList);
  48. OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
  49. }
  50. else
  51. {
  52. Console.WriteLine(String.Format("Cannot sort by {0}. Lack of IComperable implementation.", prop.Name));
  53. //throw new NotSupportedException(String.Format("Cannot sort by {0}. Lack of IComperable implementation.", prop.Name));
  54. }
  55. }
  56.  
  57. public void Sort(string property, ListSortDirection direction)
  58. {
  59. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Car));
  60. PropertyDescriptor prop = properties.Find(property, true);
  61. if (prop != null)
  62. {
  63. ApplySortCore(prop, direction);
  64. }
  65. else
  66. {
  67. throw new NotSupportedException(String.Format("Cannot sort by {0}, this property doesn\'t exist.", prop.Name));
  68. }
  69. }
  70.  
  71. protected override bool SupportsSearchingCore
  72. {
  73. get { return true; }
  74. }
  75.  
  76. protected override int FindCore(PropertyDescriptor prop, object key)
  77. {
  78. int counter = 0;
  79. if (!isEngine)
  80. {
  81. PropertyInfo property = typeof(Car).GetProperty(prop.Name);
  82. Car item;
  83.  
  84. for (int i = 0; i < Count; ++i)
  85. {
  86. item = (Car)Items[i];
  87. if (property.GetValue(item, null).Equals(key))
  88. {
  89. foundCars.Add(item);
  90. counter++;
  91. }
  92. }
  93. }
  94. else
  95. {
  96. PropertyInfo property = typeof(Engine).GetProperty(prop.Name);
  97.  
  98. for (int i = 0; i < Count; ++i)
  99. {
  100. if (property.GetValue(Items[i].motor, null).Equals(key))
  101. {
  102. foundCars.Add(Items[i]);
  103. counter++;
  104. }
  105. }
  106. }
  107.  
  108. if (counter != 0) return 1;
  109. else return -1;
  110. }
  111.  
  112. public bool Find(string propertyValue, string propertyName)
  113. {
  114. foundCars.Clear();
  115.  
  116. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Car));
  117.  
  118. if (propertyName.Contains('.'))
  119. {
  120. isEngine = true;
  121. propertyName = propertyName.Split('.')[1];
  122. properties = TypeDescriptor.GetProperties(typeof(Engine));
  123. }
  124.  
  125. object key;
  126. if (propertyValue.All(Char.IsDigit))
  127. {
  128. key = Int32.Parse(propertyValue);
  129. }
  130. else
  131. {
  132. key = propertyValue;
  133. }
  134.  
  135. PropertyDescriptor prop = properties.Find(propertyName, true);
  136. if (FindCore(prop, key) == 1)
  137. {
  138. isEngine = false;
  139. return true;
  140. }
  141.  
  142.  
  143. isEngine = false;
  144. return false;
  145. }
  146.  
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement