PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Integrator/CLRI/Cerebrum.Data.Manager/DataListUnion.cs

#
C# | 771 lines | 641 code | 91 blank | 39 comment | 35 complexity | 39ad8dfd52c15bcdcf100e1bc5aec6ae MD5 | raw file
Possible License(s): AGPL-3.0
  1. using System;
  2. namespace Cerebrum.Data
  3. {
  4. public class DataListUnion : System.ComponentModel.Component, System.ComponentModel.IBindingList, System.ComponentModel.ISupportInitialize, System.ComponentModel.ITypedList
  5. {
  6. private System.Collections.ArrayList Lists;
  7. public DataListUnion()
  8. {
  9. Lists = new System.Collections.ArrayList();
  10. }
  11. public void Add(System.ComponentModel.IBindingList v)
  12. {
  13. Lists.Add(v);
  14. v.ListChanged +=new System.ComponentModel.ListChangedEventHandler(DataView_ListChanged);
  15. this.Reset();
  16. }
  17. public void Add(System.ComponentModel.ICustomTypeDescriptor r)
  18. {
  19. int c = this.Lists.Count;
  20. DataItemUnion ru = null;
  21. if(!(c>0 && (ru=this.Lists[c - 1] as DataItemUnion)!=null))
  22. {
  23. ru = new DataItemUnion();
  24. this.Lists.Add(ru);
  25. ru.ListChanged +=new System.ComponentModel.ListChangedEventHandler(DataView_ListChanged);
  26. }
  27. ru.Add(r);
  28. //this.Reset(); <-- ru.ListChanged
  29. }
  30. public void Add(System.ComponentModel.ICustomTypeDescriptor[] r)
  31. {
  32. foreach (System.ComponentModel.ICustomTypeDescriptor row in r)
  33. {
  34. this.Add(row);
  35. }
  36. }
  37. public int Add(object value)
  38. {
  39. /*if(value is System.Data.DataRow)
  40. {
  41. this.Add((System.Data.DataRow)value);
  42. }
  43. else if(value is System.Data.DataTable)
  44. {
  45. this.Add((System.Data.DataTable)value);
  46. }
  47. else if(value is System.ComponentModel.ICustomTypeDescriptor)
  48. {
  49. this.Add((System.ComponentModel.ICustomTypeDescriptor)value);
  50. }
  51. else*/ if(value is System.ComponentModel.IBindingList)
  52. {
  53. this.Add((System.ComponentModel.IBindingList)value);
  54. }
  55. throw new System.ArgumentException("Not supported type");
  56. }
  57. int System.Collections.IList.Add(object value)
  58. {
  59. //throw new System.ArgumentException("Not supported type");
  60. return Add(value);
  61. }
  62. public void Clear()
  63. {
  64. Lists.Clear();
  65. Reset();
  66. }
  67. public void Reset()
  68. {
  69. if(this.ListChanged!=null)
  70. {
  71. this.ListChanged(this, new System.ComponentModel.ListChangedEventArgs(System.ComponentModel.ListChangedType.Reset, -1));
  72. }
  73. }
  74. public bool Contains(object value)
  75. {
  76. foreach(System.Collections.IList list in this.Lists)
  77. {
  78. if(list.Contains(value))
  79. {
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. public void CopyTo(System.Array array, int index)
  86. {
  87. int count = 0;
  88. foreach(System.Collections.IList list in this.Lists)
  89. {
  90. list.CopyTo(array, count + index);
  91. count += list.Count;
  92. }
  93. }
  94. public int Count
  95. {
  96. get
  97. {
  98. int count = 0;
  99. foreach(System.Collections.IList list in this.Lists)
  100. {
  101. count += list.Count;
  102. }
  103. return count;
  104. }
  105. }
  106. private class ListsEnumerator:System.Collections.IEnumerator
  107. {
  108. public ListsEnumerator(System.Collections.ArrayList lists)
  109. {
  110. this.Lists = lists;
  111. this.itemi= -1;
  112. this.listi = 0;
  113. }
  114. public System.Collections.ArrayList Lists;
  115. public int itemi;
  116. public int listi;
  117. #region IEnumerator Members
  118. public void Reset()
  119. {
  120. this.itemi= -1;
  121. this.listi = 0;
  122. }
  123. public object Current
  124. {
  125. get
  126. {
  127. System.Collections.IList list = this.Lists[listi] as System.Collections.IList;
  128. return list[itemi];
  129. }
  130. }
  131. public bool MoveNext()
  132. {
  133. System.Collections.IList list = null;
  134. if(listi < this.Lists.Count)
  135. {
  136. list = this.Lists[listi] as System.Collections.IList;
  137. itemi++;
  138. if(itemi < list.Count)
  139. {
  140. return true;
  141. }
  142. else
  143. {
  144. listi++;
  145. if(listi < this.Lists.Count)
  146. {
  147. itemi = -1;
  148. return MoveNext();
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. #endregion
  155. }
  156. public System.Collections.IEnumerator GetEnumerator()
  157. {
  158. return new ListsEnumerator(this.Lists);
  159. }
  160. public int IndexOf(object value)
  161. {
  162. int index = 0;
  163. foreach(System.Collections.IList list in this.Lists)
  164. {
  165. int i = list.IndexOf(value);
  166. if(i<0)
  167. {
  168. index += list.Count;
  169. }
  170. else
  171. {
  172. return index + i;
  173. }
  174. }
  175. return -1;
  176. }
  177. public void Insert(int index, object value)
  178. {
  179. throw new System.NotSupportedException();
  180. }
  181. public bool IsSynchronized
  182. {
  183. get
  184. {
  185. return Lists.IsSynchronized;
  186. }
  187. }
  188. public object this[int index]
  189. {
  190. get
  191. {
  192. int c = 0;
  193. foreach(System.Collections.IList list in this.Lists)
  194. {
  195. int c1 = c + list.Count;
  196. if(c <= index && index < c1)
  197. {
  198. return list[index-c];
  199. }
  200. else
  201. {
  202. c = c1;
  203. }
  204. }
  205. throw new System.ArgumentOutOfRangeException();
  206. }
  207. set
  208. {
  209. int c = 0;
  210. foreach(System.Collections.IList list in this.Lists)
  211. {
  212. int c1 = c + list.Count;
  213. if(c <= index && index < c1)
  214. {
  215. list[index-c] = value;
  216. return;
  217. }
  218. else
  219. {
  220. c = c1;
  221. }
  222. }
  223. throw new System.ArgumentOutOfRangeException();
  224. } }
  225. public void Remove(object value)
  226. {
  227. int i = this.IndexOf(value);
  228. if(i>=0)
  229. {
  230. this.RemoveAt(i);
  231. }
  232. }
  233. public void RemoveAt(int index)
  234. {
  235. int c = 0;
  236. foreach(System.Collections.IList list in this.Lists)
  237. {
  238. int c1 = c + list.Count;
  239. if(c <= index && index < c1)
  240. {
  241. list.RemoveAt(index-c);
  242. if(this.ListChanged!=null)
  243. {
  244. this.ListChanged(this, new System.ComponentModel.ListChangedEventArgs(System.ComponentModel.ListChangedType.ItemDeleted, index));
  245. }
  246. return;
  247. }
  248. else
  249. {
  250. c = c1;
  251. }
  252. }
  253. throw new System.ArgumentOutOfRangeException();
  254. }
  255. public int Find(System.ComponentModel.PropertyDescriptor property, object key)
  256. {
  257. int index = 0;
  258. foreach(System.Collections.IList list in this.Lists)
  259. {
  260. System.ComponentModel.ITypedList list3 = list as System.ComponentModel.ITypedList;
  261. System.ComponentModel.IBindingList list2 = list as System.ComponentModel.IBindingList;
  262. System.ComponentModel.PropertyDescriptor property2 = null;
  263. if(list2!=null && list3!=null && (property2 = list3.GetItemProperties(null)[property.Name])!=null)
  264. {
  265. int i = list2.Find(property2, key);
  266. if(i<0)
  267. {
  268. index += list.Count;
  269. }
  270. else
  271. {
  272. return index + i;
  273. }
  274. }
  275. else
  276. {
  277. for (int i=0; i < list.Count; i++)
  278. {
  279. if (key.Equals(property.GetValue(list[i])))
  280. {
  281. return index + i;
  282. }
  283. }
  284. index += list.Count;
  285. }
  286. }
  287. return -1;
  288. }
  289. #region IBindingList Members
  290. public void AddIndex(System.ComponentModel.PropertyDescriptor property)
  291. {
  292. // TODO: Add DataView.AddIndex implementation
  293. }
  294. public bool AllowNew
  295. {
  296. get
  297. {
  298. // TODO: Add DataView.AllowNew getter implementation
  299. return false;
  300. }
  301. }
  302. public void ApplySort(System.ComponentModel.PropertyDescriptor property, System.ComponentModel.ListSortDirection direction)
  303. {
  304. // TODO: Add DataView.ApplySort implementation
  305. }
  306. public System.ComponentModel.PropertyDescriptor SortProperty
  307. {
  308. get
  309. {
  310. // TODO: Add DataView.SortProperty getter implementation
  311. return null;
  312. }
  313. }
  314. public bool SupportsSorting
  315. {
  316. get
  317. {
  318. // TODO: Add DataView.SupportsSorting getter implementation
  319. return false;
  320. }
  321. }
  322. public bool IsSorted
  323. {
  324. get
  325. {
  326. // TODO: Add DataView.IsSorted getter implementation
  327. return false;
  328. }
  329. }
  330. public bool AllowRemove
  331. {
  332. get
  333. {
  334. // TODO: Add DataView.AllowRemove getter implementation
  335. return true;
  336. }
  337. }
  338. public bool SupportsSearching
  339. {
  340. get
  341. {
  342. // TODO: Add DataView.SupportsSearching getter implementation
  343. return true;
  344. }
  345. }
  346. public System.ComponentModel.ListSortDirection SortDirection
  347. {
  348. get
  349. {
  350. // TODO: Add DataView.SortDirection getter implementation
  351. return System.ComponentModel.ListSortDirection.Ascending;
  352. }
  353. }
  354. public event System.ComponentModel.ListChangedEventHandler ListChanged;
  355. public bool SupportsChangeNotification
  356. {
  357. get
  358. {
  359. // TODO: Add DataView.SupportsChangeNotification getter implementation
  360. return true;
  361. }
  362. }
  363. public void RemoveSort()
  364. {
  365. // TODO: Add DataView.RemoveSort implementation
  366. }
  367. public object AddNew()
  368. {
  369. // TODO: Add DataView.AddNew implementation
  370. return null;
  371. }
  372. public bool AllowEdit
  373. {
  374. get
  375. {
  376. // TODO: Add DataView.AllowEdit getter implementation
  377. return false;
  378. }
  379. }
  380. public void RemoveIndex(System.ComponentModel.PropertyDescriptor property)
  381. {
  382. // TODO: Add DataView.RemoveIndex implementation
  383. }
  384. #endregion
  385. #region IList Members
  386. public bool IsReadOnly
  387. {
  388. get
  389. {
  390. // TODO: Add DataView.IsReadOnly getter implementation
  391. return false;
  392. }
  393. }
  394. public bool IsFixedSize
  395. {
  396. get
  397. {
  398. // TODO: Add DataView.IsFixedSize getter implementation
  399. return false;
  400. }
  401. }
  402. #endregion
  403. #region ICollection Members
  404. public object SyncRoot
  405. {
  406. get
  407. {
  408. // TODO: Add DataView.SyncRoot getter implementation
  409. return Lists.SyncRoot;
  410. }
  411. }
  412. #endregion
  413. #region ISupportInitialize Members
  414. public void BeginInit()
  415. {
  416. // TODO: Add DataView.BeginInit implementation
  417. }
  418. public void EndInit()
  419. {
  420. // TODO: Add DataView.EndInit implementation
  421. }
  422. #endregion
  423. protected System.ComponentModel.PropertyDescriptorCollection m_SelectedColumns = new System.ComponentModel.PropertyDescriptorCollection(new System.ComponentModel.PropertyDescriptor[0]);
  424. public string[] FieldNames
  425. {
  426. get
  427. {
  428. string [] names = new string[m_SelectedColumns.Count];
  429. for(int i=0;i<m_SelectedColumns.Count;i++)
  430. {
  431. names[i] = m_SelectedColumns[i].Name;
  432. }
  433. return names;
  434. }
  435. set
  436. {
  437. m_SelectedColumns.Clear();
  438. for(int i=0;i<value.Length;i++)
  439. {
  440. m_SelectedColumns.Add(new Cerebrum.Data.DataItemPropertyDescriptor(value[i]));
  441. }
  442. }
  443. }
  444. public string SelectedColumns
  445. {
  446. get
  447. {
  448. return System.String.Join(",", FieldNames);
  449. }
  450. set
  451. {
  452. FieldNames = value.Split(new char[]{','});
  453. }
  454. }
  455. #region ITypedList Members
  456. public System.ComponentModel.PropertyDescriptorCollection GetItemProperties(System.ComponentModel.PropertyDescriptor[] listAccessors)
  457. {
  458. /*if(Lists.Count>0)
  459. {
  460. DataItemView rv = Lists[0] as DataItemView;
  461. return rv.GetProperties(null);
  462. }*/
  463. return m_SelectedColumns;
  464. }
  465. public string GetListName(System.ComponentModel.PropertyDescriptor[] listAccessors)
  466. {
  467. return System.String.Empty;
  468. }
  469. #endregion
  470. private void DataView_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
  471. {
  472. this.Reset();
  473. }
  474. public static System.ComponentModel.IBindingList FromBindingList(System.ComponentModel.IBindingList dv, string key, string caption, object keyvalue, string description)
  475. {
  476. Cerebrum.Data.DataListUnion du = new Cerebrum.Data.DataListUnion();
  477. //Cerebrum.Data.DataItemUnion du = new Cerebrum.Data.DataItemUnion();
  478. if(key==caption)
  479. {
  480. du.FieldNames = new string[] {key};
  481. }
  482. else
  483. {
  484. du.FieldNames = new string[] {key, caption};
  485. }
  486. System.Collections.Specialized.ListDictionary values = new System.Collections.Specialized.ListDictionary();
  487. values[key] = keyvalue;
  488. values[caption] = description;
  489. du.Add(new Cerebrum.Data.NameValueView(values));
  490. du.Add(dv);
  491. return du;
  492. }
  493. }
  494. public class DataItemView : System.ComponentModel.ICustomTypeDescriptor
  495. {
  496. protected internal System.ComponentModel.ICustomTypeDescriptor m_Item;
  497. public DataItemView(System.ComponentModel.ICustomTypeDescriptor item)
  498. {
  499. m_Item = item;
  500. }
  501. public override bool Equals(object obj)
  502. {
  503. if(base.Equals(obj))
  504. {
  505. return true;
  506. }
  507. else
  508. {
  509. if(obj is DataItemView)
  510. {
  511. return this.m_Item.Equals((obj as DataItemView).m_Item);
  512. }
  513. else
  514. return false;
  515. }
  516. }
  517. public override int GetHashCode()
  518. {
  519. return this.m_Item.GetHashCode();
  520. }
  521. public System.ComponentModel.AttributeCollection GetAttributes()
  522. {
  523. return new System.ComponentModel.AttributeCollection(null);
  524. }
  525. public string GetClassName()
  526. {
  527. return null;
  528. }
  529. public string GetComponentName()
  530. {
  531. return null;
  532. }
  533. public System.ComponentModel.TypeConverter GetConverter()
  534. {
  535. return null;
  536. }
  537. public System.ComponentModel.EventDescriptor GetDefaultEvent()
  538. {
  539. return null;
  540. }
  541. public System.ComponentModel.PropertyDescriptor GetDefaultProperty()
  542. {
  543. return null;
  544. }
  545. public object GetEditor(System.Type editorBaseType)
  546. {
  547. return null;
  548. }
  549. public System.ComponentModel.EventDescriptorCollection GetEvents(System.Attribute[] attributes)
  550. {
  551. return new System.ComponentModel.EventDescriptorCollection(null);
  552. }
  553. public System.ComponentModel.EventDescriptorCollection GetEvents()
  554. {
  555. return new System.ComponentModel.EventDescriptorCollection(null);
  556. }
  557. public System.ComponentModel.PropertyDescriptorCollection GetProperties(System.Attribute[] attributes)
  558. {
  559. System.ComponentModel.PropertyDescriptorCollection psItems = m_Item.GetProperties();
  560. System.ComponentModel.PropertyDescriptor[] ps = new System.ComponentModel.PropertyDescriptor[psItems.Count];
  561. int i;
  562. for (i=0; i < psItems.Count; i++)
  563. {
  564. ps[i] = new Cerebrum.Data.DataItemPropertyDescriptor(psItems[i].Name);
  565. }
  566. return new System.ComponentModel.PropertyDescriptorCollection(ps);
  567. }
  568. public System.ComponentModel.PropertyDescriptorCollection GetProperties()
  569. {
  570. return GetProperties(null);
  571. }
  572. public object GetPropertyOwner(System.ComponentModel.PropertyDescriptor pd)
  573. {
  574. return this;
  575. }
  576. public object this[string name]
  577. {
  578. get
  579. {
  580. System.ComponentModel.PropertyDescriptorCollection ps = this.m_Item.GetProperties();
  581. return ps[name].GetValue(this);
  582. }
  583. set
  584. {
  585. System.ComponentModel.PropertyDescriptorCollection ps = this.m_Item.GetProperties();
  586. ps[name].SetValue(this, value);
  587. }
  588. }
  589. }
  590. public class DataItemPropertyDescriptor : System.ComponentModel.PropertyDescriptor
  591. {
  592. private string MappingName;
  593. public DataItemPropertyDescriptor(string name) : base(name, null)
  594. {
  595. MappingName = name;
  596. }
  597. public override bool CanResetValue(object component)
  598. {
  599. return true;
  600. }
  601. public override object GetValue(object component)
  602. {
  603. Cerebrum.Data.DataItemView v0=component as Cerebrum.Data.DataItemView;
  604. if(v0!=null)
  605. {
  606. System.ComponentModel.PropertyDescriptorCollection ps = v0.m_Item.GetProperties();
  607. return /*ps==null?System.Convert.DBNull:*/(ps[MappingName].GetValue(v0.m_Item));
  608. }
  609. System.ComponentModel.ICustomTypeDescriptor v1=component as System.ComponentModel.ICustomTypeDescriptor;
  610. if(v1!=null)
  611. {
  612. System.ComponentModel.PropertyDescriptorCollection ps = v1.GetProperties();
  613. return /*ps==null?System.Convert.DBNull:*/(ps[MappingName].GetValue(v1));
  614. }
  615. return System.Convert.DBNull;
  616. }
  617. public override void ResetValue(object component)
  618. {
  619. Cerebrum.Data.DataItemView v0=component as Cerebrum.Data.DataItemView;
  620. if(v0!=null)
  621. {
  622. System.ComponentModel.PropertyDescriptorCollection ps = v0.m_Item.GetProperties();
  623. ps[MappingName].SetValue(v0.m_Item, System.Convert.DBNull);
  624. }
  625. System.ComponentModel.ICustomTypeDescriptor v1=component as System.ComponentModel.ICustomTypeDescriptor;
  626. if(v1!=null)
  627. {
  628. System.ComponentModel.PropertyDescriptorCollection ps = v1.GetProperties();
  629. ps[MappingName].SetValue(v1, System.Convert.DBNull);
  630. }
  631. }
  632. public override void SetValue(object component, object value)
  633. {
  634. Cerebrum.Data.DataItemView v0=component as Cerebrum.Data.DataItemView;
  635. if(v0!=null)
  636. {
  637. System.ComponentModel.PropertyDescriptorCollection ps = v0.m_Item.GetProperties();
  638. ps[MappingName].SetValue(v0.m_Item, value);
  639. }
  640. System.ComponentModel.ICustomTypeDescriptor v1=component as System.ComponentModel.ICustomTypeDescriptor;
  641. if(v1!=null)
  642. {
  643. System.ComponentModel.PropertyDescriptorCollection ps = v1.GetProperties();
  644. ps[MappingName].SetValue(v1, value);
  645. }
  646. }
  647. public override bool ShouldSerializeValue(object component)
  648. {
  649. return false;
  650. }
  651. public override System.Type ComponentType
  652. {
  653. get
  654. {
  655. return typeof(object); //typeof(Cerebrum.Data.DataItemView);
  656. }
  657. }
  658. public override bool IsBrowsable
  659. {
  660. get
  661. {
  662. return true;
  663. }
  664. }
  665. public override bool IsReadOnly
  666. {
  667. get
  668. {
  669. return false;
  670. }
  671. }
  672. public override System.Type PropertyType
  673. {
  674. get
  675. {
  676. return typeof(object);
  677. }
  678. }
  679. }
  680. }