HeaderDataExt.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace FastReport.AdvMatrix
  5. {
  6. internal static class HeaderDataExt
  7. {
  8. // Returns the first item of a group. For simple type items:
  9. // - returns an item in the first parent group
  10. // - if there is no parent group, or useThisGroup parameter is true, returns the first item of a group in the same level
  11. internal static HeaderData GetFirstItem(this HeaderData item, bool useIneractiveSort, bool useThisGroup)
  12. {
  13. if (item.Parent == null)
  14. return null;
  15. if (item.Value == null)
  16. {
  17. HeaderData parentGroup = GetFirstItem(item.Parent, useIneractiveSort, useThisGroup);
  18. if (parentGroup == null || useThisGroup)
  19. {
  20. // return the first item of a group in the same level
  21. foreach (HeaderDataList dl in item.Parent.Items)
  22. {
  23. if (dl.Descriptor.IsGroup)
  24. return dl.GetSortedList(useIneractiveSort).FirstOrDefault();
  25. }
  26. }
  27. else
  28. {
  29. // return an item in the parent group
  30. foreach (HeaderDataList dl in parentGroup.Items)
  31. {
  32. if (dl.Descriptor == item.Descriptor)
  33. return dl[0];
  34. }
  35. }
  36. return null;
  37. }
  38. return item.ParentDataList.GetSortedList(useIneractiveSort).FirstOrDefault();
  39. }
  40. // The same as above but with the last item
  41. internal static HeaderData GetLastItem(this HeaderData item, bool useIneractiveSort, bool useThisGroup)
  42. {
  43. if (item.Parent == null)
  44. return null;
  45. if (item.Value == null)
  46. {
  47. HeaderData parentGroup = GetLastItem(item.Parent, useIneractiveSort, useThisGroup);
  48. if (parentGroup == null || useThisGroup)
  49. {
  50. foreach (HeaderDataList dl in item.Parent.Items)
  51. {
  52. if (dl.Descriptor.IsGroup)
  53. return dl.GetSortedList(useIneractiveSort).LastOrDefault();
  54. }
  55. }
  56. else
  57. {
  58. foreach (HeaderDataList dl in parentGroup.Items)
  59. {
  60. if (dl.Descriptor == item.Descriptor)
  61. return dl[0];
  62. }
  63. }
  64. return null;
  65. }
  66. return item.ParentDataList.GetSortedList(useIneractiveSort).LastOrDefault();
  67. }
  68. // The same as above but with the previous item
  69. internal static HeaderData GetPreviousItem(this HeaderData item, bool useIneractiveSort, bool useThisGroup)
  70. {
  71. if (item.Parent == null)
  72. return null;
  73. if (item.Value == null)
  74. {
  75. HeaderData parentGroup = GetPreviousItem(item.Parent, useIneractiveSort, useThisGroup);
  76. if (parentGroup == null || useThisGroup)
  77. {
  78. foreach (HeaderDataList dl in item.Parent.Items)
  79. {
  80. if (dl.Descriptor.IsGroup)
  81. {
  82. List<HeaderData> l = dl.GetSortedList(useIneractiveSort);
  83. if (l.Count > 1)
  84. return l[l.Count - 2];
  85. return null;
  86. }
  87. }
  88. }
  89. else
  90. {
  91. foreach (HeaderDataList dl in parentGroup.Items)
  92. {
  93. if (dl.Descriptor == item.Descriptor)
  94. return dl[0];
  95. }
  96. }
  97. return null;
  98. }
  99. List<HeaderData> list = item.ParentDataList.GetSortedList(useIneractiveSort);
  100. int index = list.IndexOf(item);
  101. if (index != -1)
  102. {
  103. if (index == 0)
  104. return null;
  105. return list[index - 1];
  106. }
  107. return null;
  108. }
  109. // The same as above but with the next item
  110. internal static HeaderData GetNextItem(this HeaderData item, bool useIneractiveSort, bool useThisGroup)
  111. {
  112. if (item.Parent == null)
  113. return null;
  114. if (item.Value == null)
  115. {
  116. HeaderData parentGroup = GetNextItem(item.Parent, useIneractiveSort, useThisGroup);
  117. if (parentGroup == null || useThisGroup)
  118. {
  119. foreach (HeaderDataList dl in item.Parent.Items)
  120. {
  121. if (dl.Descriptor.IsGroup)
  122. {
  123. List<HeaderData> l = dl.GetSortedList(useIneractiveSort);
  124. if (l.Count > 1)
  125. return l[1];
  126. return null;
  127. }
  128. }
  129. }
  130. else
  131. {
  132. foreach (HeaderDataList dl in parentGroup.Items)
  133. {
  134. if (dl.Descriptor == item.Descriptor)
  135. return dl[0];
  136. }
  137. }
  138. return null;
  139. }
  140. List<HeaderData> list = item.ParentDataList.GetSortedList(useIneractiveSort);
  141. int index = list.IndexOf(item);
  142. if (index != -1)
  143. {
  144. if (index == list.Count - 1)
  145. return null;
  146. return list[index + 1];
  147. }
  148. return null;
  149. }
  150. // The same as above but with the specific item
  151. internal static HeaderData GetSpecificItem(this HeaderData item, object value)
  152. {
  153. if (item.Parent == null)
  154. return null;
  155. if (item.Value == null)
  156. {
  157. foreach (HeaderDataList dl in item.Parent.Items)
  158. {
  159. if (dl.Descriptor.IsGroup)
  160. {
  161. HeaderData d = GetSpecificItem(dl[0], value);
  162. if (d != null)
  163. return d;
  164. }
  165. }
  166. }
  167. else
  168. {
  169. foreach (HeaderData d in item.ParentDataList)
  170. {
  171. if (d.Value.Equals(value))
  172. return d;
  173. }
  174. }
  175. return null;
  176. }
  177. internal static HeaderDataList GetGroup(this HeaderData item)
  178. {
  179. HeaderData item1 = item;
  180. // trying to find a parent group
  181. while (item1 != null && item1.Value == null)
  182. item1 = item1.Parent;
  183. if (item1 != null)
  184. return item1.ParentDataList;
  185. // trying to find a group on the same level
  186. item1 = item.Parent;
  187. while (item1 != null)
  188. {
  189. foreach (HeaderDataList dl in item1.Items)
  190. {
  191. if (dl.Descriptor.IsGroup)
  192. return dl;
  193. }
  194. item1 = item1.Parent;
  195. }
  196. return null;
  197. }
  198. internal static HeaderData FindByIndex(this HeaderData item, int index)
  199. {
  200. if (item.Index == index)
  201. return item;
  202. // do not use root enumerator here, avoid additional sorting which may lead to SO
  203. foreach (HeaderDataList dl in item.Items)
  204. {
  205. foreach (HeaderData d in dl)
  206. {
  207. HeaderData result = FindByIndex(d, index);
  208. if (result != null)
  209. return result;
  210. }
  211. }
  212. return null;
  213. }
  214. internal static void ToggleVisible(this HeaderData item, int index, bool collapseAll, bool expandAll)
  215. {
  216. item = item.FindByIndex(index);
  217. if (item == null)
  218. return;
  219. MatrixCollapseButton btn = item.Descriptor.TemplateCell.GetCollapseButton();
  220. if (btn == null)
  221. return;
  222. Action<HeaderData, bool, bool> func = (root, visible, skip) =>
  223. {
  224. if (root == null)
  225. return;
  226. foreach (HeaderDataList dl in root.Items)
  227. {
  228. foreach (HeaderData d in dl)
  229. {
  230. if (d == item && skip)
  231. continue;
  232. foreach (HeaderDataList dl1 in btn.GetLinkedItems(d))
  233. {
  234. dl1.Visible = visible;
  235. }
  236. }
  237. }
  238. };
  239. if (collapseAll)
  240. {
  241. func(item.Parent, false, false);
  242. return;
  243. }
  244. if (expandAll)
  245. {
  246. func(item.Parent, true, false);
  247. return;
  248. }
  249. if (btn.Exclusive)
  250. {
  251. func(item.Parent, false, true);
  252. }
  253. foreach (HeaderDataList dl in btn.GetLinkedItems(item))
  254. {
  255. dl.Visible = !dl.Visible;
  256. }
  257. }
  258. }
  259. }