TreeItemContainerGenerator.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Perspex.Controls.Templates;
  6. namespace Perspex.Controls.Generators
  7. {
  8. /// <summary>
  9. /// Creates containers for tree items and maintains a list of created containers.
  10. /// </summary>
  11. /// <typeparam name="T">The type of the container.</typeparam>
  12. public class TreeItemContainerGenerator<T> : ItemContainerGenerator<T>, ITreeItemContainerGenerator
  13. where T : class, IControl, new()
  14. {
  15. private Dictionary<object, T> _itemToContainer;
  16. private Dictionary<IControl, object> _containerToItem;
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="TreeItemContainerGenerator{T}"/> class.
  19. /// </summary>
  20. /// <param name="owner">The owner control.</param>
  21. /// <param name="contentProperty">The container's Content property.</param>
  22. /// <param name="itemsProperty">The container's Items property.</param>
  23. /// <param name="isExpandedProperty">The container's IsExpanded property.</param>
  24. /// <param name="rootGenerator">
  25. /// The item container for the root of the tree, or null if this generator is itself the
  26. /// root of the tree.
  27. /// </param>
  28. public TreeItemContainerGenerator(
  29. IControl owner,
  30. PerspexProperty contentProperty,
  31. PerspexProperty itemsProperty,
  32. PerspexProperty isExpandedProperty,
  33. ITreeItemContainerGenerator rootGenerator)
  34. : base(owner, contentProperty)
  35. {
  36. ItemsProperty = itemsProperty;
  37. IsExpandedProperty = isExpandedProperty;
  38. RootGenerator = rootGenerator;
  39. if (rootGenerator == null)
  40. {
  41. _itemToContainer = new Dictionary<object, T>();
  42. _containerToItem = new Dictionary<IControl, object>();
  43. }
  44. }
  45. /// <summary>
  46. /// Gets the item container for the root of the tree, or null if this generator is itself
  47. /// the root of the tree.
  48. /// </summary>
  49. public ITreeItemContainerGenerator RootGenerator { get; }
  50. /// <summary>
  51. /// Gets the item container's Items property.
  52. /// </summary>
  53. protected PerspexProperty ItemsProperty { get; }
  54. /// <summary>
  55. /// Gets the item container's IsExpanded property.
  56. /// </summary>
  57. protected PerspexProperty IsExpandedProperty { get; }
  58. /// <summary>
  59. /// Gets the item container for the specified item, anywhere in the tree.
  60. /// </summary>
  61. /// <param name="item">The item.</param>
  62. /// <returns>The container, or null if not found.</returns>
  63. public IControl TreeContainerFromItem(object item)
  64. {
  65. T result;
  66. _itemToContainer.TryGetValue(item, out result);
  67. return result;
  68. }
  69. /// <summary>
  70. /// Gets the item for the specified item container, anywhere in the tree.
  71. /// </summary>
  72. /// <param name="container">The container.</param>
  73. /// <returns>The item, or null if not found.</returns>
  74. public object TreeItemFromContainer(IControl container)
  75. {
  76. object result;
  77. _containerToItem.TryGetValue(container, out result);
  78. return result;
  79. }
  80. /// <inheritdoc/>
  81. protected override IControl CreateContainer(object item)
  82. {
  83. var container = item as T;
  84. if (item == null)
  85. {
  86. return null;
  87. }
  88. else if (container != null)
  89. {
  90. return container;
  91. }
  92. else
  93. {
  94. var template = GetTreeDataTemplate(item);
  95. var result = new T();
  96. result.SetValue(ContentProperty, template.Build(item));
  97. result.SetValue(ItemsProperty, template.ItemsSelector(item));
  98. result.SetValue(IsExpandedProperty, template.IsExpanded(item));
  99. if (!(item is IControl))
  100. {
  101. result.DataContext = item;
  102. }
  103. AddToIndex(item, result);
  104. return result;
  105. }
  106. }
  107. public override IList<IControl> ClearContainers()
  108. {
  109. ClearIndex();
  110. return base.ClearContainers();
  111. }
  112. public override IList<IControl> RemoveContainers(int startingIndex, int count)
  113. {
  114. RemoveFromIndex(GetContainerRange(startingIndex, count));
  115. return base.RemoveContainers(startingIndex, count);
  116. }
  117. private void AddToIndex(object item, T container)
  118. {
  119. if (RootGenerator != null)
  120. {
  121. ((TreeItemContainerGenerator<T>)RootGenerator).AddToIndex(item, container);
  122. }
  123. else
  124. {
  125. _itemToContainer.Add(item, container);
  126. _containerToItem.Add(container, item);
  127. }
  128. }
  129. private void RemoveFromIndex(IEnumerable<IControl> containers)
  130. {
  131. if (RootGenerator != null)
  132. {
  133. ((TreeItemContainerGenerator<T>)RootGenerator).RemoveFromIndex(containers);
  134. }
  135. else
  136. {
  137. foreach (var container in containers)
  138. {
  139. var item = _containerToItem[container];
  140. _containerToItem.Remove(container);
  141. _itemToContainer.Remove(item);
  142. }
  143. }
  144. }
  145. private void ClearIndex()
  146. {
  147. if (RootGenerator != null)
  148. {
  149. ((TreeItemContainerGenerator<T>)RootGenerator).ClearIndex();
  150. }
  151. else
  152. {
  153. _containerToItem.Clear();
  154. _itemToContainer.Clear();
  155. }
  156. }
  157. /// <summary>
  158. /// Gets the data template for the specified item.
  159. /// </summary>
  160. /// <param name="item">The item.</param>
  161. /// <returns>The template.</returns>
  162. private ITreeDataTemplate GetTreeDataTemplate(object item)
  163. {
  164. IDataTemplate template = Owner.FindDataTemplate(item);
  165. if (template == null)
  166. {
  167. template = FuncDataTemplate.Default;
  168. }
  169. var treeTemplate = template as ITreeDataTemplate;
  170. if (treeTemplate == null)
  171. {
  172. treeTemplate = new FuncTreeDataTemplate(typeof(object), template.Build, x => null);
  173. }
  174. return treeTemplate;
  175. }
  176. }
  177. }