DockContentCollection.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:DockContentCollection.cs
  5. * 2.功能描述:类文件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/01 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. namespace Dongke.IBOSS.PRD.Basics.DockPanel
  14. {
  15. public class DockContentCollection : ReadOnlyCollection<IDockContent>
  16. {
  17. private static List<IDockContent> _emptyList = new List<IDockContent>(0);
  18. internal DockContentCollection()
  19. : base(new List<IDockContent>())
  20. {
  21. }
  22. internal DockContentCollection(DockPane pane)
  23. : base(_emptyList)
  24. {
  25. m_dockPane = pane;
  26. }
  27. private DockPane m_dockPane = null;
  28. private DockPane DockPane
  29. {
  30. get
  31. {
  32. return m_dockPane;
  33. }
  34. }
  35. public new IDockContent this[int index]
  36. {
  37. get
  38. {
  39. if (DockPane == null)
  40. return Items[index] as IDockContent;
  41. else
  42. return GetVisibleContent(index);
  43. }
  44. }
  45. internal int Add(IDockContent content)
  46. {
  47. #if DEBUG
  48. if (DockPane != null)
  49. throw new InvalidOperationException();
  50. #endif
  51. if (Contains(content))
  52. return IndexOf(content);
  53. Items.Add(content);
  54. return Count - 1;
  55. }
  56. internal void AddAt(IDockContent content, int index)
  57. {
  58. #if DEBUG
  59. if (DockPane != null)
  60. throw new InvalidOperationException();
  61. #endif
  62. if (index < 0 || index > Items.Count - 1)
  63. return;
  64. if (Contains(content))
  65. return;
  66. Items.Insert(index, content);
  67. }
  68. public new bool Contains(IDockContent content)
  69. {
  70. if (DockPane == null)
  71. return Items.Contains(content);
  72. else
  73. return (GetIndexOfVisibleContents(content) != -1);
  74. }
  75. public new int Count
  76. {
  77. get
  78. {
  79. if (DockPane == null)
  80. return base.Count;
  81. else
  82. return CountOfVisibleContents;
  83. }
  84. }
  85. public new int IndexOf(IDockContent content)
  86. {
  87. if (DockPane == null)
  88. {
  89. if (!Contains(content))
  90. return -1;
  91. else
  92. return Items.IndexOf(content);
  93. }
  94. else
  95. return GetIndexOfVisibleContents(content);
  96. }
  97. internal void Remove(IDockContent content)
  98. {
  99. if (DockPane != null)
  100. throw new InvalidOperationException();
  101. if (!Contains(content))
  102. return;
  103. Items.Remove(content);
  104. }
  105. private int CountOfVisibleContents
  106. {
  107. get
  108. {
  109. #if DEBUG
  110. if (DockPane == null)
  111. throw new InvalidOperationException();
  112. #endif
  113. int count = 0;
  114. foreach (IDockContent content in DockPane.Contents)
  115. {
  116. if (content.DockHandler.DockState == DockPane.DockState)
  117. count++;
  118. }
  119. return count;
  120. }
  121. }
  122. private IDockContent GetVisibleContent(int index)
  123. {
  124. #if DEBUG
  125. if (DockPane == null)
  126. throw new InvalidOperationException();
  127. #endif
  128. int currentIndex = -1;
  129. foreach (IDockContent content in DockPane.Contents)
  130. {
  131. if (content.DockHandler.DockState == DockPane.DockState)
  132. currentIndex++;
  133. if (currentIndex == index)
  134. return content;
  135. }
  136. throw (new ArgumentOutOfRangeException());
  137. }
  138. private int GetIndexOfVisibleContents(IDockContent content)
  139. {
  140. #if DEBUG
  141. if (DockPane == null)
  142. throw new InvalidOperationException();
  143. #endif
  144. if (content == null)
  145. return -1;
  146. int index = -1;
  147. foreach (IDockContent c in DockPane.Contents)
  148. {
  149. if (c.DockHandler.DockState == DockPane.DockState)
  150. {
  151. index++;
  152. if (c == content)
  153. return index;
  154. }
  155. }
  156. return -1;
  157. }
  158. }
  159. }