LayoutGroup.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Collections.ObjectModel;
  15. using System.Xml.Serialization;
  16. namespace Xceed.Wpf.AvalonDock.Layout
  17. {
  18. [Serializable]
  19. public abstract class LayoutGroup<T> : LayoutGroupBase, ILayoutContainer, ILayoutGroup, IXmlSerializable where T : class, ILayoutElement
  20. {
  21. internal LayoutGroup()
  22. {
  23. _children.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_children_CollectionChanged);
  24. }
  25. void _children_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  26. {
  27. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove ||
  28. e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
  29. {
  30. if (e.OldItems != null)
  31. {
  32. foreach (LayoutElement element in e.OldItems)
  33. {
  34. if (element.Parent == this)
  35. element.Parent = null;
  36. }
  37. }
  38. }
  39. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add ||
  40. e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
  41. {
  42. if (e.NewItems != null)
  43. {
  44. foreach (LayoutElement element in e.NewItems)
  45. {
  46. if (element.Parent != this)
  47. {
  48. if (element.Parent != null)
  49. element.Parent.RemoveChild(element);
  50. element.Parent = this;
  51. }
  52. }
  53. }
  54. }
  55. ComputeVisibility();
  56. OnChildrenCollectionChanged();
  57. NotifyChildrenTreeChanged(ChildrenTreeChange.DirectChildrenChanged);
  58. RaisePropertyChanged("ChildrenCount");
  59. }
  60. ObservableCollection<T> _children = new ObservableCollection<T>();
  61. public ObservableCollection<T> Children
  62. {
  63. get { return _children; }
  64. }
  65. IEnumerable<ILayoutElement> ILayoutContainer.Children
  66. {
  67. get { return _children.Cast<ILayoutElement>(); }
  68. }
  69. #region IsVisible
  70. private bool _isVisible = true;
  71. public bool IsVisible
  72. {
  73. get { return _isVisible; }
  74. protected set
  75. {
  76. if (_isVisible != value)
  77. {
  78. RaisePropertyChanging("IsVisible");
  79. _isVisible = value;
  80. OnIsVisibleChanged();
  81. RaisePropertyChanged("IsVisible");
  82. }
  83. }
  84. }
  85. protected virtual void OnIsVisibleChanged()
  86. {
  87. UpdateParentVisibility();
  88. }
  89. void UpdateParentVisibility()
  90. {
  91. var parentPane = Parent as ILayoutElementWithVisibility;
  92. if (parentPane != null)
  93. parentPane.ComputeVisibility();
  94. }
  95. public void ComputeVisibility()
  96. {
  97. IsVisible = GetVisibility();
  98. }
  99. protected abstract bool GetVisibility();
  100. protected override void OnParentChanged(ILayoutContainer oldValue, ILayoutContainer newValue)
  101. {
  102. base.OnParentChanged(oldValue, newValue);
  103. ComputeVisibility();
  104. }
  105. #endregion
  106. public void MoveChild(int oldIndex, int newIndex)
  107. {
  108. if (oldIndex == newIndex)
  109. return;
  110. _children.Move(oldIndex, newIndex);
  111. ChildMoved(oldIndex, newIndex);
  112. }
  113. protected virtual void ChildMoved(int oldIndex, int newIndex)
  114. {
  115. }
  116. public void RemoveChildAt(int childIndex)
  117. {
  118. _children.RemoveAt(childIndex);
  119. }
  120. public int IndexOfChild(ILayoutElement element)
  121. {
  122. return _children.Cast<ILayoutElement>().ToList().IndexOf(element);
  123. }
  124. public void InsertChildAt(int index, ILayoutElement element)
  125. {
  126. _children.Insert(index, (T)element);
  127. }
  128. public void RemoveChild(ILayoutElement element)
  129. {
  130. _children.Remove((T)element);
  131. }
  132. public void ReplaceChild(ILayoutElement oldElement, ILayoutElement newElement)
  133. {
  134. int index = _children.IndexOf((T)oldElement);
  135. _children.Insert(index, (T)newElement);
  136. _children.RemoveAt(index + 1);
  137. }
  138. public int ChildrenCount
  139. {
  140. get { return _children.Count; }
  141. }
  142. public void ReplaceChildAt(int index, ILayoutElement element)
  143. {
  144. _children[index] = (T)element;
  145. }
  146. public System.Xml.Schema.XmlSchema GetSchema()
  147. {
  148. return null;
  149. }
  150. public virtual void ReadXml(System.Xml.XmlReader reader)
  151. {
  152. reader.MoveToContent();
  153. if (reader.IsEmptyElement)
  154. {
  155. reader.Read();
  156. ComputeVisibility();
  157. return;
  158. }
  159. string localName = reader.LocalName;
  160. reader.Read();
  161. while (true)
  162. {
  163. if (reader.LocalName == localName &&
  164. reader.NodeType == System.Xml.XmlNodeType.EndElement)
  165. {
  166. break;
  167. }
  168. XmlSerializer serializer = null;
  169. if (reader.LocalName == "LayoutAnchorablePaneGroup")
  170. serializer = new XmlSerializer(typeof(LayoutAnchorablePaneGroup));
  171. else if (reader.LocalName == "LayoutAnchorablePane")
  172. serializer = new XmlSerializer(typeof(LayoutAnchorablePane));
  173. else if (reader.LocalName == "LayoutAnchorable")
  174. serializer = new XmlSerializer(typeof(LayoutAnchorable));
  175. else if (reader.LocalName == "LayoutDocumentPaneGroup")
  176. serializer = new XmlSerializer(typeof(LayoutDocumentPaneGroup));
  177. else if (reader.LocalName == "LayoutDocumentPane")
  178. serializer = new XmlSerializer(typeof(LayoutDocumentPane));
  179. else if (reader.LocalName == "LayoutDocument")
  180. serializer = new XmlSerializer(typeof(LayoutDocument));
  181. else if (reader.LocalName == "LayoutAnchorGroup")
  182. serializer = new XmlSerializer(typeof(LayoutAnchorGroup));
  183. else if (reader.LocalName == "LayoutPanel")
  184. serializer = new XmlSerializer(typeof(LayoutPanel));
  185. Children.Add((T)serializer.Deserialize(reader));
  186. }
  187. reader.ReadEndElement();
  188. }
  189. public virtual void WriteXml(System.Xml.XmlWriter writer)
  190. {
  191. foreach (var child in Children)
  192. {
  193. var type = child.GetType();
  194. XmlSerializer serializer = new XmlSerializer(type);
  195. serializer.Serialize(writer, child);
  196. }
  197. }
  198. }
  199. }