PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/LLBL Pro v3.5/AW.Winforms.Helpers/BindingListHelper.cs

#
C# | 351 lines | 315 code | 29 blank | 7 comment | 59 complexity | 3b78e596c8f7f059e225e9d0651c06bc MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, MIT, GPL-2.0, Apache-2.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8. using AW.Helper;
  9. using JesseJohnston;
  10. namespace AW.Winforms.Helpers
  11. {
  12. public static class BindingListHelper
  13. {
  14. private static readonly Dictionary<Type, Func<IEnumerable, Type, IBindingListView>> BindingListViewCreaters = new Dictionary<Type, Func<IEnumerable, Type, IBindingListView>>();
  15. public static void RegisterbindingListViewCreater(Type itemType, Func<IEnumerable, Type, IBindingListView> bindingListViewCreater)
  16. {
  17. if (BindingListViewCreaters.ContainsKey(itemType))
  18. BindingListViewCreaters[itemType] = bindingListViewCreater;
  19. else
  20. BindingListViewCreaters.Add(itemType, bindingListViewCreater);
  21. }
  22. //&& enumerable.ToString() != "System.Collections.Hashtable+KeyCollection"
  23. public static IBindingListView ToBindingListView(this IEnumerable enumerable)
  24. {
  25. var showenEnumerable = enumerable != null && !(enumerable is string);
  26. if (showenEnumerable)
  27. {
  28. if (enumerable is IBindingListView)
  29. return (IBindingListView) enumerable;
  30. if (enumerable is IListSource)
  31. {
  32. var bindingListView = ListSourceToBindingListView((IListSource) enumerable);
  33. if (bindingListView != null)
  34. return bindingListView;
  35. }
  36. return CreateBindingListView(enumerable);
  37. }
  38. return null;
  39. }
  40. public static IBindingListView ToBindingListView<T>(this IEnumerable<T> enumerable)
  41. {
  42. var showenEnumerable = enumerable != null && !(enumerable is string);
  43. if (showenEnumerable)
  44. {
  45. if (enumerable is IBindingListView)
  46. return (IBindingListView) enumerable;
  47. if (enumerable is IListSource)
  48. {
  49. var bindingListView = ListSourceToBindingListView((IListSource) enumerable);
  50. if (bindingListView != null)
  51. return bindingListView;
  52. }
  53. return CreateBindingListView(enumerable);
  54. }
  55. return null;
  56. }
  57. public static IBindingListView ListSourceToBindingListView(IListSource listSource)
  58. {
  59. if (listSource != null)
  60. {
  61. var list = listSource.GetList();
  62. if (list != null)
  63. return list.ToBindingListView();
  64. }
  65. return null;
  66. }
  67. private static ObjectListView ToObjectListView(this IList list)
  68. {
  69. if (list != null)
  70. {
  71. var objectListView = new ObjectListView(list);
  72. if (objectListView.ItemType != null)
  73. return objectListView;
  74. }
  75. return null;
  76. }
  77. private static ObjectListView ToObjectListView(this ICollection collection)
  78. {
  79. if (collection != null)
  80. {
  81. if (collection is IList)
  82. return ToObjectListView((IList) collection);
  83. var objectListView = CreateObjectListView(collection);
  84. if (objectListView.ItemType != null)
  85. return objectListView;
  86. }
  87. return null;
  88. }
  89. private static ObjectListView<T> ToObjectListView<T>(this IList<T> list)
  90. {
  91. if (list != null)
  92. {
  93. var objectListView = new ObjectListView<T>(list);
  94. return objectListView;
  95. }
  96. return null;
  97. }
  98. private static IBindingListView CreateBindingListView<T>(IEnumerable<T> enumerable)
  99. {
  100. foreach (var iBindingListView in
  101. from bindingListViewCreater in BindingListViewCreaters
  102. where bindingListViewCreater.Key.IsAssignableFrom(typeof (T))
  103. select bindingListViewCreater.Value(enumerable, typeof (T))
  104. into iBindingListView
  105. where iBindingListView != null
  106. select iBindingListView)
  107. {
  108. return iBindingListView;
  109. }
  110. return ToObjectListView(enumerable);
  111. }
  112. private static IBindingListView CreateBindingListView(IEnumerable enumerable)
  113. {
  114. var itemType = MetaDataHelper.GetEnumerableItemType(enumerable);
  115. return CreateBindingListView(enumerable, itemType);
  116. }
  117. private static IBindingListView CreateBindingListView(IEnumerable enumerable, Type itemType)
  118. {
  119. foreach (var iBindingListView in
  120. from bindingListViewCreater in BindingListViewCreaters
  121. where bindingListViewCreater.Key.IsAssignableFrom(itemType)
  122. select bindingListViewCreater.Value(enumerable, itemType)
  123. into iBindingListView
  124. where iBindingListView != null
  125. select iBindingListView)
  126. {
  127. return iBindingListView;
  128. }
  129. return CreateObjectListView(enumerable, itemType);
  130. }
  131. private static ObjectListView CreateObjectListView(ICollection collection)
  132. {
  133. return new ObjectListView(new ArrayList(collection));
  134. }
  135. /// <summary>
  136. /// Creates the object list view.
  137. /// </summary>
  138. /// <param name="enumerable">The enumerable.</param>
  139. /// <param name="itemType">Type of the item.</param>
  140. /// <returns></returns>
  141. public static ObjectListView CreateObjectListView(IEnumerable enumerable, Type itemType)
  142. {
  143. enumerable = (IEnumerable) ListBindingHelper.GetList(enumerable);
  144. ObjectListView objectListView;
  145. if (enumerable is ICollection)
  146. objectListView = CreateObjectListView((ICollection) enumerable);
  147. else
  148. {
  149. objectListView = CreateObjectListView();
  150. foreach (var item in enumerable)
  151. objectListView.Add(item);
  152. }
  153. if (objectListView.ItemType == null)
  154. objectListView.ItemType = itemType;
  155. return objectListView;
  156. }
  157. public static IBindingListView CreateObjectListViewGeneric(Type type)
  158. {
  159. return (IBindingListView) MetaDataHelper.CreateGeneric(typeof (ObjectListView<>), type, MetaDataHelper.CreateList(type));
  160. }
  161. public static IBindingListView CreateObjectListView(Type type)
  162. {
  163. var objectListView = CreateObjectListView();
  164. objectListView.ItemType = type;
  165. return objectListView;
  166. }
  167. public static ObjectListView CreateObjectListView()
  168. {
  169. return new ObjectListView(new ArrayList());
  170. }
  171. private static IBindingListView ToObjectListView<T>(this IEnumerable<T> enumerable)
  172. {
  173. enumerable = (IEnumerable<T>) ListBindingHelper.GetList(enumerable);
  174. return ToObjectListView((IList<T>) enumerable.ToList());
  175. }
  176. public static bool BindEnumerable<T>(this BindingSource bindingSource, IEnumerable<T> enumerable, bool setReadonly)
  177. {
  178. var showenEnumerable = BindEnumerable(bindingSource, enumerable);
  179. if (showenEnumerable)
  180. if (setReadonly && bindingSource.AllowEdit && bindingSource.DataSource is IBindingList)
  181. {
  182. SetReadonly<T>(bindingSource);
  183. }
  184. else
  185. bindingSource.AllowNew = !setReadonly;
  186. return showenEnumerable;
  187. }
  188. private static void SetReadonly<T>(BindingSource bindingSource)
  189. {
  190. var bindingList = (IBindingList) bindingSource.DataSource;
  191. if (!(SetReadonly<T>(bindingList)))
  192. {
  193. var list = ((IEnumerable<T>) bindingList).ToList();
  194. bindingSource.DataSource = new ObjectListView<T>(list.AsReadOnly());
  195. }
  196. }
  197. public static bool BindEnumerable(this BindingSource bindingSource, IEnumerable enumerable, bool setReadonly)
  198. {
  199. var showenEnumerable = BindEnumerable(bindingSource, enumerable);
  200. if (showenEnumerable)
  201. if (setReadonly && bindingSource.AllowEdit && bindingSource.DataSource is IBindingList)
  202. SetReadonly(((IBindingList) bindingSource.DataSource));
  203. else
  204. bindingSource.AllowNew = !setReadonly;
  205. return showenEnumerable;
  206. }
  207. private static bool BindEnumerable<T>(BindingSource bindingSource, IEnumerable<T> enumerable)
  208. {
  209. bool showenEnumerable;
  210. try
  211. {
  212. bindingSource.DataSource = enumerable.ToBindingListView();
  213. showenEnumerable = bindingSource.List != null;
  214. }
  215. catch (Exception)
  216. {
  217. try
  218. {
  219. bindingSource.DataSource = enumerable;
  220. }
  221. catch (Exception)
  222. {
  223. bindingSource.DataSource = null;
  224. }
  225. showenEnumerable = bindingSource.List != null;
  226. }
  227. return showenEnumerable;
  228. }
  229. private static bool BindEnumerable(BindingSource bindingSource, IEnumerable enumerable)
  230. {
  231. bool showenEnumerable;
  232. try
  233. {
  234. bindingSource.DataSource = enumerable.ToBindingListView();
  235. showenEnumerable = bindingSource.DataSource != null;
  236. }
  237. catch (Exception)
  238. {
  239. try
  240. {
  241. bindingSource.DataSource = enumerable;
  242. }
  243. catch (Exception)
  244. {
  245. bindingSource.DataSource = null;
  246. }
  247. showenEnumerable = bindingSource.DataSource != null;
  248. }
  249. return showenEnumerable;
  250. }
  251. public static bool SetReadonly(IBindingList bindingList)
  252. {
  253. var result = bindingList is ObjectListView;
  254. if (result)
  255. {
  256. ((ObjectListView) bindingList).AllowEdit = false;
  257. ((ObjectListView) bindingList).AllowRemove = false;
  258. ((ObjectListView) bindingList).AllowNew = false;
  259. }
  260. else
  261. {
  262. result = bindingList is DataView;
  263. if (result)
  264. {
  265. ((DataView) bindingList).AllowEdit = false;
  266. ((DataView) bindingList).AllowNew = false;
  267. }
  268. else
  269. {
  270. SetPropertyFalse(bindingList, "AllowEdit");
  271. SetPropertyFalse(bindingList, "AllowRemove");
  272. SetPropertyFalse(bindingList, "AllowNew");
  273. }
  274. }
  275. return result;
  276. }
  277. private static void SetPropertyFalse(IEnumerable bindingList, string name)
  278. {
  279. var propertyInfo = bindingList.GetType().GetProperty(name);
  280. if (propertyInfo.CanWrite)
  281. propertyInfo.SetValue(bindingList, false, null);
  282. }
  283. public static bool SetReadonly<T>(IBindingList bindingList)
  284. {
  285. var result = bindingList is ObjectListView<T>;
  286. if (result)
  287. {
  288. ((ObjectListView<T>) bindingList).AllowEdit = false;
  289. ((ObjectListView<T>) bindingList).AllowRemove = false;
  290. ((ObjectListView<T>) bindingList).AllowNew = false;
  291. }
  292. else
  293. {
  294. result = bindingList is BindingList<T>;
  295. if (result)
  296. {
  297. ((BindingList<T>) bindingList).AllowEdit = false;
  298. ((BindingList<T>) bindingList).AllowRemove = false;
  299. ((BindingList<T>) bindingList).AllowNew = false;
  300. }
  301. else
  302. result = SetReadonly(bindingList);
  303. }
  304. return result && !bindingList.AllowEdit;
  305. }
  306. public static object GetDataSource(this BindingSource bindingSource)
  307. {
  308. if (bindingSource.DataSource is BindingSource)
  309. return GetDataSource((BindingSource) bindingSource.DataSource);
  310. var objectListView = bindingSource.DataSource as ObjectListView;
  311. return objectListView == null ? bindingSource.DataSource : GetDataSource(objectListView);
  312. }
  313. public static object GetDataSource(this ObjectListView objectListView)
  314. {
  315. var bindingSource = objectListView.List as BindingSource;
  316. if (bindingSource != null)
  317. return GetDataSource(bindingSource);
  318. var objectListViewSource = objectListView.List as ObjectListView;
  319. return objectListViewSource == null ? objectListView.List : GetDataSource(objectListViewSource);
  320. }
  321. }
  322. }