ConductorBase.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. namespace Caliburn.Micro.Core {
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. /// <summary>
  6. /// A base class for various implementations of <see cref="IConductor"/>.
  7. /// </summary>
  8. /// <typeparam name="T">The type that is being conducted.</typeparam>
  9. public abstract class ConductorBase<T> : Screen, IConductor, IParent<T> where T : class {
  10. ICloseStrategy<T> closeStrategy;
  11. /// <summary>
  12. /// Gets or sets the close strategy.
  13. /// </summary>
  14. /// <value>The close strategy.</value>
  15. public ICloseStrategy<T> CloseStrategy {
  16. get { return closeStrategy ?? (closeStrategy = new DefaultCloseStrategy<T>()); }
  17. set { closeStrategy = value; }
  18. }
  19. void IConductor.ActivateItem(object item) {
  20. ActivateItem((T) item);
  21. }
  22. void IConductor.DeactivateItem(object item, bool close) {
  23. DeactivateItem((T) item, close);
  24. }
  25. IEnumerable IParent.GetChildren() {
  26. return GetChildren();
  27. }
  28. /// <summary>
  29. /// Occurs when an activation request is processed.
  30. /// </summary>
  31. public virtual event EventHandler<ActivationProcessedEventArgs> ActivationProcessed = delegate { };
  32. /// <summary>
  33. /// Gets the children.
  34. /// </summary>
  35. /// <returns>The collection of children.</returns>
  36. public abstract IEnumerable<T> GetChildren();
  37. /// <summary>
  38. /// Activates the specified item.
  39. /// </summary>
  40. /// <param name="item">The item to activate.</param>
  41. public abstract void ActivateItem(T item);
  42. /// <summary>
  43. /// Deactivates the specified item.
  44. /// </summary>
  45. /// <param name="item">The item to close.</param>
  46. /// <param name="close">Indicates whether or not to close the item after deactivating it.</param>
  47. public abstract void DeactivateItem(T item, bool close);
  48. /// <summary>
  49. /// Called by a subclass when an activation needs processing.
  50. /// </summary>
  51. /// <param name="item">The item on which activation was attempted.</param>
  52. /// <param name="success">if set to <c>true</c> activation was successful.</param>
  53. protected virtual void OnActivationProcessed(T item, bool success) {
  54. if (item == null) {
  55. return;
  56. }
  57. var handler = ActivationProcessed;
  58. if (handler != null) {
  59. handler(this, new ActivationProcessedEventArgs
  60. {
  61. Item = item,
  62. Success = success
  63. });
  64. }
  65. }
  66. /// <summary>
  67. /// Ensures that an item is ready to be activated.
  68. /// </summary>
  69. /// <param name="newItem">The item that is about to be activated.</param>
  70. /// <returns>The item to be activated.</returns>
  71. protected virtual T EnsureItem(T newItem) {
  72. var node = newItem as IChild;
  73. if (node != null && node.Parent != this)
  74. node.Parent = this;
  75. return newItem;
  76. }
  77. }
  78. }