PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/DragDrop/DragDrop.Model/CookieJar.cs

https://bitbucket.org/bu5hm4nn/sharpfellows.toolkit
C# | 138 lines | 90 code | 20 blank | 28 comment | 9 complexity | 3cd4d8f5445df25974ce90e492405eb3 MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Windows;
  5. using SharpFellows.Toolkit.Behaviours;
  6. namespace DragDrop.Model
  7. {
  8. /// <summary>
  9. /// Container for cookies
  10. /// </summary>
  11. public class CookieJar : INotifyPropertyChanged
  12. {
  13. #region Constants
  14. private const int INITIAL_NUMBER_OF_COOKIES = 5;
  15. private const int JAR_CAPACITY = 10;
  16. #endregion
  17. #region Fields
  18. private readonly Stack<Cookie> _cookies = new Stack<Cookie>(JAR_CAPACITY);
  19. private IDragSource _source;
  20. private IDropTarget _target;
  21. #endregion
  22. /// <summary>
  23. /// Occurs when a property value changes.
  24. /// </summary>
  25. public event PropertyChangedEventHandler PropertyChanged;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="CookieJar"/> class.
  28. /// </summary>
  29. /// <param name="numberOfCookies">The number of cookies to be placed in the jar</param>
  30. public CookieJar(int numberOfCookies)
  31. {
  32. for (int i = 0; i < numberOfCookies; i++)
  33. _cookies.Push(new Cookie { Jar = this });
  34. }
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="CookieJar"/> class.
  37. /// </summary>
  38. public CookieJar() : this(INITIAL_NUMBER_OF_COOKIES)
  39. {
  40. }
  41. /// <summary>
  42. /// Gets the (drag) source of cookies.
  43. /// </summary>
  44. /// <value>The source of cookies.</value>
  45. public IDragSource SourceOfCookies
  46. {
  47. get
  48. {
  49. if (_source == null)
  50. _source = new DragSource<CookieJar>(GetDragEffects, GetData);
  51. return _source;
  52. }
  53. }
  54. /// <summary>
  55. /// Gets the drop target for cookies.
  56. /// </summary>
  57. /// <value>The cookie sink.</value>
  58. public IDropTarget CookieSink
  59. {
  60. get
  61. {
  62. if (_target == null)
  63. _target = new DropTarget<Cookie>(GetDropEffects, Drop);
  64. return _target;
  65. }
  66. }
  67. /// <summary>
  68. /// Gets the number of cookies in the jar.
  69. /// </summary>
  70. /// <value>The number of cookies.</value>
  71. public int NumberOfCookies
  72. {
  73. get
  74. {
  75. return _cookies.Count;
  76. }
  77. }
  78. private DragDropEffects GetDragEffects(CookieJar jar)
  79. {
  80. // Only allow the drag and drop to start if we have any cookies
  81. return _cookies.Any() ? DragDropEffects.Move : DragDropEffects.None;
  82. }
  83. private object GetData(CookieJar jar)
  84. {
  85. return _cookies.Peek();
  86. }
  87. private void Drop(Cookie cookie)
  88. {
  89. AddCookie(cookie);
  90. RaisePropertyChangedEvent("NumberOfCookies");
  91. }
  92. private void AddCookie(Cookie cookie)
  93. {
  94. cookie.Jar.PopOne();
  95. _cookies.Push(cookie);
  96. cookie.Jar = this;
  97. }
  98. private void PopOne()
  99. {
  100. _cookies.Pop();
  101. RaisePropertyChangedEvent("NumberOfCookies");
  102. }
  103. private DragDropEffects GetDropEffects(Cookie cookie)
  104. {
  105. // Do not allow our own cookies
  106. if (cookie.Jar == this)
  107. return DragDropEffects.None;
  108. // The cookie can be dropped if we are not exceeding the capacity of the jar
  109. return _cookies.Count >= JAR_CAPACITY ? DragDropEffects.None : DragDropEffects.Move;
  110. }
  111. public void RaisePropertyChangedEvent(string propertyName)
  112. {
  113. PropertyChangedEventHandler handler = PropertyChanged;
  114. if (handler != null)
  115. handler(this, new PropertyChangedEventArgs(propertyName));
  116. }
  117. }
  118. }